SimulationController.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.control;
  2. import java.io.File;
  3. import java.util.LinkedList;
  4. import java.util.Observer;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.NetworkManipulationAlgorithm;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SimulationManager;
  8. /**
  9. * Controller which enables configuration and manipulation of the simulation
  10. *
  11. *
  12. * @author Andreas T. Meyer-Berg
  13. */
  14. public class SimulationController {
  15. /**
  16. * Main Controller which can be used internally
  17. */
  18. @SuppressWarnings("unused")
  19. private Controller controller;
  20. /**
  21. * Model which can be manipulated
  22. */
  23. private Model model;
  24. /**
  25. * Simulation Manager
  26. */
  27. private SimulationManager sim;
  28. /**
  29. * Controller for managing the packet capture
  30. */
  31. private PacketCaptureController captureController;
  32. /**
  33. * Creates a new Simulation
  34. * @param model Model to simulate
  35. * @param controller Main Controller which should be used for updates etc.
  36. */
  37. public SimulationController(Model model, Controller controller) {
  38. this.model = model;
  39. this.controller = controller;
  40. this.captureController = new PacketCaptureController(model, controller);
  41. this.sim = model.getSim();
  42. }
  43. /**
  44. * Returns the Simulation Manager of the Program
  45. *
  46. * @return Simulation Manager
  47. */
  48. public SimulationManager getSimulationManager() {
  49. return model.getSim();
  50. }
  51. /**
  52. * Returns true if the simulations will print the packets
  53. *
  54. * @return if the packets are printed
  55. */
  56. public boolean getPrintPackets() {
  57. return sim.getPacketExportManager().getPrintPackets();
  58. }
  59. /**
  60. * Sets Print Packets, if true, the simulation will print the packets
  61. * @param printPackets true if simulation should print the packets
  62. */
  63. public void setPrintPackets(boolean printPackets) {
  64. sim.getPacketExportManager().setPrintPackets(printPackets);
  65. notifyPanels();
  66. }
  67. /**
  68. * Get BaseFile of packet exports
  69. *
  70. * @return ExportFile File, where the packets are written to
  71. */
  72. public File getExportFile() {
  73. return sim.getPacketExportManager().getExportFile();
  74. }
  75. /**
  76. * Set BaseFile for the packet export
  77. *
  78. * @param exportFile File, where the packets are written to
  79. */
  80. public void setExportFile(File exportFile) {
  81. sim.getPacketExportManager().setExportFile(exportFile);
  82. notifyPanels();
  83. }
  84. /**
  85. * True if each links uses a separated File: e.g. exportFile_link.log
  86. * @return true, if each links uses a single file for export
  87. */
  88. public boolean isSplitLinkExportFiles() {
  89. return sim.getPacketExportManager().isSplitLinkExportFiles();
  90. }
  91. /**
  92. * Set whether packets should be split into different files for each link
  93. * @param splitLinkExportFiles true if each link should export to a single file
  94. */
  95. public void setSplitLinkExportFiles(boolean splitLinkExportFiles) {
  96. sim.getPacketExportManager().setSplitLinkExportFiles(splitLinkExportFiles);
  97. notifyPanels();
  98. }
  99. /**
  100. * Start the simulation
  101. */
  102. public void startSimulation(){
  103. sim.startSimulation();
  104. }
  105. /**
  106. * Stop the simulation
  107. */
  108. public void stopSimulation(){
  109. sim.stopSimulation();
  110. }
  111. /**
  112. * Resets the simulation to the startTime
  113. */
  114. public void resetSimulation(){
  115. sim.resetSimulation();
  116. }
  117. /**
  118. * Returns true if the simulation is running, false if not
  119. * @return true if running
  120. */
  121. public boolean isRunning(){
  122. return sim.isRunning();
  123. }
  124. /**
  125. * Returns the StartTime of the simulation
  126. * @return startTime
  127. */
  128. public long getStartTime(){
  129. return sim.getStartTime();
  130. }
  131. /**
  132. * Sets the new startTime
  133. * @param startTime time the simulations starts
  134. */
  135. public void setStartTime(long startTime){
  136. sim.setStartTime(startTime);
  137. }
  138. /**
  139. * Returns the end time of the simulation
  140. * @return End time of the simulation
  141. */
  142. public long getEndTime(){
  143. return sim.getEndTime();
  144. }
  145. /**
  146. * Sets the new startTime
  147. * @param endTime new EndTime
  148. */
  149. public void setEndTime(long endTime){
  150. sim.setEndTime(endTime);;
  151. }
  152. /**
  153. * @return the currentTime
  154. */
  155. public long getCurrentTime() {
  156. return sim.getCurrentTime();
  157. }
  158. /**
  159. * @param currentTime the currentTime to set
  160. */
  161. public void setCurrentTime(long currentTime) {
  162. sim.setCurrentTime(currentTime);
  163. }
  164. /**
  165. * Returns the simulation step duration in milliseconds
  166. * @return duration of each simulation step in milliseconds
  167. */
  168. public long getStepDuration(){
  169. return sim.getStepDuration();
  170. }
  171. /**
  172. * Sets the duration of simulation steps
  173. * @param duration duration in milliseconds of a step
  174. */
  175. public void setStepDuration(long duration){
  176. sim.setStepDuration(duration);
  177. }
  178. /**
  179. * Adds observer, which will receive an notification on each time step
  180. * @param o Observer, which should be notified
  181. */
  182. public void addObserver(Observer o){
  183. sim.addObserver(o);
  184. }
  185. /**
  186. * Removes Observer from the simulation, will no longer be notified on step change
  187. * @param o Observer to be removed
  188. */
  189. public void removeObserver(Observer o){
  190. sim.deleteObserver(o);
  191. }
  192. /**
  193. * Runs all registered algorithms at the currentTimeStep
  194. * @param time currentTime of the simulation
  195. */
  196. public void runAlgorithms(long time) {
  197. sim.runAlgorithms(time);
  198. }
  199. /**
  200. * Returns all registered algorithms of the simulation
  201. * @return all registered algorithms
  202. */
  203. public LinkedList<NetworkManipulationAlgorithm> getAlgorithms(){
  204. return sim.getAlgorithms();
  205. }
  206. /**
  207. * Adds an algorithm, which should be executed each timestep
  208. * @param algo new algorithm
  209. */
  210. public void addAlgorithm(NetworkManipulationAlgorithm algo, Controller controller){
  211. sim.addAlgorithm(algo, controller);
  212. }
  213. /**
  214. * Removes algorithms from the simulation
  215. * @param algo algorithm to be removed
  216. */
  217. public void removeAlgo(NetworkManipulationAlgorithm algo){
  218. sim.removeAlgo(algo);
  219. }
  220. /**
  221. * Notify the panels, which could update their GUI
  222. */
  223. public void notifyPanels(){
  224. sim.notifyPanels();
  225. }
  226. /**
  227. * Returns the PacketCaptureController of the framework
  228. * @return PacketCaptureController
  229. */
  230. public PacketCaptureController getPacketCaptureController(){
  231. return captureController;
  232. }
  233. }