Skip to content
Snippets Groups Projects
Commit dd6c5a1b authored by Selby, Nicholas Stearns's avatar Selby, Nicholas Stearns
Browse files

How does setInterval work

parent 2f811c59
No related branches found
No related tags found
No related merge requests found
......@@ -11,9 +11,17 @@ function Manager(self) {
this.addr_table = {};
this.buffer = [];
this.maxBufferSize = 252;
this.delay = 100; //ms
this.delay = 1000; //ms
this.seenFloods = [];
this.checkBuffer = function() {
console.log(`about to check buffer of node ${self.id}`);
if (this.buffer.length > 0) {
this.handlePacket(this.buffer.shift());
}
// console.log(`checked buffer of node ${self.id}`);
};
this.setup = function(numports) {
this.numports = numports;
this.ports = new Array(numports).fill(-1);
......@@ -27,7 +35,7 @@ function Manager(self) {
};
this.printPorts = function() {
console.log(this.ports);
// console.log(this.ports);
};
this.connect = function(port, id) {
......@@ -41,7 +49,7 @@ function Manager(self) {
self.disconnect(prevId);
}
console.log('im '+ self.id + ' connecting to ' + id);
// console.log('im '+ self.id + ' connecting to ' + id);
this.ports[port] = id;
self.connect(id);
......@@ -91,7 +99,7 @@ function Manager(self) {
this.handlePacket(packet);
return;
}
self.log(`checkpoint`);
if (port < this.numports && this.ports[port] >= 0) {
self.send(this.ports[port], 'packet', {name: 'packet', obj: packet});
}
......@@ -109,23 +117,19 @@ function Manager(self) {
}
};
this.checkBuffer = function() {
if (this.buffer.length > 0) {
this.handlePacket(this.buffer.shift());
}
};
this.handlePacket = function(packet) {
// If LUT does not already have the source address, add the entry
if (packet.start === STD || packet.start === ACK || packet.start === STF || packet.start === ACF) {
if (!this.addr_table[packet.port].dests.hasOwnProperty(packet.src) || this.addr_table[packet.port].dests[packet.src]>packet.hopcount) {
if ( (packet.start === STD || packet.start === ACK || packet.start === STF || packet.start === ACF) // If this is not a buffer update
&& packet.src!==self.id // ...or a packet from me
&& (!this.addr_table[packet.port].dests.hasOwnProperty(packet.src) || this.addr_table[packet.port].dests[packet.src]!==packet.hopcount) ) { // ...and my entry for the source is invalid
this.addr_table[packet.port].dests[packet.src] = packet.hopcount;
}
}
if (packet.start === STD) { // Standard Packet
if (packet.dest === self.id) { // If I am destination
// Process packet
self.log(`${self.id} got message ${packet.data}`);
const nextPort = getMinCostPort(packet.src); // Pick the port to send ACK based off minimizing cost
this.sendPacket(ACK, packet.src, 0, self.id, nextPort);
} else {
packet.hopcount++; // Increment hopcount
const nextPort = getMinCostPort(packet.dest); // Pick the port to send to based off minimizing cost
......@@ -140,7 +144,7 @@ function Manager(self) {
}
} else if (packet.start === ACK) { // Acknowledgement
if (packet.dest === self.id) { // If I am destination
// Process ACK
self.log(`${self.id} got ACK from ${packet.src}`);
} else {
packet.hopcount++; // Increment hopcount
const nextPort = getMinCostPort(packet.dest); // Pick the port to send to based off minimizing cost
......@@ -154,8 +158,14 @@ function Manager(self) {
}
}
} else if (packet.start === STF) { // Standard Flood
if (this.addr_table[packet.port].dests.hasOwnProperty(packet.dest)) // If I thought this port could send to destination, remove it
delete this.addr_table[packet.port].dests[packet.dest]; // ...if that node had known, it wouldn't have forwarded it as a flood.
if (this.seenFloods.includes(thisFlood)) // If I have seen it before, don't forward
return;
if (packet.dest === self.id) { // If I am destination
// Process packet
self.log(`${self.id} got message ${packet.data}`);
const nextPort = getMinCostPort(packet.src); // Pick the port to send ACK based off minimizing cost
this.sendPacket(ACK, packet.src, 0, self.id, nextPort);
} else {
packet.hopcount++; // Increment hopcount
const thisFlood = { // Static information within packet for comparison
......@@ -163,8 +173,6 @@ function Manager(self) {
src: packet.src,
data: packet.data
};
if (this.seenFloods.includes(thisFlood)) // If I have seen it before, don't forward
return;
this.seenFloods.push(thisFlood); // Remember the packet
const nextPort = getMinCostPort(packet.dest); // Pick the port to send to based off minimizing cost
......@@ -178,8 +186,10 @@ function Manager(self) {
}
}
} else if (packet.start === ACF) { // ACK Flood
if (this.addr_table[packet.port].dests.hasOwnProperty(packet.dest)) // If I thought this port could send to destination, remove it
delete this.addr_table[packet.port].dests[packet.dest]; // ...if that node had known, it wouldn't have forwarded it as a flood.
if (packet.dest === self.id) { // If I am destination
// Process packet
self.log(`${self.id} got ACK from ${packet.src}`);
} else {
packet.hopcount++; // Increment hopcount
const thisFlood = { // Static information within packet for comparison
......
......@@ -6,11 +6,8 @@ const connectDelay = 100;
// INITIALIZE NETWORK TOPOLOGY HERE
var initTopology = [
[1, 2, 3, 4],
[2, 3, 4, 0],
[3, 4, 0, 1],
[4, 0, 1, 2],
[0, 1, 2, 3]
[0],
[1]
];
// Don't touch this code
......@@ -46,12 +43,12 @@ for (let i = 0; i < initTopology.length; i++) {
// PUT CUSTOM CODE HERE:
send(0, 1, 'hi!', 1000);
send(3, 3, 'what is up?', 1500);
disconnect(0, 1, 2, 2, 1700);
send(2, 2, 'You cannot see this cause we are not connected', 2000);
send(2, 0, 'we are friends now', 2500);
//send(0, 1, 'hi!', 1000);
//send(3, 3, 'what is up?', 1500);
//disconnect(0, 1, 2, 2, 1700);
//send(2, 2, 'You cannot see this cause we are not connected', 2000);
//send(2, 0, 'we are friends now', 2500);
sendPacket(0,1,1,"Hello 1!",0);
//Don't add stuff below this:
......@@ -82,13 +79,13 @@ function sendPacket(from, dest, size, data, delay, periodic=false) {
if (periodic) {
clients[from].init(function() {
this.tick(delay, function() {
this.manager.sendPacket(dest, size, data);
this.manager.sendPacket(252, dest, -1, undefined, size, data, -1);
});
});
} else {
clients[from].init(function() {
this.delay(delay, function() {
this.manager.sendPacket(dest, size, data);
this.manager.sendPacket(252, dest, -1, undefined, size, data, -1);
});
});
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment