Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package routing;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import core.Connection;
- import core.DTNHost;
- import core.Message;
- import core.Settings;
- public class RoutingTableRouter extends ActiveRouter {
- private static final int ROUTING_TABLE_ENTRY_TTL = 255;
- private List<RoutingTableEntry> routingTable = new ArrayList<>();
- public List<RoutingTableEntry> getRoutingTable() {
- return routingTable;
- }
- public RoutingTableRouter(Settings settings) {
- super(settings);
- }
- protected RoutingTableRouter(RoutingTableRouter router) {
- super(router);
- }
- @Override
- public void update() {
- super.update();
- if (isTransferring() || !canStartTransfer()) {
- return; // transferring, don't try other connections yet
- }
- updateRoutingTable();
- // Try first the messages that can be delivered to final recipient
- if (exchangeDeliverableMessages() != null) {
- return; // started a transfer, don't try others (yet)
- }
- // then try any/all message to any/all connection
- this.tryAllMessagesToAllConnections();
- }
- @Override
- public RoutingTableRouter replicate() {
- return new RoutingTableRouter(this);
- }
- @Override
- protected Connection tryAllMessagesToAllConnections() {
- List<Connection> connections = getConnections();
- if (connections.size() == 0 || this.getNrofMessages() == 0) {
- return null;
- }
- List<Message> messages = new ArrayList<Message>(this.getMessageCollection());
- this.sortByQueueMode(messages);
- return tryMessagesToConnections(messages, connections);
- }
- @Override
- protected Connection tryMessagesToConnections(List<Message> messages, List<Connection> connections) {
- for (int i = 0, n = connections.size(); i < n; i++) {
- Connection con = connections.get(i);
- DTNHost otherHost = con.getOtherNode(getHost());
- otherHost.getAddress();
- Message started = tryAllMessagesIfOtherNodeKnowsDestination(con, messages);
- if (started != null) {
- return con;
- }
- }
- return null;
- }
- protected Message tryAllMessagesIfOtherNodeKnowsDestination(Connection con, List<Message> messages) {
- for (Message m : messages) {
- RoutingTableEntry shortestPathEntry = null;
- for (RoutingTableEntry entry : routingTable) {
- if (entry.getDestinationAddress() == m.getTo().getAddress()
- && con.getOtherNode(getHost()).getAddress() == entry.getNextHop()) {
- if (shortestPathEntry == null
- || (shortestPathEntry != null && shortestPathEntry.getHopCount() > entry.getHopCount())) {
- shortestPathEntry = entry;
- }
- }
- }
- if (shortestPathEntry != null) {
- int retVal = startTransfer(m, con);
- if (retVal == RCV_OK) {
- return m; // accepted a message, don't try others
- } else if (retVal > 0) {
- return null; // should try later -> don't bother trying
- // others
- }
- }
- }
- return null; // no message was accepted
- }
- private void updateRoutingTable() {
- List<Connection> connections = getConnections();
- if (connections.size() == 0 || this.getNrofMessages() == 0) {
- return;
- }
- List<Message> messages = new ArrayList<Message>(this.getMessageCollection());
- this.sortByQueueMode(messages);
- for (Connection connection : connections) {
- // Create rows about current connections
- RoutingTableEntry row = new RoutingTableEntry(connection.getOtherNode(getHost()).getAddress(),
- connection.getOtherNode(getHost()).getAddress(), 1, ROUTING_TABLE_ENTRY_TTL);
- if (!routingTable.contains(row)) {
- routingTable.add(row);
- }
- // Get routing tables from current connections
- List<RoutingTableEntry> otherHostRoutingTable = ((RoutingTableRouter) connection.getOtherNode(getHost())
- .getRouter()).routingTable;
- for (RoutingTableEntry otherHostRow : otherHostRoutingTable) {
- if (otherHostRow.getNextHop() != getHost().getAddress()
- && otherHostRow.getDestinationAddress() != getHost().getAddress()) {
- row = new RoutingTableEntry();
- row.setDestinationAddress(otherHostRow.getDestinationAddress());
- row.setNextHop(connection.getOtherNode(getHost()).getAddress());
- row.setHopCount(otherHostRow.getHopCount() + 1);
- row.setConnectionTTL(ROUTING_TABLE_ENTRY_TTL);
- if (!routingTable.contains(row)) {
- routingTable.add(row);
- }
- }
- }
- }
- updateRoutesConnectionsTTL();
- }
- private void updateRoutesConnectionsTTL() {
- Iterator<RoutingTableEntry> iterator = routingTable.iterator();
- while (iterator.hasNext()) {
- RoutingTableEntry entry = iterator.next();
- if (isRoutingTableEntryConnected(entry)) {
- entry.setConnectionTTL(ROUTING_TABLE_ENTRY_TTL);
- } else {
- entry.setConnectionTTL(entry.getConnectionTTL() - 1);
- if (entry.getConnectionTTL() == 0) {
- iterator.remove();
- }
- }
- }
- }
- private boolean isRoutingTableEntryConnected(RoutingTableEntry entry) {
- for (Connection connection : getConnections()) {
- if (connection.getOtherNode(getHost()).getAddress() == entry.getNextHop()) {
- return true;
- }
- }
- return false;
- }
- public class RoutingTableEntry {
- private int destinationAddress;
- private int nextHop;
- private int hopCount;
- private int connectionTTL;
- public RoutingTableEntry() {
- }
- public RoutingTableEntry(int destinationAddress, int nextHop, int hopCount, int connectionTTL) {
- this.destinationAddress = destinationAddress;
- this.nextHop = nextHop;
- this.hopCount = hopCount;
- this.connectionTTL = connectionTTL;
- }
- public int getDestinationAddress() {
- return destinationAddress;
- }
- public void setDestinationAddress(int destinationAddress) {
- this.destinationAddress = destinationAddress;
- }
- public int getNextHop() {
- return nextHop;
- }
- public void setNextHop(int nextHop) {
- this.nextHop = nextHop;
- }
- public int getHopCount() {
- return hopCount;
- }
- public void setHopCount(int hopCount) {
- this.hopCount = hopCount;
- }
- public int getConnectionTTL() {
- return connectionTTL;
- }
- public void setConnectionTTL(int connectionTTL) {
- this.connectionTTL = connectionTTL;
- }
- @Override
- public String toString() {
- return "destinationAddress=" + destinationAddress + ", nextHop=" + nextHop + ", hopCount="
- + hopCount + ", connectionTTL=" + connectionTTL;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + destinationAddress;
- result = prime * result + nextHop;
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- RoutingTableEntry other = (RoutingTableEntry) obj;
- if (destinationAddress != other.destinationAddress)
- return false;
- if (nextHop != other.nextHop)
- return false;
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement