?? routeadvertisement.java
字號:
if (hid == null) { continue; //may be null } if (pid.equals(hid)) { return true; } } return false; } /** * check if the route has a loop * * @return boolean true or false if the route has a loop */ public boolean hasALoop() { // Now check for any other potential loops. Vector peers = new Vector(); for (int i=0; i < hops.size(); ++i) { try { PeerID pid = ((AccessPointAdvertisement) hops.elementAt(i)).getPeerID(); if (pid == null) return true; //bad route if (peers.contains(pid)) { // This is a loop. return true; } else { peers.add(pid); } } catch (Exception ez1) { return true; } } return false; } /** * return the length of the route * * @return int size of the route */ public int size() { return hops.size(); } /** * get the nexthop after the given hop * * @param pid PeerID of the current hop * @return ap AccessPointAdvertisement of the next Hop */ public AccessPointAdvertisement nextHop(PeerID pid) { AccessPointAdvertisement nextHop = null; // check if we have a real route if ((hops == null) || (hops.size() == 0)) { // Empty vector. return null; } // find the index of the route int index = 0; boolean found = false; for (Enumeration e = hops.elements(); e.hasMoreElements();) { AccessPointAdvertisement ap = (AccessPointAdvertisement) e.nextElement(); if (pid.toString().equals(ap.getPeerID().toString())) { found = true; break; } index++; } // check if we found the local peer within the vector if (!found) { // The peer is not into the vector. Since we have got that // message, the best we can do is to send it to the first gateway // in the forward path. try { nextHop = (AccessPointAdvertisement) hops.elementAt(0); } catch (Exception ez1) { // Should not fail, but if it does, there is not much we can do return null; } return nextHop; } // Found the peer within the vector of hops. Get the next // hop try { nextHop = (AccessPointAdvertisement) hops.elementAt(index + 1); } catch (Exception ez1) { // There is no next hop return null; } return nextHop; } /** * Generate a string that displays the route * information for logging or debugging purpose * * @return String return a string containing the route info */ public String display() { StringBuffer routeBuf = new StringBuffer(); routeBuf.append( "Route to PID=" ); PeerID peerId = getDest().getPeerID(); // XXX Sometimes the embedded APA doesn't have the peer id set. if (peerId == null) { peerId = destPeer; } if (peerId == null) { routeBuf.append("<null>"); } else routeBuf.append(peerId.toString()); Iterator each = getDest().getVectorEndpointAddresses().iterator(); while (each.hasNext()) { try { routeBuf.append( "\n Addr=" + (String) each.next() ); } catch (ClassCastException ex) { routeBuf.append( "\n Addr=bad address"); } } int i = 1; Enumeration e = getHops(); while( e.hasMoreElements() ) { if (i == 1) { routeBuf.append( "\n Gateways = " ); } peerId = ((AccessPointAdvertisement) e.nextElement()).getPeerID(); if (peerId == null) routeBuf.append("Null Hop"); else routeBuf.append( "\n\t[" + i++ + "] " + peerId); } return routeBuf.toString(); } /** * remove a hop from the list of hops * * @param pid peer id of the hop * @return boolean true or false if the hop is found in the route */ public boolean removeHop(PeerID pid) { // FIXME: This is ridiculous, hops is a vector. We can remove // any item, we do not have to through the enum copying items 1 by 1. Vector newHops = new Vector(); for (Enumeration e = hops.elements(); e.hasMoreElements();) { AccessPointAdvertisement hop = (AccessPointAdvertisement) e.nextElement(); PeerID hid = hop.getPeerID(); if (hid != null) { if (pid.toString().equals(hid.toString())) continue; } // add the other one newHops.add(hop); } setHops(newHops); return true; } /** * return a hop from the list of hops * * @param pid peer id of the hop * @return accesspointadvertisement of the corresponding hop */ public AccessPointAdvertisement getHop(PeerID pid) { for (Enumeration e = hops.elements(); e.hasMoreElements();) { AccessPointAdvertisement hop = (AccessPointAdvertisement) e.nextElement(); PeerID hid = hop.getPeerID(); if (hid != null) { if (pid.toString().equals(hid.toString())) return (AccessPointAdvertisement) hop.clone(); } } return null; } /** * replace a hop from the list of hops * * @param ap accesspointadvertisement of the hop to replace */ public void replaceHop(AccessPointAdvertisement ap) { int index = 0; for (Enumeration e = hops.elements(); e.hasMoreElements();) { AccessPointAdvertisement hop = (AccessPointAdvertisement) e.nextElement(); PeerID hid = hop.getPeerID(); if (hid != null) { if (ap.getPeerID().toString().equals(hid.toString())) { hops.setElementAt(ap, index); return; } } } } /** * Add a new endpointaddress to a hop * * @param pid id of the hop * @param addr new endpoint address to add */ public void addEndpointAddressToHop(PeerID pid, EndpointAddress addr) { Vector ea = new Vector(); ea.add(addr.toString()); AccessPointAdvertisement oldHop = getHop(pid); if (oldHop != null && !oldHop.contains(addr)) { oldHop.addEndpointAddresses(ea); replaceHop(oldHop); } } /** * remove an endpointaddress to a hop * * @param pid id of the hop * @param addr new endpoint address to remove */ public void removeEndpointAddressToHop(PeerID pid, EndpointAddress addr) { Vector ea = new Vector(); ea.add(addr.toString()); AccessPointAdvertisement oldHop = getHop(pid); if (oldHop != null && !oldHop.contains(addr)) { oldHop.removeEndpointAddresses(ea); if (oldHop.size() > 0) // we still have some endpoint addresses replaceHop(oldHop); else removeHop(pid); } } /** * Return hop of the route at location index in the hops list * * @param index in the list of hops * @return hop AccessPointAdvertisement of the hops */ public AccessPointAdvertisement getHop(int index) { if (index < 0) return null; if (index > hops.size() - 1) return null; return (AccessPointAdvertisement) ((AccessPointAdvertisement)hops.elementAt(index)).clone(); } /** * construct a new route * <p/><b>WARNING hops may be MODIFIED.</b> **/ public static RouteAdvertisement newRoute(PeerID destPid, PeerID firsthop, Vector hops) { RouteAdvertisement route = (RouteAdvertisement) AdvertisementFactory.newAdvertisement( RouteAdvertisement.getAdvertisementType()); // set the route destination AccessPointAdvertisement ap = (AccessPointAdvertisement) AdvertisementFactory.newAdvertisement( AccessPointAdvertisement.getAdvertisementType()); if (destPid == null) return null; // messed up destination ap.setPeerID(destPid); route.setDest(ap); // set the route hops for (Enumeration e = hops.elements(); e.hasMoreElements();) { ap = (AccessPointAdvertisement) e.nextElement(); if (ap.getPeerID() == null) return null; // bad route } route.setHops(hops); // check if the given first hop is already in the route if not add it // (note: we do not expect it to be there, but it is acceptable). if (firsthop != null) { ap = route.getFirstHop(); if (ap == null || ! ap.getPeerID().equals(firsthop)) { ap = (AccessPointAdvertisement) AdvertisementFactory.newAdvertisement( AccessPointAdvertisement.getAdvertisementType()); ap.setPeerID(firsthop); route.setFirstHop(ap); } } return route; } /** * construct a new route, all hops are in the hops parameter. **/ public static RouteAdvertisement newRoute(PeerID destPid, Vector hops) { return newRoute(destPid, null, hops); } /** * Alter the given newRoute (which does not start from here) by using firstLeg, a known route to whence * it starts from. So that the complete route goes from here to the end-destination via firstLeg. * public static boolean stichRoute(RouteAdvertisement newRoute, **/ public static boolean stichRoute(RouteAdvertisement newRoute, RouteAdvertisement firstLeg) { return stichRoute(newRoute, firstLeg, null); } /** * Alter the given newRoute (which does not start from here) by using firstLeg, a known route to whence ** it starts from. So that the complete route goes from here to the end-destination via firstLeg ** also shortcut the route by removing the local peer **/ public static boolean stichRoute(RouteAdvertisement newRoute, RouteAdvertisement firstLeg, PeerID localPeer) { if ( newRoute.hasALoop() ) return false; Vector hops = newRoute.getVectorHops(); // Make room hops.ensureCapacity(firstLeg.getVectorHops().size() + 1 + hops.size()); // prepend the routing peer unless the routing peer happens to be // in the route already. That happens if the routing peer is the relay. // or if the route does not have a first leg PeerID routerPid = firstLeg.getDest().getPeerID(); if (newRoute.size() == 0 || (! newRoute.getFirstHop().getPeerID().equals(routerPid))) { AccessPointAdvertisement ap = (AccessPointAdvertisement) AdvertisementFactory.newAdvertisement( AccessPointAdvertisement.getAdvertisementType()); // prepend the route with the routing peer. ap.setPeerID(routerPid); hops.add(0, ap); } // prepend the rest of the route hops.addAll(0, firstLeg.getVectorHops()); // remove any llop from the root cleanupLoop(newRoute, localPeer); return true; } /** * Remove loops from the route advertisement * by shortcuting cycle from the route **/ public static void cleanupLoop(RouteAdvertisement route, PeerID localPeer) { // Note: we cleanup all enp addresses except for the last hop (which // we use to shorten routes often enough). // If we end-up removing the last hop, it means that it is the // local peer and thus the route ends up with a size 0. Vector hops = route.getVectorHops(); Vector newHops = new Vector(hops.size()); Object lastHop = null; // Replace all by PID-only entries, but keep the last hop on the side. if (hops.size() > 0) { lastHop = hops.elementAt(hops.size() - 1); } hops = ((RouteAdvertisement) route.cloneOnlyPIDs()).getVectorHops(); // remove cycle from the route for (int i=0; i < hops.size(); i++) { int loopAt = newHops.indexOf(hops.elementAt(i)); if (loopAt != -1) { // we found a cycle // remove all entries after loopAt for (int j = newHops.size(); --j > loopAt;) { newHops.remove(j); } } else { // did not find it so we add it newHops.add(hops.elementAt(i)); } } // Remove the local peer in the route if we were given one if (localPeer != null) { for (int i = newHops.size(); --i >= 0;) { if (localPeer.equals(((AccessPointAdvertisement) newHops.elementAt(i)).getPeerID())) { // remove all the entries up to that point we // need to keep the remaining of the route from that // point for (int j = 0; j <= i; j++) { newHops.remove(0); } break; } } } if (lastHop != null && newHops.size() > 0) { newHops.setElementAt(lastHop, newHops.size() - 1); } // update the new hops in the route route.setHops(newHops); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -