-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
425 lines (386 loc) · 11.6 KB
/
worker.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
'use strict';
var id;
var neighbors;
var clients = {};
var net = require('net');
var _ = require('lodash');
const chalk = require('chalk');
var allowedWait =
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
const constants = require('./constants');
var state = constants.SLEEPING; // Other possible values: FIND and FOUND
var level = 0;
var fragmentId; // Weight of the core edge
var basicEdges = []; // Neighbor uids sorted by edge weight
var branchEdges = []; // Edges part of the MST
var rejectedEdges = []; // Edges not part of the MST
var bestEdge; // The edge with the minimum weight
var bestWeight;
var testEdge; // The edge being tested
var inBranch; // The neighbor that sent the INITIATE message
var findCount; // Number of expected REPORT messages
var delayedMessages = []; // Queue to delay processing certain messages
const tasks = {};
tasks[constants.CONNECT] = onConnect;
tasks[constants.INITIATE] = onInitiate;
tasks[constants.TEST] = onTest;
tasks[constants.ACCEPT] = onAccept;
tasks[constants.REJECT] = onReject;
tasks[constants.REPORT] = onReport;
tasks[constants.CHANGE_CORE] = onChangeCore;
tasks[constants.HALT] = onHalt;
setInterval(function() {
if (delayedMessages.length > 0) {
let delayedMessage = delayedMessages.shift();
let delayedTask = tasks[delayedMessage.type];
delayedTask(delayedMessage.source, delayedMessage.payload);
}
}, 5000);
process.on('message', (message) => {
let msg = JSON.parse(message);
if (msg['message'] === 'Initiate') {
id = parseInt(msg['id']);
neighbors = msg['neighbor'];
// Initially, all the edges are classified as basic edges
basicEdges =
Object.keys(neighbors).sort((a, b) => neighbors[a] - neighbors[b]);
basicEdges = basicEdges.map((edge) => parseInt(edge));
let server = net.createServer((conn) => {
conn.on('end', function() {
});
conn.on('data', function(msg) {
msg = JSON.parse(msg);
let task = tasks[msg.type];
task(msg.source, msg.payload);
});
});
server.listen(id, function() {
console.log('Process is listening at : ' + id);
});
} else if (msg['message'] === 'Connect') {
Object.keys(neighbors).forEach((neighborID) => {
let client =
net.createConnection({port: parseInt(neighborID)}, function(){
console.log(id, 'connected to neighbor', neighborID);
});
/*client.on('end', function(){
console.log('Client is dis-connected!!');
});*/
clients[neighborID] = client;
});
} else if (msg['message'] === 'Start') {
if (state === constants.SLEEPING) {
wakeup();
}
}
});
function changeCore() {
console.log(id, 'called changeCore');
if (branchEdges.includes(bestEdge)) {
// Send CHANGE_CORE on bestEdge
// This propagates the CHANGE_CORE message to the node that found the MWOE
let message = {
source: id,
type: constants.CHANGE_CORE,
};
sendMessage(clients[bestEdge], message);
} else {
// This is the node that found the MWOE
// Send CONNECT message on bestEdge
let message = {
source: id,
type: constants.CONNECT,
payload: {
level
}
};
sendMessage(clients[bestEdge], message);
// Move the bestEdge from basicEdges to branchEdges
let index = basicEdges.indexOf(bestEdge);
basicEdges.splice(index, 1);
branchEdges.push(bestEdge);
}
}
// Random delay for each message to simulate async network
function sleep(milliseconds) {
let start = new Date().getTime();
for (let i = 0; i < 1e7; i++) {
if((new Date().getTime() - start) > milliseconds) {
break;
}
}
}
function sendDelayedMessage(client, message) {
let random = _.sample(allowedWait);
setTimeout(() => {
client.write(JSON.stringify(message));
console.log('Node ID : ' , id, 'Current Branch Edges : ', branchEdges);
console.log('Node ID : ' , id, 'Current Level : ', level);
console.log('Node ID : ' , id, 'Fragment ID: ', fragmentId);
}, random * 100);
}
function sendMessage(client, message) {
let random = _.sample(allowedWait);
sleep(random * 2000);
client.write(JSON.stringify(message));
console.log('Node ID : ' , id, 'Current Branch Edges : ', branchEdges);
console.log('Node ID : ' , id, 'Current Level : ', level);
console.log('Node ID : ' , id, 'Fragment ID: ', fragmentId);
}
function onAccept(source, payload) {
console.log(chalk.green(id + 'received ACCEPT from' + source));
testEdge = null;
if (neighbors[source] < bestWeight) {
bestEdge = parseInt(source);
bestWeight = neighbors[source];
}
report();
}
function onChangeCore(source, payload) {
console.log(chalk.magenta(id, ' received CHANGE_CORE from ' + source));
changeCore();
}
function onConnect(source, {level: l}) {
console.log(chalk.white.bold(id + ' received CONNECT from ' + source));
if (state === constants.SLEEPING) {
wakeup();
}
if (l < level) { // Absorb
console.log(chalk.bgGreen.black.bold(id + ' ABSORBS ' + source));
// Make the source edge a branch edge
let index = basicEdges.indexOf(parseInt(source));
basicEdges.splice(index, 1);
branchEdges.push(parseInt(source));
// Send INITIATE message on the source edge
let message = {
source: id,
type: constants.INITIATE,
payload: {
level,
fragmentId,
state
}
};
sendMessage(clients[source], message);
if (state === constants.FIND) {
// The fragment to be absorbed gets added to the search of the MWOE
findCount++;
}
} else if (basicEdges.includes(parseInt(source))) {
// Delay processing the message by placing it at the end of the queue
let message = {
source,
type: constants.CONNECT,
payload: {
level: l
}
};
delayedMessages.push(message);
} else { // Merge
console.log(chalk.bgGreen.black.bold(id + ' MERGES with ' + source));
// Send INITIATE message on the source edge
let message = {
source: id,
type: constants.INITIATE,
payload: {
level: level + 1,
fragmentId: neighbors[source],
state: constants.FIND
}
};
sendMessage(clients[source], message);
}
}
function onHalt(source, payload) {
console.log(chalk.red(id + ' received HALT from ' + source));
// Send HALT message on all branchEdges except source
branchEdges.filter(edge => edge !== source).forEach(edge => {
let message = {
source: id,
type: constants.HALT,
};
sendMessage(clients[edge], message);
});
console.log(chalk.bgRed.black.bold('Node ' + id + ' HALTED'));
console.log(chalk.bgYellow.black.bold('Node ' + id + '\'s FINAL BRANCH EDGES'),
branchEdges);
process.exit();
}
function onInitiate(source, {level: l, fragmentId: f, state: s}) {
console.log(chalk.green(id + ' received INITIATE from ' + source));
// Update state
level = l;
fragmentId = f;
state = s;
inBranch = parseInt(source);
bestEdge = null;
bestWeight = Number.POSITIVE_INFINITY;
// Send INITIATE messages on each branch edge except on inBranch
branchEdges.filter(edge => edge !== inBranch).forEach(edge => {
let message = {
source: id,
type: constants.INITIATE,
payload: {
level,
fragmentId,
state
}
};
sendMessage(clients[edge], message);
if (s === constants.FIND) { // The fragment is getting absorbed otherwise
findCount++;
}
});
if (s === constants.FIND) {
test();
}
}
function onReject(source, payload) {
console.log(chalk.red(id + ' received REJECT from ' + source));
// Move the edge from basicEdges to rejectedEdges
if (basicEdges.includes(parseInt(source))) {
let index = basicEdges.indexOf(parseInt(source));
basicEdges.splice(index, 1);
rejectedEdges.push(parseInt(source));
}
test();
}
function onReport(source, {bestWeight : w}) {
console.log(chalk.yellow(id + ' received REPORT from ' + source));
w = (w === 'infinity') ? Number.POSITIVE_INFINITY : w;
if (parseInt(source) !== inBranch) {
findCount--;
if (w < bestWeight) {
bestWeight = w;
bestEdge = parseInt(source);
}
report();
} else if (state === constants.FIND) {
console.log(id, 'Delays REPORT from', source);
// Delay processing the message
let message = {
source,
type: constants.REPORT,
payload: {
bestWeight: w
}
};
delayedMessages.push(message);
} else if (w > bestWeight) {
changeCore();
} else if (w === Number.POSITIVE_INFINITY &&
bestWeight === Number.POSITIVE_INFINITY) {
// Send HALT message on all branchEdges except inBranch
branchEdges.filter(edge => edge !== inBranch).forEach(edge => {
let message = {
source: id,
type: constants.HALT,
};
sendMessage(clients[edge], message);
});
// Output branch edges
console.log(chalk.bgRed.black.bold('CORE Node ' + id + ' HALTED'));
console.log(
chalk.bgYellow.black.bold('Node ' + id + '\'s FINAL BRANCH EDGES'),
branchEdges);
process.exit();
}
}
function onTest(source, {level: l, fragmentId: f}) {
console.log(chalk.white(id + ' received TEST from ' + source));
if (state === constants.SLEEPING) {
wakeup();
}
if (l > level) {
console.log(id, 'delays TEST from', source);
// Delay processing the message
let message = {
source,
type: constants.TEST,
payload: {
level: l,
fragmentId: f
}
};
delayedMessages.push(message);
} else if (f !== fragmentId) {
// Send ACCEPT message on the source edge
let message = {
source: id,
type: constants.ACCEPT,
};
sendMessage(clients[source], message);
} else {
if (basicEdges.includes(parseInt(source))) {
// Add the edge to rejectedEdges
let index = basicEdges.indexOf(parseInt(source));
basicEdges.splice(index, 1);
rejectedEdges.push(parseInt(source));
}
// Send REJECT message on source edge
if (testEdge !== parseInt(source)) {
let message = {
source: id,
type: constants.REJECT
};
sendMessage(clients[source], message);
} else {
test();
}
}
}
function report() {
console.log(id, 'called REPORT');
if (findCount === 0 && testEdge === null) {
// All REPORTS received and no basic edge left to test
state = constants.FOUND;
// Send REPORT message on inBranch
let message = {
source: id,
type: constants.REPORT,
payload: {
'bestWeight' : isFinite(bestWeight) ? bestWeight : 'infinity'
}
};
sendMessage(clients[inBranch], message);
}
}
function test() {
console.log(id, 'called TEST');
if (basicEdges.length > 0) {
// Send a TEST message on the minimum weight basic edge
// The testEdge remains a basic edge unless it receives an REJECT message
// So, it should not be removed from basicEdges
testEdge = basicEdges[0];
let message = {
source: id,
type: constants.TEST,
payload: {
level,
fragmentId
}
};
sendMessage(clients[testEdge], message);
} else { // No basic edges left to test
testEdge = null;
report();
}
}
function wakeup() {
// Zero level fragment wakes up
// Make the minimum weight adjacent edge a branch edge
let newBranchEdge = basicEdges.shift();
branchEdges.push(newBranchEdge);
level = 0;
state = constants.FOUND;
findCount = 0;
// Send CONNECT message to the minimum weight adjacent edge
let message = {
source: id,
type: constants.CONNECT,
payload: {
level
}
};
sendMessage(clients[newBranchEdge], message);
}