diff --git a/src/js/ripple/remote.js b/src/js/ripple/remote.js index e608292f..c87d081b 100644 --- a/src/js/ripple/remote.js +++ b/src/js/ripple/remote.js @@ -42,6 +42,7 @@ var sjcl = require('../../../build/sjcl'); trace max_listeners : Set maxListeners for remote; prevents EventEmitter warnings + connection_offset : Connect to remote servers on supplied interval (in seconds) trusted : truthy, if remote is trusted max_fee : Maximum acceptable transaction fee fee_cushion : Extra fee multiplier to account for async fee changes. @@ -85,8 +86,8 @@ function Remote(opts, trace) { this.fee_cushion = (typeof opts.fee_cushion === 'undefined') ? 1.5 : Number(opts.fee_cushion); this.max_fee = (typeof opts.max_fee === 'undefined') ? Infinity : Number(opts.max_fee); this.id = 0; - this.trace = opts.trace || trace; - this._server_fatal = false; // True, if we know server exited. + this.trace = Boolean(opts.trace); + this._server_fatal = false; // True, if we know server exited. this._ledger_current_index = void(0); this._ledger_hash = void(0); this._ledger_time = void(0); @@ -94,8 +95,8 @@ function Remote(opts, trace) { this._testnet = void(0); this._transaction_subs = 0; this.online_target = false; - this._online_state = 'closed'; // 'open', 'closed', 'connecting', 'closing' - this.state = 'offline'; // 'online', 'offline' + this._online_state = 'closed'; // 'open', 'closed', 'connecting', 'closing' + this.state = 'offline'; // 'online', 'offline' this.retry_timer = void(0); this.retry = void(0); @@ -107,6 +108,7 @@ function Remote(opts, trace) { this._reserve_inc = void(0); this._connection_count = 0; this._connected = false; + this._connection_offset = 1000 * (Number(opts.connection_offset) || 5); this._last_tx = null; this._cur_path_find = null; @@ -207,12 +209,20 @@ Remote.flags = { } }; +function isTemMalformed(engine_result_code) { + return (engine_result_code >= -299 && engine_result_code < 199); +}; + +function isTefFailure(engine_result_code) { + return (engine_result_code >= -299 && engine_result_code < 199); +}; + Remote.from_config = function (obj, trace) { var serverConfig = typeof obj === 'string' ? config.servers[obj] : obj; var remote = new Remote(serverConfig, trace); - Object.keys(config.accounts).forEach(function(account) { + function initialize_account(account) { var accountInfo = config.accounts[account]; if (typeof accountInfo === 'object') { if (accountInfo.secret) { @@ -222,7 +232,13 @@ Remote.from_config = function (obj, trace) { remote.set_secret(accountInfo.account, accountInfo.secret); } } - }); + } + + if (typeof config.accounts === 'object') { + for (var account in config.accounts) { + initialize_account(account); + } + } return remote; }; @@ -233,14 +249,6 @@ Remote.create_remote = function(options, callback) { return remote; }; -var isTemMalformed = function (engine_result_code) { - return (engine_result_code >= -299 && engine_result_code < 199); -}; - -var isTefFailure = function (engine_result_code) { - return (engine_result_code >= -299 && engine_result_code < 199); -}; - Remote.prototype.add_server = function (opts) { var self = this; @@ -249,13 +257,13 @@ Remote.prototype.add_server = function (opts) { (opts.port || opts.websocket_port) ; - var server = new Server(this, {url: url}); + var server = new Server(this, { url: url }); - server.on('message', function (data) { - self._handle_message(data); - }); + function server_message(data) { + self._handle_message(data, server); + } - server.on('connect', function () { + function server_connect() { self._connection_count++; self._set_state('online'); if (opts.primary || !self._primary_server) { @@ -264,14 +272,18 @@ Remote.prototype.add_server = function (opts) { if (self._connection_count === self._servers.length) { self.emit('ready'); } - }); + } - server.on('disconnect', function () { + function server_disconnect() { self._connection_count--; if (!self._connection_count) { self._set_state('offline'); } - }); + } + + server.on('message', server_message); + server.on('connect', server_connect); + server.on('disconnect', server_disconnect); this._servers.push(server); @@ -285,9 +297,7 @@ Remote.prototype.server_fatal = function () { // Set the emitted state: 'online' or 'offline' Remote.prototype._set_state = function (state) { - if (this.trace) { - console.log('remote: set_state: %s', state); - } + this._trace('remote: set_state: %s', state); if (this.state !== state) { this.state = state; @@ -317,12 +327,18 @@ Remote.prototype.set_trace = function (trace) { return this; }; +Remote.prototype._trace = function() { + if (this.trace) { + utils.logObject.apply(utils, arguments); + } +}; + /** * Connect to the Ripple network. */ Remote.prototype.connect = function (online) { // Downwards compatibility - switch(typeof online) { + switch (typeof online) { case 'undefined': break; @@ -340,13 +356,18 @@ Remote.prototype.connect = function (online) { if (!this._servers.length) { throw new Error('No servers available.'); } else { - var servers = this._servers; + var self = this; + ;(function nextServer(i) { - var server = servers[i]; - server._sid = i; + var server = self._servers[i]; + + server._sid = ++i; server.connect(); - if (++i < servers.length) { - setTimeout(nextServer.bind(this, i), 1000 * 5); + + if (i < self._servers.length) { + setTimeout(function() { + nextServer(i); + }, self._connection_offset); } })(0); } @@ -358,31 +379,22 @@ Remote.prototype.connect = function (online) { * Disconnect from the Ripple network. */ Remote.prototype.disconnect = function (online) { - for (var i=0, l=this._servers.length; i