forked from zettajs/zetta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zetta.js
451 lines (374 loc) · 10.4 KB
/
zetta.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
var os = require('os');
var AutoScout = require('zetta-auto-scout');
var async = require('async');
var HttpScout = require('./lib/http_scout');
var HttpServer = require('./lib/http_server');
var Logger = require('./lib/logger');
var PeerClient = require('./lib/peer_client');
var PeerRegistry = require('./lib/peer_registry');
var PubSub = require('./lib/pubsub_service');
var Runtime = require('./lib/runtime');
var scientist = require('zetta-scientist');
var Query = require('calypso').Query;
var Zetta = module.exports = function(opts) {
if (!(this instanceof Zetta)) {
return new Zetta(opts);
}
opts = opts || {};
this._name = os.hostname(); // optional name, defaults to OS hostname
this.id = this._name;
this._properties = {}; // custom properties
this._exposeQuery = '';
this._scouts = [];
this._apps = [];
this._peers = [];
this._peerClients = [];
this.peerRegistry = opts.peerRegistry || new PeerRegistry();
this.pubsub = opts.pubsub || new PubSub();
this.log = opts.log || new Logger({ pubsub: this.pubsub });
this.log.init();
this._silent = false;
this.httpServer = new HttpServer(this);
var runtimeOptions = {
pubsub: this.pubsub,
log: this.log,
httpServer: this.httpServer
};
if (opts && opts.registry) {
runtimeOptions.registry = opts.registry;
}
this.runtime = new Runtime(runtimeOptions);
var httpScout = scientist.create.apply(null, [HttpScout]);
httpScout.server = this.runtime;
this.httpScout = httpScout;
this._scouts.push(httpScout);
};
Zetta.prototype.silent = function() {
this._silent = true;
return this;
};
// pass in a custom logging
Zetta.prototype.logger = function(func) {
this._silent = true;
func(this.log);
return this;
};
Zetta.prototype.name = function(name) {
if (name === '*') {
throw new Error('Cannot set name to *');
}
this._name = name;
this.id = this._name;
return this;
};
Zetta.prototype.properties = function(props) {
var self = this;
if (typeof props === 'object') {
delete props.name; // cannot overide name
this._properties = props;
}
return this;
};
Zetta.prototype.getProperties = function() {
var self = this;
var ret = { name: this._name };
Object.keys(this._properties).forEach(function(k) {
ret[k] = self._properties[k];
});
return ret;
};
Zetta.prototype.use = function() {
var args = Array.prototype.slice.call(arguments);
var constructor = args[0];
var self = this;
function addScout(scout) {
scout.server = self.runtime;
self._scouts.push(scout);
}
function init() {
var machine = Object.create(constructor.prototype);
constructor.apply(machine, args.slice(1));
machine._pubsub = self.pubsub;
machine._log = self.log;
machine._registry = self.runtime.registry;
var config = scientist.config(machine);
return { config: config, instance: machine };
}
function walk(proto) {
if (!proto || !proto.__proto__) {
self.load.apply(self, args);
} else if (proto.__proto__.constructor.name === 'HttpDevice') {
var config = init().config;
self.httpScout.driverFunctions[config._type] = constructor;
} else if (proto.__proto__.constructor.name === 'Device') {
var build = init();
args.unshift(build.config._type);
var scout = Object.create(AutoScout.prototype);
scout._deviceInstance = build; // pass both machine and config to autoscout need to _generate device
AutoScout.apply(scout, args);
addScout(scout);
} else if (proto.__proto__.constructor.name === 'Scout') {
var scout = scientist.create.apply(null, args);
addScout(scout);
} else {
walk(proto.__proto__);
}
}
walk(constructor.prototype);
return this;
};
Zetta.prototype.expose = function(query) {
this._exposeQuery = query;
this.runtime.expose(query);
return this;
};
Zetta.prototype.load = function() {
var args = Array.prototype.slice.call(arguments);
var appArgs = args.slice(1, args.length);
var app = {
app: args[0],
args: appArgs
};
this._apps.push(app);
return this;
};
Zetta.prototype.link = function(peers) {
var self = this;
if(!Array.isArray(peers)) {
peers = [peers];
}
peers.forEach(function(peer) {
//self._peers.push(new PeerClient(peer, self));
self._peers.push(peer);
});
return this;
};
Zetta.prototype.listen = function() {
var self = this;
var args = Array.prototype.slice.call(arguments);
var last = args[args.length - 1];
var callback;
if (typeof last === 'function') {
callback = last;
}
this._run(function(err){
if(err) {
if (callback) {
return callback(err);
} else {
throw err;
}
}
var cb = function(err) {
if (err) {
if (callback) {
callback(err);
} else {
throw err;
}
}
var host;
if (typeof args[0] === 'string') {
host = args[0]; // UNIX socket
} else if (typeof args[0] === 'number') {
if (args.length > 1 && typeof args[1] === 'string') {
host = 'http://' + args[1] + ':' + args[0]; // host + port
} else {
host = 'http://127.0.0.1:' + args[0]; // just port
}
} else if (typeof args[0] === 'object' && args[0].fd) {
host = 'fd: ' + args[0].fd; // handle
} else {
host = '<unknown>';
}
self.log.emit('log', 'server', 'Server (' + self._name + ') ' + self.id + ' listening on ' + host);
if (callback) {
callback(err);
}
};
if (!callback) {
args.push(cb);
} else {
args[args.length - 1] = cb;
}
self.httpServer.listen.apply(self.httpServer, args);
});
return this;
};
// run scouts/apps init server but do not listening on http port
Zetta.prototype._run = function(callback) {
var self = this;
if(!callback) {
callback = function(){};
}
if (!this._silent) {
Logger.ConsoleOutput(this.log);
}
async.series([
function(next) {
self.runtime.registry._init(next);
},
function(next) {
self.peerRegistry._init(next);
},
function(next) {
self._initScouts(next);
},
function(next) {
self._initApps(next);
},
function(next) {
self._initHttpServer(next);
},
function(next) {
self._cleanupPeers(next);
},
function(next) {
self._initPeers(self._peers, next);
self.link = function(peers, cb) {
self._initPeers(peers, (cb || function() {}) );
};
}
], function(err){
setImmediate(function() {
callback(err);
});
});
return this;
};
Zetta.prototype._initScouts = function(callback) {
async.each(this._scouts, function(scout, next) {
scout.init(next);
}, function(err) {
callback(err);
});
return this;
};
Zetta.prototype._initApps = function(callback) {
var self = this;
this._apps.forEach(function(app) {
var args = app.args;
args.unshift(self.runtime);
app.app.apply(null, args);
});
callback();
return this;
};
Zetta.prototype._initHttpServer = function(callback) {
this.httpServer.init();
callback();
return this;
};
// set all peers to disconnected
Zetta.prototype._cleanupPeers = function(callback) {
var self = this;
this.peerRegistry.find(Query.of('peers'), function(err, results) {
if(err) {
callback(err);
return;
}
async.forEach(results, function(peer, next) {
peer.status = 'disconnected';
self.peerRegistry.save(peer, next);
}, callback);
});
};
Zetta.prototype._initPeers = function(peers, callback) {
var self = this;
var existingUrls = [];
var allPeers = [];
if (!Array.isArray(peers)) {
peers = [peers];
}
this.peerRegistry.find(Query.of('peers'), function(err, results) {
if(err) {
callback(err);
return;
}
results.forEach(function(peer) {
peer.status = 'disconnected';
if (peer.direction === 'initiator' && peer.url) {
allPeers.push(peer);
existingUrls.push(peer.url);
return;
}
});
// peers added through js api to registry peers if they don't already exist
allPeers = allPeers.concat(peers.filter(function(peer) {
return existingUrls.indexOf(peer) === -1;
}));
allPeers.forEach(function(obj) {
var existing = (typeof obj === 'object');
if (existing) {
if(!obj.fromLink || peers.indexOf(obj.url) > -1) {
self.peerRegistry.save(obj, function() {
self._runPeer(obj);
});
} else {
//Delete
self.peerRegistry.remove(obj, function(err){
if(err) {
console.error(err);
}
});
}
} else {
var peerData = {
url: obj,
direction: 'initiator',
fromLink:true
};
self.peerRegistry.add(peerData, function(err, newPeer) {
self._runPeer(newPeer);
});
}
});
// end after db read
callback();
});
return this;
};
Zetta.prototype._extendPeerRequest = function(client) {
this.runtime.modifyPeerRequest(client.ws);
};
Zetta.prototype._extendPeerResponse = function(client) {
this.runtime.modifyPeerResponse(client.ws);
};
Zetta.prototype._runPeer = function(peer) {
var self = this;
var peerClient = new PeerClient(peer.url, self);
this._extendPeerRequest(peerClient);
this._extendPeerResponse(peerClient);
self._peerClients.push(peerClient);
// when websocket is established
peerClient.on('connecting', function() {
peer.status = 'connecting';
self.peerRegistry.save(peer);
});
// when peer handshake is made
peerClient.on('connected', function() {
peer.status = 'connected';
peer.connectionId = peerClient.connectionId;
self.peerRegistry.save(peer);
// peer-event
self.pubsub.publish('_peer/connect', { peer: peerClient});
});
peerClient.on('error', function(error) {
self.peerRegistry.get(peer.id, function(err, result) {
result.status = 'failed';
result.error = error;
self.peerRegistry.save(result);
// peer-event
self.pubsub.publish('_peer/disconnect', { peer: peerClient });
});
});
peerClient.on('closed', function() {
self.peerRegistry.get(peer.id, function(err, result) {
result.status = 'disconnected';
// peer-event
self.pubsub.publish('_peer/disconnect', { peer: peerClient });
self.peerRegistry.save(result, function() { });
});
});
peerClient.start();
}