SimpleProtocol.java 4.5 KB

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