Port.java 10 KB

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