assign roles so ipc connections don't conflict; fallback if can't connect to ipc

This commit is contained in:
Iuri Matias 2018-06-04 18:15:27 -04:00
parent a57bce2a40
commit c0e559a7d9
4 changed files with 48 additions and 11 deletions

View File

@ -183,7 +183,7 @@ class Engine {
compiler.compile_contracts(contractFiles, cb);
});
this.ipc = new IPC({logger: this.logger});
this.ipc = new IPC({logger: this.logger, ipcRole: options.ipcRole});
this.registerModule('solidity', {useIpc: options.useIpc, ipc: this.ipc});
this.registerModule('vyper');
this.registerModule('profiler');

View File

@ -6,13 +6,31 @@ class IPC {
constructor(options) {
this.logger = options.logger;
this.socketPath = options.socketPath || fs.dappPath(".embark/embark.ipc");
this.ipcRole = options.ipcRole || "server";
ipc.config.silent = true;
this.connected = false;
}
connect(done) {
const self = this;
function connecting(socket) {
let connectedBefore = false, alreadyDisconnected = false;;
ipc.of['embark'].on('connect',function() {
done();
connectedBefore = true;
if (!alreadyDisconnected) {
self.connected = true;
done();
}
});
ipc.of['embark'].on('disconnect',function() {
self.connected = false;
ipc.disconnect('embark');
// we only want to trigger the error callback the first time
if (!connectedBefore && !alreadyDisconnected) {
alreadyDisconnected = true;
done(new Error("no connection found"));
}
});
}
@ -59,6 +77,14 @@ class IPC {
ipc.of['embark'].emit('message', {action: action, message: data});
}
isClient() {
return this.ipcRole === 'client';
}
isServer() {
return this.ipcRole === 'server';
}
}
module.exports = IPC;

View File

@ -10,7 +10,6 @@ class SolcW {
constructor(options) {
this.logger = options.logger;
this.events = options.events;
this.useIpc = options.useIpc;
this.ipc = options.ipc;
this.compilerLoaded = false;
this.solcProcess = null;
@ -18,11 +17,21 @@ class SolcW {
load_compiler(done) {
const self = this;
if (self.useIpc) {
self.compilerLoaded = true;
return self.ipc.connect(done);
if (self.ipc.isClient()) {
return self.ipc.connect((err) => {
if (err) {
return self.load_compiler_internally(done);
}
self.compilerLoaded = true;
done();
});
}
self.load_compiler_internally(done);
}
load_compiler_internally(done) {
const self = this;
if (this.compilerLoaded) {
return done();
}
@ -38,8 +47,10 @@ class SolcW {
done();
});
this.ipc.serve();
this.ipc.on('compile', self.compile.bind(this));
if (this.ipc.isServer()) {
this.ipc.serve();
this.ipc.on('compile', self.compile.bind(this));
}
this.events.request("version:get:solc", function(solcVersion) {
if (solcVersion === currentSolcVersion) {
@ -64,7 +75,7 @@ class SolcW {
const id = uuid();
let a = new Date();
if (this.useIpc) {
if (this.ipc.isClient() && this.ipc.connected) {
return this.ipc.request('compile', jsonObj, done);
}

View File

@ -65,7 +65,7 @@ class Test {
});
this.engine.startService("deployment", {
trackContracts: false,
useIpc: true
ipcRole: 'client'
});
this.engine.startService("codeGenerator");
}