📄 routemanager.java
字号:
/*JAdhoc ver 0.11 - Java AODV (RFC 3561) Protocol HandlerCopyright 2003-2004 ComNets, University of BremenThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/package jadhoc.net;import java.net.*;import java.util.*;import jadhoc.*;import jadhoc.conf.*;import jadhoc.other.*;import jadhoc.minders.*;import jadhoc.os.*;import jadhoc.msg.*;/*** This class manages all the route environment manipulation* activities. All methods are syncronized as many threads* would be requesting services thru this object and only one* thread should manipulate the routing environment at a time.* The threads that manipulate the object are,* - packet listener* - Hello minder* - route minder (one for each active route)* - RREQID minder** In addition to these it also calls other objects to provide* services. These are,* - activity logging* - AODV message sender* - OS interace** @author : Asanga Udugama* @date : 28-jul-2003* @email : adu@comnets.uni-bremen.de**/public class RouteManager { public JAdhoc jadhoc; public ConfigInfo cfgInfo; public CurrentInfo curInfo; public RREQIDList idList; public RouteList rtList; public RouteDiscoveryList rdList; public boolean rtMgrActive; public PacketSender pktSender; public OSOperations osOps; public Thread pktListener; public RREQIDMinder idMinder; public HelloMinder helloMinder; /** * Constructor creates the UDP socket to be passed * to the sender and listener for AODV mesaging * @param JAdhoc ja - uses this object to pass * messages to the GUI * @param ConfigInfo cfg - uses to obtain config * info and to pass to other * objects */ public RouteManager(ConfigInfo cfg, CurrentInfo cur, JAdhoc ja) throws Exception { cfgInfo = cfg; curInfo = cur; jadhoc = ja; rtMgrActive = false; // create the collections for route list, RREQ ID list & the // Route Discovery list idList = new RREQIDList(cfgInfo, curInfo); rtList = new RouteList(cfgInfo, curInfo, jadhoc); rdList = new RouteDiscoveryList(cfgInfo, curInfo); // create message sender pktSender = new PacketSender(cfgInfo, curInfo); // create os ops osOps = new OSOperations(cfgInfo, curInfo); // create packet listener thread (based on OS) if(cfgInfo.osInUse.toLowerCase().equals(ConfigInfo.LINUX_OS)) { pktListener = new PacketListenerLinux(cfgInfo, curInfo, this); } else if(cfgInfo.osInUse.toLowerCase().equals(ConfigInfo.WINDOWS_OS)) { pktListener = new PacketListenerWindows(cfgInfo, curInfo, this); } else { pktListener = null; } // create hello minder helloMinder = new HelloMinder(cfgInfo, curInfo, this); // create RREQ ID minder idMinder = new RREQIDMinder(cfgInfo, curInfo, this); } /** * Method to start the Protocol Handler. Starting * is done by doing the following, * * - init the routing environment * - creates & starts the packet listener * - creates & starts the AODV msg listener * - creates & starts the hello minder * - creates and starts the RREQ ID minder */ public synchronized boolean startApplication() { try { if(rtMgrActive) return true; rtMgrActive = true; // start activity logging curInfo.log.start(); // start packet sender pktSender.start(); // initialize the routing environment osOps.initializeRouteEnvironment(0); // activate the route list rtList.start(osOps); // start packet listener thread pktListener.start(); // start hello sending thread helloMinder.start(); // start RREQ ID tracking thread idMinder.start(); // log curInfo.log.write(Logging.ACTIVITY_LOGGING, "Route Manager - Protocol Handler started"); } catch (Exception e) { // log curInfo.log.write(Logging.CRITICAL_LOGGING, "Route Manager - Problem in start - " + e); // show msg box jadhoc.displayError(e.toString()); return false; } return true; } /** * Method to stop the protocol handler. Does this * by stoping all the threads that were started * start function and cleaning up the routing * environment */ public synchronized boolean stopApplication() { try { if(!rtMgrActive) return true; rtMgrActive = false; // terminate all other threads // inform other nodes using precursor list // deactivate the route list rtList.stop(); // terminate the routing environment osOps.finalizeRouteEnvironment(); // stop activity logging curInfo.log.stop(); } catch (Exception e) { // log curInfo.log.write(Logging.CRITICAL_LOGGING, "Route Manager - Problem in stop - " + e); // show msg box jadhoc.displayError(e.toString()); return false; } return true; } /** * This method returns the current state of the route * manager. If the route manager has been stopped, this * will return false, else true. * * @return boolean - route manager state */ public synchronized boolean isRouteMgrActive() { return rtMgrActive; } /** * Method to check whether there exist atleast one * unexpired route. This is required by the Hello * message sender to determine whether to send Hello * messages or not. This method passes control to * the same named method in the route list. * @return boolean - true if atleast one unexpired * route else returns false */ public synchronized boolean doUnexpiredRoutesExist() { return rtList.doUnexpiredRoutesExist(); } /*--------------------------------------------------------------------------------------*/ // Route Minder interface // ---------------------------------------- // if thread = route minder AND route status = valid // delete route from os // route expiry staus = true // active minder = null (i.e. stop route minder) // if hop count not= 1 { // route status = invalid // set lifetime to DELETE_PERIOD // dest seq in route incremented by 1 // start route deleter // } // public synchronized int checkActiveRouteLifetime(InetAddress da, RouteMinder rtMinder) throws Exception { RouteEntry destRte; int lifetime; long currTime; destRte = rtList.get(da); if(destRte == null) return 0; // if for some reason the rtMinder is not the current thread that // is managing the route, then stop the thread if(destRte.activeMinder != rtMinder) { return 0; } // if for some reason the route has become invalid, then stop the thread if(destRte.routeStatusFlag != RouteEntry.ROUTE_STATUS_FLAG_VALID) { return 0; } currTime = (new Date()).getTime(); lifetime = (int) (destRte.expiryTime - currTime); if(lifetime <= 0) { // simply expire route (also start route // delete) destRte.routeStatusFlag = RouteEntry.ROUTE_STATUS_FLAG_INVALID; destRte.destSeqNum++; destRte.expiryTime = currTime + cfgInfo.deletePeriodVal; destRte.activeMinder = new DeleteMinder(cfgInfo, curInfo, this, destRte.destIPAddr, cfgInfo.deletePeriodVal); destRte.activeMinder.start(); rtList.update(da, destRte); return 0; } else return lifetime; } /*--------------------------------------------------------------------------------------*/ // Hello Receipt Minder interface // ---------------------------------------- // if hello not heard { // send RERRs to all dependents - // find dependents on this route (i.e. as next hop) // remove route from os // send RERR to all // route status of all = invalid // set lifetime to DELETE_PERIOD // start route deleters for all // // } /** * Method called by the HelloReceiptMinder thread to check whether a route is * expired due to not receiving hello messages. * * @param InetAddress da - the destination for which this minder is active * @param HelloReceiptMinder hrMinder - the minder that calls this method * @exception Exception - thrown in case of errors * @return int - returns the lifetime */ public synchronized int checkHelloReceived(InetAddress da, HelloReceiptMinder hrMinder) throws Exception { RouteEntry destRte; long currTime; destRte = rtList.get(da); if(destRte == null) return 0; if(destRte.helloReceiptMinder != hrMinder) return 0; if(destRte.hopCount > 1) return 0; currTime = (new Date()).getTime(); // if no hellos heard but route is active, then generate RERRs // to all precursors and expire all the destinations which are // reachable thru this route and also expire himself if(currTime > destRte.nextHelloReceiveTime && destRte.routeStatusFlag == RouteEntry.ROUTE_STATUS_FLAG_VALID) { invalidateDestinations(destRte); // remove the route destRte.routeStatusFlag = RouteEntry.ROUTE_STATUS_FLAG_INVALID; destRte.destSeqNum++; destRte.expiryTime = currTime + cfgInfo.deletePeriodVal; destRte.activeMinder = new DeleteMinder(cfgInfo, curInfo, this, destRte.destIPAddr, cfgInfo.deletePeriodVal); destRte.activeMinder.start(); rtList.update(da, destRte); return 0; } return (int) (destRte.nextHelloReceiveTime - currTime); } /** * Method to send RERRs dependent on a route that has expired and no hellos * have been heard. * * @RouteEntry destRte - the destination that is expiring * @return Exception - any errors * */ private synchronized void invalidateDestinations(RouteEntry destRte) throws Exception { LinkedList unreachableIPList; LinkedList unreachableSeqList; RouteEntry entry; Object array[]; int i, j; InetAddress adrList[]; int seqList[]; RERR rerr; array = rtList.getRouteArray(); // if RERR unicast, send a RERR to each precursor in a // invalidating route if(cfgInfo.RERRSendingModeVal == ConfigInfo.RERR_UNICAST_VAL) { adrList = new InetAddress[1]; seqList = new int[1]; for(i = 0; i < array.length; i++) { entry = (RouteEntry) array[i]; // send RERR only if the 'link break' (destRte) route // was the next hop if(!entry.destIPAddr.equals(destRte.destIPAddr) && entry.nextHopIPAddr.equals(destRte.destIPAddr)) { adrList[0] = entry.destIPAddr; seqList[0] = entry.destSeqNum; if(entry.validDestSeqNumFlag == RouteEntry.DEST_SEQ_FLAG_VALID) { entry.destSeqNum++; } // send RERR to each precursor for(j = 0; j < entry.precursorList.size(); j++) { rerr = new RERR(cfgInfo, curInfo, false, (InetAddress) entry.precursorList.get(j), (short) 1, false, (byte) 1, adrList, seqList); pktSender.sendMessage(rerr); } // invalidate route & start route delete entry.routeStatusFlag = RouteEntry.ROUTE_STATUS_FLAG_INVALID; entry.expiryTime = (new Date()).getTime() + cfgInfo.deletePeriodVal; entry.activeMinder = new DeleteMinder(cfgInfo, curInfo, this, entry.destIPAddr, cfgInfo.deletePeriodVal); entry.activeMinder.start(); rtList.update(entry.destIPAddr, entry); } } // send RERR to the precursors of the link broken route adrList[0] = destRte.destIPAddr; seqList[0] = destRte.destSeqNum; // send RERR to each precursor for(j = 0; j < destRte.precursorList.size(); j++) { rerr = new RERR(cfgInfo, curInfo, false, (InetAddress) destRte.precursorList.get(j), (short) 1, false, (byte) 1, adrList, seqList); pktSender.sendMessage(rerr); } // if RERR multicast, send one RERR with all the invalidating // destinations } else { unreachableIPList = new LinkedList(); unreachableSeqList = new LinkedList(); // collect all the destinations that become // invalid & start route delete for(i = 0; i < array.length; i++) { entry = (RouteEntry) array[i]; // collect dest only if the 'link break' (destRte) route // was the next hop if(!entry.destIPAddr.equals(destRte.destIPAddr) && entry.nextHopIPAddr.equals(destRte.destIPAddr)) { unreachableIPList.add(entry.destIPAddr); unreachableSeqList.add(new Integer(entry.destSeqNum)); // invalidate route & start route delete entry.routeStatusFlag = RouteEntry.ROUTE_STATUS_FLAG_INVALID; entry.expiryTime = (new Date()).getTime() + cfgInfo.deletePeriodVal; entry.activeMinder = new DeleteMinder(cfgInfo, curInfo, this, entry.destIPAddr, cfgInfo.deletePeriodVal); entry.activeMinder.start(); rtList.update(entry.destIPAddr, entry); } } // add the link broken IP address unreachableIPList.add(destRte.destIPAddr); unreachableSeqList.add(new Integer(destRte.destSeqNum)); if(unreachableIPList.size() > 0) { adrList = (InetAddress []) unreachableIPList.toArray(); seqList = new int[unreachableSeqList.size()]; for(i = 0; i < seqList.length; i++) { seqList[i] = ((Integer) unreachableSeqList.get(i)).intValue(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -