-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolony.js
More file actions
706 lines (663 loc) · 17.8 KB
/
Copy pathcolony.js
File metadata and controls
706 lines (663 loc) · 17.8 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
var wssServer = require('ws').Server;
var wssClient = require('ws');
var sys = require('sys');
var _ = require('underscore');
var net = require('net');
var os = require('os');
var simpleserver = require('./simpleserver').simpleserver;
var simpleclient = require('./simpleclient').simpleclient;
/*
search for a colony member
if found
request the entity's addresses
Create alphaComm on a free slot
request the alpha's address
connect to the alpha (betaComm)
request shared vars
share our address
if not found
Setup alphaComm on the first spot available
*/
function colony(options) {
this.options = options;
this.host = findServerIP();
this.hosts = ["localhost"];
this.range = [8080,8090];
this.shared = {};
this.alpha = false;
this.ocount = 0; // online count. Value sent from outside the class.
this.max = 2000;
this.log("Online on "+this.host);
}
colony.prototype.log = function(){
var red, blue, reset;
red = '\u001b[31m';
blue = '\u001b[34m';
reset = '\u001b[0m';
console.log(red+"<COLONY>");
for (i in arguments) {
console.log(reset, arguments[i],reset);
}
};
colony.prototype.error = function(){
var red, blue, reset;
red = '\u001b[31m';
blue = '\u001b[34m';
reset = '\u001b[0m';
console.log(red+"<COLONY>");
for (i in arguments) {
console.log(red,arguments[i],reset);
}
};
colony.prototype.info = function(){
var red, blue, reset;
red = '\u001b[31m';
blue = '\u001b[34m';
reset = '\u001b[0m';
console.log(red+"<COLONY>");
for (i in arguments) {
console.log(blue,arguments[i],reset);
}
};
colony.prototype.connect = function(callback){
this.connect_callback = callback;
// Find the colony
this.map();
};
// Map the colony
colony.prototype.map = function(){
var i;
var j;
var r = 0;
var scope = this;
this.colonymap = [];
// We generate the list of possible entity addresses
for (i=0;i<this.hosts.length;i++) {
for (j=this.range[0];j<=this.range[this.range.length-1];j++) {
r++;
this.colonymap.push({
rank: r,
host: this.hosts[i],
port: j,
uuid: this.hosts[i]+":"+j,
online: false,
alpha: false
});
}
}
this.l = this.colonymap.length;
this.c = 0;
// We'll try to connect to all the possible entities, to check who is online and who is offline
// Then we call onMap()
var probes = {};
for (i=0;i<this.l;i++) {
(function() {
var n = i;
probes[scope.colonymap[n].uuid] = new simpleclient(scope.colonymap[n].host,scope.colonymap[n].port,{
onConnect: function(instance) {
scope.colonymap[n].online = true;
scope.log(scope.colonymap[n].uuid+" is online.");
scope.c++;
instance.send("alpha?");
if (scope.c == scope.l) {
scope.onMap();
}
},
onFail: function(instance) {
scope.colonymap[n].online = false;
scope.log(scope.colonymap[n].uuid+" is offline.");
scope.c++;
instance.close(); // close the connection
if (scope.c == scope.l) {
scope.onMap();
}
},
onReceive: function(instance, message) {
scope.log("Message received from "+scope.colonymap[n].uuid+": ",message);
if (message == "yes") {
scope.info(scope.colonymap[n].uuid+" is the alpha");
scope.colonymap[n].alpha = true;
}
instance.close(); // close the connection
}
});
})();
}
};
// Called when the colony is mapped
colony.prototype.onMap = function(){
var i;
var j;
var r = 0;
// How many entities are online?
var online = 0;
for (i=0;i<this.l;i++) {
if (this.colonymap[i].online == true) {
online++;
}
}
this.log(online+" entities online");
if (online == 0) {
this.log("We are the Alpha entity.");
this.alpha = true;
}
// search for an available spot to setup our server
for (i=0;i<this.l;i++) {
if (this.colonymap[i].host == this.host) {
/*for (j=this.range[0];j<=this.range[this.range.length-1];j++) {
r++;
if (this.colonymap[i].port == j && this.colonymap[i].online == false) {
// we found an available port
this.rank = r;
this.log("Available spot on "+this.host+":"+j+" (rank #"+r+")");
this.setupServer(j);
return j;
}
}*/
if (this.colonymap[i].online == false) {
// we found an available port
this.rank = i;
this.log("Available spot on "+this.host+":"+this.colonymap[i].port+" (rank #"+i+")");
this.setupServer(this.colonymap[i].port);
return j;
}
}
}
// if we get to this point in the code, then there was no available slot to create a new server
this.error("No available slot found. Server can't be created.");
process.exit(1);
};
// Setup the server and client
colony.prototype.setupServer = function(port){
this.port = port;
var scope = this;
this.server = new simpleserver(this.port, {
onConnect: function(client) {
scope.log("INCOMING CONNECTION:", client.uid);
},
onReceive: function(client, data) {
scope.log("Message from #"+client.uid+":", data);
switch (data) {
// Client asking if we are the alpha
case "alpha?":
scope.server.send(client.uid, scope.alpha?"yes":"no");
break;
// Client asking for the UID
case "uid":
scope.server.send(client.uid, {
uid: client.uid
});
break;
// Client asking to join the colony
case "join":
var i;
var output = {};
for (i in scope.shared) {
output[i] = scope.shared[i].value;
}
scope.server.send(client.uid, {
welcome: true,
shared: output // Sending the shared variables so that the entity can be up to date
});
break;
default:
// broadcast request
if (data.broadcast) {
scope.server.broadcast(data.broadcast, data.except);
}
break;
}
},
onQuit: function(client) {
scope.log("Client Quit:", client.uid);
if (scope.alpha) {
// find the dead entity and remove it from the entity array
var deadEntity = scope.getEntity("uid", client.uid);
if (deadEntity !== false) {
scope.shared["entities"].remove(deadEntity.index);
}
scope.log("Dead Entity",deadEntity);
}
}
});
// Our server is created.
// Now we need to join the colony
// We'll join the Alpha and tell him we want to join.
if (this.alpha) {
// We are the alpha, and the server is created
// setup shared variables
this.share("entities", []);
/*this.nobroadcast();
this.shared["entities"].push({
host: this.host,
port: this.port,
uuid: this.host+":"+this.port
});*/
}
// setup the client
this.setupClient();
};
colony.prototype.setupClient = function(host, port){
var scope = this;
// handle defaults, to be able to change the alpha's address if we want
if (!host) {
host = scope.getAlpha().host;
}
if (!port) {
port = scope.getAlpha().port;
}
this.client = new simpleclient(host,port,{
onConnect: function(instance) {
// We are now connected to the alpha.
// Asking for our UID
instance.send("uid");
},
onFail: function(instance) {
},
onClose: function(instance, code) {
scope.error("Alpha is dead");
// find the new alpha, based on rank
if (scope.shared["entities"].value.length > 1) { // making sure we're not the only one left here
var deadAlpha = scope.getMinBut(scope.shared["entities"].value, "rank", false);
var newAlpha = scope.getMinBut(scope.shared["entities"].value, "rank", deadAlpha.data.rank).data; //scope.shared["entities"].at(1);
scope.log("Dead alpha:", deadAlpha);
scope.log("New alpha:", newAlpha);
scope.log("##", newAlpha.uuid,scope.uuid);
if (newAlpha.uuid == scope.uuid) {
scope.info("We are the new alpha.");
scope.alpha = true;
// empty the list of entities locally. We will rebuild it completely.
//scope.shared["entities"].value = new Array();
scope.share("entities", []);
// connect to the server now
scope.setupClient(newAlpha.host, newAlpha.port);
} else {
scope.info("We are *NOT* the new alpha.");
// connect to the client after one second
setTimeout(function() {
scope.setupClient(newAlpha.host, newAlpha.port);
}, 1000);
}
} else {
scope.info("We are the only entity left online");
}
},
onReceive: function(instance, message) {
//scope.log("!!!!!!!!!!!Receiving", message);
// uid
if (message.uid) {
scope.client_uid = message.uid;
// ask to join the colony
instance.send("join");
}
// Welcome message
if (message.welcome) {
// Get the shared variables (shared memory)
for (i in message.shared) {
scope.share(i, message.shared[i]);
}
// register ourselves into the list of entities
scope.uuid = scope.host+":"+scope.port;
scope.shared["entities"].push({
rank: scope.rank,
uid: scope.client_uid,
host: scope.host,
port: scope.port,
uuid: scope.host+":"+scope.port
});
// if there is a connect callback
if (scope.connect_callback) {
scope.connect_callback(scope);
}
}
// Shared memory update
if (message.share && message.share.from != scope.uuid) {
scope.log("Executing :: scope.shared["+message.share.label+"]."+message.share.type+"(",message.share.data,")");
scope.nobroadcast(); // turn off the broadcasting of the method for the next call (else it does an infinite broadcast loop)
scope.shared[message.share.label][message.share.type](message.share.data);
scope.info("scope.shared["+message.share.label+"] is now: ",scope.shared[message.share.label].value);
}
// A server is full. We need to check if we have space to receive the user
if (message.full) {
if (scope.ocount >= scope.max) {
scope.error("We are full. Can't accept any user.");
/*scope.broadcast({
entity: scope.uuid,
available: false,
uuid: message.uuid // replying with the user's UUIDv4, to track the request to its source
});*/
} else {
scope.info("We are *NOT* full. We can accept more users.");
scope.broadcast({
available: true,
_for: message._from,
entity: scope.uuid,
host: scope.host,
port: scope.port+scope.portUpdater, // portUpdater is used to calculate the public server's port number
uuid: message.uuid // replying with the user's UUIDv4, to track the request to its source
});
}
}
// A server volunteered to receive more players.
if (message.available && message.available === true && message._for == scope.uuid) {
scope.info("A SERVER VOLUNTEERED:",message.entity);
if (scope.sInstance) {
scope.sInstance.onReceive(message);
} else {
scope.error("You don't have any circular reference to the Fleet server (scope.sInstance)");
}
}
}
});
};
colony.prototype.nobroadcast = function(){
this.skipBroadcast = true;
};
// Get an entity ref (from the entities found by scanning)
colony.prototype.getDetectedEntity = function(prop, val){
var i;
for (i=0;i<this.l;i++) {
if (this.colonymap[i][prop] == val) {
return this.colonymap[i];
}
}
};
// Get an entity ref (from the entities in the colony)
colony.prototype.getEntity = function(prop, val){
var i;
for (i=0;i<this.shared["entities"].value.length;i++) {
console.log("-->",this.shared["entities"].value[i]);
if (this.shared["entities"].value[i][prop] == val) {
return {
index: i,
value: this.shared["entities"].value[i]
};
}
}
return false;
};
colony.prototype.getAlpha = function(){
var i;
for (i=0;i<this.l;i++) {
if (this.colonymap[i].alpha == true) {
return this.colonymap[i];
}
}
return this.colonymap[0];
};
// get minimum which isn't a particular value
colony.prototype.getMinBut = function(data, prop, except){
//this.log("*** getMinBut() ***",data, prop, except);
var i;
var index;
var min = 1000000000;
for (i=0;i<data.length;i++) {
if (data[i][prop] <= min && data[i][prop] !== except) {
min = data[i][prop];
index = i;
}
}
return {
index: index,
min: min,
data: data[index]
}
};
// share data with the colony
colony.prototype.share = function(label, data){
if (this.shared[label]) {
return false; // var already exist
}
if (data instanceof Array) {
this.shared[label] = new sharedArray(this, label, data);
this.shared[label].log(label,"data type Array",data);
} else if (data instanceof Object) {
this.shared[label] = new sharedObject(this, label, data);
this.shared[label].log(label,"data type Object",data);
} else if (typeof data == "string") {
this.shared[label] = new sharedString(this, label, data);
this.shared[label].log(label,"data type String",data);
} else if (typeof data == "number") {
this.shared[label] = new sharedNumber(this, label, data);
this.shared[label].log(label,"data type Number",data);
} else {
console.log(label,"data type Unknown ("+(typeof data)+")",data);
}
return this.shared[label];
}
colony.prototype.sharedExport = function(){
var i;
var output = {};
for (i in this.shared) {
output[i] = this.shared[i].value;
}
return output;
}
colony.prototype.broadcast = function(data){
if (this.skipBroadcast) {
this.skipBroadcast = false;
return false;
}
// add source
data._from = this.uuid;
if (this.alpha) {
this.server.broadcast(data);
} else {
this.client.send({
broadcast: data,
except: [this.client_uid]
});
}
}
/*************
* SHAREDARRAY
*************/
function sharedArray(instance, label, value) {
this.colony = instance;
this.label = label;
this.value = value.splice(0,value.length);
}
sharedArray.prototype.send = function(type,data) {
this.colony.broadcast({
share:{
type: type,
label: this.label,
data: data,
from: this.colony.uuid
}
});
};
sharedArray.prototype.push = function(data) {
this.value.push(data);
this.send("push", data);
return this.value;
};
sharedArray.prototype.remove = function(index) {
this.value.splice(index,1);
this.send("remove", index);
return this.value;
};
sharedArray.prototype.at = function(index) {
return this.value[index];
};
sharedArray.prototype.replace = function(value) {
this.value = value.splice(0,value.length);
return this.value;
};
sharedArray.prototype.log = function(){
var red, blue, reset;
red = '\u001b[31m';
blue = '\u001b[34m';
reset = '\u001b[0m';
console.log(red+"<sharedArray>");
for (i in arguments) {
console.log(reset, arguments[i],reset);
}
};
/*************
* SHAREDOBJECT
*************/
function sharedObject(instance, label, value) {
this.colony = instance;
this.label = label;
this.value = _.extend({},value);
}
sharedObject.prototype.send = function(type,data) {
this.colony.broadcast({
share:{
type: type,
label: this.label,
data: data,
from: this.colony.uuid
}
});
};
sharedObject.prototype.at = function(index) {
return this.value[index];
};
sharedObject.prototype.set = function(opt) {
this.value[opt.label] = opt.value;
this.send("set", opt);
return this.value;
};
sharedObject.prototype.setsub = function(opt) {
this.value[opt.label][opt.prop] = opt.value;
this.send("setsub", opt);
return this.value;
};
sharedObject.prototype.push = function(data) {
var i;
for (i in data) {
this.value[i] = data[i];
}
this.send("push", data);
return this.value;
};
sharedObject.prototype.remove = function(index) {
delete this.value[index];
this.send("remove", index);
return this.value;
};
sharedObject.prototype.replace = function(value) {
this.value = _.extend({},value);
return this.value;
};
sharedObject.prototype.log = function(){
var red, blue, reset;
red = '\u001b[31m';
blue = '\u001b[34m';
reset = '\u001b[0m';
console.log(red+"<sharedObject>");
for (i in arguments) {
console.log(reset, arguments[i],reset);
}
};
/*************
* SHAREDSTRING
*************/
function sharedString(instance, label, value) {
this.colony = instance;
this.label = label;
this.value = value.toString();
}
sharedString.prototype.send = function(type,data) {
this.colony.broadcast({
share:{
type: type,
label: this.label,
data: data,
from: this.colony.uuid
}
});
};
sharedString.prototype.val = function(value) {
this.value = value.toString();
this.send("val", value);
return this.value;
};
sharedString.prototype.replace = function(value) {
this.value = value.toString();
return this.value;
};
sharedString.prototype.log = function(){
var red, blue, reset;
red = '\u001b[31m';
blue = '\u001b[34m';
reset = '\u001b[0m';
console.log(red+"<sharedString>");
for (i in arguments) {
console.log(reset, arguments[i],reset);
}
};
/*************
* SHAREDNUMBER
*************/
function sharedNumber(instance, label, value) {
this.colony = instance;
this.label = label;
this.value = value*1;
}
sharedNumber.prototype.send = function(type,data) {
this.colony.broadcast({
share:{
type: type,
label: this.label,
data: data,
from: this.colony.uuid
}
});
};
sharedNumber.prototype.val = function(value) {
this.value = value*1;
this.send("val", value);
return this.value;
};
sharedNumber.prototype.replace = function(value) {
this.value = value*1;
return this.value;
};
sharedNumber.prototype.add = function(value) {
this.value += value;
this.send("add", value);
return this.value;
};
sharedNumber.prototype.substract = function(value) {
this.value -= value;
this.send("substract", value);
return this.value;
};
sharedNumber.prototype.multiply = function(value) {
this.value *= value;
this.send("multiply", value);
return this.value;
};
sharedNumber.prototype.divide = function(value) {
this.value /= value;
this.send("divide", value);
return this.value;
};
sharedNumber.prototype.log = function(){
var red, blue, reset;
red = '\u001b[31m';
blue = '\u001b[34m';
reset = '\u001b[0m';
console.log(red+"<sharedNumber>");
for (i in arguments) {
console.log(reset, arguments[i],reset);
}
};
function findServerIP() {
return "localhost";
var i;
var interfaces = os.networkInterfaces();
for(name in interfaces) {
var interface = interfaces[name];
for (i=0;i<interface.length;i++) {
if(interface[i].family === 'IPv4') {
console.log("interface",interface[i]);
return interface[i].address;
}
}
}
}
exports.colony = colony;