web3.js/lib/web3/requestmanager.js

207 lines
5.3 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-22 08:37:12 +01:00
var RequestManager = function(provider) {
2015-03-21 23:34:24 +01:00
this.jsonrpc = new Jsonrpc();
2015-03-22 08:37:12 +01:00
this.provider = provider;
2015-03-21 23:10:34 +01:00
this.polls = [];
this.timeout = null;
this.poll();
};
2015-02-06 00:02:14 +01:00
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) {
2015-03-22 18:12:52 +01:00
console.error(errors.InvalidProvider);
2015-03-21 23:16:15 +01:00
return null;
}
2015-03-22 08:37:12 +01:00
var payload = this.jsonrpc.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-21 23:34:24 +01:00
if (!this.jsonrpc.isValidResponse(result)) {
2015-03-22 18:12:52 +01:00
throw errors.InvalidResponse;
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) {
2015-03-22 18:12:52 +01:00
return callback(errors.InvalidProvider);
2015-03-22 08:37:12 +01:00
}
var payload = this.jsonrpc.toPayload(data.method, data.params);
2015-03-21 23:34:24 +01:00
var self = this;
2015-03-21 23:10:34 +01:00
this.provider.sendAsync(payload, function (err, result) {
if (err) {
return callback(err);
}
2015-03-21 23:34:24 +01:00
if (!self.jsonrpc.isValidResponse(result)) {
2015-03-22 18:12:52 +01:00
return callback(errors.InvalidResponse);
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-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-03-02 13:00:20 +01:00
2015-03-21 23:10:34 +01:00
/*jshint maxparams:4 */
2015-02-06 00:02:14 +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) {
this.polls.push({data: data, id: pollId, callback: callback, uninstall: uninstall});
};
/*jshint maxparams:3 */
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) {
for (var i = this.polls.length; i--;) {
var poll = this.polls[i];
if (poll.id === pollId) {
this.polls.splice(i, 1);
2015-02-25 17:07:16 +01:00
}
2015-03-21 23:10:34 +01:00
}
};
/**
* Should be called to reset polling mechanism of request manager
*
* @method reset
*/
RequestManager.prototype.reset = function () {
this.polls.forEach(function (poll) {
poll.uninstall(poll.id);
});
this.polls = [];
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 () {
2015-03-22 08:37:12 +01:00
this.timeout = setTimeout(this.poll.bind(this), c.ETH_POLLING_TIMEOUT);
2015-03-23 15:41:02 +01:00
if (!this.polls.length) {
return;
}
2015-03-22 08:37:12 +01:00
if (!this.provider) {
2015-03-22 18:12:52 +01:00
console.error(errors.InvalidProvider);
2015-03-22 08:37:12 +01:00
return;
}
2015-03-21 23:10:34 +01:00
2015-03-22 08:37:12 +01:00
var payload = this.jsonrpc.toBatchPayload(this.polls.map(function (data) {
return data.data;
}));
var self = this;
this.provider.sendAsync(payload, function (error, results) {
// TODO: console log?
if (error) {
return;
}
if (!utils.isArray(results)) {
2015-03-22 18:12:52 +01:00
return console.error(errors.InvalidResponse);
2015-03-22 08:37:12 +01:00
}
results.map(function (result, index) {
result.callback = self.polls[index].callback;
return result;
}).filter(function (result) {
var valid = self.jsonrpc.isValidResponse(result);
if (!valid) {
2015-03-22 18:12:52 +01:00
result.callback(errors.InvalidResponse);
2015-03-22 08:37:12 +01:00
}
return valid;
}).filter(function (result) {
return utils.isArray(result.result) && result.result.length > 0;
}).forEach(function (result) {
result.callback(null, 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