This commit is contained in:
Howard Stearns 2016-02-02 14:15:03 -08:00
parent 61393818fe
commit 3d01b3ec2b
2 changed files with 21 additions and 6 deletions

View file

@ -29,7 +29,6 @@ virtualBaton = function virtualBaton(options) {
var key = options.key,
channel = "io.highfidelity.virtualBaton:" + key,
exports = options.exports || {},
timeout = options.timeout || 5, // seconds
claimCallback,
releaseCallback,
// paxos proposer state
@ -37,6 +36,8 @@ virtualBaton = function virtualBaton(options) {
nQuorum,
mostRecentInterested,
bestPromise = {number: 0},
electionTimeout = options.electionTimeout || 1000, // ms. If no winner in this time, hold a new election
electionWatchdog,
// paxos acceptor state
bestProposal = {number: 0},
accepted = null;
@ -79,6 +80,7 @@ virtualBaton = function virtualBaton(options) {
}
function propose(claim) {
debug('baton: propose', claim);
if (electionWatchdog) { Script.clearTimeout(electionWatchdog); }
if (!claimCallback) { return; } // We're not participating.
nPromises = 0;
nQuorum = Math.floor(AvatarList.getAvatarIdentifiers().length / 2) + 1;
@ -86,9 +88,10 @@ virtualBaton = function virtualBaton(options) {
bestPromise.number++;
bestPromise.winner = claim;
send('prepare!', bestPromise);
// Fixme: set a watchdog that is cancelled when we send accept!, and which propose(claim) when it goes off.
electionWatchdog = Script.setTimeout(function () {
propose(claim);
}, electionTimeout);
}
function messageHandler(messageChannel, messageString, senderID) {
if (messageChannel !== channel) { return; }
var message = JSON.parse(messageString), data = message.data;
@ -134,6 +137,10 @@ virtualBaton = function virtualBaton(options) {
case 'accepted':
accepted = data;
if (acceptedId() === MyAvatar.sessionUUID) { // Note that we might not been the proposer.
if (electionWatchdog) {
Script.clearTimeout(electionWatchdog);
electionWatchdog = null;
}
if (claimCallback) {
var callback = claimCallback;
claimCallback = undefined;

View file

@ -1,8 +1,18 @@
"use strict";
/*jslint nomen: true, plusplus: true, vars: true*/
var Vec3, Quat, MyAvatar, Entities, Camera, Script, print;
//
// Created by Howard Stearns
// Copyright 2016 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// test libraries/virtualBaton.js
// All participants should run the test script.
Script.include("../libraries/virtualBaton.19.js");
Script.include("../libraries/virtualBaton.20.js");
var TICKER_INTERVAL = 1000; // ms
var baton = virtualBaton({key: 'io.highfidelity.testBaton'});
var ticker, countDown;
@ -23,5 +33,3 @@ function lostBaton(key) {
baton.claim(gotBaton, lostBaton);
}
baton.claim(gotBaton, lostBaton);