-
Notifications
You must be signed in to change notification settings - Fork 17
/
engine.js
84 lines (66 loc) · 1.73 KB
/
engine.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
var config = require('./config');
var winston = require('winston');
var mysql = require('mysql');
var ripple = require('ripple-lib');
var events = require('events');
var util = require('util');
var Aggregator = require('./aggregator').Aggregator;
var Processor = require('./processor').Processor;
var Engine = function () {
events.EventEmitter.call(this);
var self = this;
// Ripple client
this.remote = ripple.Remote.from_config(config.remote);
// MySQL
this.db = mysql.createConnection({
debug: config.db.mysql_debug,
host: config.db.mysql_ip,
user: config.db.mysql_user,
password: config.db.mysql_pass,
database: config.db.mysql_db,
multipleStatements: true
});
// Statistical data aggregator
this.aggregator = new Aggregator(this.db);
// Historic data processor
this.processor = new Processor(this.db, this.remote);
};
util.inherits(Engine, events.EventEmitter);
Engine.prototype.startup = function (callback)
{
if ("function" === typeof callback) {
this.once('started', callback);
}
this._startupMysql();
};
Engine.prototype._startupMysql = function ()
{
var self = this;
self.db.connect(function (err) {
if (err) {
winston.error(err);
process.exit(1);
}
winston.info("Connected to MySQL server");
self._startupRipple();
});
};
Engine.prototype._startupRipple = function ()
{
var self = this;
this.remote.connect();
this.remote.once('connected', function () {
winston.info("Connected to Ripple network");
self._notifyStartupComplete();
});
};
Engine.prototype._notifyStartupComplete = function ()
{
this.emit('started');
};
Engine.prototype.shutdown = function ()
{
this.db.end();
this.remote.disconnect();
};
exports.Engine = Engine;