Protocol.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
  2. import java.util.Collection;
  3. import java.util.Iterator;
  4. import java.util.LinkedList;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
  6. /**
  7. * Protocol according to which the packages are created. Can contain different
  8. * Roles and multiple participating SmartDevice(Port)s.
  9. *
  10. * @author Andreas T. Meyer-Berg
  11. */
  12. public interface Protocol {
  13. /*
  14. * Topology options
  15. */
  16. /**
  17. * Every Port/Device is connected to every other Port/Device
  18. */
  19. public static final byte FULLY_CONNECTED = 0;
  20. /**
  21. * Every Port/Device is connected to the Device of Role 0. (Has to be exactly one device of Role 0)
  22. */
  23. public static final byte STAR = 1;
  24. /**
  25. * Custom topology, getTopology has to be overwritten.
  26. */
  27. public static final byte CUSTOM = 127;
  28. /**
  29. * Generates the next packets<br>
  30. * If {@code port==null} the terminating packets shall be sent.<br>
  31. * This method should update port.setLastTrigger.<br>
  32. *
  33. * @param port SmartDevice(Port) which sends the package, null if terminating packets shall be sent.
  34. * @param timestep
  35. * Time the package should be sent, in System.currentTimeMillis
  36. * @param packetLost True if the packet was lost
  37. * @return next Packet, which was sent
  38. */
  39. public Collection<Packet> generateNextPackets(Port port, long timestep, boolean packetLost);
  40. /**
  41. * Returns the number of different roles the participating SmartDevice(Port)s
  42. * could have
  43. *
  44. * @return number of different roles
  45. */
  46. public default int getNumberOfRoles(){
  47. return getRoles() == null ? 0 : getRoles().length;
  48. }
  49. /**
  50. * Returns the textual representation of the different Roles. The Array
  51. * should contain {@code NumberOfRoles} Strings. {@code Array[role]} should be
  52. * a human-readable String representation of the Role at its position
  53. * {@code role}.
  54. *
  55. * @return String representations of the roles
  56. */
  57. public String[] getRoles();
  58. /**
  59. * Returns all SmartDevice(Port)s of the given Role. Returns {@code null}, if the role
  60. * number was invalid.
  61. *
  62. * @param role
  63. * Position of the role in {@code getNumberOfRoles}
  64. * @return SmartDevices of Role with index {@code role}
  65. */
  66. public Collection<Port> getDevicesWithRole(int role);
  67. /**
  68. * Returns the role of the given device, returns -1, if the Device is not part of the protocol
  69. *
  70. * @param device device which roles should be calculated
  71. * @return role of the device, -1 if it has no role
  72. */
  73. public default int getRoleOfDevice(Port device){
  74. if(device == null)
  75. return -1;
  76. for(int i = 0; i<getNumberOfRoles(); i++)
  77. if(getDevicesWithRole(i).contains(device))
  78. return i;
  79. return -1;
  80. }
  81. /**
  82. * Adds a new SmartDevice to the role, returns {@code true} if it was
  83. * assigned successfully, {@code false} if it wasn't. (Either invalid role
  84. * number or maximum number of devices for the role reached)
  85. *
  86. * @param device
  87. * SmartDevice(Port) that should be assigned to the given role
  88. * @param role
  89. * Position of the role in {@code getNumberOfRoles}
  90. * @return true, if the SmartDevice(Port) was added
  91. */
  92. public boolean addDeviceOfRole(Port device, int role);
  93. /**
  94. * Remove a SmartDevice(Port) from this Protocol
  95. *
  96. * @param device
  97. * device that should be removed
  98. */
  99. public void removeDevice(Port device);
  100. /**
  101. * Returns all Devices which participate in this Protocol
  102. *
  103. * @return devices in this Protocol
  104. */
  105. public default Collection<Port> getDevices(){
  106. LinkedList<Port> devices = new LinkedList<Port>();
  107. for(int i=0; i<getNumberOfRoles(); i++)
  108. devices.addAll(getDevicesWithRole(i));
  109. return devices;
  110. }
  111. /**
  112. * Returns name of the protocol
  113. *
  114. * @return name of the protocol
  115. */
  116. public String getName();
  117. /**
  118. * Returns the topology of this protocol as specified by the static final protocol fields.
  119. *
  120. * @return used topology
  121. */
  122. public default byte getTopologyType() {
  123. return FULLY_CONNECTED;
  124. }
  125. /**
  126. * Returns the different connections of this protocol, how the Ports are connected and transmitting packets.
  127. *
  128. * @return Connections
  129. */
  130. public default Collection<Pair<Port,Port>> getTopology(){
  131. LinkedList<Pair<Port,Port>> topology = new LinkedList<Pair<Port,Port>>();
  132. switch (getTopologyType()){
  133. case STAR:
  134. Collection<Port> devices = getDevicesWithRole(0);
  135. Port router = null;
  136. if(devices.size() == 1)
  137. router = devices.iterator().next();
  138. for (Iterator<Port> iterator = getDevices().iterator(); iterator.hasNext();) {
  139. Port right = (Port) (iterator.next());
  140. if(router != right)
  141. topology.add(new Pair<Port, Port>(router, right));
  142. }
  143. break;
  144. case FULLY_CONNECTED:
  145. case CUSTOM:
  146. default:
  147. for (Iterator<Port> iterator = getDevices().iterator(); iterator.hasNext();) {
  148. Port left = (Port) (iterator.next());
  149. boolean samePosition = false;
  150. for(Iterator<Port> iterator2 = getDevices().iterator(); iterator2.hasNext();){
  151. Port right = iterator2.next();
  152. if(!samePosition){
  153. if(left==right)
  154. samePosition=true;
  155. }else{
  156. topology.add(new Pair<Port, Port>(left, right));
  157. }
  158. }
  159. }
  160. break;
  161. }
  162. return topology;
  163. }
  164. /**
  165. * Returns the deleted
  166. *
  167. * @return deleted connection parts
  168. */
  169. public Collection<Pair<Port,Port>> getDeletedTopology();
  170. }