CountingMetric.java 4.8 KB

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