Browse Source

Export packets to SmartHomeNetworkSim/testPackets.log instead of console

Andreas T. Meyer-Berg 6 năm trước cách đây
mục cha
commit
84a3d5dbcb

+ 34 - 10
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimulationManager.java

@@ -1,5 +1,10 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
 /**
  * Manages the Simulation by calling the relevant methods.
  *
@@ -14,6 +19,7 @@ public class SimulationManager {
 	 * True if packets should be printed
 	 */
 	boolean printPackets = false;
+	BufferedWriter writer;
 	/**
 	 * Creates a new Simulationmanager
 	 * 
@@ -32,21 +38,39 @@ public class SimulationManager {
 	 * @param duration
 	 *            Duration of the simulation interval in milliseconds
 	 */
-	public void simulateTimeIntervall(long startTime, long duration){
+	public void simulateTimeIntervall(long startTime, long duration) {
 		//Simulate all Links, and their connections
 		model.getConnectionNetworks().forEach(d -> d.simulateTimeInterval(startTime, duration));
 		//Simulate SmartDevices - if they need some logic
 		model.getDevices().forEach(d -> d.simulateTimeStep(startTime, duration));
 		//Store Packages/Export Packages etc. (for debug purposes)
-		if(printPackets)
-			model.getConnectionNetworks().forEach(d->d.getPackets()
-				.forEach(p-> 
-				{
-					if(p == null)
-						System.out.println("Packet: Null");
-					else 
-						System.out.println(p.getTextualRepresentation());
-				}));
+		
+		if(printPackets){
+			try {
+				File f = new File("testPackets.log");
+				f.createNewFile();
+				writer = new BufferedWriter(new FileWriter(f.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) {}				
+			}
+		}
 		
 		
 	}

+ 1 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/SimulationConfigurator.java

@@ -99,6 +99,7 @@ public class SimulationConfigurator extends JFrame {
 		chckbxPrintpackets.setSelected(sim.getPrintPackets());
 		getContentPane().add(chckbxPrintpackets);
 		chckbxPrintpackets.addActionListener(l->sim.setPrintPackets(chckbxPrintpackets.isSelected()));
+		chckbxPrintpackets.setToolTipText("Print Packets as human readable String to testPackets.log in the projekt folder.");
 	}
 	
 	private void startStopButtonAction(){