|
| 1 | +/* |
| 2 | + * Copyright 2019-2020 Fujitsu Laboratories Ltd. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * ServerMonitorPlugin.js |
| 6 | + */ |
| 7 | + |
| 8 | +/* |
| 9 | + * Summary: |
| 10 | + * Connector: monitoring class of a part dependent on end-chains |
| 11 | + * Used in the case of monitoring continuously. |
| 12 | + * Processing that ends with the acceptance of a single event is handled by a custom function implementation in server plugins. |
| 13 | + * Unlike server plugins, it basically does not handle its own functions. |
| 14 | + */ |
| 15 | + |
| 16 | +// configuration file |
| 17 | +var SplugConfig = require('./PluginConfig.js'); |
| 18 | +var config = require('config'); |
| 19 | +// Log settings |
| 20 | +var log4js = require('log4js'); |
| 21 | +var logger = log4js.getLogger('ServerMonitorPlugin[' + process.pid + ']'); |
| 22 | +logger.level = config.logLevel; |
| 23 | +// Load libraries, SDKs, etc. according to specifications of endchains as needed |
| 24 | +var Web3 = require('web3'); |
| 25 | + |
| 26 | +/* |
| 27 | + * ServerMonitorPlugin |
| 28 | + * Class definitions of server monitoring |
| 29 | + */ |
| 30 | +var ServerMonitorPlugin = class { |
| 31 | + /* |
| 32 | + * constructors |
| 33 | + */ |
| 34 | + constructor(){ |
| 35 | + // Define dependent specific settings |
| 36 | + // Initialize monitored filter |
| 37 | + this._filterTable = {}; |
| 38 | + } |
| 39 | + |
| 40 | + /* |
| 41 | + * startMonitor |
| 42 | + * Start Monitoring |
| 43 | + * @param {string} clientId: Client ID from which monitoring start request was made |
| 44 | + * @param {function} cb: A callback function that receives monitoring results at any time. |
| 45 | + */ |
| 46 | + startMonitor(clientId, cb) { |
| 47 | + logger.info('*** START MONITOR ***'); |
| 48 | + logger.info('Client ID :' + clientId); |
| 49 | + // Implement handling to receive events from an end-chain and return them in a callback function |
| 50 | + var filter = this._filterTable[clientId]; |
| 51 | + if (!filter) { |
| 52 | + logger.info('create new web3 filter and start watching.'); |
| 53 | + try { |
| 54 | + var web3 = new Web3(); |
| 55 | + var provider = new web3.providers.HttpProvider(SplugConfig.provider); |
| 56 | + web3.setProvider(provider); |
| 57 | + filter = web3.eth.filter("latest"); |
| 58 | + // filter should be managed by client ID |
| 59 | + // (You should never watch multiple urls from the same client) |
| 60 | + this._filterTable[clientId] = filter; |
| 61 | + filter.watch(function (error, blockHash) { |
| 62 | + if (!error) { |
| 63 | + console.log('##[HL-BC] Notify new block data(D2)'); |
| 64 | + var blockData = web3.eth.getBlock(blockHash, true); |
| 65 | + var trLength = blockData.transactions.length; |
| 66 | + logger.info(trLength + " transactions in block " + blockData.number); |
| 67 | + console.log('##[HL-BC] Validate transactions(D3)'); |
| 68 | + console.log('##[HL-BC] digital sign on valid transaction(D4)'); |
| 69 | + if (trLength > 0) { |
| 70 | + logger.info('*** SEND BLOCK DATA ***'); |
| 71 | + // Notify only if transaction exists |
| 72 | + var ret_obj = { |
| 73 | + "status" : 200, |
| 74 | + "blockData" : blockData |
| 75 | + }; |
| 76 | + cb(ret_obj); |
| 77 | + } |
| 78 | + } else { |
| 79 | + var err_obj = { |
| 80 | + "status" : 504, |
| 81 | + "errorDetail" : error |
| 82 | + }; |
| 83 | + cb(err_obj); |
| 84 | + } |
| 85 | + }); |
| 86 | + } catch (e) { |
| 87 | + var emsg = e.toString().replace(/Error: /g , ""); |
| 88 | + var err_obj = { |
| 89 | + "status" : 504, |
| 90 | + "errorDetail" : emsg |
| 91 | + }; |
| 92 | + cb(err_obj); |
| 93 | + } |
| 94 | + } else { |
| 95 | + logger.info('target filter has already start watching.'); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /* |
| 100 | + * stopMonitor |
| 101 | + * monitoring stop |
| 102 | + * @param {string} clientId: Client ID from which monitoring stop request was made |
| 103 | + */ |
| 104 | + stopMonitor(clientId) { |
| 105 | + // Implement a process to end EC monitoring |
| 106 | + var filter = this._filterTable[clientId]; |
| 107 | + if (filter) { |
| 108 | + // Stop the filter & Remove it from table |
| 109 | + logger.info('stop watching and remove filter.'); |
| 110 | + filter.stopWatching(); |
| 111 | + delete this._filterTable[clientId]; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | +} /* class */ |
| 116 | + |
| 117 | +module.exports = ServerMonitorPlugin; |
| 118 | + |
0 commit comments