-
Notifications
You must be signed in to change notification settings - Fork 8
/
port.js
149 lines (124 loc) · 4.27 KB
/
port.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var net = require('net'),
spawn = require('child_process').spawn,
event = require('events').EventEmitter;
function Port(options){
if (!(this instanceof Port)) return new Port(options);
this.setOptions(options);
}
Port.prototype = Object.create(event.prototype);
Port.prototype.setOptions = function(options){
if (!this.options) this.options = {
host: 'localhost',
read: null, // [connect <port>( -> [netsend]
write: null, // [netreceive <port>]
encoding: null, // 'ascii', 'utf8', 'base64', 'hex'
max: 1, // max connections
basepath: '', // prefix -path flags
pd: (('darwin' == process.platform)
? '/Applications/Pd-0.45-5-64bit.app/Contents/Resources/bin/pd'
: 'pd'),
flags: {} // {'nogui': true, 'stderr': true, 'open': './port.pd'}
};
for (var key in options){
this.options[key] = options[key];
}
return this;
};
Port.prototype.parseFlags = function(flags){
var array = [],
basepath = (this.options && this.options.basepath) || '';
if (Array.isArray(flags)) return flags;
for (var f in flags){
if (/debug|path|open/.test(f) || !flags[f]) continue;
array.push(/^-/.test(f) ? f : '-' + f);
if (typeof flags[f] != 'boolean') array.push(flags[f]);
}
var path = flags['-path'] || flags['path'] || '';
if (!!basepath || !!path){
if (!!basepath && !(/\/$/.test(basepath))) basepath += '/';
if (!Array.isArray(path)) array.push('-path', basepath + path);
else for (var i = 0, l = path.length; i < l; ++i){
array.push('-path', basepath + path[i]);
}
}
array.push('-open', flags['-open'] || flags['open']);
return array;
};
// start pd process
Port.prototype.spawn = function(){
if (this.options.debug) console.log(this.options.pd, this.parseFlags(this.options.flags));
if (!this.options.pd) return this;
var child = this.child = spawn(this.options.pd, this.parseFlags(this.options.flags));
if (!!this.options.encoding) child.stderr.setEncoding(this.options.encoding);
child.on('exit', this.emit.bind(this, 'exit'));
child.stderr.on('data', this.emit.bind(this, 'stderr'));
return this;
};
Port.prototype.isRunning = function(){
return (!!this.child && !this.child.killed && !!this.sender && this.sender.writable) || false;
};
// on [netsend] connection
function connection(socket){
this.socket = socket;
if (!!this.options.encoding) socket.setEncoding(this.options.encoding);
socket.on('data', this.emit.bind(this, 'data'));
this.emit('connection', socket);
}
// listen for [netsend]
Port.prototype.listen = function(){
var receiver = this.receiver = net.createServer();
if (!!this.options.max) receiver.maxConnections = this.options.max;
receiver.listen(this.options.read, this.options.host);
receiver.on('listening', this.emit.bind(this, 'listening'));
receiver.on('connection', connection.bind(this));
receiver.on('error', this.emit.bind(this, 'error'));
};
// connect to [netreceive]
Port.prototype.connect = function(){
var sender = this.sender = new net.Socket();
if (!!this.options.encoding) sender.setEncoding(this.options.encoding);
sender.on('connect', this.emit.bind(this, 'connect', sender));
sender.on('error', this.emit.bind(this, 'error'));
this.sender.connect(this.options.write, this.options.host);
};
// send data to [netreceive]
Port.prototype.write = function(data){
if (data != null) this.sender.write(data);
return this;
};
// start Port
Port.prototype.create = function(){
process.once('exit', this.destroy);
if (!!this.options.write) this.on('connection', this.connect);
if (!this.options.read) return this.spawn();
this.on('listening', this.spawn);
this.listen();
return this;
};
// stop Port
Port.prototype.destroy = function(){
process.removeListener('exit', this.destroy);
this.removeAllListeners('listening');
this.removeAllListeners('connection');
if (!!this.sender){
this.sender.destroy();
this.sender.removeAllListeners('connect');
this.sender.removeAllListeners('error');
delete this.sender;
}
if (!!this.receiver){
this.receiver.close();
this.receiver.removeAllListeners('listening');
this.receiver.removeAllListeners('connection');
this.receiver.removeAllListeners('error');
delete this.receiver;
}
if (!!this.socket) this.socket.destroy();
if (!!this.child){
this.child.stderr.removeAllListeners('data');
this.child.kill();
}
this.emit('destroy');
return this;
};
module.exports = Port;