web3.js/lib/web3/requestmanager.js

272 lines
6.9 KiB
JavaScript
Raw Normal View History

2015-02-06 00:02:14 +01:00
/*
This file is part of ethereum.js.
ethereum.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethereum.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
2015-03-22 18:12:52 +01:00
/**
* @file requestmanager.js
* @author Jeffrey Wilcke <jeff@ethdev.com>
* @author Marek Kotewicz <marek@ethdev.com>
* @author Marian Oancea <marian@ethdev.com>
* @author Fabian Vogelsteller <fabian@ethdev.com>
* @author Gav Wood <g@ethdev.com>
2015-02-06 00:02:14 +01:00
* @date 2014
*/
2015-03-21 23:34:24 +01:00
var Jsonrpc = require('./jsonrpc');
2015-03-21 23:16:15 +01:00
var utils = require('../utils/utils');
2015-03-08 18:18:52 +01:00
var c = require('../utils/config');
2015-03-22 18:12:52 +01:00
var errors = require('./errors');
2015-03-21 22:57:44 +01:00
2015-02-06 00:02:14 +01:00
/**
* It's responsible for passing messages to providers
* It's also responsible for polling the ethereum node for incoming messages
* Default poll timeout is 1 second
2015-03-26 15:43:35 +01:00
* Singleton
2015-02-06 00:02:14 +01:00
*/
2015-03-26 15:43:35 +01:00
var RequestManager = function (provider) {
// singleton pattern
2015-10-07 04:32:32 +02:00
//if (arguments.callee._singletonInstance) {
//return arguments.callee._singletonInstance;
//}
//arguments.callee._singletonInstance = this;
2015-03-26 15:43:35 +01:00
2015-03-22 08:37:12 +01:00
this.provider = provider;
2015-06-08 14:38:16 +02:00
this.polls = {};
2015-03-21 23:10:34 +01:00
this.timeout = null;
this.isPolling = false;
2015-03-21 23:10:34 +01:00
};
2015-02-06 00:02:14 +01:00
2015-03-26 15:43:35 +01:00
/**
* @return {RequestManager} singleton
*/
RequestManager.getInstance = function () {
var instance = new RequestManager();
return instance;
};
2015-03-21 23:10:34 +01:00
/**
* Should be used to synchronously send request
*
* @method send
2015-03-22 19:48:25 +01:00
* @param {Object} data
2015-03-21 23:10:34 +01:00
* @return {Object}
*/
RequestManager.prototype.send = function (data) {
2015-03-21 23:16:15 +01:00
if (!this.provider) {
console.error(errors.InvalidProvider());
2015-03-21 23:16:15 +01:00
return null;
}
2015-03-26 15:43:35 +01:00
var payload = Jsonrpc.getInstance().toPayload(data.method, data.params);
2015-03-21 23:10:34 +01:00
var result = this.provider.send(payload);
2015-02-24 13:00:24 +01:00
2015-03-26 15:43:35 +01:00
if (!Jsonrpc.getInstance().isValidResponse(result)) {
throw errors.InvalidResponse(result);
2015-03-21 23:10:34 +01:00
}
return result.result;
};
2015-03-10 11:03:03 +01:00
2015-03-21 23:10:34 +01:00
/**
* Should be used to asynchronously send request
*
* @method sendAsync
2015-03-22 19:48:25 +01:00
* @param {Object} data
2015-03-21 23:10:34 +01:00
* @param {Function} callback
*/
RequestManager.prototype.sendAsync = function (data, callback) {
2015-03-22 08:37:12 +01:00
if (!this.provider) {
return callback(errors.InvalidProvider());
2015-03-22 08:37:12 +01:00
}
2015-03-26 15:43:35 +01:00
var payload = Jsonrpc.getInstance().toPayload(data.method, data.params);
2015-03-21 23:10:34 +01:00
this.provider.sendAsync(payload, function (err, result) {
if (err) {
return callback(err);
}
2015-03-26 15:43:35 +01:00
if (!Jsonrpc.getInstance().isValidResponse(result)) {
return callback(errors.InvalidResponse(result));
2015-02-06 00:02:14 +01:00
}
2015-03-21 23:10:34 +01:00
callback(null, result.result);
});
};
2015-03-02 13:00:20 +01:00
2015-05-12 07:01:33 +02:00
/**
* Should be called to asynchronously send batch request
*
* @method sendBatch
* @param {Array} batch data
* @param {Function} callback
*/
RequestManager.prototype.sendBatch = function (data, callback) {
if (!this.provider) {
return callback(errors.InvalidProvider());
}
var payload = Jsonrpc.getInstance().toBatchPayload(data);
this.provider.sendAsync(payload, function (err, results) {
if (err) {
return callback(err);
}
if (!utils.isArray(results)) {
return callback(errors.InvalidResponse(results));
}
callback(err, results);
});
};
2015-03-21 23:10:34 +01:00
/**
* Should be used to set provider of request manager
*
* @method setProvider
* @param {Object}
*/
RequestManager.prototype.setProvider = function (p) {
this.provider = p;
2015-06-15 07:00:38 +02:00
if (this.provider && !this.isPolling) {
this.poll();
this.isPolling = true;
}
2015-03-21 23:10:34 +01:00
};
2015-03-02 13:00:20 +01:00
2015-03-21 23:10:34 +01:00
/**
* Should be used to start polling
*
* @method startPolling
2015-03-22 19:48:25 +01:00
* @param {Object} data
* @param {Number} pollId
* @param {Function} callback
* @param {Function} uninstall
2015-03-21 23:10:34 +01:00
*
* @todo cleanup number of params
*/
RequestManager.prototype.startPolling = function (data, pollId, callback, uninstall) {
2015-08-06 18:12:06 +02:00
this.polls[pollId] = {data: data, id: pollId, callback: callback, uninstall: uninstall};
2015-03-21 23:10:34 +01:00
};
2015-02-25 17:07:16 +01:00
2015-03-21 23:10:34 +01:00
/**
* Should be used to stop polling for filter with given id
*
* @method stopPolling
2015-03-22 19:48:25 +01:00
* @param {Number} pollId
2015-03-21 23:10:34 +01:00
*/
RequestManager.prototype.stopPolling = function (pollId) {
2015-08-06 18:12:06 +02:00
delete this.polls[pollId];
2015-03-21 23:10:34 +01:00
};
/**
2015-06-08 14:38:16 +02:00
* Should be called to reset the polling mechanism of the request manager
2015-03-21 23:10:34 +01:00
*
* @method reset
*/
2015-09-15 15:52:57 +02:00
RequestManager.prototype.reset = function (keepIsSyncing) {
2015-06-08 14:38:16 +02:00
for (var key in this.polls) {
// remove all polls, except sync polls,
// they need to be removed manually by calling syncing.stopWatching()
2015-09-15 15:52:57 +02:00
if(!keepIsSyncing || key.indexOf('syncPoll_') === -1) {
this.polls[key].uninstall();
delete this.polls[key];
}
2015-06-08 14:38:16 +02:00
}
2015-03-21 23:10:34 +01:00
2015-03-22 08:37:12 +01:00
if (this.timeout) {
clearTimeout(this.timeout);
this.timeout = null;
2015-03-21 23:10:34 +01:00
}
this.poll();
};
/**
* Should be called to poll for changes on filter with given id
*
* @method poll
*/
RequestManager.prototype.poll = function () {
/*jshint maxcomplexity: 6 */
2015-03-22 08:37:12 +01:00
this.timeout = setTimeout(this.poll.bind(this), c.ETH_POLLING_TIMEOUT);
2015-06-15 07:00:38 +02:00
if (Object.keys(this.polls).length === 0) {
2015-03-23 15:41:02 +01:00
return;
}
2015-03-22 08:37:12 +01:00
if (!this.provider) {
console.error(errors.InvalidProvider());
2015-03-22 08:37:12 +01:00
return;
}
2015-03-21 23:10:34 +01:00
2015-06-08 14:38:16 +02:00
var pollsData = [];
var pollsIds = [];
2015-06-08 14:38:16 +02:00
for (var key in this.polls) {
2015-06-15 07:00:38 +02:00
pollsData.push(this.polls[key].data);
pollsIds.push(key);
2015-06-08 14:38:16 +02:00
}
if (pollsData.length === 0) {
return;
}
var payload = Jsonrpc.getInstance().toBatchPayload(pollsData);
// map the request id to they poll id
var pollsIdMap = {};
payload.forEach(function(load, index){
pollsIdMap[load.id] = pollsIds[index];
});
2015-03-22 08:37:12 +01:00
var self = this;
this.provider.sendAsync(payload, function (error, results) {
2015-03-22 08:37:12 +01:00
// TODO: console log?
if (error) {
return;
}
2015-06-08 14:38:16 +02:00
2015-03-22 08:37:12 +01:00
if (!utils.isArray(results)) {
throw errors.InvalidResponse(results);
2015-03-22 08:37:12 +01:00
}
2015-09-15 10:55:06 +02:00
results.map(function (result) {
var id = pollsIdMap[result.id];
2015-06-05 11:04:06 +02:00
// make sure the filter is still installed after arrival of the request
if (self.polls[id]) {
result.callback = self.polls[id].callback;
2015-06-05 11:04:06 +02:00
return result;
} else
return false;
2015-03-22 08:37:12 +01:00
}).filter(function (result) {
2015-06-15 07:00:38 +02:00
return !!result;
2015-06-08 14:38:16 +02:00
}).filter(function (result) {
2015-03-26 15:43:35 +01:00
var valid = Jsonrpc.getInstance().isValidResponse(result);
2015-03-22 08:37:12 +01:00
if (!valid) {
result.callback(errors.InvalidResponse(result));
2015-03-22 08:37:12 +01:00
}
return valid;
}).forEach(function (result) {
2015-03-25 13:17:21 +01:00
result.callback(null, result.result);
2015-02-06 00:02:14 +01:00
});
2015-03-21 23:10:34 +01:00
});
2015-02-06 00:02:14 +01:00
};
2015-03-21 23:16:15 +01:00
module.exports = RequestManager;
2015-02-06 00:02:14 +01:00