forked from jsjohnst/hook.io
/
hook.js
233 lines (168 loc) · 4.95 KB
/
hook.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var dnode = require('dnode'),
util = require('util'),
colors = require('colors'),
argv = require('optimist').argv,
EventEmitter = require('eventemitter2').EventEmitter2;
var Hook = exports.Hook = function (options) {
EventEmitter.call(this);
self = this;
self.id = 0;
self._outputs = [];
self.name = options.name;
// All servers and clients will listen and connect port 5000 by default
self.port = 5000;
self.host = "localhost";
if (argv.p) {
self.port = argv.p;
}
if (argv.h) {
self.host = argv.h;
}
self.on('o.*', function(event, data){
console.log('I am currently sending data to my outputs on: '.green + event.toString().yellow + ' ' + JSON.stringify(data).grey);
var c = event.split('.');
c.shift();
c.unshift('i');
if (self.remote && self.remote.input) {
self.remote.input(c.join('.'), data);
}
if(self.client && self.client.input){
self.client.input(c.join('.'), data);
}
});
self.on('i.*', function(event, data){
console.log('I am currently getting data on my inputs from: '.green + event.toString().yellow + ' ' + JSON.stringify(data).grey);
var c = event.split('.');
c.shift();
c.unshift('o');
if (self.remote && self.remote.output) {
self.remote.output(c.join('.'), data);
}
if(self.client && self.client.output){
// TODO: Don't rebroadcast to self
//self.client.output(c.join('.'), data);
}
});
}
util.inherits(Hook, EventEmitter);
Hook.prototype.start = function (options, callback) {
var self = this;
if (!options) {
var options = {};
}
self.port = options.port || self.port;
self.host = options.host || self.host;
if (options.server) {
self.port = options.server;
}
self._listen();
};
Hook.prototype._listen = function(options, callback){
var self = this;
// TODO: Replace try/catch with proper error handler for server instance binding errors
try {
server = dnode(function(client, conn){
self.client = client;
this.report = function (name, callback) {
var that = this;
that.id = 0;
//
// ### function checkName (name)
// #### @name {String} Name of hook to check
// Recurisively checks hook's name until it
// finds an available name for hook.
//
function checkName(name) {
var attemptedName = name + '-' + that.id;
if (self._outputs.indexOf(attemptedName) === -1) {
self._outputs.push(attemptedName);
console.log('hook input connected: '.green, attemptedName.yellow)
return attemptedName;
}
else {
that.id++;
return checkName( name );
}
}
callback(checkName(name, that.id), that.id);
};
this.input = function ( event, data ) {
self.emit(event, data);
}
this.output = function ( event, data ) {
self.emit(event, data);
}
}).listen(self.port, callback);
console.log('hook output started: '.green, self.name.yellow);
}
catch (err) {
if (err.message === "EADDRINUSE, Address already in use") {
return self.connect();
} else {
return console.log(err);
}
}
}
Hook.prototype.listen = function(options, callback){
var self = this, client = null;
if (!options) {
var options = {};
}
self.port = options.port || self.port;
self.host = options.host || self.host;
if (options.server) {
self.port = options.server;
}
self._listen();
};
Hook.prototype.connect = function (options, callback) {
var self = this;
if (!options) {
var options = {};
}
self.port = options.port || self.port;
self.host = options.host || self.host;
var client = dnode({
input : function (event, data) {
self.emit(event, data);
},
output: function ( event, data ) {
//self.emit(event, data);
}
});
client.connect(self.port, function (remote, conn) {
self.remote = remote;
self.conn = conn;
self.client = client;
remote.report(self.name, function(newName, newID){
self.name = newName;
self.id = newID;
console.log('I have connected to an output, my name there is: '.green + newName.yellow)
self.emit('ready');
});
});
}
// Hook.spawn()
// Uses forever to spawn up more hooks programatically
/* TODO: Move to new hook
Hook.prototype.spawn = function(args){
var hooks = [];
if (typeof args === "string") {
hooks.push(args);
}
else {
hooks = args;
}
// TODO: remove hard-coded path and replace with npm lookup / system lookup
for(var i = 0; i < hooks.length; i++) {
var child = new (forever.Forever)(__dirname+'/../../hooks/'+hooks[i], {
max: 10,
silent: false,
logFile: '../forever.log',
options: [hooks, 5000]
});
//child.on('exit', self.callback);
child.start();
}
};
*/