Remote: Only log set_state when the state will actually change. Only set state online when connection_count === 1

This commit is contained in:
wltsmrz
2013-12-19 17:46:57 -08:00
parent 1b6945f74d
commit 6cdeacdb90
2 changed files with 20 additions and 10 deletions

View File

@@ -267,13 +267,16 @@ Remote.prototype.addServer = function(opts) {
server.on('message', serverMessage); server.on('message', serverMessage);
function serverConnect() { function serverConnect() {
self._connection_count++;
self._set_state('online');
if (opts.primary || !self._primary_server) { if (opts.primary || !self._primary_server) {
self._setPrimaryServer(server); self._setPrimaryServer(server);
} }
if (self._connection_count === self._servers.length) { switch (++self._connection_count) {
self.emit('ready'); case 1:
self._setState('online');
break;
case self._servers.length:
self.emit('ready');
break;
} }
}; };
@@ -281,8 +284,8 @@ Remote.prototype.addServer = function(opts) {
function serverDisconnect() { function serverDisconnect() {
self._connection_count--; self._connection_count--;
if (!self._connection_count) { if (self._connection_count === 0) {
self._set_state('offline'); self._setState('offline');
} }
}; };
@@ -300,9 +303,9 @@ Remote.prototype.serverFatal = function() {
// Set the emitted state: 'online' or 'offline' // Set the emitted state: 'online' or 'offline'
Remote.prototype._setState = function(state) { Remote.prototype._setState = function(state) {
this._trace('remote: set_state: %s', state);
if (this.state !== state) { if (this.state !== state) {
this._trace('remote: set_state: %s', state);
this.state = state; this.state = state;
this.emit('state', state); this.emit('state', state);
@@ -343,6 +346,8 @@ Remote.prototype._trace = function() {
/** /**
* Connect to the Ripple network. * Connect to the Ripple network.
*
* param {Function} callback
*/ */
Remote.prototype.connect = function(online) { Remote.prototype.connect = function(online) {
@@ -353,9 +358,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:
// Downwards compatibility // Downwards compatibility
if (!Boolean(online)) { if (!Boolean(online)) {
@@ -762,6 +769,7 @@ Remote.prototype.requestTransactionEntry = function(hash, ledger_hash, callback)
case 'string': case 'string':
request.ledgerHash(ledger_hash); request.ledgerHash(ledger_hash);
break; break;
default: default:
request.ledgerIndex('validated'); request.ledgerIndex('validated');
callback = ledger_hash; callback = ledger_hash;
@@ -918,7 +926,7 @@ Remote.prototype.requestBookOffers = function(gets, pays, taker, callback) {
if (typeof lastArg === 'function') { if (typeof lastArg === 'function') {
callback = lastArg; callback = lastArg;
} }
var request = new Request(this, 'book_offers'); var request = new Request(this, 'book_offers');
request.message.taker_gets = { request.message.taker_gets = {

View File

@@ -29,7 +29,7 @@ function Server(remote, opts) {
this._ws = void(0); this._ws = void(0);
this._connected = false; this._connected = false;
this._shouldConnect = false; this._shouldConnect = false;
this._state = void(0); this._state = 'offline';
this._id = 0; this._id = 0;
this._retry = 0; this._retry = 0;
this._requests = { }; this._requests = { };
@@ -110,6 +110,8 @@ Server.onlineStates = [
Server.prototype._setState = function(state) { Server.prototype._setState = function(state) {
if (state !== this._state) { if (state !== this._state) {
this._remote._trace('server: set_state: %s', state);
this._state = state; this._state = state;
this.emit('state', state); this.emit('state', state);