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

This commit is contained in:
JoelKatz
2012-09-25 19:27:25 -07:00
5 changed files with 159 additions and 41 deletions

View File

@@ -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();

View File

@@ -134,8 +134,8 @@ public:
// Directory functions
//
STVector256 getDirNodeInfo(const uint256& uLedger, const uint256& uRootIndex,
uint64& uNodePrevious, uint64& uNodeNext);
STVector256 getDirNodeInfo(const uint256& uLedger, const uint256& uRootIndex,
uint64& uNodePrevious, uint64& uNodeNext);
//
// Nickname functions
@@ -150,7 +150,6 @@ public:
Json::Value getOwnerInfo(const uint256& uLedger, const NewcoinAddress& naAccount);
Json::Value getOwnerInfo(Ledger::pointer lpLedger, const NewcoinAddress& naAccount);
// raw object operations
bool findRawLedger(const uint256& ledgerHash, std::vector<unsigned char>& rawLedger);
bool findRawTransaction(const uint256& transactionHash, std::vector<unsigned char>& rawTransaction);

View File

@@ -75,7 +75,10 @@ public:
Json::Value invokeCommand(const Json::Value& jvRequest);
boost::unordered_set<NewcoinAddress> parseAccountIds(const Json::Value& jvArray);
// Commands
// Request-Response Commands
void doLedgerCurrent(Json::Value& jvResult, const Json::Value& jvRequest);
// Streaming Commands
void doAccountInfoSubscribe(Json::Value& jvResult, const Json::Value& jvRequest);
void doAccountInfoUnsubscribe(Json::Value& jvResult, const Json::Value& jvRequest);
void doAccountTransactionSubscribe(Json::Value& jvResult, const Json::Value& jvRequest);
@@ -293,6 +296,10 @@ Json::Value WSConnection::invokeCommand(const Json::Value& jvRequest)
const char* pCommand;
doFuncPtr dfpFunc;
} commandsA[] = {
// Request-Response Commands:
{ "ledger_current", &WSConnection::doLedgerCurrent },
// Streaming commands:
{ "account_info_subscribe", &WSConnection::doAccountInfoSubscribe },
{ "account_info_unsubscribe", &WSConnection::doAccountInfoUnsubscribe },
{ "account_transaction_subscribe", &WSConnection::doAccountTransactionSubscribe },
@@ -541,6 +548,11 @@ void WSConnection::doLedgerAccountsUnsubscribe(Json::Value& jvResult, const Json
}
}
void WSConnection::doLedgerCurrent(Json::Value& jvResult, const Json::Value& jvRequest)
{
jvResult["ledger"] = theApp->getOPs().getCurrentLedgerID();
}
void WSConnection::doTransactionSubcribe(Json::Value& jvResult, const Json::Value& jvRequest)
{
if (!theApp->getOPs().subTransaction(this))

View File

@@ -1,4 +1,7 @@
// Manage test servers
//
// YYY Would be nice to be able to hide server output.
//
// Provide servers
//

View File

@@ -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