📄 routetable.java
字号:
package dsr;import java.net.*;import java.util.*;//this class implements the route table functionpublic class RouteTable { static Vector routingTable ; //vector of route table entries //constructor RouteTable() { routingTable = new Vector(3,2); } //prints out the routing table info public static void PrintRouteTable() { Iterator r = routingTable.iterator(); System.out.println("************************************************************************"); System.out.println("The entries in the routing table are"); System.out.println("************************************************************************"); while (r.hasNext()) { System.out.println(((RouteTableEntry)r.next()).toString()); } System.out.println("************************************************************************"); } //checks to see if there is an entry in the routing table that matches this interface entry public static boolean IfEntryPresentInRouteTable(InterfaceListEntry tmpEntry){ Iterator r = routingTable.iterator(); while (r.hasNext()){ RouteTableEntry tmpRoute = (RouteTableEntry)r.next(); if (tmpEntry.ipAddress.equals(tmpRoute.dstIP) && tmpRoute.selfRoute == 1) return true; } return false; } //adds a routing entry into the route table public static void AddRouteTableEntry(RouteTableEntry tmpEntry) { routingTable.addElement(new RouteTableEntry(tmpEntry)); } //gets the first element in the route table public static RouteTableEntry GetFirstRouteTableEntry() { RouteTableEntry tmpEntry = new RouteTableEntry(); tmpEntry = (RouteTableEntry)routingTable.firstElement(); return tmpEntry; } //finds an entry in the table which maches the given destination ip address //returns the position in the routing table if found otherwise returns -1 static public int FindRouteTableEntry(InetAddress IP) { RouteTableEntry tmpEntry = new RouteTableEntry(); for (int i = 0; i < routingTable.size(); i++ ) { tmpEntry = (RouteTableEntry)routingTable.elementAt(i); if (tmpEntry.dstIP.equals(IP)){ return i; } } return -1; } //deletes the routing table entry at this particular position public static void DeleteRouteTableEntry(int pos) { routingTable.remove(pos); } //get the entry at this particular position public static RouteTableEntry getThisEntry(int i) { return (new RouteTableEntry((RouteTableEntry)routingTable.elementAt(i))); } //this function updates the routing table for the reverse route. //this is called when an RREQ packet is arrived. So we update the table for the address //where the RREQ packet was generated and from where it was immediately received. //so for every RREQ packet receive two entries might be inserted/updated in the route table public static void updateReverseRouteTableEntry(EventQueueEntry eEntry){ //the revrse path will only be updated when there is an RREQ packet. RREQPacket rreq = new RREQPacket(); rreq = rreq.decode(eEntry.packet); //take care of the current hop rreq.hopCount++; //update the reverse route from the RREQ int pos = FindRouteTableEntry(rreq.srcIPAddr); if (pos == -1) { //no valid entry was found RouteTableEntry tmpEntry = new RouteTableEntry(rreq.srcIPAddr, eEntry.srcIP, rreq.hopCount, (byte)0); AddRouteTableEntry(tmpEntry); } //code needs to be updated to incorporate lifetime of routes. after a given time the //route is no longer valid. right now. else { RouteTableEntry tmpEntry = getThisEntry(pos); if (tmpEntry.hopCount > rreq.hopCount) AddRouteTableEntry(new RouteTableEntry(rreq.srcIPAddr, eEntry.srcIP, rreq.hopCount, (byte)0)); } //update the reverse route for the host from where this packet was obtained pos = FindRouteTableEntry(eEntry.srcIP); if (pos == -1){ AddRouteTableEntry(new RouteTableEntry(eEntry.srcIP, eEntry.srcIP, (byte)1, (byte)0)); } }}/*class PrecursorEntry { int preIP; public PrecursorEntry () {}; public PrecursorEntry(int IP) { preIP = IP;} public PrecursorEntry(PrecursorEntry rhs) {this.preIP = rhs.preIP;}}public class RouteTable { private final int ACTIVE_ROUTE_TIMEOUT = 3000; private final int HELLO_INTERVAL = 1000; Vector routingTable; public void AddRouteTableEntry(RouteTableEntry tmpEntry) { routingTable.addElement(new RouteTableEntry(tmpEntry)); } public RouteTableEntry GetFirstRouteTableEntry() { RouteTableEntry tmpEntry = new RouteTableEntry(); tmpEntry = (RouteTableEntry)routingTable.firstElement(); return tmpEntry; } public void FindInactiveRouteTableEntries() { RouteTableEntry tmpEntry = new RouteTableEntry(); Date currTime = new Date(); for (int i = 0; i < routingTable.size(); i++ ) { tmpEntry = (RouteTableEntry)routingTable.elementAt(i); if (tmpEntry.lifetime < currTime.getTime()) { if (tmpEntry.hopCount >= 255) { routingTable.remove(i); //code to delete the actual kernel route entry as well //code to delete the precursor entries. } else { if (tmpEntry.nextHop == tmpEntry.dstIP) { linkBreak(tmpEntry.dstIP); } else { routeExpiry(tmpEntry); } } } } } public int findMax(int i, int j) { return ((i > j)? i : j); } public void routeExpiry(RouteTableEntry tmpEntry) { tmpEntry.lastHopCount = tmpEntry.hopCount; tmpEntry.hopCount = (byte)255; Date currTime = new Date(); int DELETE_PERIOD = 5*findMax(ACTIVE_ROUTE_TIMEOUT, HELLO_INTERVAL); tmpEntry.lifetime = currTime.getTime() + DELETE_PERIOD; //code to delete the kernel entry for this route. } public RouteTableEntry FindRouteTableEntry(int IP) { RouteTableEntry tmpEntry = new RouteTableEntry(); for (int i = 0; i < routingTable.size(); i++ ) { tmpEntry = (RouteTableEntry)routingTable.elementAt(i); if (tmpEntry.dstIP == IP) return tmpEntry; } return null; } public boolean DeleteRouteTableEntry(int IP) { RouteTableEntry tmpEntry = new RouteTableEntry(); if ((tmpEntry = FindRouteTableEntry(IP)) != null) { routingTable.remove(tmpEntry); //code for deleting kernel routing table entry //code to delete the precursor entries return true; } else return false; } public void linkBreak(int IP) { //delete the precursor entries for this IP in the routing table DeletePrecursorEntriesWithThisIP(IP); RouteTableEntry tmpEntry = new RouteTableEntry(); for (int i = 0; i < routingTable.size(); i++ ) { tmpEntry = (RouteTableEntry)routingTable.elementAt(i); if (tmpEntry.nextHop == IP && tmpEntry.hopCount != 255) { routeExpiry(tmpEntry); //create a rerr packet and broadcast it. DeleteAllPrecursorsFromThisRoutingTableEntry(tmpEntry); } } } public void DeletePrecursorEntriesWithThisIP(int IP) { RouteTableEntry tmpEntry = new RouteTableEntry(); for (int i = 0; i < routingTable.size(); i++) { tmpEntry = (RouteTableEntry)routingTable.elementAt(i); PrecursorEntry tmpPreEntry = new PrecursorEntry(); if ((tmpPreEntry = FindPrecursorEntry(tmpEntry, IP)) != null) tmpEntry.precursors.remove(tmpPreEntry); } } public PrecursorEntry FindPrecursorEntry(RouteTableEntry tmpEntry, int IP) { PrecursorEntry tmpPreEntry = new PrecursorEntry(); for (int i=0; i < tmpEntry.precursors.size(); i++) { tmpPreEntry = (PrecursorEntry)tmpEntry.precursors.elementAt(i); if (tmpPreEntry.preIP == IP) return (tmpPreEntry); } return null; } public void DeleteAllPrecursorsFromThisRoutingTableEntry(RouteTableEntry tmpEntry) { tmpEntry.precursors.clear(); }}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -