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

📄 relayclient.java

📁 jxta_src_2.41b jxta 2.41b 最新版源码 from www.jxta.org
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            addRelay(pg, relayRoute);        }                // maintain the list of active relays        activeRelays.put(address, relayRoute);        return true;    }        /**     * Notify of a relay connection removal     *     **/    public synchronized boolean removeActiveRelay(EndpointAddress address, RouteAdvertisement relayRoute) {                // need to notify all our listeners        Iterator e = activeRelayListeners.iterator();        while (e.hasNext()) {            PeerGroup pg = (PeerGroup) e.next();            removeRelay(pg, relayRoute);        }                activeRelays.remove(address);                return true;    }        /**     * Register an active Relay to the endpoint. This is done     * so the Route Advertisement of the PeerAdvertisement is     * updated     *     * @param address address opf the relay to add     * @return boolean true of false if it suceeded     **/    private void addRelay(PeerGroup pg, RouteAdvertisement relayRoute) {                DiscoveryService discovery = pg.getDiscoveryService();        // Publish the route adv for that relay. This is not done        // by the relay itself (which cares only for the relay's rdvAdv).                try {            if (discovery != null) {                discovery.publish(relayRoute, DEFAULT_EXPIRATION, DEFAULT_EXPIRATION);            }                    } catch (Throwable theMatter) {            // Abnormal but not criticial            if (LOG.isEnabledFor(Level.WARN)) {                LOG.warn("could not publish relay route adv", theMatter);            }        }                ID assignedID = PeerGroup.endpointClassID;                try {            // get the advertisement of the associated endpoint address as we            // need to get the peer Id and available route                        // update our own peer advertisement            PeerAdvertisement padv = pg.getPeerAdvertisement();            StructuredDocument myParam = (StructuredDocument) padv.getServiceParam(assignedID);                        RouteAdvertisement route = null;            if (myParam == null) {                // we should have found a route here. This is not good                if (LOG.isEnabledFor(Level.WARN)) {                    LOG.warn("no route found in peer adv");                }                return;            } else {                Enumeration paramChilds = myParam.getChildren(RouteAdvertisement.getAdvertisementType());                Element param = null;                                if (paramChilds.hasMoreElements()) {                    param = (Element) paramChilds.nextElement();                }                                route = (RouteAdvertisement)                        AdvertisementFactory.newAdvertisement((TextElement) param);            }                        if (route == null) { // we should have a route here                return;            }                        // ready to stich the Relay route in our route advertisement            if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("found route info for local peer \n" + route.display());            }                        // update the new hops info            if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("OLD route info to local peer \n" + route.display());            }                        // If we already have the relay in our list of hops, remove it.            // The new version can only be more accurate.            route.removeHop(relayRoute.getDestPeerID());                        // Get a hold of the hops list AFTER removing: removeHop            // rebuilds the vector !            Vector hops = route.getVectorHops();                        // Create the new relay Hop            hops.add(relayRoute.getDest());                        // update the new hops info            if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("NEW route info to local peer" + route.display());            }                        // create the new param route            myParam = StructuredDocumentFactory.newStructuredDocument                    (MimeMediaType.XMLUTF8, "Parm");            StructuredTextDocument xptDoc = (StructuredTextDocument)                    route.getDocument(MimeMediaType.XMLUTF8);            StructuredDocumentUtils.copyElements(myParam, myParam, xptDoc);                        padv.putServiceParam(assignedID, myParam);                        // publish the new peer advertisement            // We could publish the radv itself, but currently            // radvs are never searched through disco. PeerAdv is a superset.            // Maybe we'll want to change that one day.            if (discovery != null) {                discovery.publish(padv, DiscoveryService.DEFAULT_LIFETIME, DiscoveryService.DEFAULT_EXPIRATION);            }        } catch (Exception ex) {            if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("exception adding relay route ", ex);            }        }    }        /**     * remove relay hop from the peer advertisement     *     * @param group which peer advertisement needs to be updated     * @param address address of the relay to be removed     * @return boolean true if operation succeeded     */    private void removeRelay(PeerGroup pg, RouteAdvertisement relayRoute) {                // we can keep the advertisement for now (should remove it)        // remove the relay from its active list        ID assignedID = PeerGroup.endpointClassID;        PeerID relayPid = relayRoute.getDestPeerID();                try {            // get the advertisement of the associated endpoint address as we            // need to get the peer Id and available route                        PeerAdvertisement padv = null;                        // update our peer advertisement            padv = pg.getPeerAdvertisement();            StructuredDocument myParam = (StructuredDocument) padv.getServiceParam(assignedID);                        RouteAdvertisement route = null;            if (myParam == null) {                // no route found we should really have one                if (LOG.isEnabledFor(Level.WARN)) {                    LOG.warn("no route found in peer adv");                    return;                }            } else {                Enumeration paramChilds = myParam.getChildren(RouteAdvertisement.getAdvertisementType());                Element param = null;                                if (paramChilds.hasMoreElements()) {                    param = (Element) paramChilds.nextElement();                }                                route = (RouteAdvertisement)                        AdvertisementFactory.newAdvertisement((TextElement) param);            }                        if (route == null) {                return;            } // we should have a route here                        // update the new hops info            route.removeHop(relayPid);                        if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("new route info to the peer" + route.display());            }                        // create the new param route            myParam = StructuredDocumentFactory.newStructuredDocument                    (MimeMediaType.XMLUTF8, "Parm");            StructuredTextDocument xptDoc = (StructuredTextDocument)                    route.getDocument(MimeMediaType.XMLUTF8);            StructuredDocumentUtils.copyElements(myParam, myParam, xptDoc);                        padv.putServiceParam(assignedID, myParam);                        // publish the new advertisement            DiscoveryService discovery = pg.getDiscoveryService();            if (discovery != null) {                discovery.publish(padv, DiscoveryService.DEFAULT_LIFETIME, DiscoveryService.DEFAULT_EXPIRATION);            }        } catch (Throwable theMatter) {            if (LOG.isEnabledFor(Level.WARN)) {                LOG.warn("Failed adding relay route", theMatter);            }        }    }        /**     * return the list of connected relays     */    public Vector getActiveRelays(PeerGroup pg) {        try {                        if (LOG.isEnabledFor(Level.DEBUG)) {                LOG.debug("get active Relays list");            }                        if (activeRelays.size() != 0) {                Vector hops = new Vector();                for (Iterator e = activeRelays.values().iterator(); e.hasNext();) {                    RouteAdvertisement route = (RouteAdvertisement) e.next();                                        // publish our route if pg is not null                    if (pg != null) {                        DiscoveryService discovery = pg.getDiscoveryService();                        if (discovery != null) {                            if (LOG.isEnabledFor(Level.DEBUG)) {                                LOG.debug("publishing route to active relay " + route.display());                            }                            discovery.publish(route, DEFAULT_EXPIRATION, DEFAULT_EXPIRATION);                        }                    }                    hops.add(route.getDest());                }                return hops;            } else {                return null;            }                    } catch (Exception ex) {            if (LOG.isEnabledFor(Level.WARN)) {                LOG.warn("error publishing active relay", ex);            }            return null;        }    }        static EndpointAddress[] loadSeeds( URI seedingURI ) throws IOException {        URL seedingURL = seedingURI.toURL();                URLConnection connection = seedingURL.openConnection();        connection.setDoInput(true);        InputStream is = connection.getInputStream();        BufferedReader seeds = new BufferedReader( new InputStreamReader( is ) );                List seedURIs = new ArrayList();        while( true ) {            String aSeed = seeds.readLine();                        if( null == aSeed ) {                break;            }                        aSeed = aSeed.trim();                        if( 0 == aSeed.length() ) {                continue;            }                        try {                seedURIs.add( new EndpointAddress(new URI(aSeed)) );            } catch(URISyntaxException badURI) {                if (LOG.isEnabledFor(Level.WARN)) {                    LOG.warn("bad URI in seeding list :" + aSeed, badURI );                }            } catch(IllegalArgumentException badURI) {                if (LOG.isEnabledFor(Level.WARN)) {                    LOG.warn("bad URI in seeding list :" + aSeed, badURI );                }            }        }                is.close();                return (EndpointAddress[]) seedURIs.toArray( new EndpointAddress[seedURIs.size()]);    }        // convert an endpointRouterAddress into a PeerID    private final static PeerID addr2pid(EndpointAddress addr) {        try {            URI asURI = new URI(ID.URIEncodingName, ID.URNNamespace + ":" + addr.getProtocolAddress(), null);            return (PeerID) IDFactory.fromURI(asURI);        } catch (Exception ex) {            return null;        }    }}

⌨️ 快捷键说明

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