2013-11-13 15:21:58 +00:00
|
|
|
/** @module rules_server */
|
|
|
|
|
'use strict';
|
|
|
|
|
/*
|
|
|
|
|
A First Level Header
|
|
|
|
|
====================
|
|
|
|
|
|
|
|
|
|
A Second Level Header
|
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
Now is the time for all good men to come to
|
|
|
|
|
the aid of their country. This is just a
|
|
|
|
|
regular paragraph.
|
|
|
|
|
|
|
|
|
|
The quick brown fox jumped over the lazy
|
|
|
|
|
dog's back.
|
|
|
|
|
|
|
|
|
|
### Header 3
|
|
|
|
|
|
|
|
|
|
> This is a blockquote.
|
|
|
|
|
>
|
|
|
|
|
> This is the second paragraph in the blockquote.
|
|
|
|
|
>
|
|
|
|
|
> ## This is an H2 in a blockquote
|
|
|
|
|
*/
|
2013-11-13 16:22:37 +00:00
|
|
|
|
2013-11-13 15:21:58 +00:00
|
|
|
var http_listener = require('./http_listener'),
|
|
|
|
|
db = require('./db_interface'),
|
|
|
|
|
engine = require('./engine'),
|
|
|
|
|
mm = require('./module_manager'),
|
|
|
|
|
log = require('./logging'),
|
|
|
|
|
fs = require('fs'),
|
|
|
|
|
path = require('path'),
|
2013-11-13 16:22:37 +00:00
|
|
|
procCmds = {
|
|
|
|
|
'die': function() { shutDown(); }
|
|
|
|
|
},
|
2013-11-13 15:21:58 +00:00
|
|
|
objCmds = {
|
|
|
|
|
'loadrules': mm.loadRulesFile,
|
|
|
|
|
'loadaction': mm.loadActionModule,
|
|
|
|
|
'loadactions': mm.loadActionModules,
|
|
|
|
|
'loadevent': engine.loadEventModule,
|
|
|
|
|
'loadevents': engine.loadEventModules,
|
|
|
|
|
'shutdown': shutDown,
|
|
|
|
|
'restart': null //TODO implement
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function handleAdminCommands(args, answHandler) {
|
|
|
|
|
if(args && args.cmd) {
|
|
|
|
|
var func = objCmds[args.cmd];
|
|
|
|
|
if(func) func(args, answHandler);
|
|
|
|
|
} else log.print('RS', 'No command in request');
|
|
|
|
|
setTimeout(function(ah) {
|
|
|
|
|
answHandler = ah;
|
|
|
|
|
return function() {
|
|
|
|
|
if(!answHandler.isAnswered()) answHandler.answerError('Not handeled...');
|
|
|
|
|
};
|
|
|
|
|
}, 2000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function shutDown(args, answHandler) {
|
2013-11-13 16:22:37 +00:00
|
|
|
if(answHandler) answHandler.answerSuccess('Goodbye!');
|
2013-11-13 15:21:58 +00:00
|
|
|
log.print('RS', 'Received shut down command!');
|
2013-11-13 16:22:37 +00:00
|
|
|
if(engine && typeof engine.shutDown === 'function') engine.shutDown();
|
|
|
|
|
else {
|
|
|
|
|
console.error(typeof engine.shutDown);
|
|
|
|
|
console.error('no function!');
|
|
|
|
|
}
|
|
|
|
|
if(http_listener) http_listener.shutDown();
|
2013-11-13 15:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs.readFile(path.resolve(__dirname, '..', 'config', 'config.json'), 'utf8', function (err, data) {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
log.error('RS', 'Loading config file');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var config = JSON.parse(data);
|
|
|
|
|
if(!config.http_port || !config.db_port || !config.crypto_key) {
|
2013-11-13 19:37:47 +00:00
|
|
|
log.error('RS', new Error('you forgot to define either http_port, db_port, crypto_key, or even all of them!'));
|
2013-11-13 15:21:58 +00:00
|
|
|
} else {
|
|
|
|
|
log.print('RS', 'Initialzing DB');
|
|
|
|
|
db.init(config.db_port, config.crypto_key, function() {
|
|
|
|
|
engine.init(db, config.db_port, config.crypto_key);
|
|
|
|
|
});
|
|
|
|
|
log.print('RS', 'Initialzing http listener');
|
|
|
|
|
http_listener.init(config.http_port, handleAdminCommands, engine.pushEvent);
|
|
|
|
|
log.print('RS', 'Initialzing module manager');
|
|
|
|
|
mm.init(db, engine.loadActionModule, engine.loadRule);
|
|
|
|
|
}
|
2013-11-13 16:22:37 +00:00
|
|
|
});
|
|
|
|
|
|
2013-11-13 19:35:17 +00:00
|
|
|
// Send message
|
2013-11-13 16:22:37 +00:00
|
|
|
process.on('message', function(cmd) {
|
|
|
|
|
if(typeof procCmds[cmd] === 'function') procCmds[cmd]();
|
|
|
|
|
else console.error('err with command');
|
|
|
|
|
});
|