CountingMetric.java 5.0 KB

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