diff --git a/examples/libraries/virtualBaton.19.js b/examples/libraries/virtualBaton.20.js similarity index 94% rename from examples/libraries/virtualBaton.19.js rename to examples/libraries/virtualBaton.20.js index 653681dda6..76811d2f48 100644 --- a/examples/libraries/virtualBaton.19.js +++ b/examples/libraries/virtualBaton.20.js @@ -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; diff --git a/examples/tests/testBaton.js b/examples/tests/testBaton.js index 02ac2e5232..5ba5d99565 100644 --- a/examples/tests/testBaton.js +++ b/examples/tests/testBaton.js @@ -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); - -