refactor ipc to a module

This commit is contained in:
Iuri Matias 2018-06-04 15:36:43 -04:00
parent 6e37c2d91b
commit 1821e89346
4 changed files with 73 additions and 37 deletions

View File

@ -1,6 +1,7 @@
const async = require('async'); const async = require('async');
const utils = require('../utils/utils'); const utils = require('../utils/utils');
const IPC = require('./ipc');
class Engine { class Engine {
constructor(options) { constructor(options) {
@ -182,7 +183,8 @@ class Engine {
compiler.compile_contracts(contractFiles, cb); 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('vyper');
this.registerModule('profiler'); this.registerModule('profiler');
this.registerModule('deploytracker'); this.registerModule('deploytracker');

57
lib/core/ipc.js Normal file
View File

@ -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;

View File

@ -6,6 +6,7 @@ class Solidity {
constructor(embark, options) { constructor(embark, options) {
this.logger = embark.logger; this.logger = embark.logger;
this.events = embark.events; this.events = embark.events;
this.ipc = options.ipc;
this.contractDirectories = embark.config.contractDirectories; this.contractDirectories = embark.config.contractDirectories;
this.solcAlreadyLoaded = false; this.solcAlreadyLoaded = false;
this.solcW = null; this.solcW = null;
@ -49,7 +50,7 @@ class Solidity {
if (self.solcAlreadyLoaded) { if (self.solcAlreadyLoaded) {
return callback(); 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.logger.info(__("loading solc compiler") + "..");
self.solcW.load_compiler(function (err) { self.solcW.load_compiler(function (err) {

View File

@ -11,6 +11,7 @@ class SolcW {
this.logger = options.logger; this.logger = options.logger;
this.events = options.events; this.events = options.events;
this.useIpc = options.useIpc; this.useIpc = options.useIpc;
this.ipc = options.ipc;
this.compilerLoaded = false; this.compilerLoaded = false;
this.solcProcess = null; this.solcProcess = null;
} }
@ -18,20 +19,9 @@ class SolcW {
load_compiler(done) { load_compiler(done) {
const self = this; const self = this;
if (self.useIpc) { if (self.useIpc) {
// try to connect, if it can't then continue
self.compilerLoaded = true; self.compilerLoaded = true;
ipc.config.silent = true; return self.ipc.connect(done);
function connecting(socket) {
ipc.of['embark'].on('connect',function() {
done();
});
}
ipc.connectTo('embark', fs.dappPath(".embark/embark.ipc"), connecting);
return;
} }
if (this.compilerLoaded) { if (this.compilerLoaded) {
return done(); return done();
@ -48,26 +38,12 @@ class SolcW {
done(); done();
}); });
ipc.config.silent = true; this.ipc.serve();
this.ipc.on('compile', (data, client) => {
function _connecting() { self.compile(data, (result) => {
ipc.server.on( self.ipc.reply(client, 'compilation', result);
'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.events.request("version:get:solc", function(solcVersion) { this.events.request("version:get:solc", function(solcVersion) {
if (solcVersion === currentSolcVersion) { if (solcVersion === currentSolcVersion) {
@ -93,11 +69,11 @@ class SolcW {
let a = new Date(); let a = new Date();
if (this.useIpc) { if (this.useIpc) {
ipc.of['embark'].once('message', function(msg) { this.ipc.once('compilation', (data) => {
done(msg.message); done(data);
}); });
ipc.of['embark'].emit('message', {action: 'compile', message: jsonObj}); this.ipc.request('compile', jsonObj);
return; return;
} }