|
@@ -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(){
|
|
|
-
|
|
|
- if(endTime <= currentTime)
|
|
|
+ private void simulateTimeStep() {
|
|
|
+
|
|
|
+ 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) {
|
|
|
-
|
|
|
+
|
|
|
statusChanged = false;
|
|
|
-
|
|
|
+
|
|
|
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;
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
model.getDevices().forEach(d -> d.simulateTimeStep(startTime, duration));
|
|
|
-
|
|
|
- if(statusChanged){
|
|
|
+
|
|
|
+ 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) {
|
|
|
-
|
|
|
- }
|
|
|
- }));
|
|
|
- } catch (Exception e) {
|
|
|
-
|
|
|
- } 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{
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
* 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();
|
|
|
}
|