Merge branch 'master' of github.com:jedmccaleb/NewCoin into serialize

This commit is contained in:
JoelKatz
2012-09-25 22:48:40 -07:00
2 changed files with 76 additions and 61 deletions

View File

@@ -20,10 +20,10 @@ var Remote = function(trusted, websocket_ip, websocket_port, trace) {
this.trace = trace;
};
var remoteConfig = function(config, server) {
var remoteConfig = function(config, server, trace) {
var serverConfig = config.servers[server];
return new Remote(serverConfig.trusted, serverConfig.websocket_ip, serverConfig.websocket_port);
return new Remote(serverConfig.trusted, serverConfig.websocket_ip, serverConfig.websocket_port, trace);
};
Remote.method('connect_helper', function() {
@@ -36,6 +36,8 @@ Remote.method('connect_helper', function() {
var ws = this.ws;
ws.response = {};
ws.onopen = function() {
if (this.trace)
console.log("remote: onopen: %s", ws.readyState);
@@ -80,23 +82,36 @@ Remote.method('connect_helper', function() {
self.done(ws.readyState);
};
if (this.onmessage) {
ws.onmessage = this.onmessage;
}
// XXX Why doesn't onmessage work?
ws.on('message', function(json, flags) {
var message = JSON.parse(json);
// console.log("message: %s", json);
if (message.type !== 'response') {
console.log("unexpected message: %s", json);
} else {
var done = ws.response[message.id];
if (done) {
done(message);
} else {
console.log("unexpected message id: %s", json);
}
}
});
});
// Target state is connectted.
// done(readyState):
// --> readyState: OPEN, CLOSED
Remote.method('connect', function(done, onmessage, timeout) {
Remote.method('connect', function(done, 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");
@@ -146,14 +161,15 @@ Remote.method('request', function(command, done) {
ws.response[command.id] = done;
ws.send(command);
if (this.trace)
console.log("remote: send: %s", JSON.stringify(command));
ws.send(JSON.stringify(command));
});
// Request the current ledger.
// done(index)
// index: undefined = error
Remote.method('ledger', function(done) {
// Get the current ledger entry (may be live or not).
Remote.method('ledger_current', function(done) {
this.request({ 'command' : 'ledger_current' }, done);
});
@@ -166,20 +182,12 @@ Remote.method('submit', function(json, done) {
// });
});
// ==> entry_spec
Remote.method('ledger_entry', function(entry_spec, done) {
entry_spec.command = 'ledger_entry';
this.request(entry_spec, function() {
});
});
// done(value)
// --> value: { 'status', status, 'result' : result, ... }
// done may be called up to 3 times.
Remote.method('account_root', function(account_id, done) {
this.request({
'command' : 'ledger_entry',
'command' : 'ledger_current',
}, function() {
});
});

View File

@@ -12,7 +12,6 @@ var serverDelay = 1500;
buster.testRunner.timeout = 5000;
buster.testCase("Standalone server startup", {
"server start and stop": function(done) {
server.start("alpha",
@@ -56,44 +55,52 @@ buster.testCase("WebSocket connection", {
buster.assert(3 == stat); // CLOSED
done();
});
}, undefined, serverDelay);
}, serverDelay);
},
});
// 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);
// }
// });
// XXX Figure out a way to stuff this into the test case.
var alpha;
buster.testCase("Websocket commands", {
'setUp' :
function(done) {
server.start("alpha",
function(e) {
buster.refute(e);
alpha = remote.remoteConfig(config, "alpha");
alpha.connect(function(stat) {
buster.assert(1 == stat); // OPEN
done();
}, serverDelay);
});
},
'tearDown' :
function(done) {
alpha.disconnect(function(stat) {
buster.assert(3 == stat); // CLOSED
server.stop("alpha", function(e) {
buster.refute(e);
done();
});
});
},
"ledger_current" :
function(done) {
alpha.ledger_current(function (r) {
console.log(r);
buster.assert(r.ledger === 2);
done();
});
}
});
// vim:ts=4