2018-09-13 10:13:52 +00:00
|
|
|
const fs = require('./fs.js');
|
|
|
|
const ipc = require('node-ipc');
|
|
|
|
const {parse, stringify} = require('flatted/cjs');
|
2018-06-04 19:36:43 +00:00
|
|
|
|
|
|
|
class IPC {
|
|
|
|
|
|
|
|
constructor(options) {
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.socketPath = options.socketPath || fs.dappPath(".embark/embark.ipc");
|
2018-09-05 09:39:09 +00:00
|
|
|
this.ipcRole = options.ipcRole;
|
2018-06-04 19:36:43 +00:00
|
|
|
ipc.config.silent = true;
|
2018-06-04 22:15:27 +00:00
|
|
|
this.connected = false;
|
2018-06-04 19:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
connect(done) {
|
2018-06-04 22:15:27 +00:00
|
|
|
const self = this;
|
2018-06-05 12:37:27 +00:00
|
|
|
function connecting(_socket) {
|
|
|
|
let connectedBefore = false, alreadyDisconnected = false;
|
2018-06-04 19:36:43 +00:00
|
|
|
ipc.of['embark'].on('connect',function() {
|
2018-06-27 18:32:13 +00:00
|
|
|
if (!alreadyDisconnected && !connectedBefore) {
|
2018-06-04 22:15:27 +00:00
|
|
|
connectedBefore = true;
|
|
|
|
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"));
|
|
|
|
}
|
2018-06-04 19:36:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc.connectTo('embark', this.socketPath, connecting);
|
|
|
|
}
|
|
|
|
|
|
|
|
serve() {
|
2018-08-07 15:37:46 +00:00
|
|
|
fs.mkdirpSync(fs.dappPath(".embark"));
|
2018-06-05 12:37:27 +00:00
|
|
|
ipc.serve(this.socketPath, () => {});
|
|
|
|
ipc.server.start();
|
2018-06-04 19:36:43 +00:00
|
|
|
|
|
|
|
this.logger.info(`pid ${process.pid} listening on ${this.socketPath}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
on(action, done) {
|
2018-06-04 20:39:31 +00:00
|
|
|
const self = this;
|
2018-09-13 10:13:52 +00:00
|
|
|
ipc.server.on('message', function(messageString, socket) {
|
|
|
|
const message = parse(messageString);
|
|
|
|
if (message.action !== action) {
|
2018-06-04 19:36:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-15 09:40:51 +00:00
|
|
|
let reply = function(error, replyData) {
|
|
|
|
self.reply(socket, action, error, replyData);
|
2018-06-05 12:37:27 +00:00
|
|
|
};
|
2018-09-13 10:13:52 +00:00
|
|
|
done(message.data, reply, socket);
|
2018-06-04 19:36:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-15 09:40:51 +00:00
|
|
|
reply(client, action, error, data) {
|
2018-09-13 10:13:52 +00:00
|
|
|
const message = stringify({action, data, error: (error && error.stack)});
|
|
|
|
ipc.server.emit(client, 'message', message);
|
2018-06-04 19:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 12:42:45 +00:00
|
|
|
listenTo(action, callback) {
|
2018-09-13 10:13:52 +00:00
|
|
|
ipc.of['embark'].on(action, (messageString) => {
|
|
|
|
callback(parse(messageString));
|
|
|
|
});
|
2018-08-08 12:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
broadcast(action, data) {
|
2018-09-13 10:13:52 +00:00
|
|
|
ipc.server.broadcast(action, stringify(data));
|
2018-08-08 12:42:45 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 19:36:43 +00:00
|
|
|
once(action, cb) {
|
2018-09-13 10:13:52 +00:00
|
|
|
ipc.of['embark'].once('message', function(messageString) {
|
|
|
|
const message = parse(messageString);
|
|
|
|
if (message.action !== action) {
|
2018-06-04 19:36:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-09-13 10:13:52 +00:00
|
|
|
cb(message.error, message.data);
|
2018-06-04 19:36:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-04 20:39:31 +00:00
|
|
|
request(action, data, cb) {
|
|
|
|
if (cb) {
|
|
|
|
this.once(action, cb);
|
|
|
|
}
|
2018-09-13 10:13:52 +00:00
|
|
|
ipc.of['embark'].emit('message', stringify({action: action, data: data}));
|
2018-06-04 19:36:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 22:15:27 +00:00
|
|
|
isClient() {
|
|
|
|
return this.ipcRole === 'client';
|
|
|
|
}
|
|
|
|
|
|
|
|
isServer() {
|
|
|
|
return this.ipcRole === 'server';
|
|
|
|
}
|
|
|
|
|
2018-06-04 19:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = IPC;
|