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); 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('solidity', {useIpc: options.useIpc, ipc: this.ipc});
this.registerModule('vyper'); this.registerModule('vyper');
this.registerModule('profiler'); this.registerModule('profiler');

View File

@ -6,13 +6,31 @@ class IPC {
constructor(options) { constructor(options) {
this.logger = options.logger; this.logger = options.logger;
this.socketPath = options.socketPath || fs.dappPath(".embark/embark.ipc"); this.socketPath = options.socketPath || fs.dappPath(".embark/embark.ipc");
this.ipcRole = options.ipcRole || "server";
ipc.config.silent = true; ipc.config.silent = true;
this.connected = false;
} }
connect(done) { connect(done) {
const self = this;
function connecting(socket) { function connecting(socket) {
let connectedBefore = false, alreadyDisconnected = false;;
ipc.of['embark'].on('connect',function() { 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}); ipc.of['embark'].emit('message', {action: action, message: data});
} }
isClient() {
return this.ipcRole === 'client';
}
isServer() {
return this.ipcRole === 'server';
}
} }
module.exports = IPC; module.exports = IPC;

View File

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

View File

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