providermanager.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. This file is part of ethereum.js.
  3. ethereum.js is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. ethereum.js is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file providermanager.js
  15. * @authors:
  16. * Jeffrey Wilcke <jeff@ethdev.com>
  17. * Marek Kotewicz <marek@ethdev.com>
  18. * Marian Oancea <marian@ethdev.com>
  19. * Gav Wood <g@ethdev.com>
  20. * @date 2014
  21. */
  22. var web3 = require('./web3');
  23. var jsonrpc = require('./jsonrpc');
  24. /**
  25. * Provider manager object prototype
  26. * It's responsible for passing messages to providers
  27. * If no provider is set it's responsible for queuing requests
  28. * It's also responsible for polling the ethereum node for incoming messages
  29. * Default poll timeout is 12 seconds
  30. * If we are running ethereum.js inside ethereum browser, there are backend based tools responsible for polling,
  31. * and provider manager polling mechanism is not used
  32. */
  33. var ProviderManager = function() {
  34. this.polls = [];
  35. this.provider = undefined;
  36. var self = this;
  37. var poll = function () {
  38. self.polls.forEach(function (data) {
  39. var result = self.send(data.data);
  40. if (!(result instanceof Array) || result.length === 0) {
  41. return;
  42. }
  43. data.callback(result);
  44. });
  45. setTimeout(poll, 1000);
  46. };
  47. poll();
  48. };
  49. /// sends outgoing requests
  50. /// @params data - an object with at least 'method' property
  51. ProviderManager.prototype.send = function(data) {
  52. var payload = jsonrpc.toPayload(data.method, data.params);
  53. if (this.provider === undefined) {
  54. console.error('provider is not set');
  55. return null;
  56. }
  57. var result = this.provider.send(payload);
  58. if (!jsonrpc.isValidResponse(result)) {
  59. console.log(result);
  60. return null;
  61. }
  62. return result.result;
  63. };
  64. /// setups provider, which will be used for sending messages
  65. ProviderManager.prototype.set = function(provider) {
  66. this.provider = provider;
  67. };
  68. /// this method is only used, when we do not have native qt bindings and have to do polling on our own
  69. /// should be callled, on start watching for eth/shh changes
  70. ProviderManager.prototype.startPolling = function (data, pollId, callback) {
  71. this.polls.push({data: data, id: pollId, callback: callback});
  72. };
  73. /// should be called to stop polling for certain watch changes
  74. ProviderManager.prototype.stopPolling = function (pollId) {
  75. for (var i = this.polls.length; i--;) {
  76. var poll = this.polls[i];
  77. if (poll.id === pollId) {
  78. this.polls.splice(i, 1);
  79. }
  80. }
  81. };
  82. module.exports = ProviderManager;