📄 routecm.java
字号:
/* * * $Id: RouteCM.java,v 1.15 2006/02/22 01:31:15 bondolo Exp $ * * Copyright (c) 2001 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Sun Microsystems, Inc. for Project JXTA." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" * must not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", * nor may "JXTA" appear in their name, without prior written * permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of Project JXTA. For more * information on Project JXTA, please see * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache Foundation. *//** * This class is used to manage a persistent CM cache of route * for the router */package net.jxta.impl.endpoint.router;import java.net.URI;import java.util.Enumeration;import java.util.Vector;import java.util.Iterator;import java.util.ArrayList;import java.util.List;import java.io.IOException;import java.net.URISyntaxException;import org.apache.log4j.Level;import org.apache.log4j.Logger;import net.jxta.id.ID;import net.jxta.peer.PeerID;import net.jxta.discovery.DiscoveryService;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.XMLElement;import net.jxta.endpoint.EndpointAddress;import net.jxta.protocol.ConfigParams;import net.jxta.peergroup.PeerGroup;import net.jxta.platform.Module;import net.jxta.protocol.PeerAdvertisement;import net.jxta.protocol.RouteAdvertisement;import net.jxta.protocol.AccessPointAdvertisement;import net.jxta.exception.PeerGroupException;import net.jxta.impl.util.TimeUtils;class RouteCM { /** * Log4j Logger **/ private final static transient Logger LOG = Logger.getLogger(RouteCM.class.getName()); /** * Default expiration time for Route advertisements. This is the amount * of time which advertisements will live in caches. After this time, the * advertisement should be refreshed from the source. **/ public final static long DEFAULT_EXPIRATION = 20L * TimeUtils.AMINUTE; /** * Configuration property that disables the usage * of the CM to persistently store and retrieve * Route advertisements as well as cache route advertisements * Only the in-memory route table is used. **/ private boolean useCM = false; /** * Configuration useCM property specified by the * user and provided in the EndpointRouter service * configuration. By default we use the CM. **/ private boolean useCMDesired = true; /** * PeerGroup Service Handle **/ private PeerGroup group = null; /** * Discovery service handle **/ private DiscoveryService discovery = null; /** * EndpointRouter pointer **/ private EndpointRouter router = null; /** * return routeCM usage */ protected boolean useRouteCM() { return useCM; } /** * disable routeCM usage */ protected void disableRouteCM() { useCM = false; } /** * disable routeCM usage */ protected void enableRouteCM() { useCM = true; } /** * Constructor */ public RouteCM() {} /** * initialize CM route */ public void init(PeerGroup group, ID assignedID, Advertisement impl, EndpointRouter router) throws PeerGroupException { // extract Router service configuration properties ConfigParams confAdv = (ConfigParams) group.getConfigAdvertisement(); XMLElement paramBlock = null; if (confAdv != null) { paramBlock = (XMLElement) confAdv.getServiceParam(assignedID); } if (paramBlock != null) { // get our tunable router parameter Enumeration param; param = paramBlock.getChildren("useCM"); if (param.hasMoreElements()) { useCMDesired = Boolean.getBoolean(((XMLElement) param.nextElement()).getTextValue()); } } this.group = group; // save the router object pointer this.router = router; } /** * Make this protocol as up and running. * When this method is called, all the services are already registered * with the peergroup. So we do not need to delay binding any further. * All the public methods, which could be called between init and startApp * are defensive regarding the services possibly not being there. */ public int startApp(String[] arg) { discovery = group.getDiscoveryService(); if (null == discovery) { if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("Endpoint Router start stalled until discovery service available"); } return Module.START_AGAIN_STALLED; } // ok, we are initialized, go ahead and enable CM usage desired useCM = useCMDesired; return 0; } /** * Stop the service */ public void stopApp() { discovery = null; } /** * get route advertisements from the local discovery cache. * We collect straight RouteAdvertisements as well as what can be * found in PeerAdvertisements. * We can find both, and there's no way to know which is most relevant, * so we have to return all and let the invoker try its luck with each. * * @param pId the target peer's logical address * @return Iterator of advertisements (route, peer) */ protected Iterator getRouteAdv(EndpointAddress pId) { // check if we use the CM, if not then nothing // to retrieve if (!useCM) { return null; } // What we refer to in the router as PeerIDs, are generaly not // peerIDs, they're endpoint addresses based on a peer ID. String realPeerID = null; try { URI asUri = new URI(ID.URIEncodingName, ID.URNNamespace + ":" + pId.getProtocolAddress(), null); realPeerID = asUri.toString(); } catch (URISyntaxException e) { if (LOG.isEnabledFor(Level.WARN)) { LOG.warn("bad peer address: " + pId); } return null; } List result = new ArrayList(2); try { // check first if we have a route advertisement Enumeration advs = discovery.getLocalAdvertisements(DiscoveryService.ADV, "DstPID", realPeerID); while (advs.hasMoreElements()) { RouteAdvertisement adv = (RouteAdvertisement) advs.nextElement(); if (!result.contains(adv)) { result.add(adv); } } } catch (Exception e1) { if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug(" failed with ", e1); } // Try to continue with peer advs. } try { // get the local peer advertisements
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -