Sfoglia il codice sorgente

Adds SimulationExportManager as part of the SimulationManager

Andreas T. Meyer-Berg 6 anni fa
parent
commit
926050819e

+ 16 - 6
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/SimulationController.java

@@ -55,7 +55,7 @@ public class SimulationController {
 	 * @return if the packets are printed
 	 */
 	public boolean getPrintPackets() {
-		return sim.getPrintPackets();
+		return sim.getPacketExportManager().getPrintPackets();
 	}
 
 	/**
@@ -63,7 +63,8 @@ public class SimulationController {
 	 * @param printPackets true if simulation should print the packets
 	 */
 	public void setPrintPackets(boolean printPackets) {
-		sim.setPrintPackets(printPackets);
+		sim.getPacketExportManager().setPrintPackets(printPackets);
+		notifyPanels();
 	}
 
 	/**
@@ -72,7 +73,7 @@ public class SimulationController {
 	 * @return ExportFile File, where the packets are written to
 	 */
 	public File getExportFile() {
-		return sim.getExportFile();
+		return sim.getPacketExportManager().getExportFile();
 	}
 	
 	/**
@@ -81,7 +82,8 @@ public class SimulationController {
 	 * @param exportFile File, where the packets are written to
 	 */
 	public void setExportFile(File exportFile) {
-		sim.setExportFile(exportFile);
+		sim.getPacketExportManager().setExportFile(exportFile);
+		notifyPanels();
 	}
 
 	/**
@@ -89,7 +91,7 @@ public class SimulationController {
 	 * @return true, if each links uses a single file for export
 	 */
 	public boolean isSplitLinkExportFiles() {
-		return sim.isSplitLinkExportFiles();
+		return sim.getPacketExportManager().isSplitLinkExportFiles();
 	}
 
 	/**
@@ -97,7 +99,8 @@ public class SimulationController {
 	 * @param splitLinkExportFiles true if each link should export to a single file
 	 */
 	public void setSplitLinkExportFiles(boolean splitLinkExportFiles) {
-		sim.setSplitLinkExportFiles(splitLinkExportFiles);
+		sim.getPacketExportManager().setSplitLinkExportFiles(splitLinkExportFiles);
+		notifyPanels();
 	}
 	
 	/**
@@ -236,4 +239,11 @@ public class SimulationController {
 	public void removeAlgo(NetworkManipulationAlgorithm algo){
 		sim.removeAlgo(algo);
 	}
+	
+	/**
+	 * Notify the panels, which could update their GUI
+	 */
+	public void notifyPanels(){
+		sim.notifyPanels();
+	}
 }

+ 190 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/PacketExportManager.java

@@ -0,0 +1,190 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.LinkedList;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.PacketComparator;
+
+/**
+ * Manager which handles the exports of packets during the simulation
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class PacketExportManager {
+
+	/**
+	 * Model
+	 */
+	private Model model;
+	
+	/**
+	 * True if packets should be printed
+	 */
+	private boolean printPackets = false;
+
+	/**
+	 * Writer to write the packets to a file
+	 */
+	private BufferedWriter writer;
+
+	/**
+	 * Base File for the exported Packages
+	 */
+	private File exportFile = new File("testPackets.log");
+
+	/**
+	 * Split Links into differentFiles
+	 */
+	private boolean splitLinkExportFiles = false;
+
+	/**
+	 * Creates a new PacketExportManager
+	 * @param model main model, which links should be exported
+	 */
+	public PacketExportManager(Model model) {
+		this.model = model;
+	}
+	
+	/**
+	 * Exports the packets which were sent during the last time step according
+	 * to the settings (whether packages should be printed, specified export
+	 * File, splitLinks setting etc.)
+	 */
+	public void exportPacketsOfLastTimeStep() {
+		if (printPackets) {
+			if (!splitLinkExportFiles) {
+				/**
+				 * Packets of all links, merged together
+				 */
+				LinkedList<Packet> packets = new LinkedList<Packet>();
+				model.getConnectionNetworks().forEach(a->packets.addAll(a.getPackets()));
+				packets.sort(new PacketComparator());
+				
+				writePacketsToFile(packets, exportFile);
+			}else{
+				for(Link link:model.getConnectionNetworks()){
+					/**
+					 * File name:
+					 * test.log
+					 * to
+					 * test_linkName.log
+					 */
+					String fileName = exportFile.getName();
+					//Add linkName
+					if(fileName.contains(".")){
+						String baseName = fileName.substring(0, fileName.lastIndexOf('.'));
+						String ending = fileName.substring(fileName.lastIndexOf('.')+1, fileName.length());
+						fileName = baseName +"_"+link.getName()+"."+ending;
+					}else{
+						fileName = fileName +"_"+link.getName();
+					}
+					/**
+					 * Path to parent directory
+					 */
+					String parent = exportFile.getParent();
+					/**
+					 * File with the fileName
+					 */
+					File linkExportFile = new File((parent == null?"":parent)+fileName);
+					/**
+					 * Write Packets
+					 */
+					writePacketsToFile(link.getPackets(), linkExportFile);
+				}
+			}
+		}
+	}
+
+	/**
+	 * Writes all packets of the given Collection to the specified file
+	 * @param packets packets which should be written
+	 * @param file file the packets should be written to
+	 */
+	private void writePacketsToFile(Collection<Packet> packets, File file){
+		if(file == null || packets == null)
+			return;
+		try {
+			//Check if file exists, if not create
+			file.createNewFile();
+			//Start Writing
+			writer = new BufferedWriter(new FileWriter(file.getAbsolutePath(), true));
+			//Write all packets
+			packets.forEach(p -> {
+				try {
+					writer.append(p.getTextualRepresentation() + "\n");
+				} catch (Exception e) {
+					System.err.println("Warning: Error on exporting packets: "+e.toString());
+				}
+			});
+		} catch (Exception e) {
+			System.err.println("Warning: Writing failed on file "+file.getPath()+"\n"+e.toString());
+		} finally {
+			//Close open writer
+			if (writer != null)
+				try {
+					writer.close();
+				} catch (IOException e) {}
+		}
+	}
+
+	/**
+	 * Returns true if the simulations will print the packets
+	 * 
+	 * @return if the packets are printed
+	 */
+	public boolean getPrintPackets() {
+		return printPackets;
+	}
+
+	/**
+	 * Sets Print Packets, if true, the simulation will print the packets
+	 * 
+	 * @param printPackets
+	 *            true if simulation should print the packets
+	 */
+	public void setPrintPackets(boolean printPackets) {
+		this.printPackets = printPackets;
+	}
+
+	/**
+	 * Get BaseFile of packet exports
+	 * 
+	 * @return ExportFile File, where the packets are written to
+	 */
+	public File getExportFile() {
+		return exportFile;
+	}
+
+	/**
+	 * Set BaseFile for the packet export
+	 * 
+	 * @param exportFile
+	 *            File, where the packets are written to
+	 */
+	public void setExportFile(File exportFile) {
+		this.exportFile = exportFile;
+	}
+
+	/**
+	 * True if each links uses a separated File: e.g. exportFile_link.log
+	 * 
+	 * @return true, if each links uses a single file for export
+	 */
+	public boolean isSplitLinkExportFiles() {
+		return splitLinkExportFiles;
+	}
+
+	/**
+	 * Set whether packets should be split into different files for each link
+	 * 
+	 * @param splitLinkExportFiles
+	 *            true if each link should export to a single file
+	 */
+	public void setSplitLinkExportFiles(boolean splitLinkExportFiles) {
+		this.splitLinkExportFiles = splitLinkExportFiles;
+	}
+}

+ 18 - 170
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimulationManager.java

@@ -1,17 +1,11 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.Collection;
 import java.util.LinkedList;
 import java.util.Observable;
 
 import javax.swing.Timer;
 
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
-import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.PacketComparator;
 
 /**
  * Manages the Simulation by calling the relevant methods.
@@ -29,32 +23,12 @@ public class SimulationManager extends Observable {
 	 */
 	private Controller controller;
 
-	/**
-	 * True if packets should be printed
-	 */
-	private boolean printPackets = false;
-
 	/**
 	 * Whether the model status changed during the last simulation and the panel
 	 * should be repainted
 	 */
 	private boolean statusChanged = false;
-
-	/**
-	 * Writer to write the packets to a file
-	 */
-	private BufferedWriter writer;
-
-	/**
-	 * Base File for the exported Packages
-	 */
-	private File exportFile = new File("testPackets.log");
-
-	/**
-	 * Split Links into differentFiles
-	 */
-	private boolean splitLinkExportFiles = false;
-
+	
 	/**
 	 * Timer which triggers each simulation step
 	 */
@@ -90,6 +64,11 @@ public class SimulationManager extends Observable {
 	 */
 	private PacketCollectionManager packetCollectionManager;
 	
+	/**
+	 * PacketExportManager which handles packet export
+	 */
+	private PacketExportManager packetExportManager;
+	
 	/**
 	 * Creates a new Simulationmanager
 	 * 
@@ -100,6 +79,7 @@ public class SimulationManager extends Observable {
 		this.model = model;
 		this.controller = null;
 		this.packetCollectionManager = new PacketCollectionManager(model);
+		this.packetExportManager = new PacketExportManager(model);
 		timer = new Timer(0, t -> simulateTimeStep());
 	}
 
@@ -154,150 +134,10 @@ public class SimulationManager extends Observable {
 		}
 		runAlgorithms(startTime);
 		packetCollectionManager.collectPackets();
-		exportPacketsOfLastTimeStep();
-	}
-
-	/**
-	 * Exports the packets which were sent during the last time step according
-	 * to the settings (whether packages should be printed, specified export
-	 * File, splitLinks setting etc.)
-	 */
-	public void exportPacketsOfLastTimeStep() {
-		if (printPackets) {
-			if (!splitLinkExportFiles) {
-				/**
-				 * Packets of all links, merged together
-				 */
-				LinkedList<Packet> packets = new LinkedList<Packet>();
-				model.getConnectionNetworks().forEach(a->packets.addAll(a.getPackets()));
-				packets.sort(new PacketComparator());
-				
-				writePacketsToFile(packets, exportFile);
-			}else{
-				for(Link link:model.getConnectionNetworks()){
-					/**
-					 * File name:
-					 * test.log
-					 * to
-					 * test_linkName.log
-					 */
-					String fileName = exportFile.getName();
-					//Add linkName
-					if(fileName.contains(".")){
-						String baseName = fileName.substring(0, fileName.lastIndexOf('.'));
-						String ending = fileName.substring(fileName.lastIndexOf('.')+1, fileName.length());
-						fileName = baseName +"_"+link.getName()+"."+ending;
-					}else{
-						fileName = fileName +"_"+link.getName();
-					}
-					/**
-					 * Path to parent directory
-					 */
-					String parent = exportFile.getParent();
-					/**
-					 * File with the fileName
-					 */
-					File linkExportFile = new File((parent == null?"":parent)+fileName);
-					/**
-					 * Write Packets
-					 */
-					writePacketsToFile(link.getPackets(), linkExportFile);
-				}
-			}
-		}
-	}
-
-	/**
-	 * Writes all packets of the given Collection to the specified file
-	 * @param packets packets which should be written
-	 * @param file file the packets should be written to
-	 */
-	private void writePacketsToFile(Collection<Packet> packets, File file){
-		if(file == null || packets == null)
-			return;
-		try {
-			//Check if file exists, if not create
-			file.createNewFile();
-			//Start Writing
-			writer = new BufferedWriter(new FileWriter(file.getAbsolutePath(), true));
-			//Write all packets
-			packets.forEach(p -> {
-				try {
-					writer.append(p.getTextualRepresentation() + "\n");
-				} catch (Exception e) {
-					System.err.println("Warning: Error on exporting packets: "+e.toString());
-				}
-			});
-		} catch (Exception e) {
-			System.err.println("Warning: Writing failed on file "+file.getPath()+"\n"+e.toString());
-		} finally {
-			//Close open writer
-			if (writer != null)
-				try {
-					writer.close();
-				} catch (IOException e) {}
-		}
-	}
-
-	/**
-	 * Returns true if the simulations will print the packets
-	 * 
-	 * @return if the packets are printed
-	 */
-	public boolean getPrintPackets() {
-		return printPackets;
+		packetExportManager.exportPacketsOfLastTimeStep();
 	}
 
-	/**
-	 * Sets Print Packets, if true, the simulation will print the packets
-	 * 
-	 * @param printPackets
-	 *            true if simulation should print the packets
-	 */
-	public void setPrintPackets(boolean printPackets) {
-		this.printPackets = printPackets;
-		notifyPanels();
-	}
-
-	/**
-	 * Get BaseFile of packet exports
-	 * 
-	 * @return ExportFile File, where the packets are written to
-	 */
-	public File getExportFile() {
-		return exportFile;
-	}
-
-	/**
-	 * Set BaseFile for the packet export
-	 * 
-	 * @param exportFile
-	 *            File, where the packets are written to
-	 */
-	public void setExportFile(File exportFile) {
-		this.exportFile = exportFile;
-		notifyPanels();
-	}
-
-	/**
-	 * True if each links uses a separated File: e.g. exportFile_link.log
-	 * 
-	 * @return true, if each links uses a single file for export
-	 */
-	public boolean isSplitLinkExportFiles() {
-		return splitLinkExportFiles;
-	}
-
-	/**
-	 * Set whether packets should be split into different files for each link
-	 * 
-	 * @param splitLinkExportFiles
-	 *            true if each link should export to a single file
-	 */
-	public void setSplitLinkExportFiles(boolean splitLinkExportFiles) {
-		this.splitLinkExportFiles = splitLinkExportFiles;
-		notifyPanels();
-	}
+	
 
 	/**
 	 * Start the simulation
@@ -424,7 +264,7 @@ public class SimulationManager extends Observable {
 	/**
 	 * Notify the panels, which could update their GUI
 	 */
-	private void notifyPanels() {
+	public void notifyPanels() {
 		this.setChanged();
 		this.notifyObservers();
 	}
@@ -473,4 +313,12 @@ public class SimulationManager extends Observable {
 	public PacketCollectionManager getPacketCollectionManager(){
 		return packetCollectionManager;
 	}
+	
+	/**
+	 * Returns the PacketExportManager
+	 * @return the packetExportManager
+	 */
+	public PacketExportManager getPacketExportManager(){
+		return packetExportManager;
+	}
 }