PacketCollectionManager.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
  2. import java.util.Collection;
  3. import java.util.LinkedList;
  4. import java.util.Observable;
  5. import java.util.stream.Collectors;
  6. /**
  7. * The PacketCollectionManager, which stores all the active {@link PacketCollector} and adds their packets after each {@link SimulationManager#simulateTimeIntervall(long, long)} step.
  8. *
  9. *
  10. * @author Andreas T. Meyer-Berg
  11. */
  12. public class PacketCollectionManager extends Observable {
  13. /**
  14. * All collectors registered in the framework
  15. */
  16. private LinkedList<PacketCollector> collectors = new LinkedList<PacketCollector>();
  17. /**
  18. * Model used by the framework
  19. */
  20. private Model model;
  21. /**
  22. * Initializes a PacketCollectionManager for the framework
  23. */
  24. public PacketCollectionManager(Model model) {
  25. this.model = model;
  26. }
  27. /**
  28. * Let all packet collectors collect their packets
  29. */
  30. public void collectPackets(){
  31. /**
  32. * Let all collectors collect
  33. */
  34. for(PacketCollector col: collectors){
  35. col.resetPackets();
  36. if(!col.isActive())continue;
  37. /**
  38. * Add all links, which packets should be collected
  39. */
  40. Collection<Link> links = col.getLinks();
  41. /**
  42. * Devices which packets should be collected
  43. */
  44. LinkedList<SmartDevice> devices = new LinkedList<SmartDevice>(col.getDevices());
  45. for(Link link: model.getConnectionNetworks()){
  46. /**
  47. * Collect all packets of the links, which should be collected
  48. */
  49. if(links.contains(link)){
  50. col.addPackets(link, link.getPackets());
  51. }else if(!devices.isEmpty()){
  52. /**
  53. * Devices which are part of the link and should be collected by the PacketCollector
  54. */
  55. LinkedList<SmartDevice> linkDevices = new LinkedList<SmartDevice>(link.getDevices());
  56. linkDevices.retainAll(devices);
  57. /**
  58. * Check packets just if devices, which are part of the link, should be collected
  59. */
  60. if(!linkDevices.isEmpty())
  61. col.addPackets(link, link.getPackets().stream().filter(col.getFilter()).collect(Collectors.toList()));
  62. }
  63. }
  64. }
  65. }
  66. /**
  67. * Runs all registered PacketAlgorithms of the different collectors
  68. */
  69. public void runPacketAlgorithms(){
  70. for(PacketCollector collector:collectors){
  71. if(collector.isActive() && collector.getPacketAlgorithm()!=null){
  72. collector.getPacketAlgorithm().processPackets(collector.getPackets());
  73. }
  74. }
  75. }
  76. /**
  77. * Adds a packetCollector, which will collect packages from now on
  78. * @param collector new package collector
  79. */
  80. public void addPacketCollector(PacketCollector collector){
  81. if(!collectors.contains(collector))
  82. collectors.add(collector);
  83. }
  84. /**
  85. * Returns the packet collectors, which are collecting packets
  86. * @return active packet collectors
  87. */
  88. public LinkedList<PacketCollector> getPacketCollectors(){
  89. return collectors;
  90. }
  91. /**
  92. * Removes the given packet collector, it will no longer collect packets
  93. * @param collector collector to be removed
  94. */
  95. public void removePacketCollector(PacketCollector collector){
  96. collectors.remove(collector);
  97. }
  98. /**
  99. * Notify all observers
  100. */
  101. public void notifyObservers(){
  102. this.setChanged();
  103. this.notifyObservers(null);
  104. }
  105. }