-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc.js
62 lines (55 loc) · 1.11 KB
/
ipc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
/**
* Makes it possible to communicate with the u413 server while it's running.
**/
const
fs = require("fs"),
net = require("net"),
{spawn} = require("child_process");
const
log = require("./log");
app.ipc = {
restart() {
log.info("Restarting server...");
spawn("/bin/bash", ["tools/restart", process.pid], {
detached: true,
stdio: 'inherit'
}).unref();
},
update() {
spawn("/bin/bash", ["tools/update", process.pid], {
detached: true,
stdio: 'inherit'
}).unref();
},
kill() {
log.info("Killing server...");
process.exit(0);
},
ls() {
console.log(Object.keys(app.ipc).join(" "));
}
}
const ipcname = 'private/ipc.sock';
// Make sure there isn't a file where we want the socket.
try {
if(fs.statSync(ipcname)) {
fs.unlinkSync(ipcname);
}
}
catch(e) {}
net.createServer(conn => {
conn.on('data', data => {
data += '';
let m = /^\s*(\S+)(.*)\s*$/.exec((data));
if(m) {
let fn = app.ipc[m[1]];
if(typeof fn === 'function') {
fn(m[2]);
}
else {
log.error("Received invalid IPC signal:", JSON.stringify(data));
}
}
});
}).listen(ipcname);