PrecisionLink.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. /**
  43. * Delay between two devices, fixed
  44. */
  45. private long fixedDelay=3;
  46. /**
  47. * Initializes a simple Link with name and an empty devices list.
  48. *
  49. * @param name
  50. * Name the Link should have
  51. */
  52. public PrecisionLink(String name) {
  53. this.name = name;
  54. this.devices = new ArrayList<SmartDevice>();
  55. this.packets = new LinkedList<Packet>();
  56. this.connections = new ArrayList<Connection>();
  57. }
  58. /**
  59. * Initializes a simple Link with default name and an empty devices list.
  60. */
  61. public PrecisionLink() {
  62. this.name = "link name";
  63. this.devices = new ArrayList<SmartDevice>();
  64. this.packets = new LinkedList<Packet>();
  65. this.connections = new ArrayList<Connection>();
  66. }
  67. /**
  68. * Returns the name of this Link
  69. *
  70. * @return name of this Link
  71. */
  72. public String getName() {
  73. return name;
  74. }
  75. /**
  76. * Sets the name of this Link
  77. *
  78. * @param name
  79. * the name to set
  80. */
  81. public void setName(String name) {
  82. this.name = name;
  83. }
  84. /**
  85. * Returns the device that are part of this connection
  86. *
  87. * @return the devices of this Link
  88. */
  89. public Collection<SmartDevice> getDevices() {
  90. return devices;
  91. }
  92. /**
  93. * Adds the SmartDevice to this Link
  94. *
  95. * @param device
  96. * the devices to add
  97. */
  98. public void addDevice(SmartDevice device) {
  99. this.devices.add(device);
  100. }
  101. /**
  102. * Remove SmartDevice from this Link
  103. *
  104. * @param device
  105. * the device to remove
  106. */
  107. public void removeDevice(SmartDevice device) {
  108. this.devices.remove(device);
  109. }
  110. @Override
  111. public void simulateTimeInterval(long startTime, long duration) {
  112. packets.clear();
  113. statusChanged = false;
  114. packets.addAll(outOfBoundsPackets);
  115. for (Connection c : connections) {
  116. if (c.getLink() != this)
  117. continue;
  118. // Simulate just if source and link match
  119. if ((c.getStatus() == Connection.ACTIVE || c.getStatus() == Connection.HALTED))
  120. packets.addAll(c.simulateTimeInterval(startTime, duration));
  121. else if (c.getStatus() == Connection.FINISHED
  122. || c.getStatus() == Connection.TERMINATED)
  123. //Produce Termination packages
  124. packets.addAll(c.getTerminationPackages(startTime));
  125. statusChanged|=c.getStatusChanged();
  126. }
  127. packets.sort(new PacketComparator());
  128. /**
  129. * Remove packets which are not being sent in this time interval
  130. */
  131. outOfBoundsPackets.clear();
  132. /**
  133. * Last package, which should be sent in the next time step
  134. */
  135. Packet last = packets.isEmpty()? null : packets.getLast();
  136. while (last != null&&last.getTimestamp()>startTime+duration) {
  137. outOfBoundsPackets.addFirst(packets.removeLast());
  138. if(packets.isEmpty())
  139. break;
  140. last = packets.getLast();
  141. }
  142. }
  143. @Override
  144. public Collection<Packet> encapsulatePackages(Collection<Packet> packets){
  145. return packets;
  146. }
  147. @Override
  148. public Collection<Packet> getPackets() {
  149. return packets;
  150. }
  151. @Override
  152. public Collection<Connection> getConnections() {
  153. return connections;
  154. }
  155. @Override
  156. public void addConnection(Connection connection) {
  157. connections.add(connection);
  158. }
  159. @Override
  160. public void removeConnection(Connection connection) {
  161. connections.remove(connection);
  162. }
  163. @Override
  164. public boolean getStatusChanged() {
  165. return statusChanged;
  166. }
  167. @Override
  168. public long getTransmissionDelayFrom(SmartDevice from, SmartDevice to) {
  169. return fixedDelay;
  170. }
  171. /**
  172. * Set the delay of this Link
  173. * @param delay fixed transmission delay
  174. */
  175. public void setFixedDelay(long delay){
  176. this.fixedDelay = delay;
  177. }
  178. /**
  179. * Returns the fixed transmission delay
  180. * @return transmission delay
  181. */
  182. public long getFixedDelay(){
  183. return this.fixedDelay;
  184. }
  185. @Override
  186. public void addPackets(Collection<Packet> packets) {
  187. this.packets.addAll(packets);
  188. }
  189. @Override
  190. public void initSimulationInterval(long startTime, long duration) {
  191. /**
  192. * Reset packets
  193. */
  194. packets.clear();
  195. /**
  196. * Add out of Bounds packets
  197. */
  198. packets.addAll(outOfBoundsPackets);
  199. }
  200. @Override
  201. public void finalizeSimulationInterval(long startTime, long duration) {
  202. //Remove out of Bounds Packets
  203. /**
  204. * Remove packets which are not being sent in this time interval
  205. */
  206. outOfBoundsPackets.clear();
  207. /**
  208. * Last package, which should be sent in the next time step
  209. */
  210. Packet last = packets.isEmpty()? null : packets.getLast();
  211. while (last != null&&last.getTimestamp()>startTime+duration) {
  212. outOfBoundsPackets.addFirst(packets.removeLast());
  213. if(packets.isEmpty())
  214. break;
  215. last = packets.getLast();
  216. }
  217. }
  218. }