Connect to servers on an interval

This commit is contained in:
wltsmrz
2013-07-26 06:52:20 +09:00
parent d08e06ad53
commit 4e876511a3

View File

@@ -319,9 +319,9 @@ function Remote(opts, trace) {
this.local_sequence = opts.local_sequence; // Locally track sequence numbers this.local_sequence = opts.local_sequence; // Locally track sequence numbers
this.local_fee = opts.local_fee; // Locally set fees this.local_fee = opts.local_fee; // Locally set fees
this.local_signing = (typeof opts.local_signing === 'undefined') this.local_signing = (typeof opts.local_signing === 'undefined')
? true : opts.local_signing; ? true : Boolean(opts.local_signing);
this.fee_cushion = (typeof opts.fee_cushion === 'undefined') this.fee_cushion = (typeof opts.fee_cushion === 'undefined')
? 1.5 : opts.fee_cushion; ? 1.5 : Number(opts.fee_cushion);
this.id = 0; this.id = 0;
this.trace = opts.trace || trace; this.trace = opts.trace || trace;
@@ -352,10 +352,10 @@ function Remote(opts, trace) {
// Local signing implies local fees and sequences // Local signing implies local fees and sequences
if (this.local_signing) { if (this.local_signing) {
this.local_sequence = true; this.local_sequence = true;
this.local_fee = true; this.local_fee = true;
} }
this._servers = [ ]; this._servers = [ ];
this._primary_server = void(0); this._primary_server = void(0);
// Cache information for accounts. // Cache information for accounts.
@@ -367,13 +367,13 @@ function Remote(opts, trace) {
// account : { seq : __ } // account : { seq : __ }
}; };
// Hash map of Account objects by AccountId. // Account objects by AccountId.
this._accounts = {}; this._accounts = {};
// Hash map of OrderBook objects // OrderBook objects
this._books = {}; this._books = {};
// List of secrets that we know about. // Secrets that we know about.
this.secrets = { this.secrets = {
// Secrets can be set by calling set_secret(account, secret). // Secrets can be set by calling set_secret(account, secret).
@@ -383,8 +383,8 @@ function Remote(opts, trace) {
// Cache for various ledgers. // Cache for various ledgers.
// XXX Clear when ledger advances. // XXX Clear when ledger advances.
this.ledgers = { this.ledgers = {
'current' : { current : {
'account_root' : {} account_root : {}
} }
}; };
@@ -491,11 +491,14 @@ Remote.prototype.add_server = function (opts) {
}); });
server.on('connect', function () { server.on('connect', function () {
self._connection_count++;
self._set_state('online');
if (opts.primary || !self._primary_server) { if (opts.primary || !self._primary_server) {
self._set_primary_server(server); self._set_primary_server(server);
} }
self._connection_count++; if (self._connection_count === self._servers.length) {
self._set_state('online'); self.emit('ready');
}
}); });
server.on('disconnect', function () { server.on('disconnect', function () {
@@ -555,9 +558,11 @@ Remote.prototype.connect = function (online) {
switch(typeof online) { switch(typeof online) {
case 'undefined': case 'undefined':
break; break;
case 'function': case 'function':
this.once('connect', online); this.once('connect', online);
break; break;
default: default:
if (!Boolean(online)) if (!Boolean(online))
return this.disconnect() return this.disconnect()
@@ -567,9 +572,15 @@ Remote.prototype.connect = function (online) {
if (!this._servers.length) { if (!this._servers.length) {
throw new Error('No servers available.'); throw new Error('No servers available.');
} else { } else {
for (var i=0; i<this._servers.length; i++) { var servers = this._servers;
this._servers[i].connect(); ;(function nextServer(i) {
} var server = servers[i];
server._sid = i;
server.connect();
if (++i < servers.length) {
setTimeout(nextServer.bind(this, i), 1000 * 5);
}
})(0);
} }
return this; return this;
@@ -579,7 +590,7 @@ Remote.prototype.connect = function (online) {
* Disconnect from the Ripple network. * Disconnect from the Ripple network.
*/ */
Remote.prototype.disconnect = function (online) { Remote.prototype.disconnect = function (online) {
for (var i = 0, l = this._servers.length; i < l; i++) { for (var i=0, l=this._servers.length; i<l; i++) {
this._servers[i].disconnect(); this._servers[i].disconnect();
} }