CountingMetric.java 4.7 KB

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