ConnectionPrecision.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
  2. import java.util.Collection;
  3. import java.util.Comparator;
  4. import java.util.Iterator;
  5. import java.util.LinkedList;
  6. import java.util.TreeSet;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.PacketComparator;
  8. /**
  9. * Implementation of the Connection Interface, with focus on precision
  10. *
  11. * @author Andreas T. Meyer-Berg
  12. */
  13. public class ConnectionPrecision extends ConnectionPerformance {
  14. /**
  15. * Packets which should be returned in the next time step
  16. */
  17. private LinkedList<Packet> outOfBoundsPackets = new LinkedList<Packet>();
  18. /**
  19. * Initializes the connection, adds participants of the protocol
  20. *
  21. * @param l Link which this connection uses
  22. * @param p Protocol of the connection
  23. */
  24. public ConnectionPrecision(Link l, Protocol p) {
  25. super(l,p);
  26. }
  27. /**
  28. * Initializes the connection
  29. */
  30. public ConnectionPrecision() {
  31. super();
  32. }
  33. @Override
  34. public Collection<Packet> simulateTimeInterval(long startTime, long duration) {
  35. /**
  36. * Time step the simulation interval ends
  37. */
  38. long endTime = startTime+duration;
  39. /**
  40. * All packets which should be returned in this time step
  41. */
  42. LinkedList<Packet> returnPackets = new LinkedList<Packet>();
  43. returnPackets.addAll(getTerminationPackages(startTime));
  44. startTime += returnPackets.size();
  45. /**
  46. * Add packets of the last time step
  47. */
  48. returnPackets.addAll(outOfBoundsPackets);
  49. /**
  50. * Sorted Tree (Sorted by next trigger time)
  51. */
  52. TreeSet<Port> portTree = new TreeSet<Port>(new PortComparator());
  53. portTree.addAll(participants);
  54. /**
  55. * Iterator to move through the Tree
  56. */
  57. Iterator<Port> it = portTree.iterator();
  58. /**
  59. * Traverse the tree ascending
  60. */
  61. while(it.hasNext()){
  62. Port p = it.next();
  63. if(p.getStatus()==Port.SENDING && p.getLastTrigger()+p.getTriggerInterval()<endTime){
  64. /**
  65. * Remove current Port (as the nextTrigger time will change, and therefore its position in the tree)
  66. */
  67. it.remove();
  68. /**
  69. * Generate first package in the simulation interval (not before startTime though), and at least 1ms after the last Trigger Time
  70. */
  71. returnPackets.addAll(protocol.generateNextPackets(p, (long)Math.max( p.getLastTrigger()+p.getTriggerInterval()+p.getJitter()*Math.random(),Math.max(p.getLastTrigger(),startTime)),Math.random()<packetLossRate));
  72. /**
  73. * If Port should simulate again in this interval -> add back to the tree
  74. */
  75. if(p.getLastTrigger()+p.getTriggerInterval()<endTime){
  76. portTree.add(p);
  77. /**
  78. * Reset iterator, to start from the next simulating port
  79. */
  80. it = portTree.iterator();
  81. }
  82. }else{
  83. /**
  84. * Remove ports, which are not sending or which won't simulate in this interval again
  85. */
  86. it.remove();
  87. }
  88. }
  89. /**
  90. * Sort and return packages
  91. */
  92. returnPackets.sort(new PacketComparator());
  93. /**
  94. * Remove packets which are not being sent in this time interval
  95. */
  96. outOfBoundsPackets.clear();
  97. /**
  98. * Last package, which should be sent in the next time step
  99. */
  100. Packet last = returnPackets.isEmpty()? null : returnPackets.getLast();
  101. while (last != null&&last.getTimestamp()>endTime) {
  102. outOfBoundsPackets.addFirst(returnPackets.removeLast());
  103. if(returnPackets.isEmpty())
  104. break;
  105. last = returnPackets.getLast();
  106. }
  107. if(label!=0)
  108. returnPackets.forEach(p->p.setLabel(label));
  109. return returnPackets;
  110. }
  111. /**
  112. * Comparator for comparing the next trigger time of two ports, the lower one should trigger first
  113. *
  114. * @author Andreas T. Meyer-Berg
  115. */
  116. private class PortComparator implements Comparator<Port>{
  117. @Override
  118. public int compare(Port o1, Port o2) {
  119. return Long.compare(o1.getLastTrigger()+o1.getTriggerInterval(),o2.getLastTrigger()+o2.getTriggerInterval());
  120. }
  121. }
  122. }