This repository has been archived by the owner on Jan 15, 2020. It is now read-only.
forked from mwarning/MeshNetSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
244 lines (208 loc) · 7.38 KB
/
node.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
function Node(mac, meta = null, active = true) {
/* Required fields */
// Basic data
this.mac = mac;
if (!active)
return // Dont init active components if not active
this.meta = meta;
this.incoming = [];
this.outgoing = [];
/* Additional fields */
this.tick = -1;
// Record next hop neighbors
this.neighbors = {};
// Keep a local version of the network
this.nodes = [];
this.links = [];
this.changedLinks = [];
// Send a PEERS message on join network
this.outgoing.push(new Packet(13, 4, this.mac, BROADCAST_MAC, this.mac, BROADCAST_MAC, new Peers(0, null)));
}
Node.prototype.step = function() {
this.tick += 1;
dijkstra = createDijkstra(this.nodes, this.links);
this.handlePackets();
this.broadcastChangedLinks();
this.handleTimeout();
}
// TODO: Need to find a better place for this function
Node.prototype.getNodeByMac = function(nodes, mac) {
for (var i = 0, iLen = this.nodes.length; i < iLen; i++) {
if (this.nodes[i].o.mac == mac) {
return this.nodes[i];
}
}
return false;
}
Node.prototype.getLinkByMacs = function(links, source, target) {
for (var i = 0, iLen = links.length; i < iLen; i++) {
if (links[i].source.o.mac == source && links[i].target.o.mac == target)
return links[i];
if (links[i].source.o.mac == target && links[i].target.o.mac == source)
return links[i];
}
return false;
}
Node.prototype.sendPacket = function(destinationAddress, packetType, data = []) {
var currentHop = this.getNodeByMac(this.nodes, this.mac);
var finalHop = this.getNodeByMac(this.nodes, destinationAddress);
var nextHop = dijkstra.getShortestPath(finalHop, currentHop)[0];
this.outgoing.push(new Packet(16 + data.length, packetType, this.mac, nextHop, this.mac, destinationAddress, data));
}
Node.prototype.ping = function(destinationAddress) {
this.sendPacket(destinationAddress, 1);
}
Node.prototype.pong = function(destinationAddress) {
this.sendPacket(destinationAddress, 2);
}
Node.prototype.handlePackets = function() {
for (var i = 0; i < this.incoming.length; i += 1) {
var packet = this.incoming[i];
console.log(this.mac + " received packet from " + packet.transmitterAddress + " for " + packet.destinationAddress + " with type " + packet.packetType);
// Update network
this.updateNetwork(this.links, this.nodes, this.changedLinks, this.mac, packet.transmitterAddress, 100, tick = this.tick);
this.getNodeByMac(this.nodes, packet.sourceAddress).lastSeen = this.tick;
this.getNodeByMac(this.nodes, packet.sourceAddress).timeoutState = 0;
// Packet arrived at the destination
if (packet.destinationAddress === this.mac) {
if (packet.packetType == 1) { // Ping packet
this.pong(packet.sourceAddress);
} else if (packet.type == 2) { // Pong packet
}
continue;
}
// Catch broadcast packets and record neighbor
if (packet.receiverAddress === BROADCAST_MAC) {
if (packet.packetType == 4) { // Packet is PEERS packet
for (var ii = 0; ii < packet.data.dataLength; ii++) {
var newLink = packet.data.data[ii];
this.updateNetwork(this.links, this.nodes, this.changedLinks, newLink.source, newLink.target, newLink.value, tick = this.tick);
}
}
continue;
}
// The packet needs to be routed
var currentHop = this.getNodeByMac(this.nodes, this.mac);
var finalHop = this.getNodeByMac(this.nodes, packet.destinationAddress);
var nextHop = dijkstra.getShortestPath(finalHop, currentHop)[0];
console.log("Next hop: " + nextHop);
console.log(this.mac + " send packet to " + nextHop + " for " + packet.destinationAddress + " with type " + packet.packetType);
packet.transmitterAddress = this.mac;
packet.receiverAddress = nextHop;
this.outgoing.push(packet);
}
}
Node.prototype.broadcastChangedLinks = function() {
if (this.changedLinks.length > 0) {
var packet = new Packet(12 + (1 + this.changedLinks.length * 5), 4, this.mac, BROADCAST_MAC, this.mac, BROADCAST_MAC, new Peers(this.changedLinks.length, []));
for (var i = 0; i < this.changedLinks.length; i++) {
var changedLink = this.changedLinks[i];
packet.data.data.push({"source": changedLink.source.o.mac, "target": changedLink.target.o.mac, "value": changedLink.o.quality});
}
this.outgoing.push(packet);
this.changedLinks = [];
}
}
Node.prototype.handleTimeout = function() {
for (var i = 0; i < this.nodes.length; i++) {
var node = this.nodes[i];
if (node.o.mac == this.mac)
continue;
if ((node.lastSeen + 30) < this.tick && node.timeoutState != 1) {
node.timeoutState = 1;
this.ping(node.o.mac);
} // TODO: Remove node from memory if it doesn't respond in time
this.nodes[i] = node;
}
}
Node.prototype.updateNetwork = function(links, nodes, changedLinks, sourceNodeMac, targetNodeMac, value) {
var sourceNode;
(sourceNode = this.getNodeByMac(nodes, sourceNodeMac))
? true
: sourceNode = nodes[nodes.push({
"o": new Node(sourceNodeMac, null, false),
"index": nodes.length
}) - 1];
if (typeof tick !== 'undefined')
nodes[sourceNode.index].lastSeen = tick;
nodes[sourceNode.index].timeoutState = 0;
var targetNode;
(targetNode = this.getNodeByMac(nodes, targetNodeMac))
? true
: targetNode = nodes[nodes.push({
"o": new Node(targetNodeMac, null, false),
"index": nodes.length
}) - 1];
if (typeof tick !== 'undefined')
nodes[targetNode.index].lastSeen = tick;
nodes[targetNode.index].timeoutState = 0;
if (!(link = this.getLinkByMacs(links, sourceNodeMac, targetNodeMac))) {
link = {
"index": links.length,
"o": new Link(),
"source": {
"index": sourceNode.index,
"o": sourceNode.o
},
"target": {
"index": targetNode.index,
"o": targetNode.o
}
}
links.push(link);
this.changedLinks.push(link);
return;
}
// Link now contains the link
// Just need to modify the link and put it back
if (link.o.quality == value)
return;
link.o.quality = value;
links[link.index] = link;
this.changedLinks.push(link);
}
// Name displayed under the node
Node.prototype.getNodeName = function() {
// Find hostname in meta data, display MAC address as fallback
return findValue(this.meta, 'hostname', this.mac);
}
// Label on top of the node body
Node.prototype.getNodeLabel = function() {
// Count unicast packets
var count = 0;
for (var i in this.outgoing) {
count += 1;
}
return count
? count.toString()
: '';
}
// Color of the ring around the node body
Node.prototype.getRingColor = function() {
return isEmpty(this.neighbors)
? ''
: '#008000';
}
// Color of the round node body
Node.prototype.getBodyColor = function() {
return '#fff';
}
// Number of small red circles around the node
// body indicating the number of connected clients
Node.prototype.getClientCount = function() {
return findValue(this.meta, 'clients', '').toString();
}
Node.prototype.reset = function() {
this.incoming = [];
this.outgoing = [];
this.neighbors = {};
this.nodes = [];
this.links = [];
this.changedLinks = [];
// Send a PEERS message on join network
this.outgoing.push(new Packet(13, 4, this.mac, BROADCAST_MAC, this.mac, BROADCAST_MAC, new Peers(0, null)));
}
// For the transition to new implementations
Node.prototype.copyFromOldImplementation = function(oldNode) {
copyExistingFields(oldNode, this);
};