Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. package routing;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6.  
  7. import core.Connection;
  8. import core.DTNHost;
  9. import core.Message;
  10. import core.Settings;
  11.  
  12. public class RoutingTableRouter extends ActiveRouter {
  13. private static final int ROUTING_TABLE_ENTRY_TTL = 255;
  14.  
  15. private List<RoutingTableEntry> routingTable = new ArrayList<>();
  16.  
  17. public List<RoutingTableEntry> getRoutingTable() {
  18. return routingTable;
  19. }
  20.  
  21. public RoutingTableRouter(Settings settings) {
  22. super(settings);
  23. }
  24.  
  25. protected RoutingTableRouter(RoutingTableRouter router) {
  26. super(router);
  27. }
  28.  
  29. @Override
  30. public void update() {
  31. super.update();
  32. if (isTransferring() || !canStartTransfer()) {
  33. return; // transferring, don't try other connections yet
  34. }
  35.  
  36. updateRoutingTable();
  37.  
  38. // Try first the messages that can be delivered to final recipient
  39. if (exchangeDeliverableMessages() != null) {
  40. return; // started a transfer, don't try others (yet)
  41. }
  42.  
  43. // then try any/all message to any/all connection
  44. this.tryAllMessagesToAllConnections();
  45. }
  46.  
  47. @Override
  48. public RoutingTableRouter replicate() {
  49. return new RoutingTableRouter(this);
  50. }
  51.  
  52. @Override
  53. protected Connection tryAllMessagesToAllConnections() {
  54. List<Connection> connections = getConnections();
  55. if (connections.size() == 0 || this.getNrofMessages() == 0) {
  56. return null;
  57. }
  58.  
  59. List<Message> messages = new ArrayList<Message>(this.getMessageCollection());
  60. this.sortByQueueMode(messages);
  61.  
  62. return tryMessagesToConnections(messages, connections);
  63. }
  64.  
  65. @Override
  66. protected Connection tryMessagesToConnections(List<Message> messages, List<Connection> connections) {
  67. for (int i = 0, n = connections.size(); i < n; i++) {
  68. Connection con = connections.get(i);
  69. DTNHost otherHost = con.getOtherNode(getHost());
  70. otherHost.getAddress();
  71.  
  72. Message started = tryAllMessagesIfOtherNodeKnowsDestination(con, messages);
  73. if (started != null) {
  74. return con;
  75. }
  76. }
  77.  
  78. return null;
  79. }
  80.  
  81. protected Message tryAllMessagesIfOtherNodeKnowsDestination(Connection con, List<Message> messages) {
  82. for (Message m : messages) {
  83. RoutingTableEntry shortestPathEntry = null;
  84.  
  85. for (RoutingTableEntry entry : routingTable) {
  86. if (entry.getDestinationAddress() == m.getTo().getAddress()
  87. && con.getOtherNode(getHost()).getAddress() == entry.getNextHop()) {
  88. if (shortestPathEntry == null
  89. || (shortestPathEntry != null && shortestPathEntry.getHopCount() > entry.getHopCount())) {
  90. shortestPathEntry = entry;
  91. }
  92. }
  93. }
  94.  
  95. if (shortestPathEntry != null) {
  96. int retVal = startTransfer(m, con);
  97. if (retVal == RCV_OK) {
  98. return m; // accepted a message, don't try others
  99. } else if (retVal > 0) {
  100. return null; // should try later -> don't bother trying
  101. // others
  102. }
  103. }
  104. }
  105.  
  106. return null; // no message was accepted
  107. }
  108.  
  109. private void updateRoutingTable() {
  110. List<Connection> connections = getConnections();
  111.  
  112. if (connections.size() == 0 || this.getNrofMessages() == 0) {
  113. return;
  114. }
  115.  
  116. List<Message> messages = new ArrayList<Message>(this.getMessageCollection());
  117. this.sortByQueueMode(messages);
  118.  
  119. for (Connection connection : connections) {
  120.  
  121. // Create rows about current connections
  122. RoutingTableEntry row = new RoutingTableEntry(connection.getOtherNode(getHost()).getAddress(),
  123. connection.getOtherNode(getHost()).getAddress(), 1, ROUTING_TABLE_ENTRY_TTL);
  124.  
  125. if (!routingTable.contains(row)) {
  126. routingTable.add(row);
  127. }
  128.  
  129. // Get routing tables from current connections
  130. List<RoutingTableEntry> otherHostRoutingTable = ((RoutingTableRouter) connection.getOtherNode(getHost())
  131. .getRouter()).routingTable;
  132.  
  133. for (RoutingTableEntry otherHostRow : otherHostRoutingTable) {
  134. if (otherHostRow.getNextHop() != getHost().getAddress()
  135. && otherHostRow.getDestinationAddress() != getHost().getAddress()) {
  136.  
  137. row = new RoutingTableEntry();
  138.  
  139. row.setDestinationAddress(otherHostRow.getDestinationAddress());
  140. row.setNextHop(connection.getOtherNode(getHost()).getAddress());
  141. row.setHopCount(otherHostRow.getHopCount() + 1);
  142. row.setConnectionTTL(ROUTING_TABLE_ENTRY_TTL);
  143.  
  144. if (!routingTable.contains(row)) {
  145. routingTable.add(row);
  146. }
  147. }
  148. }
  149. }
  150.  
  151. updateRoutesConnectionsTTL();
  152. }
  153.  
  154. private void updateRoutesConnectionsTTL() {
  155. Iterator<RoutingTableEntry> iterator = routingTable.iterator();
  156.  
  157. while (iterator.hasNext()) {
  158. RoutingTableEntry entry = iterator.next();
  159.  
  160. if (isRoutingTableEntryConnected(entry)) {
  161. entry.setConnectionTTL(ROUTING_TABLE_ENTRY_TTL);
  162. } else {
  163. entry.setConnectionTTL(entry.getConnectionTTL() - 1);
  164.  
  165. if (entry.getConnectionTTL() == 0) {
  166. iterator.remove();
  167. }
  168. }
  169. }
  170. }
  171.  
  172. private boolean isRoutingTableEntryConnected(RoutingTableEntry entry) {
  173. for (Connection connection : getConnections()) {
  174. if (connection.getOtherNode(getHost()).getAddress() == entry.getNextHop()) {
  175. return true;
  176. }
  177. }
  178.  
  179. return false;
  180. }
  181.  
  182. public class RoutingTableEntry {
  183. private int destinationAddress;
  184. private int nextHop;
  185. private int hopCount;
  186. private int connectionTTL;
  187.  
  188. public RoutingTableEntry() {
  189. }
  190.  
  191. public RoutingTableEntry(int destinationAddress, int nextHop, int hopCount, int connectionTTL) {
  192. this.destinationAddress = destinationAddress;
  193. this.nextHop = nextHop;
  194. this.hopCount = hopCount;
  195. this.connectionTTL = connectionTTL;
  196. }
  197.  
  198. public int getDestinationAddress() {
  199. return destinationAddress;
  200. }
  201.  
  202. public void setDestinationAddress(int destinationAddress) {
  203. this.destinationAddress = destinationAddress;
  204. }
  205.  
  206. public int getNextHop() {
  207. return nextHop;
  208. }
  209.  
  210. public void setNextHop(int nextHop) {
  211. this.nextHop = nextHop;
  212. }
  213.  
  214. public int getHopCount() {
  215. return hopCount;
  216. }
  217.  
  218. public void setHopCount(int hopCount) {
  219. this.hopCount = hopCount;
  220. }
  221.  
  222. public int getConnectionTTL() {
  223. return connectionTTL;
  224. }
  225.  
  226. public void setConnectionTTL(int connectionTTL) {
  227. this.connectionTTL = connectionTTL;
  228. }
  229.  
  230. @Override
  231. public String toString() {
  232. return "destinationAddress=" + destinationAddress + ", nextHop=" + nextHop + ", hopCount="
  233. + hopCount + ", connectionTTL=" + connectionTTL;
  234. }
  235.  
  236. @Override
  237. public int hashCode() {
  238. final int prime = 31;
  239. int result = 1;
  240. result = prime * result + destinationAddress;
  241. result = prime * result + nextHop;
  242. return result;
  243. }
  244.  
  245. @Override
  246. public boolean equals(Object obj) {
  247. if (this == obj)
  248. return true;
  249. if (obj == null)
  250. return false;
  251. if (getClass() != obj.getClass())
  252. return false;
  253. RoutingTableEntry other = (RoutingTableEntry) obj;
  254. if (destinationAddress != other.destinationAddress)
  255. return false;
  256. if (nextHop != other.nextHop)
  257. return false;
  258. return true;
  259. }
  260. }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement