diff --git a/js/remote.js b/js/remote.js index 8903c87472..016679718e 100644 --- a/js/remote.js +++ b/js/remote.js @@ -4,65 +4,132 @@ // http://www.w3.org/TR/websockets/#the-websocket-interface // // YYY Will later provide a network access which use multiple instances of this. +// YYY A better model might be to allow requesting a target state: keep connected or not. // var util = require('util'); var WebSocket = require('ws'); -// YYY This is wrong should not use anything in test directory. -var config = require("../test/config.js"); - // --> trusted: truthy, if remote is trusted -var Remote = function(trusted, websocket_ip, websocket_port) { +var Remote = function(trusted, websocket_ip, websocket_port, trace) { this.trusted = trusted; this.websocket_ip = websocket_ip; this.websocket_port = websocket_port; this.id = 0; + this.trace = trace; }; -var remoteConfig = function(server) { +var remoteConfig = function(config, server) { var serverConfig = config.servers[server]; return new Remote(serverConfig.trusted, serverConfig.websocket_ip, serverConfig.websocket_port); }; -// Target state is connectted. -// done(readyState): -// --> readyState: OPEN, CLOSED -Remote.method('connect', function(done, onmessage) { - var url = util.format("ws://%s:%s", this.websocket_ip, this.websocket_port); +Remote.method('connect_helper', function() { + var self = this; - console.log("remote: connect: %s", url); + if (this.trace) + console.log("remote: connect: %s", this.url); - this.ws = new WebSocket(url); + this.ws = new WebSocket(this.url); var ws = this.ws; - ws.onopen = function() { - console.log("remote: onopen: %s", ws.readyState); - ws.onclose = undefined; - done(ws.readyState); + ws.onopen = function() { + if (this.trace) + console.log("remote: onopen: %s", ws.readyState); + + ws.onclose = undefined; + ws.onerror = undefined; + + self.done(ws.readyState); }; - // Also covers failure to open. - ws.onclose = function() { - console.log("remote: onclose: %s", ws.readyState); - done(ws.readyState); + ws.onerror = function() { + if (this.trace) + console.log("remote: onerror: %s", ws.readyState); + + ws.onclose = undefined; + + if (self.expire) { + if (this.trace) + console.log("remote: was expired"); + + self.done(ws.readyState); + } + else + { + // Delay and retry. + setTimeout(function() { + if (this.trace) + console.log("remote: retry"); + + self.connect_helper(); + }, 50); // Retry rate 50ms. + } }; - if (onmessage) { - ws.onmessage = onmessage; + // Covers failure to open. + ws.onclose = function() { + if (this.trace) + console.log("remote: onclose: %s", ws.readyState); + + ws.onerror = undefined; + + self.done(ws.readyState); + }; + + if (this.onmessage) { + ws.onmessage = this.onmessage; } }); +// Target state is connectted. +// done(readyState): +// --> readyState: OPEN, CLOSED +Remote.method('connect', function(done, onmessage, timeout) { + var self = this; + + this.url = util.format("ws://%s:%s", this.websocket_ip, this.websocket_port); + this.done = done; + + if (onmessage) + this.onmessage = onmessage; + + if (timeout) { + if (this.trace) + console.log("remote: expire: false"); + + this.expire = false; + + setTimeout(function () { + if (this.trace) + console.log("remote: expire: timeout"); + + self.expire = true; + }, timeout); + } + else { + if (this.trace) + console.log("remote: expire: false"); + + this.expire = true; + } + + this.connect_helper(); + +}); + // Target stated is disconnected. Remote.method('disconnect', function(done) { var ws = this.ws; ws.onclose = function() { - console.log("remote: onclose: %s", ws.readyState); - done(ws.readyState); + if (this.trace) + console.log("remote: onclose: %s", ws.readyState); + + done(ws.readyState); }; ws.close(); diff --git a/test/server.js b/test/server.js index 0bd0f582c3..9e20569a58 100644 --- a/test/server.js +++ b/test/server.js @@ -1,4 +1,7 @@ // Manage test servers +// +// YYY Would be nice to be able to hide server output. +// // Provide servers // diff --git a/test/standalone-test.js b/test/standalone-test.js index c50e50154a..b36929eb44 100644 --- a/test/standalone-test.js +++ b/test/standalone-test.js @@ -5,8 +5,15 @@ var buster = require("buster"); var server = require("./server.js"); var remote = require("../js/remote.js"); +var config = require("./config.js"); -buster.testCase("Check standalone server startup", { +// How long to wait for server to start. +var serverDelay = 1500; + +buster.testRunner.timeout = 5000; + + +buster.testCase("Standalone server startup", { "server start and stop": function(done) { server.start("alpha", function(e) { @@ -19,14 +26,15 @@ buster.testCase("Check standalone server startup", { } }); -buster.testCase("Check websocket connection", { +buster.testCase("WebSocket connection", { 'setUp' : function(done) { server.start("alpha", function(e) { buster.refute(e); done(); - }); + } + ); }, 'tearDown' : @@ -39,24 +47,53 @@ buster.testCase("Check websocket connection", { "websocket connect and disconnect" : function(done) { - var alpha = remote.remoteConfig("alpha"); + var alpha = remote.remoteConfig(config, "alpha"); alpha.connect(function(stat) { - buster.assert(1 == stat); // OPEN + buster.assert(1 == stat); // OPEN alpha.disconnect(function(stat) { - buster.assert(3 == stat); // CLOSED + buster.assert(3 == stat); // CLOSED done(); }); - }); + }, undefined, serverDelay); }, }); -buster.testCase("Check assert", { - "assert" : - function() { - buster.assert(true); - } -}); +// var alpha = remote.remoteConfig("alpha"); +// +// buster.testCase("Websocket commands", { +// 'setUp' : +// function(done) { +// server.start("alpha", +// function(e) { +// buster.refute(e); +// +// alpha.connect(function(stat) { +// buster.assert(1 == stat); // OPEN +// +// done(); +// }); +// }); +// }, +// +// 'tearDown' : +// function(done) { +// alpha.disconnect(function(stat) { +// buster.assert(3 == stat); // CLOSED +// +// server.stop("alpha", function(e) { +// buster.refute(e); +// +// done(); +// }); +// }); +// }, +// +// "assert" : +// function() { +// buster.assert(true); +// } +// }); // vim:ts=4