-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemonHub.js
More file actions
71 lines (61 loc) · 1.9 KB
/
Copy pathdaemonHub.js
File metadata and controls
71 lines (61 loc) · 1.9 KB
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
var fs = require( 'fs' );
var u = require( 'util' );
/*
* Variables
*/
var dList = { };
/*
* Exports
*/
exports.process = processDaemons;
exports.init = init;
/*
* Functions
*/
//checks the file system for daemons and fetches them in an array.
function init( daemonPath, callback ) {
console.log( 'starting DaemonHub...' )
fs.readdir( daemonPath, function( err, files ) {
if ( err ) {
throw err;
}
for ( var i in files ) {
file = files [ i ];
dot = file.lastIndexOf( '.' );
dName = file.substring( 0, dot );
dExtension = file.substring( dot + 1, file.length );
if( dExtension.toLowerCase() == "js" ) {
//import daemon
currDaemon = require( daemonPath + '/' + dName );
//check if daemon have a valid structure
if ( currDaemon.handle != undefined && typeof currDaemon.handle === "function"
&& currDaemon.isInterestedOn != undefined && typeof currDaemon.isInterestedOn === "function"
&& ( currDaemon.isInterestedOn() || !currDaemon.isInterestedOn() ) ) {
//add it to the deamon list
dList[ dName ] = currDaemon;
console.log( ' ' + dName + ' initialized.' );
} else {
console.log( dName + ' does not export the expected set of functions. Ignoring the daemon.' );
}
}
}
//check if at least one valid daemon is loaded
if( isEmpty( dList ) ) {
err = 'no daemon initialized.';
}
console.log( 'DaemonHub started.' );
callback( err );
} );
}
function processDaemons( doc, helperObj ) {
for( var i in dList ) {
console.log( 'calling daemon: ' + i + ' with document: ' + u.inspect( doc) );
dList[ i ].handle( doc, helperObj );
}
};
function isEmpty( obj ) {
for( var i in obj ) {
return false;
}
return true;
}