diff --git a/sim/manager.js b/sim/manager.js index 41ff431763c173e566249bf35ce6631252de9675..59e6822a10e350f40cfe66bc884bbb155eb0bd91 100644 --- a/sim/manager.js +++ b/sim/manager.js @@ -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}); } @@ -108,24 +116,20 @@ function Manager(self) { this.buffer.push(packet); } }; - - 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) { - 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 diff --git a/sim/sim.js b/sim/sim.js index 48ae3204b23293a1d8c60bd82755dca89af7e1dd..303dc40157c468b8c964d9030d89b0ee2a1252c1 100755 --- a/sim/sim.js +++ b/sim/sim.js @@ -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); }); }); }