-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·97 lines (87 loc) · 2.9 KB
/
app.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
#!/usr/bin/env node
var events = require('events');
var rp = require('ripple-lib-promise');
var usecase = require('ripple-usecase');
var pluginLoader = require('./lib/plugin_loader');
var agentLoader = require('./lib/agent_loader');
var log = console.log;
var ledger_loop = function(remote, update_func){
var prevtime = process.uptime();
remote.on('ledger_closed', function(v){
var currenttime = process.uptime();
var deltatime = currenttime - prevtime;
var ledger_time = usecase.util.time.rippleTimeToMoment(v.ledger_time);
var info = {
uptime:currenttime,
deltatime:deltatime,
time:ledger_time.format(),
unixtime:ledger_time.unix(),
index:v.ledger_index,
txn_count:v.txn_count,
};
update_func(info);
prevtime = currenttime
})
}
var sensor_init = function(){
var plugins = pluginLoader.scan(__dirname + '/plugins');
var list = [];
agentLoader.scan(__dirname + '/sensors').forEach(function(a){
plugins.filter(function(v){
return a.data.load === true
&& v.name === a.data.sensor
}).forEach(function(p){
list.push(function(remote, appEvent){
console.log('sensor install:', a.name)
return p.createInstance(remote, a.data, appEvent);
})
});
})
return list;
}
var agent_init = function(){
var plugins = pluginLoader.scan(__dirname + '/plugins');
var list = [];
agentLoader.scan(__dirname + '/agents').forEach(function(a){
plugins.filter(function(v){
return a.data.load === true
&& v.name === a.data.agent
}).forEach(function(p){
list.push(function(remote, appEvent){
console.log('agent install:', a.name)
return p.createInstance(remote, a.data, appEvent);
})
});
})
return list;
}
var initialize = function(remote, sensors, agents, appEvent){
ledger_loop(remote, function(info){
appEvent.emit('ledger', info);
sensors.forEach(function(sensor){
sensor.update();
})
agents.forEach(function(agent){
agent.update();
})
});
}
var main = module.exports = function(){
var appEvent = new events.EventEmitter();
appEvent.setMaxListeners(512);
var sensorlist = sensor_init(appEvent);
var agentlist = agent_init(appEvent);
var connection_timeout = 60;
log('lemmings-ripple startup');
rp.createConnect(connection_timeout).then(function(remote){
log('lemmings-ripple rippled websocket connected');
var sensors = sensorlist.map(function(v){
return v(remote, appEvent);
})
var agents = agentlist.map(function(v){
return v(remote, appEvent);
})
initialize(remote, sensors, agents, appEvent);
})
}
main();