⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 routecm.java

📁 jxta_src_2.41b jxta 2.41b 最新版源码 from www.jxta.org
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            Enumeration advs = discovery.getLocalAdvertisements(DiscoveryService.PEER, "PID", realPeerID);                        // extract the Route Advertisement            while (advs.hasMoreElements()) {                PeerAdvertisement adv = (PeerAdvertisement) advs.nextElement();                                // Get its EndpointService advertisement                XMLElement endpParam = (XMLElement) adv.getServiceParam(PeerGroup.endpointClassID);                                if (endpParam == null) {                    if (LOG.isEnabledFor(Level.DEBUG)) {                        LOG.debug("no Endpoint Params");                    }                    continue;                }                                // get the Route Advertisement element                Enumeration paramChilds = endpParam.getChildren(RouteAdvertisement.getAdvertisementType());                XMLElement param = null;                                if (paramChilds.hasMoreElements()) {                    param = (XMLElement) paramChilds.nextElement();                } else {                    if (LOG.isEnabledFor(Level.DEBUG)) {                        LOG.debug("no Route Adv in Peer Adv");                    }                    continue;                }                                // build the new route and publish it                try {                    RouteAdvertisement route = (RouteAdvertisement) AdvertisementFactory.newAdvertisement(param);                                        route.setDestPeerID(adv.getPeerID());                    if (!result.contains(route)) {                                                // We get a new route just publish it locally                        try {                            discovery.publish(route, DEFAULT_EXPIRATION, DEFAULT_EXPIRATION);                        } catch (IOException failed) {                            if (LOG.isEnabledFor(Level.DEBUG)) {                                LOG.debug("Failed publishing route");                            }                        }                                                result.add(route);                    }                } catch (Exception ex) {                    if (LOG.isEnabledFor(Level.DEBUG)) {                        LOG.debug("Error processing route from padv", ex);                    }                }            }        } catch (Exception e2) {            if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("  failed with ", e2);            }        }                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) {                // check if CM is used        if (!useCM) {            return;        }                if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("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 advs = discovery.getLocalAdvertisements(DiscoveryService.ADV, "DstPID", realPeerID);            if ((advs == null) || (!advs.hasMoreElements())) {                // No route, sorry                if (LOG.isEnabledFor(Level.DEBUG)) {                    LOG.debug("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 = (Advertisement) advs.nextElement();            if (adv instanceof RouteAdvertisement)  {                RouteAdvertisement dest = (RouteAdvertisement) adv;                newRoute.setDest((AccessPointAdvertisement) dest.getDest());            }            // let's get the endpoint addresses for each hops            Vector newHops = new Vector();            for (Enumeration e = route.getHops(); e.hasMoreElements();) {                AccessPointAdvertisement ap = (AccessPointAdvertisement) e.nextElement();                realPeerID = ap.getPeerID().toString();                // check first if we have a route advertisement                advs = discovery.getLocalAdvertisements(DiscoveryService.ADV, "DstPID", realPeerID);                if ((advs == null) || (!advs.hasMoreElements())) {                    // No route, sorry                    if (LOG.isEnabledFor(Level.DEBUG)) {                        LOG.debug("could not find a route advertisement for hop " + realPeerID);                    }                    return;                }                adv = (Advertisement) advs.nextElement();                //  Can't always assume only RouteAdvertisement define DstPID                if (adv instanceof RouteAdvertisement)  {                    newHops.add( ((RouteAdvertisement) adv).getDest());                }            }            // last check to see that we have a route            if (newHops.size() == 0) {                return;            }            newRoute.setHops(newHops);            if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("publishing new " + newRoute.display());            }            discovery.publish(newRoute, DEFAULT_EXPIRATION, DEFAULT_EXPIRATION);        } catch (Exception ex) {            if (LOG.isEnabledFor(Level.WARN)) {                LOG.warn("error publishing route" + route.display(), ex);            }        }    }        /**     *  Publish a route advertisement to the CM     *     *  @param route advertisement to be published     **/    protected void publishRoute(RouteAdvertisement route) {                // check if CM is in used, if not nothing to do        if (!useCM) {            return;        }                if (route == null) {            if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("No publishing null route argument");            }            return;        }                if (LOG.isEnabledFor(Level.DEBUG)) {            LOG.debug("Publishing route for " + route.getDestPeerID());        }                // publish route adv        try {            discovery.publish(route, DEFAULT_EXPIRATION, DEFAULT_EXPIRATION);        } catch (Exception ex) {            if (LOG.isEnabledFor(Level.ERROR)) {                LOG.error("error publishing route adv \n" + route, ex);            }        }    }        /**     * flush route adv from CM     *     * @param addr  endpoint address     */    protected void flushRoute(EndpointAddress addr) {                // check if CM is in used, if not nothing to do        if (!useCM) {            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 realPeerID;                try {            URI asUri = new URI(ID.URIEncodingName, ID.URNNamespace + ":" + addr.getProtocolAddress(), null);                        realPeerID = asUri.toString();        } catch (URISyntaxException ex) { // ok if not there.            if (LOG.isEnabledFor(Level.WARN)) {                LOG.warn("Bad peer id : " + addr, ex);            }                        return;        }        try {            Enumeration advs = discovery.getLocalAdvertisements(DiscoveryService.ADV, "DstPID", realPeerID);            if (advs.hasMoreElements()) {                Advertisement badRouteAdv = (Advertisement) advs.nextElement();                if (badRouteAdv instanceof RouteAdvertisement)  {                    // ok so let's delete the advertisement                    discovery.flushAdvertisement(badRouteAdv);                    if (LOG.isEnabledFor(Level.DEBUG)) {                        LOG.debug("remove RouteAdvertisement " + ((RouteAdvertisement) badRouteAdv).display());                    }                }            }        } catch (IOException ex) {// ok if not there.            // protect against flush IOException when the entry is not there        }                try {            // let's remove the peer advertisement            discovery.flushAdvertisements(realPeerID, DiscoveryService.PEER);            if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("removed PeerAdvertisement " + realPeerID);            }        } catch (IOException ex) {// ok, if not there            // protect against flush IOException when the entry is not there        }    }        /**     * 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) {        // check if CM is in used        if (!useCM) {            return false;        }        try {            String realPeerID = route.getDestPeerID().toString();            // check first if we have a route advertisement            Enumeration advs = discovery.getLocalAdvertisements(DiscoveryService.ADV, "DstPID", realPeerID);            if ((advs != null) && (advs.hasMoreElements())) {                Advertisement adv = (Advertisement) 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                        discovery.publish(route, DEFAULT_EXPIRATION, DEFAULT_EXPIRATION);                        return true;                    }                }            } else {                // publish the new route                discovery.publish(route, DEFAULT_EXPIRATION, DEFAULT_EXPIRATION);                return true;            }        } catch (Exception e) {            if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("  failure to publish route advertisement  response" + e);            }        }        return false;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -