SimpleLink.java 4.6 KB

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