📄 routecm.java
字号:
RouteAdvertisement route = EndpointUtils.extractRouteAdv(padv); // Publish the route if it was previously unknown. if (!result.contains(route)) { // We found a new route just publish it locally try { // XXX 20060106 bondolo These publication values won't be obeyed if // the route had been previously published. //FIXME by hamada: This operation may lead to overwriting an existing and valid rout adv, no? discovery.publish(route, DEFAULT_EXPIRATION, DEFAULT_EXPIRATION); } catch (IOException failed) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Failed publishing route", failed); } } result.add(route); } } return result.iterator(); } /** * Create a new persistent route to the cache only if we can find set of * endpoint addresses * * @param route to be published */ protected void createRoute(RouteAdvertisement route) { DiscoveryService discovery; // check if CM is used if (!useCM) { return; } else { discovery = group.getDiscoveryService(); if (null == discovery) { return; } } if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("try to publish route "); } // we need to retrieve the current adv to get all the known // endpoint addresses try { RouteAdvertisement newRoute = (RouteAdvertisement) AdvertisementFactory.newAdvertisement(RouteAdvertisement.getAdvertisementType()); PeerID pId = route.getDestPeerID(); String realPeerID = pId.toString(); // check first if we have a route advertisement Enumeration<Advertisement> advs = discovery.getLocalAdvertisements(DiscoveryService.ADV, RouteAdvertisement.DEST_PID_TAG, realPeerID); if (!advs.hasMoreElements()) { // No route, sorry if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("could not find a route advertisement " + realPeerID); } return; } // make sure we are returning the longest route we know either // from the peer or route advertisement Advertisement adv = advs.nextElement(); if (adv instanceof RouteAdvertisement) { RouteAdvertisement dest = (RouteAdvertisement) adv; newRoute.setDest(dest.getDest()); } // let's get the endpoint addresses for each hops Vector<AccessPointAdvertisement> newHops = new Vector<AccessPointAdvertisement>(); Enumeration<AccessPointAdvertisement> e = route.getHops(); while (e.hasMoreElements()) { AccessPointAdvertisement ap = e.nextElement(); realPeerID = ap.getPeerID().toString(); // check first if we have a route advertisement advs = discovery.getLocalAdvertisements(DiscoveryService.ADV, RouteAdvertisement.DEST_PID_TAG, realPeerID); if (!advs.hasMoreElements()) { // No route, sorry if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("could not find a route advertisement for hop " + realPeerID); } return; } adv = advs.nextElement(); // ensure it is a RouteAdvertisement if (adv instanceof RouteAdvertisement) { newHops.add(((RouteAdvertisement) adv).getDest()); } } // last check to see that we have a route if (newHops.isEmpty()) { return; } newRoute.setHops(newHops); if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("publishing new route \n" + newRoute.display()); } lruCache.put(route.getDestPeerID(), route); // XXX 20060106 bondolo These publication values won't be obeyed if // the route had been previously published. discovery.publish(newRoute, DEFAULT_EXPIRATION, DEFAULT_EXPIRATION); } catch (Exception ex) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "error publishing route" + route.display(), ex); } } } /** * Publish a route advertisement to the CM * * @param route advertisement to be published */ protected void publishRoute(RouteAdvertisement route) { DiscoveryService discovery; // check if CM is in used, if not nothing to do if (!useCM) { return; } else { discovery = group.getDiscoveryService(); if (null == discovery) { return; } } if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Publishing route for " + route.getDestPeerID()); } // publish route adv if (!lruCache.contains(route.getDestPeerID())) { try { // XXX 20060106 bondolo These publication values won't be obeyed if // the route had been previously published. discovery.publish(route, DEFAULT_EXPIRATION, DEFAULT_EXPIRATION); } catch (Exception ex) { if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) { LOG.log(Level.SEVERE, "error publishing route adv \n" + route, ex); } } } lruCache.put(route.getDestPeerID(), route); } /** * flush route adv from CM * * @param peerID the PeerID */ protected void flushRoute(ID peerID) { DiscoveryService discovery; // check if CM is in used, if not nothing to do if (!useCM) { return; } else { discovery = group.getDiscoveryService(); if (null == discovery) { return; } } // leqt's remove any advertisements (route, peer) related to this peer // this should force a route query to try to find a new route // check first if we have a route advertisement String peerIDStr = peerID.toString(); Enumeration<Advertisement> advs = null; // Flush the local route advertisements for the peer. try { advs = discovery.getLocalAdvertisements(DiscoveryService.ADV, RouteAdvertisement.DEST_PID_TAG, peerIDStr); } catch (IOException failed) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Failure recovering route advertisements.", failed); } } while ((null != advs) && advs.hasMoreElements()) { Advertisement adv = advs.nextElement(); if (!(adv instanceof RouteAdvertisement)) { continue; } // ok so let's delete the advertisement try { discovery.flushAdvertisement(adv); if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("removed RouteAdvertisement for " + peerIDStr); } } catch (IOException ex) {// protect against flush IOException when the entry is not there } } // Flush the local peer advertisements for the peer. advs = null; try { advs = discovery.getLocalAdvertisements(DiscoveryService.PEER, "PID", peerIDStr); } catch (IOException failed) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Failed discovering peer advertisements for " + peerIDStr, failed); } } while ((null != advs) && advs.hasMoreElements()) { Advertisement adv = advs.nextElement(); if (!(adv instanceof PeerAdvertisement)) { continue; } // ok so let's delete the advertisement try { discovery.flushAdvertisement(adv); if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("removed PeerAdvertisement for " + peerIDStr); } } catch (IOException ex) {// protect against flush IOException when the entry is not there } } // remove it from the cache as well lruCache.remove(peerID); } /** * publish or update new route from the advertisement cache * * @param route to be published or updated * @return boolean true or false if adv cache was updated */ protected boolean updateRoute(RouteAdvertisement route) { DiscoveryService discovery; // check if CM is in used if (!useCM) { return false; } else { discovery = group.getDiscoveryService(); if (null == discovery) { return true; } } try { String realPeerID = route.getDestPeerID().toString(); // check first if we have a route advertisement Enumeration<Advertisement> advs = discovery.getLocalAdvertisements(DiscoveryService.ADV, RouteAdvertisement.DEST_PID_TAG, realPeerID); if (advs.hasMoreElements()) { Advertisement adv = advs.nextElement(); if (adv instanceof RouteAdvertisement) { RouteAdvertisement oldRouteAdv = (RouteAdvertisement) adv; // check if the old route is equal to the new route if (!route.equals(oldRouteAdv)) { // publish the new route // XXX 20060106 bondolo These publication values won't be obeyed if // the route had been previously published. discovery.publish(route, DEFAULT_EXPIRATION, DEFAULT_EXPIRATION); lruCache.put(route.getDestPeerID(), route); return true; } } } else { // publish the new route discovery.publish(route, DEFAULT_EXPIRATION, DEFAULT_EXPIRATION); lruCache.put(route.getDestPeerID(), route); return true; } } catch (Exception e) { if (Logging.SHOW_FINE && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, " failure to publish route advertisement response", e); } } return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -