-
Notifications
You must be signed in to change notification settings - Fork 0
/
Computer.js
119 lines (103 loc) · 3.8 KB
/
Computer.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
var Packet = require('./Packet');
var NetworkNode = require('./NetworkNode');
function range(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
var waitBeforeSendMin = 1500;
var waitBeforeSendMax = 3000;
var waitingTimeAfterCollisionMin = 10000;
var waitingTimeAfterCollisionMax = 20000;
var GLOBAL_SENT = document.getElementById('sent');
var GLOBAL_REACHED = document.getElementById('reached');
var GLOBAL_STATS = document.getElementById('stats');
function increaseSENT() {
GLOBAL_SENT.innerText = parseInt(GLOBAL_SENT.innerText) + 1;
updateStatistic()
}
function increaseREACHED() {
GLOBAL_REACHED.innerText = parseInt(GLOBAL_REACHED.innerText) + 1;
updateStatistic()
}
function updateStatistic() {
GLOBAL_STATS.innerText = (parseFloat(GLOBAL_REACHED.innerText) / parseFloat(GLOBAL_SENT.innerText) * 100).toFixed(2) + '%';
}
class Computer extends NetworkNode {
constructor(htmlNode, label) {
super(htmlNode);
this.label = label;
this.simulationTimer = this.simulate(waitBeforeSendMin, waitBeforeSendMax);
this.destinations = [];
this.mailbox = [];
this.waitingTimer = null;
}
addDestination(host) {
this.destinations.push(host);
}
getRandomDestination() {
return this.destinations[Math.floor(Math.random() * this.destinations.length)];
}
getPacketId() {
return parseInt(GLOBAL_SENT.innerText);
}
simulate(min, max) {
var timeout = range(min, max);
var wasSent = false;
return setTimeout(() => {
var dest = this.getRandomDestination();
var packetId = this.getPacketId();
if (this.interfaces[0] !== null) {
if (this.interfaces[0].data == null) {
this.send(0, new Packet(this.label, this, dest, packetId));
wasSent = true;
}
}
if (this.interfaces[1] !== null) {
if (this.interfaces[1].data == null) {
this.send(1, new Packet(this.label, this, dest, packetId));
wasSent = true;
}
}
if (wasSent) {
increaseSENT();
}
this.simulationTimer = this.simulate(min, max);
}, timeout);
}
receive(packet, input) {
if (packet.data == "###") {
if(this.waitingTimer == null) {
if (this.htmlNode != null) {
this.htmlNode.setAttribute('disabled', 'true');
}
clearTimeout(this.simulationTimer);
var timeout = range(waitingTimeAfterCollisionMin, waitingTimeAfterCollisionMax);
this.waitingTimer = setTimeout(() => {
if (this.htmlNode != null) {
this.htmlNode.setAttribute('disabled', 'false');
}
this.waitingTimer = null;
this.simulationTimer = this.simulate(waitBeforeSendMin, waitBeforeSendMax);
}, timeout);
}
} else {
if (packet.from === this) {
console.log("Packet returned to sending host.");
} if (packet.to === this) {
var index = this.mailbox.findIndex(function(v, i, a) {
return v.id == packet.id;
});
if (index < 0) {
increaseREACHED();
this.mailbox.push(packet);
} else {
this.mailbox = this.mailbox.splice(index, 1);
console.error("DUPLICATE");
}
console.log("Packet reached desired host.");
} else {
super.receive(packet, input);
}
}
}
}
module.exports = Computer;