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 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');

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) {
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) {

View File

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