SimpleConnection.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation;
  2. import java.util.Arrays;
  3. import java.util.Collection;
  4. import java.util.LinkedList;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
  10. @Deprecated
  11. public class SimpleConnection implements Connection {
  12. /**
  13. * SmartDevice(Port) which is the source of this connection
  14. */
  15. private Port source;
  16. /**
  17. * SmartDevice(Port) which is the destination of this connection
  18. */
  19. private Port destination;
  20. /**
  21. * Link which connects {@code source} and {@code destination}
  22. */
  23. private Link link;
  24. /**
  25. * Protocol, which specifies the packet generation
  26. */
  27. private Protocol p;
  28. /**
  29. * Name of the Connection
  30. */
  31. private String name;
  32. // for Termination
  33. /**
  34. * SmartDevice(Port) which started the termination process
  35. */
  36. private Port srcOfTermination;
  37. /* *is connection uses
  38. * SmartDevice which responds in the termination process
  39. */
  40. // private SmartDevice other;
  41. /**
  42. * Transmission status of the connection
  43. */
  44. private byte status = Connection.ACTIVE;
  45. /**
  46. * True if the status of the connection changed during the last simulation step
  47. */
  48. private boolean statusChanged = false;
  49. /**
  50. * Probability of packet loss. (default: 0.0 -> no packets are lost. 1.0 -> all packets are lost)
  51. */
  52. private double packetLossProbability = 0.0;
  53. /**
  54. * Default label assigned to packets
  55. */
  56. protected short label = 0;
  57. /**
  58. * Creates a new connection between two SmartDevice Ports on a given Link, which
  59. * communicate with a protocol p
  60. *
  61. * @param src
  62. * Port which is the source of this connection
  63. * @param dest
  64. * Port which is the destination of this connection
  65. * @param link
  66. * Link which connects is connection uses{@code src} and {@code dest}
  67. * @param p
  68. * Protocol, which specifies the packet generation
  69. */
  70. public SimpleConnection(Port src, Port dest, Link link, Protocol p) {
  71. source = src;
  72. destination = dest;
  73. this.link = link;
  74. this.p = p;
  75. this.name = "SimpleConnection";
  76. }
  77. @Override
  78. public Link getLink() {
  79. return link;
  80. }
  81. @Override
  82. public void setLink(Link link){
  83. this.link = link;
  84. }
  85. @Override
  86. public Collection<Packet> simulateTimeInterval(long startTime, long duration) {
  87. LinkedList<Packet> list = new LinkedList<Packet>();
  88. if(status != ACTIVE)return list;
  89. list.addAll(getTerminationPackages(startTime));
  90. if(list.isEmpty())
  91. statusChanged = true;
  92. else
  93. statusChanged = false;
  94. //Generate packets by source
  95. if(source.getLastTrigger()+source.getTriggerInterval()<startTime && source.getStatus()==Port.SENDING){
  96. list.addAll(p.generateNextPackets(source, startTime,Math.random()<packetLossProbability));
  97. }
  98. while(source.getLastTrigger()+source.getTriggerInterval()<startTime+duration &&source.getStatus()==Port.SENDING)
  99. list.addAll(p.generateNextPackets(source, source.getLastTrigger()+source.getTriggerInterval(),Math.random()<packetLossProbability));
  100. if(destination.getLastTrigger()+destination.getTriggerInterval()<startTime && destination.getStatus()==Port.SENDING){
  101. list.addAll(p.generateNextPackets(destination, startTime,Math.random()<packetLossProbability));
  102. }
  103. while(destination.getLastTrigger()+destination.getTriggerInterval()<startTime+duration &&destination.getStatus()==Port.SENDING)
  104. list.addAll(p.generateNextPackets(destination, destination.getLastTrigger()+destination.getTriggerInterval(),Math.random()<packetLossProbability));
  105. list.sort((x,y) -> Long.compare(x.getTimestamp(),y.getTimestamp()));
  106. return list;
  107. }
  108. @Override
  109. public Collection<Packet> encapsulatePackages(Collection<Packet> packets) {
  110. return packets;
  111. }
  112. @Override
  113. public Collection<Port> getParticipants() {
  114. return new LinkedList<Port>(Arrays.asList(source, destination));
  115. }
  116. @Override
  117. public Collection<Port> getParticipantsAndRemoved() {
  118. return new LinkedList<Port>(Arrays.asList(source, destination));
  119. }
  120. @Override
  121. public boolean removeSmartDevice(Port sd) {
  122. if (sd == source || sd == destination) {
  123. // of source == null - connection was already terminated
  124. status = Connection.TERMINATED;
  125. srcOfTermination = sd;
  126. return true;
  127. } else
  128. return false;
  129. }
  130. @Override
  131. public Collection<Packet> getTerminationPackages(long startTime) {
  132. if(status!=TERMINATED&&status!=FINISHED)return new LinkedList<Packet>();
  133. status = DONE;
  134. return new LinkedList<Packet>(Arrays.asList((Packet) new SimplePacket(
  135. startTime, srcOfTermination,
  136. srcOfTermination == source ? destination : source, "Terminated", label)));
  137. }
  138. @Override
  139. public boolean addSmartDevice(Port sd) {
  140. // Not possible to add Devices
  141. return false;
  142. }
  143. @Override
  144. public Protocol getProtocol() {
  145. return p;
  146. }
  147. @Override
  148. public boolean setProtocol(Protocol protocol) {
  149. return false;
  150. }
  151. @Override
  152. public byte getStatus() {
  153. return status;
  154. }
  155. @Override
  156. public void setStatus(byte status) {
  157. this.status = status;
  158. }
  159. @Override
  160. public void setPacketLossProbability(double lossPercentage) {
  161. packetLossProbability = lossPercentage;
  162. }
  163. @Override
  164. public double getPacketLossProbability() {
  165. return packetLossProbability;
  166. }
  167. @Override
  168. public boolean getStatusChanged() {
  169. return statusChanged;
  170. }
  171. @Override
  172. public String getName() {
  173. return name;
  174. }
  175. @Override
  176. public void setName(String name) {
  177. this.name = name;
  178. }
  179. @Override
  180. public short getLabel() {
  181. return label;
  182. }
  183. @Override
  184. public void setLabel(short label) {
  185. this.label = label;
  186. }
  187. }