PacketCaptureController.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.control;
  2. import java.util.LinkedList;
  3. import java.util.Observer;
  4. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollectionManager;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketCollector;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketSniffer;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  10. /**
  11. * Controller which controls access to the {@link PacketCollector} and {@link PacketSniffer}
  12. *
  13. * @author Andreas T. Meyer-Berg
  14. */
  15. public class PacketCaptureController {
  16. /**
  17. * Main Controller
  18. */
  19. @SuppressWarnings("unused")
  20. private Controller controller;
  21. /**
  22. * Main model
  23. */
  24. private Model model;
  25. /**
  26. * PacketCollectionManager for access to the
  27. */
  28. private PacketCollectionManager packetCollectionManager;
  29. /**
  30. * Creates a new PacketCaptureController
  31. * @param controller Main Controller
  32. */
  33. public PacketCaptureController(Model model, Controller controller){
  34. this.model = model;
  35. this.controller = controller;
  36. packetCollectionManager =this. model.getSim().getPacketCollectionManager();
  37. }
  38. /**
  39. * Adds a new PacketController for the given PacketSniffingAlgorithm
  40. * @param packetSniffer Packet Sniffer which can process the collected packets
  41. * @return the created PacketCollector with PacketSniffer
  42. */
  43. public PacketCollector addPacketCapturer(PacketSniffer packetSniffer){
  44. PacketCollector collector = new PacketCollector(packetSniffer);
  45. packetCollectionManager.addPacketCollector(collector);
  46. notifyObservers();
  47. return collector;
  48. }
  49. /**
  50. * Returns the PacketCollector, which contains the given PacketSniffer.
  51. * Returns null if no collector exists.
  52. * @param packetSniffer PacketSniffer which PacketCollector should be returned
  53. * @return PacketCollector, which contains the PacketSniffer, null if non was found
  54. */
  55. public PacketCollector getPacketCapturer(PacketSniffer packetSniffer){
  56. if(packetSniffer == null)
  57. return null;
  58. for(PacketCollector collector:packetCollectionManager.getPacketCollectors()){
  59. if(packetSniffer.equals(collector.getPacketAlgorithm()))
  60. return collector;
  61. }
  62. return null;
  63. }
  64. /**
  65. * Remove packetCollector of the given packetSniffer
  66. * @param packetSniffer PacketSniffer to be removed
  67. */
  68. public void removePacketCapturer(PacketSniffer packetSniffer){
  69. if(packetSniffer == null)
  70. return;
  71. PacketCollector packetCollector = null;
  72. for(PacketCollector collector:packetCollectionManager.getPacketCollectors()){
  73. if(packetSniffer.equals(collector.getPacketAlgorithm())){
  74. packetCollector = collector;
  75. break;
  76. }
  77. }
  78. if(packetCollector!=null){
  79. removePacketCollector(packetCollector);
  80. notifyObservers();
  81. }
  82. }
  83. /**
  84. * Returns all registered PacketCollectos
  85. * @return
  86. */
  87. public LinkedList<PacketCollector> getPacketCollectors(){
  88. return packetCollectionManager.getPacketCollectors();
  89. }
  90. /**
  91. * Add a packetCollector to the simulation
  92. * @param collector packetCollector which should collect packets
  93. */
  94. public void addPacketCollector(PacketCollector collector){
  95. packetCollectionManager.addPacketCollector(collector);
  96. notifyObservers();
  97. }
  98. /**
  99. * Removes the given packetCollector from the active Collectors
  100. * @param collector collector to be removed
  101. */
  102. public void removePacketCollector(PacketCollector collector){
  103. packetCollectionManager.removePacketCollector(collector);
  104. notifyObservers();
  105. }
  106. /**
  107. * Adds a link to a collector, it will collect all packets send via this link
  108. * @param collector Collector which should collect packet of the link
  109. * @param link Link which should be captured
  110. */
  111. public void addLinkToCollector(PacketCollector collector, Link link){
  112. if(collector!=null && link != null){
  113. collector.addLink(link);
  114. notifyObservers();
  115. }
  116. }
  117. /**
  118. * Removes a link from a collector, it will no longer collect all packets send via this link
  119. * @param collector Collector which should no longer collect the packets of the link
  120. * @param link Link which should no longer be captured
  121. */
  122. public void removeLinkFromCollector(PacketCollector collector, Link link){
  123. if(collector!=null && link != null){
  124. collector.removeLink(link);
  125. notifyObservers();
  126. }
  127. }
  128. /**
  129. * Adds a SmartDevice to a collector, it will collect all packets send via this SmartDevice
  130. * @param collector Collector which should collect packets of the Device
  131. * @param device Device which packets should be captured
  132. */
  133. public void addDeviceToCollector(PacketCollector collector, SmartDevice device){
  134. if(collector!=null && device != null){
  135. collector.addDevice(device);
  136. notifyObservers();
  137. }
  138. }
  139. /**
  140. * Removes a link from a collector, it will no longer collect all packets send via this SmartDevice
  141. * @param collector Collector which should no longer collect the packets of the Device
  142. * @param device SmartDevice which should no longer be captured
  143. */
  144. public void removeDeviceFromCollector(PacketCollector collector, SmartDevice device){
  145. if(collector!=null && device != null){
  146. collector.removeDevice(device);
  147. notifyObservers();
  148. }
  149. }
  150. /**
  151. * Returns training or testing mode of the algorithm.
  152. * @param collector Collector which is used
  153. * @return true if testing or false if training the algorithm.
  154. */
  155. public boolean getMode(PacketCollector collector) {
  156. if(collector == null)
  157. return false;
  158. return collector.getMode();
  159. }
  160. /**
  161. * Sets the training or testing mode
  162. * @param collector Collector which is used
  163. * @param mode true if testing or false if training the algorithm.
  164. */
  165. public void setMode(PacketCollector collector, boolean mode) {
  166. if(collector == null) return;
  167. collector.setMode(mode);
  168. notifyObservers();
  169. }
  170. /**
  171. * Whether the algorithm should run
  172. * @param collector Collector which is used
  173. * @return true if algorithm will be executed
  174. */
  175. public boolean isActive(PacketCollector collector) {
  176. if(collector == null)
  177. return false;
  178. return collector.isActive();
  179. }
  180. /**
  181. * Set to true, if it should run
  182. * @param collector Collector which is used
  183. * @param active true if it should be active
  184. */
  185. public void setActive(PacketCollector collector, boolean active) {
  186. if(collector == null)return;
  187. collector.setActive(active);
  188. notifyObservers();
  189. }
  190. /**
  191. * Notify all observers of the packet collection managers
  192. */
  193. public void notifyObservers(){
  194. packetCollectionManager.notifyObservers();
  195. }
  196. /**
  197. * Add Observer, will be notified on updates
  198. * @param o Observer which should be notified
  199. */
  200. public void addObserver(Observer o){
  201. packetCollectionManager.addObserver(o);
  202. }
  203. /**
  204. * Remove Observer, which will no longer be notified
  205. * @param o Observer to be removed
  206. */
  207. public void removeObserver(Observer o){
  208. packetCollectionManager.deleteObserver(o);
  209. }
  210. }