Browse Source

Refactors simulationManager's main loop, improves readability

Andreas T. Meyer-Berg 5 năm trước cách đây
mục cha
commit
3abec48832

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

@@ -69,6 +69,17 @@ public class PacketCollectionManager extends Observable {
 		}
 	}
 	
+	/**
+	 * Runs all registered PacketAlgorithms of the different collectors
+	 */
+	public void runPacketAlgorithms(){
+		for(PacketCollector collector:collectors){
+			if(collector.getPacketAlgorithm()!=null){
+				collector.getPacketAlgorithm().processPackets(collector.getPackets());
+			}
+		}
+	}
+	
 	/**
 	 * Adds a packetCollector, which will collect packages from now on
 	 * @param collector new package collector

+ 28 - 19
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimulationManager.java

@@ -62,12 +62,12 @@ public class SimulationManager extends Observable {
 	/**
 	 * PacketCollectionManager which manages the collection of packets
 	 */
-	private PacketCollectionManager packetCollectionManager;
+	private PacketCollectionManager collectionMan;
 	
 	/**
 	 * PacketExportManager which handles packet export
 	 */
-	private PacketExportManager packetExportManager;
+	private PacketExportManager exportMan;
 	
 	/**
 	 * Creates a new Simulationmanager
@@ -78,8 +78,8 @@ public class SimulationManager extends Observable {
 	public SimulationManager(Model model) {
 		this.model = model;
 		this.controller = null;
-		this.packetCollectionManager = new PacketCollectionManager(model);
-		this.packetExportManager = new PacketExportManager(model);
+		this.collectionMan = new PacketCollectionManager(model);
+		this.exportMan = new PacketExportManager(model);
 		timer = new Timer(0, t -> simulateTimeStep());
 	}
 
@@ -104,7 +104,7 @@ public class SimulationManager extends Observable {
 	}
 
 	/**
-	 * Simulates one time step at a given time fur a given duration
+	 * Simulates one time step at a given time for a given duration
 	 * 
 	 * @param startTime
 	 *            Time the simulation interval starts in
@@ -113,6 +113,26 @@ public class SimulationManager extends Observable {
 	 *            Duration of the simulation interval in milliseconds
 	 */
 	public void simulateTimeIntervall(long startTime, long duration) {
+		//Simulates the network
+		simulateNetwork(startTime, duration);
+		runAlgorithms(startTime+duration);
+		collectionMan.collectPackets();
+		//Run all 
+		collectionMan.runPacketAlgorithms();
+		//Export Packets
+		exportMan.exportPacketsOfLastTimeStep();
+	}
+	
+	/**
+	 * Simulates one time step of the network at a given time for a given duration
+	 * 
+	 * @param startTime
+	 *            Time the simulation interval starts in
+	 *            System.currentTimeMillis() time
+	 * @param duration
+	 *            Duration of the simulation interval in milliseconds
+	 */
+	private void simulateNetwork(long startTime, long duration){
 		// Nothing changed so far
 		statusChanged = false;
 		// Simulate all Links, and their connections
@@ -134,19 +154,8 @@ public class SimulationManager extends Observable {
 			model.setChanged();
 			model.notifyObservers();
 		}
-		runAlgorithms(startTime);
-		packetCollectionManager.collectPackets();
-		//Run all 
-		for(PacketCollector collector:packetCollectionManager.getPacketCollectors()){
-			if(collector.getPacketAlgorithm()!=null){
-				collector.getPacketAlgorithm().processPackets(collector.getPackets());
-			}
-		}
-		packetExportManager.exportPacketsOfLastTimeStep();
 	}
 
-	
-
 	/**
 	 * Start the simulation
 	 */
@@ -322,14 +331,14 @@ public class SimulationManager extends Observable {
 	 * @return PacketCollectionManager, which contains all the different packet collectors
 	 */
 	public PacketCollectionManager getPacketCollectionManager(){
-		return packetCollectionManager;
+		return collectionMan;
 	}
 	
 	/**
 	 * Returns the PacketExportManager
-	 * @return the packetExportManager
+	 * @return the exportMan
 	 */
 	public PacketExportManager getPacketExportManager(){
-		return packetExportManager;
+		return exportMan;
 	}
 }