PrecisionLink.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
  2. import java.util.Collection;
  3. import java.util.LinkedList;
  4. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  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.SmartDevice;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.PacketComparator;
  9. import java.util.ArrayList;
  10. /**
  11. * Simple Implementation of {@link Link}, which allows connection of multiple
  12. * devices, with an more precise calculation than {@link de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink}
  13. *
  14. * @author Andreas T. Meyer-Berg
  15. */
  16. public class PrecisionLink implements Link {
  17. /**
  18. * Name of the connection
  19. */
  20. private String name;
  21. /**
  22. * Devices connected by this Link
  23. */
  24. private ArrayList<SmartDevice> devices;
  25. /**
  26. * Connections running via this Link
  27. */
  28. private ArrayList<Connection> connections;
  29. /**
  30. * List of packages to store packages sent during simulation intervals, or
  31. * after termination
  32. */
  33. private LinkedList<Packet> packets;
  34. /**
  35. * Packets which should be returned in the next time step
  36. */
  37. private LinkedList<Packet> outOfBoundsPackets = new LinkedList<Packet>();
  38. /**
  39. * whether the status changed during the last simulation step
  40. */
  41. private boolean statusChanged;
  42. private long fixedDelay=3;
  43. /**
  44. * Initializes a simple Link with name and an empty devices list.
  45. *
  46. * @param name
  47. * Name the Link should have
  48. */
  49. public PrecisionLink(String name) {
  50. this.name = name;
  51. this.devices = new ArrayList<SmartDevice>();
  52. this.packets = new LinkedList<Packet>();
  53. this.connections = new ArrayList<Connection>();
  54. }
  55. /**
  56. * Initializes a simple Link with default name and an empty devices list.
  57. */
  58. public PrecisionLink() {
  59. this.name = "link name";
  60. this.devices = new ArrayList<SmartDevice>();
  61. this.packets = new LinkedList<Packet>();
  62. this.connections = new ArrayList<Connection>();
  63. }
  64. /**
  65. * Returns the name of this Link
  66. *
  67. * @return name of this Link
  68. */
  69. public String getName() {
  70. return name;
  71. }
  72. /**
  73. * Sets the name of this Link
  74. *
  75. * @param name
  76. * the name to set
  77. */
  78. public void setName(String name) {
  79. this.name = name;
  80. }
  81. /**
  82. * Returns the device that are part of this connection
  83. *
  84. * @return the devices of this Link
  85. */
  86. public Collection<SmartDevice> getDevices() {
  87. return devices;
  88. }
  89. /**
  90. * Adds the SmartDevice to this Link
  91. *
  92. * @param device
  93. * the devices to add
  94. */
  95. public void addDevice(SmartDevice device) {
  96. this.devices.add(device);
  97. }
  98. /**
  99. * Remove SmartDevice from this Link
  100. *
  101. * @param device
  102. * the device to remove
  103. */
  104. public void removeDevice(SmartDevice device) {
  105. this.devices.remove(device);
  106. }
  107. @Override
  108. public void simulateTimeInterval(long startTime, long duration) {
  109. packets.clear();
  110. statusChanged = false;
  111. packets.addAll(outOfBoundsPackets);
  112. for (Connection c : connections) {
  113. if (c.getLink() != this)
  114. continue;
  115. // Simulate just if source and link match
  116. if ((c.getStatus() == Connection.ACTIVE || c.getStatus() == Connection.HALTED))
  117. packets.addAll(c.simulateTimeInterval(startTime, duration));
  118. else if (c.getStatus() == Connection.FINISHED
  119. || c.getStatus() == Connection.TERMINATED)
  120. //Produce Termination packages
  121. packets.addAll(c.getTerminationPackages(startTime));
  122. statusChanged|=c.getStatusChanged();
  123. }
  124. packets.sort(new PacketComparator());
  125. /**
  126. * Remove packets which are not being sent in this time interval
  127. */
  128. outOfBoundsPackets.clear();
  129. /**
  130. * Last package, which should be sent in the next time step
  131. */
  132. Packet last = packets.isEmpty()? null : packets.getLast();
  133. while (last != null&&last.getTimestamp()>startTime+duration) {
  134. outOfBoundsPackets.addFirst(packets.removeLast());
  135. if(packets.isEmpty())
  136. break;
  137. last = packets.getLast();
  138. }
  139. }
  140. @Override
  141. public Collection<Packet> getPackets() {
  142. return packets;
  143. }
  144. @Override
  145. public Collection<Connection> getConnections() {
  146. return connections;
  147. }
  148. @Override
  149. public void addConnection(Connection connection) {
  150. connections.add(connection);
  151. }
  152. @Override
  153. public void removeConnection(Connection connection) {
  154. connections.remove(connection);
  155. }
  156. @Override
  157. public boolean getStatusChanged() {
  158. return statusChanged;
  159. }
  160. @Override
  161. public long getTransmissionDelayFrom(SmartDevice from, SmartDevice to) {
  162. return fixedDelay;
  163. }
  164. /**
  165. * Set the delay of this Link
  166. * @param delay fixed transmission delay
  167. */
  168. public void setFixedDelay(long delay){
  169. this.fixedDelay = delay;
  170. }
  171. /**
  172. * Returns the fixed transmission delay
  173. * @return transmission delay
  174. */
  175. public long getFixedDelay(){
  176. return this.fixedDelay;
  177. }
  178. @Override
  179. public void addPackets(Collection<Packet> packets) {
  180. this.packets.addAll(packets);
  181. }
  182. }