This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.js
78 lines (62 loc) · 2.11 KB
/
hub.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
var colors = require('colors')
, async = require('async')
, webui = require('./webui');
function InfoHub(config) {
var self = this;
self.outputs = [];
self.config = config;
self.webui = new webui(this);
self.initOutputs();
}
InfoHub.prototype.initOutputs = function(cb) {
var self = this, outputs = self.config.outputs || {};
outputs = Object.keys(outputs).map(function loadModules(out) {
var module = outputs[out].module;
try { // Try to require module
console.log(outputs[out]);
self.outputs[out.toLowerCase()] =
new (require(__dirname + '/output/' + module + '/' + module))(self, outputs[out].config);
return out.green;
} catch (e) {
console.error('Failed to load module "%s".', out);
console.error(e.stack);
return out.red;
}
});
console.log('Loaded %s output module(s): %s', String(outputs.length), outputs.join(', '));
// Initializes a module by it's name, returns true/false
function moduleInit(module, cb) {
process.stdout.write('Starting ' + module + ' module...');
self.outputs[module].init(function (e) {
console.log(' -> ' + (e === null ? ' ok'.green : ' fail'.red));
cb(e === null);
});
}
// Initialize output modules
function initializeModules(cb) {
var keys = Object.keys(self.outputs);
async.filterSeries(keys, moduleInit, function (results) { cb(null); });
}
// Time to use these functions above
console.log('Initializing output modules...');
initializeModules(function (err) {
console.log('Modules initialized!');
});
/*async.parallel([initializeModules],
// Init done!
function (e, d) {
//cb(null);
});*/
};
InfoHub.prototype.broadcast = function(message, filter, callback) {
var self = this, modules = Object.keys(self.outputs);
console.log('filtering', modules);
modules = modules.filter(function (id) {
return filter[id];
})
console.log('broadcasting', message, filter, modules);
async.forEach(modules, function (module, cb) {
self.outputs[module].broadcast(message, cb);
}, callback);
};
var hub = new InfoHub(require('./config').hub || {});