Port.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
  2. import java.util.Collection;
  3. import java.util.Random;
  4. import org.apache.commons.math3.distribution.AbstractRealDistribution;
  5. import org.apache.commons.math3.distribution.LaplaceDistribution;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.distributionHandler.ConstantValueDistributionHandler;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler.Schedulable;
  8. /**
  9. * Representation of connection EndPoints, which allows configuration of timings
  10. * and if it reacts to incoming traffic or even triggers new connections.
  11. *
  12. * @author Andreas T. Meyer-Berg
  13. */
  14. public class Port implements Schedulable {
  15. /**
  16. * A closed Port which does not react to incoming traffic
  17. */
  18. public static final short CLOSED = 0;
  19. /**
  20. * Open Port which reacts to incoming traffic
  21. */
  22. public static final short OPEN = 1;
  23. /**
  24. * Open Port which reacts to incoming traffic and sends packets every
  25. * triggerInterval
  26. */
  27. public static final short SENDING = 2;
  28. /**
  29. * Status of the Port, whether it is CLOSED, OPEN or SENDING
  30. */
  31. private short status;
  32. /**
  33. * SmartDevice which owns the port
  34. */
  35. private SmartDevice owner;
  36. /**
  37. * Connection this Port listens to
  38. */
  39. private Connection connection;
  40. /**
  41. * Last timestep traffic was triggered
  42. */
  43. private long lastTrigger;
  44. /**
  45. * Time in milliseconds needed for responds to incoming
  46. */
  47. private short responseTime;
  48. /**
  49. * Jitter of the connection of this port. Random fluctuation of delivery times.
  50. */
  51. private short jitter;
  52. /**
  53. * Current Jitter, updated after every simulation
  54. */
  55. private short currentJitter;
  56. /**
  57. * Port number of this Port.
  58. */
  59. private short portNumber;
  60. /**
  61. * Constant time interval, which triggers new traffic every
  62. * {@code triggerInterval} milliseconds.
  63. */
  64. private long triggerInterval;
  65. /**
  66. * Probability Distribution for calculating the next Trigger
  67. */
  68. private ProbabilityDistributionHandler triggerIntervalStat;
  69. /**
  70. * Creates a new Port for the given Device with the specified PortNumber
  71. * @param device SmartDevice this port listens on
  72. * @param portNumber Number of the Port
  73. */
  74. public Port(SmartDevice device, short portNumber){
  75. status = SENDING;
  76. owner = device;
  77. connection = null;
  78. triggerIntervalStat = new ConstantValueDistributionHandler(new Random().nextInt(1000)+1);
  79. triggerInterval = Math.max(1, triggerIntervalStat.sampleNextValue());
  80. //setTriggerInterval(new Random().nextInt(1000)+1);
  81. lastTrigger = 0;
  82. jitter = (short)(new Random().nextInt(5)+1);
  83. currentJitter = (short)Math.round(Math.random()*jitter);
  84. responseTime = 0;
  85. this.portNumber = portNumber;
  86. }
  87. /**
  88. * Creates a new Port for the given Device with the specified PortNumber
  89. * @param device SmartDevice this port listens on
  90. * @param portNumber Number of the Port
  91. * @param triggerInterval frequency the port should trigger
  92. */
  93. public Port(SmartDevice device, short portNumber, long triggerInterval){
  94. status = SENDING;
  95. owner = device;
  96. connection = null;
  97. triggerIntervalStat = new ConstantValueDistributionHandler(triggerInterval);
  98. this.triggerInterval = Math.max(1, triggerIntervalStat.sampleNextValue());
  99. lastTrigger = 0;
  100. jitter = 0;
  101. currentJitter = (short)Math.round(Math.random()*jitter);
  102. responseTime = 0;
  103. this.portNumber = portNumber;
  104. }
  105. /**
  106. * Creates a new Port for the given Device with the specified PortNumber
  107. * @param device SmartDevice this port listens on
  108. * @param portNumber Number of the Port
  109. * @param triggerInterval frequency the port should trigger
  110. * @param jitter Jitter
  111. * @param lastTrigger last time a packet was sent
  112. * @param responseTime response time
  113. */
  114. public Port(SmartDevice device, short portNumber, long triggerInterval, short jitter, long lastTrigger, short responseTime){
  115. status = SENDING;
  116. owner = device;
  117. connection = null;
  118. triggerIntervalStat = new ConstantValueDistributionHandler(triggerInterval);
  119. this.triggerInterval = Math.max(1, triggerIntervalStat.sampleNextValue());
  120. this.lastTrigger = lastTrigger;
  121. this.jitter = jitter;
  122. currentJitter = (short)Math.round(Math.random()*jitter);
  123. this.responseTime = responseTime;
  124. this.portNumber = portNumber;
  125. }
  126. /**
  127. * @return the status
  128. */
  129. public short getStatus() {
  130. return status;
  131. }
  132. /**
  133. * @param status
  134. * the status to set
  135. */
  136. public void setStatus(short status) {
  137. this.status = status;
  138. }
  139. /**
  140. * @return the owner
  141. */
  142. public SmartDevice getOwner() {
  143. return owner;
  144. }
  145. /**
  146. * @param owner
  147. * the owner to set
  148. */
  149. public void setOwner(SmartDevice owner) {
  150. this.owner = owner;
  151. }
  152. /**
  153. * TriggerInterval is sampled from distribution after previous simulation
  154. * @return the triggerInterval
  155. */
  156. public long getTriggerInterval() {
  157. return triggerInterval;
  158. }
  159. /**
  160. * Returns the Probability Handler for the trigger interval, which allows configuration of the Distribution
  161. * @return Distribution Handler for configuration
  162. */
  163. public ProbabilityDistributionHandler getTriggerHandler(){
  164. return triggerIntervalStat;
  165. }
  166. /**
  167. * Set a new Probability Distribution Handler to sample the trigger interval
  168. * @param triggerHandler Sets the Probability Distribution to generate the trigger interval
  169. */
  170. public void setTriggerHandler(ProbabilityDistributionHandler triggerHandler){
  171. this.triggerIntervalStat = triggerHandler;
  172. boolean removed = SimulationManager.removeEvent(this);
  173. this.triggerInterval = Math.max(1, triggerHandler.sampleNextValue());
  174. if(removed)
  175. SimulationManager.scheduleEvent(this);
  176. }
  177. /**
  178. * Samples a new Value for the TriggerInterval
  179. */
  180. public void resampleTriggerInterval(){
  181. boolean removed = SimulationManager.removeEvent(this);
  182. this.triggerInterval = Math.max(triggerIntervalStat.sampleNextValue(),1);
  183. if(removed)
  184. SimulationManager.scheduleEvent(this);
  185. }
  186. /**
  187. * @return the responseTime
  188. */
  189. public short getResponseTime() {
  190. return responseTime;
  191. }
  192. /**
  193. * @param responseTime
  194. * the responseTime to set
  195. */
  196. public void setResponseTime(short responseTime) {
  197. if(responseTime<=0)
  198. this.responseTime = 0;
  199. else
  200. this.responseTime = responseTime;
  201. }
  202. /**
  203. * @return the portNumber
  204. */
  205. public short getPortNumber() {
  206. return portNumber;
  207. }
  208. /**
  209. * @param portNumber
  210. * the portNumber to set
  211. */
  212. public void setPortNumber(short portNumber) {
  213. this.portNumber = portNumber;
  214. }
  215. /**
  216. * @return the last timestep the port sent a package
  217. */
  218. public long getLastTrigger() {
  219. return lastTrigger;
  220. }
  221. /**
  222. * @param lastTrigger the last timestep the port sent
  223. */
  224. public void setLastTrigger(long lastTrigger) {
  225. this.lastTrigger = lastTrigger;
  226. }
  227. /**
  228. * @return the connection
  229. */
  230. public Connection getConnection() {
  231. return connection;
  232. }
  233. /**
  234. * @param connection the connection to set
  235. */
  236. public void setConnection(Connection connection) {
  237. this.connection = connection;
  238. }
  239. /**
  240. * Return the Jitter of the port, which represents random fluctuations of the delivery times
  241. * @return the Jitter
  242. */
  243. public short getJitter() {
  244. return jitter;
  245. }
  246. /**
  247. * Set Jitter of the port, which represents random fluctuations of the delivery times
  248. * @param jitter the Jitter to set
  249. */
  250. public void setJitter(short jitter) {
  251. this.jitter = jitter;
  252. }
  253. /**
  254. * Returns a String representation for the status byte
  255. * Returns 0:closed, 1:open, 2:sending
  256. *
  257. * @param s as specified by {@link Port}
  258. * @return textual representation of the status
  259. */
  260. public static String statusToString(short s){
  261. switch (s) {
  262. case CLOSED:
  263. return "closed";
  264. case OPEN:
  265. return "open";
  266. case SENDING:
  267. return "sending";
  268. default:
  269. return "unknown";
  270. }
  271. }
  272. @Override
  273. public String toString() {
  274. String protocol = "";
  275. if(connection!=null&&connection.getProtocol()!=null)
  276. protocol=connection.getProtocol().getName();
  277. return "Port: " + portNumber + protocol + " Status:" + statusToString(status) + " " + "TriggerInterval:"+triggerInterval+" "+(owner == null ? "" : " on " + owner.getName());
  278. }
  279. /**
  280. * Returns the transmission delay in milliseconds to the other port, which is specified in the Link of the Connection.
  281. * @param to Port, which should receive the transmission
  282. * @return delay between this port and the to Port.
  283. */
  284. public long getTransmissionDelayTo(Port to){
  285. if(connection != null && connection.getLink() != null)
  286. if(owner != null && to != null && to.getOwner()!=null)
  287. return connection.getLink().getTransmissionDelayFrom(owner, to.getOwner());
  288. return Long.MAX_VALUE;
  289. }
  290. @Override
  291. public long getEventTime() {
  292. return lastTrigger+triggerInterval+currentJitter;
  293. }
  294. @Override
  295. public void simulateEvent(long time) {
  296. if(connection==null || connection.getProtocol()==null || connection.getLink()==null || owner == null)
  297. return;
  298. /**
  299. * Packets of the transfer initiated by this port
  300. */
  301. Collection<Packet> packets = connection.getProtocol().generateNextPackets(this, time, false);
  302. //Packets encapsulated by Connection
  303. packets = connection.encapsulatePackages(packets);
  304. //Encapsulate packets in Links
  305. packets = connection.getLink().encapsulatePackages(packets);
  306. connection.getLink().addPackets(packets);
  307. //TODO: Sample next game
  308. triggerInterval = Math.max(1,triggerIntervalStat.sampleNextValue());
  309. if(status==Port.SENDING && connection.getStatus()==Connection.ACTIVE)
  310. SimulationManager.scheduleEvent(this);
  311. }
  312. }