ValidProtocol.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package de.tu_darmstadt.tk.shNetSimTests.control;
  2. import java.util.Arrays;
  3. import java.util.Collection;
  4. import java.util.LinkedList;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Protocol;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimplePacket;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.util.Pair;
  10. /**
  11. * Simple Implementation of a protocol, which sends
  12. *
  13. * @author Andreas T. Meyer-Berg
  14. */
  15. public class ValidProtocol implements Protocol {
  16. /**
  17. * Source of the protocol, and destination which responds
  18. */
  19. Port source, destination;
  20. /**
  21. * Deleted Connection if participants of the connection were removed
  22. */
  23. private Pair<Port,Port> deletedConnection = null;
  24. /**
  25. * Package which is sent on termination
  26. */
  27. private Packet terminationPacket = null;
  28. /**
  29. * True if the next Package is send by source, false if the next one will be send by destination
  30. */
  31. boolean srcSends;
  32. public ValidProtocol(){
  33. source = null;
  34. destination = null;
  35. srcSends = true;
  36. }
  37. public ValidProtocol(Port src, Port dest){
  38. source = src;
  39. destination = dest;
  40. srcSends = true;
  41. }
  42. @Override
  43. public Collection<Packet> generateNextPackets(Port p, long timestep, boolean packetLost) {
  44. LinkedList<Packet> ret = new LinkedList<Packet>();
  45. if(terminationPacket!=null){
  46. terminationPacket.setTimestamp(timestep);
  47. ret.add(terminationPacket);
  48. terminationPacket = null;
  49. }
  50. if(p==null) return ret;
  51. p.setLastTrigger(timestep);
  52. if(packetLost)ret.add(new SimplePacket(timestep, p, p == source ? destination : source, "Lost"));
  53. if(p == destination)
  54. ret.add(new SimplePacket(timestep, destination, source));
  55. else
  56. ret.add(new SimplePacket(timestep, source, destination));
  57. return ret;
  58. }
  59. @Override
  60. public int getNumberOfRoles() {
  61. // two different roles
  62. return 2;
  63. }
  64. @Override
  65. public String[] getRoles() {
  66. return new String[]{"Sender", "Receiver"};
  67. }
  68. @Override
  69. public Collection<Port> getDevicesWithRole(int role) {
  70. Port ret = null;
  71. switch (role) {
  72. case 0:
  73. ret = source;
  74. break;
  75. case 1:
  76. ret = destination;
  77. break;
  78. default:
  79. return null;
  80. }
  81. if(ret!=null)
  82. return Arrays.asList(ret);
  83. else
  84. return Arrays.asList();
  85. }
  86. @Override
  87. public boolean addDeviceOfRole(Port device, int role) {
  88. if(role == 0 && source == null){
  89. source = device;
  90. }else if(role == 1 && destination == null){
  91. destination = device;
  92. }else{
  93. return false;
  94. }
  95. return true;
  96. }
  97. @Override
  98. public void removeDevice(Port device) {
  99. if(device == source){
  100. if(destination!=null){
  101. deletedConnection = new Pair<Port, Port>(new Port(source.getOwner(),(short)-1),destination);
  102. terminationPacket = new SimplePacket(0, source, destination, "Disconnect");
  103. }else if(deletedConnection!=null){
  104. deletedConnection.setLeft(new Port(source.getOwner(),(short)-1));
  105. }
  106. source = null;
  107. }
  108. if(device == destination){
  109. if(source!=null){
  110. deletedConnection = new Pair<Port, Port>(source, new Port(destination.getOwner(),(short)-1));
  111. terminationPacket = new SimplePacket(0, destination, source, "Disconnect");
  112. }else if(deletedConnection!=null){
  113. deletedConnection.setRight(new Port(destination.getOwner(),(short)-1));
  114. }
  115. destination = null;
  116. }
  117. }
  118. @Override
  119. public String getName() {
  120. return "SimpleProtocol";
  121. }
  122. @Override
  123. public int getRoleOfDevice(Port device){
  124. if(device == null) return -1;
  125. else if(device == source) return 0;
  126. else if(device == destination) return 1;
  127. else return -1;
  128. }
  129. @Override
  130. public Collection<Port> getDevices(){
  131. LinkedList<Port> returnDevices = new LinkedList<Port>();
  132. if(source!=null)
  133. returnDevices.add(source);
  134. if(destination!=null)
  135. returnDevices.add(destination);
  136. return returnDevices;
  137. }
  138. @Override
  139. public byte getTopologyType() {
  140. return FULLY_CONNECTED;
  141. }
  142. @Override
  143. public Collection<Pair<Port,Port>> getTopology(){
  144. LinkedList<Pair<Port,Port>> topology = new LinkedList<Pair<Port,Port>>();
  145. if(source!=null && destination!=null)
  146. topology.add(new Pair<Port, Port>(source, destination));
  147. return topology;
  148. }
  149. @Override
  150. public Collection<Pair<Port, Port>> getDeletedTopology() {
  151. LinkedList<Pair<Port, Port>> deleted = new LinkedList<Pair<Port,Port>>();
  152. if(deletedConnection!=null)deleted.add(deletedConnection);
  153. return deleted;
  154. }
  155. }