Browse Source

Minor fixes, Setup/Finalize for Link simulation

Andreas T. Meyer-Berg 5 years ago
parent
commit
1fa364ecc0

+ 17 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Link.java

@@ -66,6 +66,14 @@ public interface Link {
 	@Deprecated
 	public void simulateTimeInterval(long startTime, long duration);
 	
+	/**
+	 * Initializes the simulation interval, by clearing the previously generated 
+	 * packets and adds Packets of previous iterations, which are are part of this interval.
+	 * @param startTime startTime of the simulation interval
+	 * @param duration duration of the simulation interval
+	 */
+	public void initSimulationInterval(long startTime, long duration);
+	
 	/**
 	 * Encapsulates the given Packets
 	 * @param packets Packets which should be encapsulated
@@ -73,6 +81,15 @@ public interface Link {
 	 */
 	public Collection<Packet> encapsulatePackages(Collection<Packet> packets);
 
+	/**
+	 * Time at the end of an simulation interval, to remove Packets, which are not in bound, sort the array
+	 * or manipulate the packets slightly.
+	 * 
+	 * @param startTime startTime of the simulation interval
+	 * @param duration duration of the interval
+	 */
+	public void finalizeSimulationInterval(long startTime, long duration);
+	
 	/**
 	 * Returns all packets which where sent during the last Simulation time step
 	 * 

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

@@ -214,4 +214,35 @@ public class PrecisionLink implements Link {
 	public void addPackets(Collection<Packet> packets) {
 		this.packets.addAll(packets);
 	}
+
+	@Override
+	public void initSimulationInterval(long startTime, long duration) {
+		/**
+		 * Reset packets
+		 */
+		packets.clear();
+		/**
+		 * Add out of Bounds packets
+		 */
+		packets.addAll(outOfBoundsPackets);
+	}
+
+	@Override
+	public void finalizeSimulationInterval(long startTime, long duration) {
+		//Remove out of Bounds Packets
+		/**
+		 * 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();
+		}
+	}
 }

+ 6 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimulationManager.java

@@ -136,18 +136,22 @@ public class SimulationManager extends Observable {
 		if(oldWay)
 			simulateNetwork(startTime, duration);
 		else{
-			long maxTime = startTime+duration;
+			long maxTime = startTime + duration;
 			scheduler.scheduleAll(model);
 			for(Connection con:model.getConnections()){
 				Link l = con.getLink();
-				if(l!=null)
+				if(l!=null){
+					l.getPackets().clear();
 					l.addPackets(con.getTerminationPackages(startTime));
+				}
 			}
 			while(scheduler.hasNext(maxTime)){
 				Schedulable currentEvent = scheduler.getAndRemoveFirst();
 				System.out.println("Event time: "+currentEvent.getEventTime());
 				currentEvent.simulateEvent();
 			}
+			// Simulate SmartDevices - if they need some logic -> TODO: Insert as schedulable
+			model.getDevices().forEach(d -> d.simulateTimeStep(startTime, duration));
 		}
 		runAlgorithms(startTime+duration);
 		collectionMan.collectPackets();

+ 11 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimpleLink.java

@@ -194,4 +194,15 @@ public class SimpleLink implements Link {
 	public void addPackets(Collection<Packet> packets) {
 		this.packets.addAll(packets);
 	}
+
+	@Override
+	public void initSimulationInterval(long startTime, long duration) {
+		packets.clear();
+		
+	}
+
+	@Override
+	public void finalizeSimulationInterval(long startTime, long duration) {
+		
+	}
 }