Browse Source

Adds splitLink option to configuration panel and splitLink=false case

Andreas T. Meyer-Berg 5 years ago
parent
commit
e94d8a278f

+ 114 - 88
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimulationManager.java

@@ -4,51 +4,55 @@ import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.util.LinkedList;
 import java.util.Observable;
 
 import javax.swing.Timer;
 
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.PacketComparator;
+
 /**
  * Manages the Simulation by calling the relevant methods.
  *
  * @author Andreas T. Meyer-Berg
  */
-public class SimulationManager extends Observable{
+public class SimulationManager extends Observable {
 	/**
 	 * Model which should be simulated
 	 */
 	private Model model;
-	
+
 	/**
 	 * 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
+	 * 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
 	 */
 	private Timer timer;
-	
+
 	/**
 	 * First Timestep of the simulation
 	 */
@@ -65,34 +69,35 @@ public class SimulationManager extends Observable{
 	 * Duration of the simulation
 	 */
 	private long duration = 100L;
-	
+
 	/**
 	 * Creates a new Simulationmanager
 	 * 
-	 * @param model Model that should be simulated
+	 * @param model
+	 *            Model that should be simulated
 	 */
 	public SimulationManager(Model model) {
 		this.model = model;
 		timer = new Timer(0, t -> simulateTimeStep());
 	}
-	
+
 	/**
 	 * Simulate the nexte Timestep
 	 */
-	private void simulateTimeStep(){
-		//Just simulate if timer is running
-		if(endTime <= currentTime)
+	private void simulateTimeStep() {
+		// Just simulate if timer is running
+		if (endTime <= currentTime)
 			stopSimulation();
-		if(currentTime + duration <= endTime){
+		if (currentTime + duration <= endTime) {
 			simulateTimeIntervall(currentTime, duration);
 			currentTime += duration;
-		}else{
-			simulateTimeIntervall(currentTime,endTime-currentTime);
+		} else {
+			simulateTimeIntervall(currentTime, endTime - currentTime);
 			currentTime = endTime;
 		}
 		notifyPanels();
 	}
-	
+
 	/**
 	 * Simulates one time step at a given time fur a given duration
 	 * 
@@ -103,24 +108,24 @@ public class SimulationManager extends Observable{
 	 *            Duration of the simulation interval in milliseconds
 	 */
 	public void simulateTimeIntervall(long startTime, long duration) {
-		//Nothing changed so far
+		// Nothing changed so far
 		statusChanged = false;
-		//Simulate all Links, and their connections
+		// Simulate all Links, and their connections
 		model.getConnectionNetworks().forEach(d -> {
 			d.simulateTimeInterval(startTime, duration);
-			if(d.getStatusChanged()){
-				for(Connection c:d.getConnections()){
-					if(c.getStatus()==Connection.DONE){
+			if (d.getStatusChanged()) {
+				for (Connection c : d.getConnections()) {
+					if (c.getStatus() == Connection.DONE) {
 						model.getConnections().remove(c);
 					}
 				}
 				statusChanged = true;
 			}
 		});
-		//Simulate SmartDevices - if they need some logic
+		// Simulate SmartDevices - if they need some logic
 		model.getDevices().forEach(d -> d.simulateTimeStep(startTime, duration));
-		//Store Packages/Export Packages etc. (for debug purposes)
-		if(statusChanged){
+		// Store Packages/Export Packages etc. (for debug purposes)
+		if (statusChanged) {
 			model.setChanged();
 			model.notifyObservers();
 		}
@@ -128,37 +133,42 @@ public class SimulationManager extends Observable{
 	}
 
 	/**
-	 * 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.)
+	 * 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){
-			try {
-				exportFile.createNewFile();
-				writer = new BufferedWriter(new FileWriter(exportFile.getAbsolutePath(),true));
-				model.getConnectionNetworks().forEach(d->d.getPackets()
-						.forEach(p-> 
-						{
-							try {
-								if(p == null)
-									writer.append("Packet: Null\n");
-								else 
-									writer.append(p.getTextualRepresentation()+"\n");								
-							} catch (Exception e) {
-								// TODO: handle exception
-							}
-						}));				
-			} catch (Exception e) {
-				// TODO: handle exception
-			} finally {
-				if(writer != null)
-					try {
-						writer.close();
-					} catch (IOException e) {}				
+		if (printPackets) {
+			if (!splitLinkExportFiles) {
+				try {
+					exportFile.createNewFile();
+					writer = new BufferedWriter(new FileWriter(exportFile.getAbsolutePath(), true));
+					
+					LinkedList<Packet> packets = new LinkedList<Packet>();
+					packets.sort(new PacketComparator());
+					model.getConnectionNetworks().forEach(a->packets.addAll(a.getPackets()));
+					packets.forEach(p -> {
+						try {
+							if (p == null)
+								writer.append("Packet: Null\n");
+							else
+								writer.append(p.getTextualRepresentation() + "\n");
+						} catch (Exception e) {}
+					});
+				} catch (Exception e) {
+					
+				} finally {
+					if (writer != null)
+						try {
+							writer.close();
+						} catch (IOException e) {}
+				}
+			}else{
+				//TODO: Different Link Files
 			}
 		}
 	}
-	
+
 	/**
 	 * Returns true if the simulations will print the packets
 	 * 
@@ -170,7 +180,9 @@ public class SimulationManager extends Observable{
 
 	/**
 	 * Sets Print Packets, if true, the simulation will print the packets
-	 * @param printPackets true if simulation should print the packets
+	 * 
+	 * @param printPackets
+	 *            true if simulation should print the packets
 	 */
 	public void setPrintPackets(boolean printPackets) {
 		this.printPackets = printPackets;
@@ -185,11 +197,12 @@ public class SimulationManager extends Observable{
 	public File getExportFile() {
 		return exportFile;
 	}
-	
+
 	/**
 	 * Set BaseFile for the packet export
 	 * 
-	 * @param exportFile File, where the packets are written to
+	 * @param exportFile
+	 *            File, where the packets are written to
 	 */
 	public void setExportFile(File exportFile) {
 		this.exportFile = exportFile;
@@ -198,6 +211,7 @@ public class SimulationManager extends Observable{
 
 	/**
 	 * 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() {
@@ -206,94 +220,102 @@ public class SimulationManager extends Observable{
 
 	/**
 	 * Set whether packets should be split into different files for each link
-	 * @param splitLinkExportFiles true if each link should export to a single file
+	 * 
+	 * @param splitLinkExportFiles
+	 *            true if each link should export to a single file
 	 */
 	public void setSplitLinkExportFiles(boolean splitLinkExportFiles) {
 		this.splitLinkExportFiles = splitLinkExportFiles;
 		notifyPanels();
 	}
-	
+
 	/**
 	 * Start the simulation
 	 */
-	public void startSimulation(){
+	public void startSimulation() {
 		timer.start();
 		notifyPanels();
 	}
-	
+
 	/**
 	 * Stop the simulation
 	 */
-	public void stopSimulation(){
+	public void stopSimulation() {
 		timer.stop();
 		notifyPanels();
 	}
-	
+
 	/**
 	 * Resets the Simulation to the start time
 	 */
-	public void resetSimulation(){
+	public void resetSimulation() {
 		timer.stop();
 		timer = new Timer(0, a -> simulateTimeStep());
 		currentTime = startTime;
 		resetSimulation(currentTime);
 		notifyPanels();
 	}
-	
+
 	/**
-	 * Reset Simulation 
+	 * Reset Simulation
 	 * 
 	 * @param timestep
 	 */
-	public void resetSimulation(long timestep){
-		for(SmartDevice d: model.getDevices())
-			for(Port p:d.getPorts())
-				if(p.getLastTrigger()>timestep)
+	public void resetSimulation(long timestep) {
+		for (SmartDevice d : model.getDevices())
+			for (Port p : d.getPorts())
+				if (p.getLastTrigger() > timestep)
 					p.setLastTrigger(timestep);
 	}
-	
+
 	/**
 	 * Returns true if the simulation is running, false if not
+	 * 
 	 * @return
 	 */
-	public boolean isRunning(){
+	public boolean isRunning() {
 		return timer.isRunning();
 	}
-	
+
 	/**
 	 * Returns the StartTime of the simulation
+	 * 
 	 * @return startTime
 	 */
-	public long getStartTime(){
+	public long getStartTime() {
 		return startTime;
 	}
-	
+
 	/**
 	 * Sets the new startTime
-	 * @param startTime time the simulations starts
+	 * 
+	 * @param startTime
+	 *            time the simulations starts
 	 */
-	public void setStartTime(long startTime){
+	public void setStartTime(long startTime) {
 		this.startTime = startTime;
 		notifyPanels();
 	}
-	
+
 	/**
 	 * Returns the end time of the simulation
+	 * 
 	 * @return End time of the simulation
 	 */
-	public long getEndTime(){
+	public long getEndTime() {
 		return endTime;
 	}
-	
+
 	/**
 	 * Sets the new startTime
+	 * 
 	 * @return true on success
 	 */
-	public void setEndTime(long endTime){
+	public void setEndTime(long endTime) {
 		this.endTime = endTime;
 		notifyPanels();
 	}
-	
+
 	/**
 	 * @return the currentTime
 	 */
@@ -302,7 +324,8 @@ public class SimulationManager extends Observable{
 	}
 
 	/**
-	 * @param currentTime the currentTime to set
+	 * @param currentTime
+	 *            the currentTime to set
 	 */
 	public void setCurrentTime(long currentTime) {
 		this.currentTime = currentTime;
@@ -311,25 +334,28 @@ public class SimulationManager extends Observable{
 
 	/**
 	 * Returns the simulation step duration in milliseconds
+	 * 
 	 * @return duration of each simulation step in milliseconds
 	 */
-	public long getStepDuration(){
+	public long getStepDuration() {
 		return duration;
 	}
-	
+
 	/**
 	 * Sets the duration of simulation steps
-	 * @param duration duration in milliseconds of a step
+	 * 
+	 * @param duration
+	 *            duration in milliseconds of a step
 	 */
-	public void setStepDuration(long duration){
+	public void setStepDuration(long duration) {
 		this.duration = duration;
 		notifyPanels();
 	}
-	
+
 	/**
 	 * Notify the panels, which could update their GUI
 	 */
-	private void notifyPanels(){
+	private void notifyPanels() {
 		this.setChanged();
 		this.notifyObservers();
 	}

+ 15 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/SimulationConfigurator.java

@@ -48,6 +48,10 @@ public class SimulationConfigurator extends JFrame implements Observer{
 	 * Checkbox if packets should be printed
 	 */
 	private JCheckBox chckbxPrintpackets;
+	/**
+	 * Checkbox if packets should be split into different Files for each link
+	 */
+	private JCheckBox chckbxSplitLinks;
 	/**
 	 * Label which shows the name of the export File
 	 */
@@ -177,12 +181,18 @@ public class SimulationConfigurator extends JFrame implements Observer{
 		chckbxPrintpackets
 		.setToolTipText("Print Packets as human readable String to testPackets.log in the projekt folder.");
 		
+		chckbxSplitLinks = new JCheckBox("Split Links");
+		chckbxSplitLinks.setBounds(130, 130, 100, 20);
+		getContentPane().add(chckbxSplitLinks);
+		chckbxSplitLinks
+		.setToolTipText("Export packets into a different file for each Link.");
+		
 		btnExportFile = new JButton("Choose Export File");
-		btnExportFile.setBounds(140, 130, 180, 20);
+		btnExportFile.setBounds(230, 130, 180, 20);
 		getContentPane().add(btnExportFile);
 		
 		lblExportFileName = new JLabel();
-		lblExportFileName.setBounds(330, 130, 300, 20);
+		lblExportFileName.setBounds(430, 130, 300, 20);
 		getContentPane().add(lblExportFileName);
 		
 		JSeparator botSeparator = new JSeparator(JSeparator.HORIZONTAL);
@@ -252,6 +262,7 @@ public class SimulationConfigurator extends JFrame implements Observer{
 			tfStepDuration.setBackground(Color.WHITE);
 		}
 		chckbxPrintpackets.setSelected(sim.getPrintPackets());
+		chckbxSplitLinks.setSelected(sim.isSplitLinkExportFiles());
 		lblExportFileName.setText(sim.getExportFile().getName());
 		lblExportFileName.setToolTipText("Path: "+sim.getExportFile().getPath());
 		lblCurrentTimeShow.setText(currentTime+" ms");
@@ -394,6 +405,8 @@ public class SimulationConfigurator extends JFrame implements Observer{
 		
 		chckbxPrintpackets.addActionListener(l -> sim.setPrintPackets(chckbxPrintpackets.isSelected()));
 		
+		chckbxSplitLinks.addActionListener(l -> sim.setSplitLinkExportFiles(chckbxSplitLinks.isSelected()));
+		
 		/**
 		 * Export location:
 		 */