-
Notifications
You must be signed in to change notification settings - Fork 0
/
unix.js
82 lines (71 loc) · 2 KB
/
unix.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var net = require('net');
const fs = require('fs');
function handler(obj, sockpath) {
var server = net.createServer(function (s) {
_handler(server, s, obj);
}).on('error', function(e) {
console.log(e);
trycleanup(e, server, sockpath);
});
_listen(server, sockpath);
return server;
}
function _handler(server, stream, obj) {
// console.log('_handler', stream);
console.log('connected');
stream.on('data', function(c) {
cmds = c.toString()
// Handle (backslash + comma)-separated commands
cmds.split('\\,').forEach(c => {
_handleCommand(c, obj);
});
});
}
function _handleCommand(rawCmd, obj) {
// FIXME: very inefficient
[cmd, ...toks] = rawCmd.trim().split(':');
const args = toks.join(':')
console.log('cmd:', cmd, 'args:', args);
obj.emit(cmd, args);
}
function trycleanup(e, server, sockpath) {
// Strategy: try to connect the unix socket as a client, and if
// the connection is succeeded, we give up.
if (e.code != 'EADDRINUSE') {
_quit();
}
console.log('address is in use');
var clientSocket = new net.Socket();
clientSocket.on('error', function(e) {
if (e.code != 'ECONNREFUSED') {
_quit();
}
// We can't connect the server. Clean up.
console.log('cleaning up');
fs.unlinkSync(sockpath);
_listen(server, sockpath);
});
clientSocket.connect({path: sockpath}, function() {
_quit();
});
}
function _quit() {
console.log('Cannot open the command handler. exit.');
process.exit();
}
function _listen(server, sockpath) {
console.log('listen to ' + sockpath);
server.listen(sockpath);
}
function client(sockpath) {
var client = net.connect(sockpath, function() {
console.log('connected to server', sockpath);
}).on('error', function(err) {
console.log('failed to connect to server', sockpath);
});
return client;
}
module.exports = {
handler: handler,
client: client,
};