routeadvertisement.java
来自「JXTA™ is a set of open, generalize」· Java 代码 · 共 860 行 · 第 1/2 页
JAVA
860 行
/** * Remove the specified endpoint address to destination peer. * * @param addr EndpointAddress to add. */ public void removeDestEndpointAddress(EndpointAddress addr) { dest.removeEndpointAddress(addr); } /** * Remove the specified endpoint addresses from destination peer. * * @param addrs EndpointAddress to add. */ public void removeDestEndpointAddresses(Collection<EndpointAddress> addrs) { dest.removeEndpointAddresses(addrs); } /** * Remove a list of EndpointAddresses from the Route Destination * access point * * @param addresses vector of endpoint addresses to remove from the * destination access point. * @deprecated Use {@link #removeDestEndpointAddresses(Collection)}. */ @Deprecated public void removeDestEndpointAddresses(Vector<String> addresses) { dest.removeEndpointAddresses(addresses); } /** * Returns the endpoint addresses of the destination peer in their * preferred order. * * @return The {@code EndpointAddress}es of the destination peer. */ public List<EndpointAddress> getDestEndpointAddresses() { List<EndpointAddress> result = new ArrayList<EndpointAddress>(); Enumeration<String> eachEA = dest.getEndpointAddresses(); while (eachEA.hasMoreElements()) { result.add(new EndpointAddress(eachEA.nextElement())); } return result; } /** * Set the route destination endpoint addresses * * @param ea vector of endpoint addresses. Warning: The vector is not copied * and is used directly. * @deprecated Use {@link #addDestEndpointAddress(EndpointAddress)} instead. */ @Deprecated public void setDestEndpointAddresses(Vector<String> ea) { dest.setEndpointAddresses(ea); } /** * returns the list of hops * * @return Enumeration list of hops as AccessPointAdvertisement */ public Enumeration<AccessPointAdvertisement> getHops() { return hops.elements(); } /** * returns the list of hops * * @return Vector list of hops as AccessPointAdvertisement */ public Vector<AccessPointAdvertisement> getVectorHops() { return hops; } /** * Sets the list of hops associated with this route. * * @param newHops AccessPointAdvertisements which form the hops. The * Vector is <b>NOT</b> copied. */ public void setHops(Vector<AccessPointAdvertisement> newHops) { // It is legal to set it to null but it is automatically converted // to an empty vector. The member hops is NEVER null. if (null == newHops) { hops = new Vector<AccessPointAdvertisement>(); } else { for (AccessPointAdvertisement hop : newHops) { if (null == hop.getPeerID()) { throw new IllegalArgumentException("Bad hop"); } } hops = newHops; } } /** * Check if the route contains the following hop * * @param pid peer id of the hop * @return boolean true or false if the hop is found in the route */ public boolean containsHop(PeerID pid) { for (AccessPointAdvertisement hop : hops) { PeerID hid = hop.getPeerID(); if (pid.equals(hid)) { return true; } } return false; } /** * Returns the AccessPointAdvertisement of first hop. <b>The * AccessPointAdvertisement is <i>not</i> cloned.</b> * * @return AccessPointAdvertisement of first hop. */ public AccessPointAdvertisement getFirstHop() { return hops.isEmpty() ? null : hops.firstElement(); } /** * Sets the AccessPointAdvertisement for the first hop. <b>The * AccessPointAdvertisement is <i>not</i> cloned.</b> * * @param ap AccessPointAdvertisement of the first hop. */ public void setFirstHop(AccessPointAdvertisement ap) { if (null == ap.getPeerID()) { throw new IllegalArgumentException("Bad hop"); } hops.add(0, ap); } /** * Returns the access point for the last hop. <b>The * AccessPointAdvertisement is <i>not</i> cloned.</b> * * @return AccessPointAdvertisement last hop. */ public AccessPointAdvertisement getLastHop() { return hops.isEmpty() ? null : hops.lastElement(); } /** * Sets the AccessPointAdvertisement of the last hop. <b>The * AccessPointAdvertisement is <i>not</i> cloned.</b> * * @param ap AccessPointAdvertisement of the last hop. */ public void setLastHop(AccessPointAdvertisement ap) { if (null == ap.getPeerID()) { throw new IllegalArgumentException("Bad hop"); } hops.add(ap); } /** * 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. Set<PeerID> seenPeers = new HashSet<PeerID>(hops.size()); for (AccessPointAdvertisement anAPA : hops) { PeerID pid = anAPA.getPeerID(); if (seenPeers.contains(pid)) { return true; // There is a loop. } seenPeers.add(pid); } return false; } /** * return the length of the route * * @return int size of the route */ public int size() { return hops.size(); } /** * Return the hop that follows the specified currentHop. <b>The * AccessPointAdvertisement is <i>not</i> cloned.</b> * * @param currentHop PeerID of the current hop * @return ap AccessPointAdvertisement of the next Hop */ public AccessPointAdvertisement nextHop(PeerID currentHop) { // check if we have a real route if (hops.isEmpty()) { // Empty vector. return null; } // find the index of the route int index = 0; boolean found = false; for (AccessPointAdvertisement ap : hops) { if (currentHop.equals(ap.getPeerID())) { found = true; break; } index++; } AccessPointAdvertisement nextHop = null; if (!found) { // The peer is not into the list. Since we have got that message, // the best we can do is to send it to the first gateway in the // forward path. nextHop = hops.get(0); } else { // Found the peer within the vector of hops. Get the next hop. if (index < hops.size()) { nextHop = hops.get(index); } } 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() { StringBuilder routeBuf = new StringBuilder(); routeBuf.append("Dest APA : "); AccessPointAdvertisement dest = getDest(); routeBuf.append(dest.display()); routeBuf.append("\n"); int i = 1; Enumeration<AccessPointAdvertisement> e = getHops(); while (e.hasMoreElements()) { AccessPointAdvertisement hop = e.nextElement(); if (i == 1) { routeBuf.append("HOPS = "); } routeBuf.append("\n\t[").append(i++).append("] "); routeBuf.append(hop.display()); } 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) { Iterator<AccessPointAdvertisement> eachHop = hops.iterator(); while (eachHop.hasNext()) { AccessPointAdvertisement hop = eachHop.next(); PeerID hid = hop.getPeerID(); if (pid.equals(hid)) { eachHop.remove(); return true; } } return false; } /** * 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 (AccessPointAdvertisement hop : hops) { PeerID hid = hop.getPeerID(); if (pid.equals(hid)) { return hop.clone(); } } return 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. * public static boolean stichRoute(RouteAdvertisement newRoute, * * @param newRoute the new route * @param firstLeg the first route * @return true if successful */ 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. * * @param newRoute the new route * @param firstLeg first hop * @param localPeer local PeerID * @return true if successful */ public static boolean stichRoute(RouteAdvertisement newRoute, RouteAdvertisement firstLeg, PeerID localPeer) { if (newRoute.hasALoop()) { return false; } Vector<AccessPointAdvertisement> 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 loop from the root cleanupLoop(newRoute, localPeer); return true; } /** * Remove loops from the route advertisement * by shortcutting cycle from the route * * @param route the route advertisement * @param localPeer local PeerID */ 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<AccessPointAdvertisement> hops = route.getVectorHops(); Vector<AccessPointAdvertisement> newHops = new Vector<AccessPointAdvertisement>(hops.size()); AccessPointAdvertisement lastHop = null; // Replace all by PID-only entries, but keep the last hop on the side. if (!hops.isEmpty()) { lastHop = hops.get(hops.size() - 1); } hops = (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.get(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(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 + =
减小字号Ctrl + -
显示快捷键?