CountingMetric.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import java.util.HashMap;
  2. import java.util.LinkedList;
  3. import java.util.Map.Entry;
  4. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.PacketSniffer;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets.MQTT_packet;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.packets.Ping_packet;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimplePacket;
  11. /**
  12. * Metric which can be used as PacketSniffer. Will count the number of packets sent per Link.
  13. * Furthermore statistics like number of packets sent between different devices, number of packets
  14. * per type etc. will be printed to the console.
  15. *
  16. * @author Andreas T. Meyer-Berg
  17. */
  18. public class CountingMetric implements PacketSniffer {
  19. /**
  20. * Mode of the algorithm, True = testing
  21. */
  22. private boolean mode = false;
  23. @Override
  24. public void processPackets(HashMap<Link, LinkedList<Packet>> packets) {
  25. System.out.println("Counting Metric: ");
  26. System.out.println("Mode: "+(mode?"Testing":"Training"));
  27. if(packets==null)return;
  28. /**
  29. * Print all links and their number of packets sent
  30. */
  31. for(Entry<Link, LinkedList<Packet>> e:packets.entrySet()){
  32. if(e == null || e.getKey() == null || e.getValue() == null)continue;
  33. //System.out.println(e.getKey().getName()+": "+e.getValue().size()+" Packets");
  34. printStatistics(e.getValue());
  35. }
  36. System.out.println("");
  37. }
  38. /**
  39. * Calculates and prints statistics for the given packets
  40. * @param packets Packets which should be evaluated
  41. */
  42. private void printStatistics(LinkedList<Packet> packets){
  43. //Calculate first and last packet time
  44. /**
  45. * Timestep the first packet was sent
  46. */
  47. long minTime = Long.MAX_VALUE;
  48. /**
  49. * Timestep the last pacekt was sent
  50. */
  51. long maxTime = Long.MIN_VALUE;
  52. /**
  53. * Number of ping packets
  54. */
  55. int pingPackets = 0;
  56. /**
  57. * Number of ping packets
  58. */
  59. int mqttPackets = 0;
  60. /**
  61. * Number of MQTT packets
  62. */
  63. int simplePackets = 0;
  64. /**
  65. * Number of unknown packets
  66. */
  67. int unknownPackets = 0;
  68. //HashMap for packets per collection
  69. HashMap<SmartDevice,HashMap<SmartDevice,Integer>> connections = new HashMap<SmartDevice, HashMap<SmartDevice, Integer>>();
  70. for(Packet p: packets){
  71. minTime = Math.min(minTime, p.getTimestamp());
  72. maxTime = Math.max(maxTime, p.getTimestamp());
  73. if(p instanceof MQTT_packet)
  74. mqttPackets++;
  75. else if(p instanceof Ping_packet)
  76. pingPackets++;
  77. else if(p instanceof SimplePacket)
  78. simplePackets++;
  79. else
  80. unknownPackets++;
  81. HashMap<SmartDevice,Integer> map = connections.get(p.getSource().getOwner());
  82. if(map == null)
  83. map = new HashMap<SmartDevice, Integer>();
  84. //Increment counter for each packet by on. Add HashMap/Numbers if number non existent
  85. Integer lastVal = map.get(p.getDestination().getOwner());
  86. int i = lastVal == null?0:lastVal;
  87. map.put(p.getDestination().getOwner(), i+1);
  88. connections.put(p.getSource().getOwner(), map);
  89. }
  90. // Print number of packets per Protocol Type
  91. System.out.println("In Total "+packets.size()+" Packets were sent.");
  92. if(mqttPackets != 0){
  93. if(mqttPackets==packets.size()){
  94. System.out.println("All of them were MQTT packets");
  95. }else{
  96. System.out.println(mqttPackets+ "("+Math.round(mqttPackets*100.0/packets.size())+"%) of them were MQTT packets");
  97. }
  98. }
  99. if(pingPackets != 0){
  100. if(pingPackets==packets.size()){
  101. System.out.println("All of them were ping packets");
  102. }else{
  103. System.out.println(pingPackets+ "("+Math.round(pingPackets*100.0/packets.size())+"%) of them were ping packets");
  104. }
  105. }
  106. if(simplePackets != 0){
  107. if(simplePackets==packets.size()){
  108. System.out.println("All of them were simple packets");
  109. }else{
  110. System.out.println(simplePackets+ "("+Math.round(simplePackets*100.0/packets.size())+"%) of them were simple packets");
  111. }
  112. }
  113. if(unknownPackets != 0){
  114. if(unknownPackets==packets.size()){
  115. System.out.println("All of them were unknown packets");
  116. }else{
  117. System.out.println(unknownPackets+ "("+Math.round(unknownPackets*100.0/packets.size())+"%) of them were unknwon packets");
  118. }
  119. }
  120. if(minTime<=maxTime)
  121. System.out.println("First packet was captured at "+minTime+" ms, and the last at "+maxTime+" ms.");
  122. else
  123. System.out.println("No packets were captured.");
  124. //Print the number of packets per connection
  125. for (Entry<SmartDevice, HashMap<SmartDevice, Integer>> e: connections.entrySet()) {
  126. String src = e.getKey() == null || e.getKey().getName()==null ? "null":e.getKey().getName();
  127. System.out.println(src + " sent:");
  128. for (Entry<SmartDevice, Integer> f: e.getValue().entrySet()) {
  129. String dest = f.getKey() == null || f.getKey().getName()==null ? "null":f.getKey().getName();
  130. System.out.println(f.getValue() + " packets to "+dest);
  131. }
  132. }
  133. }
  134. @Override
  135. public void setMode(boolean testing) {
  136. mode = testing;
  137. }
  138. @Override
  139. public boolean getMode() {
  140. return mode;
  141. }
  142. }