From d0acb5fa5dc8500c101282b43c6b7fd31f92b9fa Mon Sep 17 00:00:00 2001
From: Patrick Wahl <patrickjwahl@gmail.com>
Date: Mon, 6 Nov 2017 23:04:12 -0500
Subject: [PATCH] Add buffer and delay to manager

---
 sim/manager.js | 37 ++++++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 13 deletions(-)

diff --git a/sim/manager.js b/sim/manager.js
index 0bce41f..f3af7b5 100644
--- a/sim/manager.js
+++ b/sim/manager.js
@@ -4,10 +4,15 @@ function Manager(self) {
 	this.ports = [];
 	this.numports = 0;
 	this.addr_table = {};
+	this.buffer = [];
+	this.maxBufferSize = 252;
+	this.delay = 100; //ms
 
 	this.setup = function(numports) {
 		this.numports = numports;
 		this.ports = new Array(numports).fill(-1);
+
+		setInterval(this.checkBuffer, this.delay);
 	};
 
 	this.printPorts = function() {
@@ -17,7 +22,6 @@ function Manager(self) {
 	this.connect = function(port, id) {
 
 		if (!(port < this.numports)) {
-			console.log('fuck');
 			return;
 		}
 
@@ -31,9 +35,6 @@ function Manager(self) {
 
 			this.ports[port] = id;
 			self.connect(id);
-		} else {
-			console.log('double fuck');	
-		}
 	};
 
 	this.disconnect = function(port) {
@@ -92,19 +93,29 @@ function Manager(self) {
 		}
 
 		var packet = o.obj;
-		packet.edges += 1;
+		
+		if (this.buffer.length < this.maxBufferSize) {
+			this.buffer.push(packet);
+		}
 
-		if (!this.addr_table.hasOwnProperty(packet.src)) {
-			this.addr_table[packet.src] = new Array(numports).fill(Infinity);
-			this.addr_table[packet.src][port] = packet.edges;
-		} else {
-			if (packet.edges < this.addr_table[packet.src][port]) {
+	};
+
+	this.checkBuffer = function() {
+		if (this.buffer.length > 0) {
+			let packet = this.buffer.shift();
+			packet.edges += 1;
+
+			if (!this.addr_table.hasOwnProperty(packet.src)) {
+				this.addr_table[packet.src] = new Array(numports).fill(Infinity);
 				this.addr_table[packet.src][port] = packet.edges;
+			} else {
+				if (packet.edges < this.addr_table[packet.src][port]) {
+					this.addr_table[packet.src][port] = packet.edges;
+				}
 			}
-		}
-
-		this.handlePacket(packet);
 
+			this.handlePacket(packet);
+		}
 	};
 
 	this.handlePacket = function(packet) {
-- 
GitLab