Bladeren bron

Fixes Link & Connection Precision sending packets after endTime

Sometimes packets could be sent at the wrong time (e.g. past the
simulation interval). Will be sent in the next time step
Andreas T. Meyer-Berg 5 jaren geleden
bovenliggende
commit
03460b79d3

+ 41 - 4
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/ConnectionPrecision.java

@@ -14,6 +14,11 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.PacketComparator;
  */
 public class ConnectionPrecision extends ConnectionPerformance {
 
+	/**
+	 * Packets which should be returned in the next time step
+	 */
+	private LinkedList<Packet> outOfBoundsPackets = new LinkedList<Packet>();
+	
 	/**
 	 * Initializes the connection, adds participants of the protocol
 	 * 
@@ -38,9 +43,18 @@ public class ConnectionPrecision extends ConnectionPerformance {
 		 */
 		long endTime = startTime+duration;
 		
-		LinkedList<Packet> returnPackets=new LinkedList<Packet>();
+		/**
+		 * All packets which should be returned in this time step
+		 */
+		LinkedList<Packet> returnPackets = new LinkedList<Packet>();
 		returnPackets.addAll(getTerminationPackages(startTime));
-		startTime+=returnPackets.size();
+		startTime += returnPackets.size();
+		
+		/**
+		 * Add packets of the last time step
+		 */
+		returnPackets.addAll(outOfBoundsPackets);
+		
 		/**
 		 * Self Sorting LinkedList for consistency
 		 */
@@ -81,8 +95,8 @@ public class ConnectionPrecision extends ConnectionPerformance {
 			
 			if(p.getStatus()==Port.SENDING){
 				if(p.getLastTrigger()+p.getTriggerInterval()<endTime){
-					//Generate first package in the simulation interval
-					returnPackets.addAll(protocol.generateNextPakets(p, (long) (p.getLastTrigger()+p.getTriggerInterval()+p.getJitter()*(Math.random()-0.5)),Math.random()<packetLossRate));
+					//Generate first package in the simulation interval (not before startTime though)
+					returnPackets.addAll(protocol.generateNextPakets(p, Math.max((long) (p.getLastTrigger()+p.getTriggerInterval()+p.getJitter()*(Math.random()-0.5)),startTime),Math.random()<packetLossRate));
 
 					//Remove Ports, which won't simulate again during this time step
 					it.remove();
@@ -117,6 +131,29 @@ public class ConnectionPrecision extends ConnectionPerformance {
 		 * Sort and return packages
 		 */
 		returnPackets.sort(new PacketComparator());
+		
+		/**
+		 * Remove packets which are not being sent in this time interval
+		 */
+		outOfBoundsPackets.clear();
+		/**
+		 * Last package, which should be sent in the next time step
+		 */
+		Packet last = returnPackets.isEmpty()? null : returnPackets.getLast();
+		
+		while (last != null&&last.getTimestamp()>endTime) {
+			outOfBoundsPackets.addFirst(returnPackets.removeLast());
+			if(returnPackets.isEmpty())
+				break;
+			last = returnPackets.getLast();
+		}
+		if(!outOfBoundsPackets.isEmpty()||true){
+			System.out.println("Connection: "+name);
+			System.out.println("Returned: ");
+			returnPackets.forEach(p->System.out.println(p.getTextualRepresentation()));
+			System.out.println("Not returned:");
+			outOfBoundsPackets.forEach(p->System.out.println(p.getTextualRepresentation()));
+		}
 		return returnPackets;
 	}
 	

+ 20 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/PrecisionLink.java

@@ -38,6 +38,10 @@ public class PrecisionLink implements Link {
 	 * after termination
 	 */
 	private LinkedList<Packet> packets;
+	/**
+	 * Packets which should be returned in the next time step
+	 */
+	private LinkedList<Packet> outOfBoundsPackets = new LinkedList<Packet>();
 	
 	/**
 	 * whether the status changed during the last simulation step
@@ -122,6 +126,7 @@ public class PrecisionLink implements Link {
 	public void simulateTimeInterval(long startTime, long duration) {
 		packets.clear();
 		statusChanged = false;
+		packets.addAll(outOfBoundsPackets);
 		for (Connection c : connections) {
 			if (c.getLink() != this)
 				continue;
@@ -135,6 +140,21 @@ public class PrecisionLink implements Link {
 			statusChanged|=c.getStatusChanged();
 		}
 		packets.sort(new PacketComparator());
+
+		/**
+		 * Remove packets which are not being sent in this time interval
+		 */
+		outOfBoundsPackets.clear();
+		/**
+		 * Last package, which should be sent in the next time step
+		 */
+		Packet last = packets.isEmpty()? null : packets.getLast();
+		while (last != null&&last.getTimestamp()>startTime+duration) {
+			outOfBoundsPackets.addFirst(packets.removeLast());
+			if(packets.isEmpty())
+				break;
+			last = packets.getLast();
+		}
 	}
 
 	@Override