PacketCollector.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.function.Predicate;
  6. /**
  7. * A packet collector allows collection of {@link Packet}s from one or multiple {@link Link}s or {@link SmartDevice}s.<br>
  8. * The Packets are stored per Link.<br>
  9. * All packets which are sent from/to one of the devices can be filtered by the
  10. * {@link #getFilter() getFilter()}.<br>
  11. * Packets should be collected by adding them via the
  12. * {@link #addPackets(Link, Collection)} method.<br>
  13. *
  14. * @author Andreas T. Meyer-Berg
  15. */
  16. public class PacketCollector {
  17. /**
  18. * Devices which packets should be collected. Just devices from/to the
  19. * device will be collected.
  20. */
  21. private LinkedList<SmartDevice> devices = new LinkedList<SmartDevice>();
  22. /**
  23. * All packets which are sent via this link are collected.
  24. */
  25. private LinkedList<Link> links = new LinkedList<Link>();
  26. /**
  27. * Packets which were collected by this packet collector
  28. */
  29. private HashMap<Link,LinkedList<Packet>> collectedPackets = new HashMap<Link, LinkedList<Packet>>();
  30. /**
  31. * Packet Sniffing Algorithm, which
  32. */
  33. private PacketSniffer packetAlgorithm = null;
  34. /**
  35. * Mode of the Collector, whether it should train or test the packetSniffer algorithm
  36. */
  37. private boolean mode = false;
  38. /**
  39. * True if it should collect packages
  40. */
  41. private boolean active = true;
  42. /**
  43. * Creates a PacketCollector without assigning and algorithm to process the packets
  44. */
  45. public PacketCollector() {
  46. packetAlgorithm = null;
  47. }
  48. /**
  49. * Creates a PacketCollector and assigns an algorithm to process the packets
  50. * @param sniffer PacketSniffer to process the packets collected by this collector
  51. */
  52. public PacketCollector(PacketSniffer sniffer) {
  53. packetAlgorithm = sniffer;
  54. }
  55. /**
  56. * Adds a new Link, which packets should be collected. All packets send via
  57. * the link will be collected from now on.
  58. *
  59. * @param link
  60. * link, which packets should be collected.
  61. */
  62. public void addLink(Link link) {
  63. if(!links.contains(link)){
  64. links.add(link);
  65. collectedPackets.put(link, new LinkedList<Packet>());
  66. }
  67. }
  68. /**
  69. * Remove link from Links that should be collected. Packets sent via this
  70. * Link will no longer be collected. Just if a separate Device which
  71. * contains the link should be collected.
  72. *
  73. * @param link link which packets should no longer be collected
  74. */
  75. public void removeLink(Link link) {
  76. links.remove(link);
  77. collectedPackets.remove(link).clear();
  78. }
  79. /**
  80. * Returns all links, which should be collected by this PacketCollector
  81. * @return links, which packets should be collected
  82. */
  83. public Collection<Link> getLinks() {
  84. return links;
  85. }
  86. /**
  87. * Adds device, which packets should be collected. All packets sent to or received from this device will be collected.
  88. * @param device device, which packets should be collected
  89. */
  90. public void addDevice(SmartDevice device) {
  91. if(!devices.contains(device))
  92. devices.add(device);
  93. }
  94. /**
  95. * Remove device. Packets sent to or from this device will no longer be collected. Unless it participates in one of the links.
  96. * @param device device to be removed
  97. */
  98. public void removeDevice(SmartDevice device) {
  99. devices.remove(device);
  100. }
  101. /**
  102. * Returns all devices which packets are being collected by this Packet Collector.
  103. * @return devices, which packets are being collected
  104. */
  105. public Collection<SmartDevice> getDevices() {
  106. return devices;
  107. }
  108. /**
  109. * Predicate which is true if the given packet was sent from or to one of
  110. * the devices, stored in this PacketCollector
  111. *
  112. * @return true if it was sent from or to one of the devices stored in this
  113. * packet collector
  114. */
  115. public Predicate<? super Packet> getFilter() {
  116. return p -> /* filter devices where source or destination is null */
  117. (p.getSource() != null && p.getDestination() != null && p.getSource().getOwner() != null
  118. && p.getDestination().getOwner() != null)
  119. /* return true if */
  120. /*&& (devices.contains(p.getSource().getOwner())||devices.contains(p.getDestination().getOwner()))*/
  121. && devices.stream().anyMatch(d -> d == p.getSource().getOwner() || d == p.getDestination().getOwner());
  122. }
  123. /**
  124. * Adds packets which should be collected by this PacketCollector.
  125. * @param link link, which the packets were sent on
  126. * @param packets packets which were sent
  127. */
  128. public void addPackets(Link link, Collection<Packet> packets) {
  129. if(link == null)
  130. return;
  131. LinkedList<Packet> packetsOfLink = collectedPackets.get(link);
  132. if(packetsOfLink !=null)
  133. packetsOfLink.addAll(packets);
  134. collectedPackets.put(link, new LinkedList<>(packets));
  135. }
  136. /**
  137. * Returns the packets which were collected
  138. * @return collected packets
  139. */
  140. public HashMap<Link,LinkedList<Packet>> getPackets(){
  141. return collectedPackets;
  142. }
  143. /**
  144. * Return the collected Packets of the link
  145. * @param link Link, which packets should be returned
  146. * @return packets, collected on the given link
  147. */
  148. public LinkedList<Packet> getPacketsOfLink(Link link){
  149. return collectedPackets.get(link);
  150. }
  151. /**
  152. * Resets the collected Packets list, by clearing the list.
  153. */
  154. public void resetPackets(){
  155. collectedPackets.clear();
  156. }
  157. /**
  158. * @return the packetSnifferAlgorithm
  159. */
  160. public PacketSniffer getPacketAlgorithm() {
  161. return packetAlgorithm;
  162. }
  163. /**
  164. * @param packetAlgorithm the packetSnifferAlgorithm to set
  165. */
  166. public void setPacketAlgorithm(PacketSniffer packetAlgorithm) {
  167. this.packetAlgorithm = packetAlgorithm;
  168. }
  169. /**
  170. * Returns training or testing mode of the algorithm.
  171. * @return true if testing or false if training the algorithm.
  172. */
  173. public boolean getMode() {
  174. if(packetAlgorithm!=null)
  175. mode = packetAlgorithm.getMode();
  176. return mode;
  177. }
  178. /**
  179. * Sets the training or testing mode
  180. * @param mode true if testing or false if training the algorithm.
  181. */
  182. public void setMode(boolean mode) {
  183. this.mode = mode;
  184. if(packetAlgorithm != null)
  185. packetAlgorithm.setMode(mode);
  186. }
  187. /**
  188. * Whether the algorithm should run
  189. * @return true if algorithm will be executed
  190. */
  191. public boolean isActive() {
  192. return active;
  193. }
  194. /**
  195. * Set to true, if it should run
  196. * @param active true if it should be active
  197. */
  198. public void setActive(boolean active) {
  199. this.active = active;
  200. }
  201. }