-
Notifications
You must be signed in to change notification settings - Fork 23
/
sim-example-alert.js
executable file
·55 lines (42 loc) · 1.07 KB
/
sim-example-alert.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
/*
Example: Alert Message
An alert message needs to be broadcasted to all nodes as fast as possible!
Let's calculate how long it takes the message to be received by all nodes.
*/
var net = require("./network"),
peermgr = require("./peermgr"),
client = new net.Node()
client.use(peermgr)
client.init(function() {
this.alertflag = false;
if (this.id == 0) {
this.delay(20000, function() {
this.log("dispatching alert")
// give the network about 20 seconds so everybody is connected
this.alertflag = true;
this.setColor("red")
this.peermgr.broadcast("alert")
})
}
this.on("alert", function() {
if (this.alertflag)
return;
this.alertflag = true;
this.setColor("red")
this.peermgr.broadcast("alert")
})
})
net.add(100, client)
net.check(20, function() {
var aware = 0;
net.nodes.forEach(function(n) {
if (n.alertflag) {
aware++;
}
})
if (aware == net.nodes.length) {
net.visualizer.log(net.now + ": ALL NODES HAVE RECEIVED ALERT MESSAGE");
net.stop();
}
})
net.run(Infinity)