From 1821e8934699887cc3e740957b1e7c866b70e7df Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 4 Jun 2018 15:36:43 -0400 Subject: [PATCH] refactor ipc to a module --- lib/core/engine.js | 4 ++- lib/core/ipc.js | 57 +++++++++++++++++++++++++++++++++++ lib/modules/solidity/index.js | 3 +- lib/modules/solidity/solcW.js | 46 +++++++--------------------- 4 files changed, 73 insertions(+), 37 deletions(-) create mode 100644 lib/core/ipc.js diff --git a/lib/core/engine.js b/lib/core/engine.js index c167b5f8c..f31d770d6 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -1,6 +1,7 @@ const async = require('async'); const utils = require('../utils/utils'); +const IPC = require('./ipc'); class Engine { constructor(options) { @@ -182,7 +183,8 @@ class Engine { compiler.compile_contracts(contractFiles, cb); }); - this.registerModule('solidity', {useIpc: options.useIpc}); + this.ipc = new IPC({logger: this.logger}); + this.registerModule('solidity', {useIpc: options.useIpc, ipc: this.ipc}); this.registerModule('vyper'); this.registerModule('profiler'); this.registerModule('deploytracker'); diff --git a/lib/core/ipc.js b/lib/core/ipc.js new file mode 100644 index 000000000..efc3b9778 --- /dev/null +++ b/lib/core/ipc.js @@ -0,0 +1,57 @@ +let fs = require('./fs.js'); +let ipc = require('node-ipc'); + +class IPC { + + constructor(options) { + this.logger = options.logger; + this.socketPath = options.socketPath || fs.dappPath(".embark/embark.ipc"); + ipc.config.silent = true; + } + + connect(done) { + function connecting(socket) { + ipc.of['embark'].on('connect',function() { + done(); + }); + } + + ipc.connectTo('embark', this.socketPath, connecting); + } + + serve() { + ipc.serve(this.socketPath, () => {}) + ipc.server.start() + + this.logger.info(`pid ${process.pid} listening on ${this.socketPath}`); + } + + on(action, done) { + ipc.server.on('message', function(data, socket) { + if (data.action !== action) { + return; + } + done(data.message, socket); + }); + } + + reply(client, action, data) { + ipc.server.emit(client, 'message', {action: action, message: data}); + } + + once(action, cb) { + ipc.of['embark'].once('message', function(msg) { + if (msg.action !== action) { + return; + } + cb(msg.message); + }); + } + + request(action, data) { + ipc.of['embark'].emit('message', {action: action, message: data}); + } + +} + +module.exports = IPC; diff --git a/lib/modules/solidity/index.js b/lib/modules/solidity/index.js index 3bfc75835..d7b533575 100644 --- a/lib/modules/solidity/index.js +++ b/lib/modules/solidity/index.js @@ -6,6 +6,7 @@ class Solidity { constructor(embark, options) { this.logger = embark.logger; this.events = embark.events; + this.ipc = options.ipc; this.contractDirectories = embark.config.contractDirectories; this.solcAlreadyLoaded = false; this.solcW = null; @@ -49,7 +50,7 @@ class Solidity { if (self.solcAlreadyLoaded) { return callback(); } - self.solcW = new SolcW({logger: self.logger, events: self.events, useIpc: self.useIpc}); + self.solcW = new SolcW({logger: self.logger, events: self.events, useIpc: self.useIpc, ipc: self.ipc}); self.logger.info(__("loading solc compiler") + ".."); self.solcW.load_compiler(function (err) { diff --git a/lib/modules/solidity/solcW.js b/lib/modules/solidity/solcW.js index 2be126f03..2e77e01c9 100644 --- a/lib/modules/solidity/solcW.js +++ b/lib/modules/solidity/solcW.js @@ -11,6 +11,7 @@ class SolcW { this.logger = options.logger; this.events = options.events; this.useIpc = options.useIpc; + this.ipc = options.ipc; this.compilerLoaded = false; this.solcProcess = null; } @@ -18,20 +19,9 @@ class SolcW { load_compiler(done) { const self = this; if (self.useIpc) { - // try to connect, if it can't then continue self.compilerLoaded = true; - ipc.config.silent = true; - - function connecting(socket) { - ipc.of['embark'].on('connect',function() { - done(); - }); - } - - ipc.connectTo('embark', fs.dappPath(".embark/embark.ipc"), connecting); - - return; + return self.ipc.connect(done); } if (this.compilerLoaded) { return done(); @@ -48,26 +38,12 @@ class SolcW { done(); }); - ipc.config.silent = true; - - function _connecting() { - ipc.server.on( - 'message', - function(data, socket) { - if (data.action != 'compile') { - return; - } - - self.compile(data.message, (result) => { - ipc.server.emit(socket, 'message', {action: 'compilation', message: result}); - }); - } - ); - } - - ipc.serve(fs.dappPath(".embark/embark.ipc"), _connecting) - ipc.server.start() - this.logger.info(`pid ${process.pid} listening on ${fs.dappPath(".embark/embark.ipc")}`); + this.ipc.serve(); + this.ipc.on('compile', (data, client) => { + self.compile(data, (result) => { + self.ipc.reply(client, 'compilation', result); + }); + }); this.events.request("version:get:solc", function(solcVersion) { if (solcVersion === currentSolcVersion) { @@ -93,11 +69,11 @@ class SolcW { let a = new Date(); if (this.useIpc) { - ipc.of['embark'].once('message', function(msg) { - done(msg.message); + this.ipc.once('compilation', (data) => { + done(data); }); - ipc.of['embark'].emit('message', {action: 'compile', message: jsonObj}); + this.ipc.request('compile', jsonObj); return; }