📄 networkmanager.java
字号:
/* * Copyright (c) 2006-2007 Sun Microsystems, Inc. All rights reserved. * * The Sun Project JXTA(TM) Software License * * 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 Sun Microsystems, Inc. for JXTA(TM) technology." * 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. * * JXTA is a registered trademark of Sun Microsystems, Inc. in the United * States and other countries. * * Please see the license information page at : * <http://www.jxta.org/project/www/license.html> for instructions on use of * the license in source files. * * ==================================================================== * * 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. */package net.jxta.platform;import javax.security.cert.CertificateException;import java.io.File;import java.io.IOException;import java.net.URI;import java.util.logging.Level;import java.util.logging.Logger;import net.jxta.credential.AuthenticationCredential;import net.jxta.credential.Credential;import net.jxta.exception.PeerGroupException;import net.jxta.exception.ProtocolNotSupportedException;import net.jxta.id.IDFactory;import net.jxta.logging.Logging;import net.jxta.membership.InteractiveAuthenticator;import net.jxta.membership.MembershipService;import net.jxta.peer.PeerID;import net.jxta.peergroup.NetPeerGroupFactory;import net.jxta.peergroup.PeerGroup;import net.jxta.peergroup.PeerGroupID;import net.jxta.rendezvous.RendezVousService;import net.jxta.rendezvous.RendezvousEvent;import net.jxta.rendezvous.RendezvousListener;import net.jxta.impl.membership.pse.StringAuthenticator;/** * NetworkManager provides a simplified JXTA platform configuration abstraction, and provides a JXTA platform life-cycle * management. The node configuration is created during construction of this object and can be obtained for fine tuning * or alteration. Note that all alterations must be done prior to calling #startNetwork(), otherwise the default * configuration is used. Configuration persistence is on by default and maybe overridden by call to #setEnableConfigPersistence * <p/> * NetworkManager defines six abstractions of a node configurations as follows : * ADHOC : A node which typically deployed in an ad-hoc network * EDGE : In addition to supporting ADHOC function, an Edge node can attach to a infrastructure (a Rendezvous, Relay, or both) * RENDEZVOUS: provides network bootstrapping services, such as discovery, pipe resolution, etc. * RELAY: provides message relaying services, enabling cross firewall traversal * PROXY: provide JXME JXTA for J2ME proxying services * SUPER: provide the functionality of a Rendezvous, Relay, Proxy node. */public class NetworkManager implements RendezvousListener { /** * Logger */ private final static transient Logger LOG = Logger.getLogger(NetworkManager.class.getName()); protected final transient URI publicSeedingRdvURI = URI.create("http://rdv.jxtahosts.net/cgi-bin/rendezvous.cgi?3"); protected final transient URI publicSeedingRelayURI = URI.create("http://rdv.jxtahosts.net/cgi-bin/relays.cgi?3"); /** * Define node standard node operating modes */ public enum ConfigMode { /** * A AD-HOC node */ ADHOC, /** * A Edge node */ EDGE, /** * A Rendezvous node */ RENDEZVOUS, /** * A Relay node */ RELAY, /** * Rendezvous and a Relay */ RENDEZVOUS_RELAY, /** * JXME Proxy node */ PROXY, /** * A Rendezvous, Relay, and JXME Proxy node */ SUPER } private final Object networkConnectLock = new String("rendezvous connection lock"); private PeerGroup netPeerGroup = null; private volatile boolean started = false; private volatile boolean connected = false; private volatile boolean stopped = false; private RendezVousService rendezvous; private String instanceName = "NA"; private ShutdownHook shutdownHook; private ConfigMode mode; private URI instanceHome; private PeerGroupID infrastructureID = PeerGroupID.defaultNetPeerGroupID; private PeerID peerID = IDFactory.newPeerID(PeerGroupID.defaultNetPeerGroupID); private NetworkConfigurator config; private boolean configPersistent = true; private boolean useDefaultSeeds; /** * Creates NetworkManger instance with default instance home set to "$CWD"/.jxta" * At this point, alternate Infrastructure PeerGroupID maybe specified, as well as a PeerID. if neither are * specified, the default NetPeerGroupID will be used, and a new PeerID will be generated. Also note the default * seeding URIs are the to development. Alternate values must be specified, if desired, prior to a call to {@link #startNetwork} * * @param mode Operating mode the node operating {@link ConfigMode} * @param instanceName Node name * @throws IOException if an io error occurs */ public NetworkManager(ConfigMode mode, String instanceName) throws IOException { this(mode, instanceName, new File(".jxta/").toURI()); } /** * Creates NetworkManger instance. * At this point, alternate Infrastructure PeerGroupID maybe specified, as well as a PeerID. if neither are * specified, the default NetPeerGroupID will be used, and a new PeerID will be generated. Also note the default * seeding URIs are the to development. Alternate values must be specified, if desired, prior to a call to {@link #startNetwork} * * @param mode Operating mode the node operating {@link ConfigMode} * @param instanceName Node name * @param instanceHome instance home is a uri to the instance persistent store (aka Cache Manager store home) * @throws IOException if an io error occurs */ public NetworkManager(ConfigMode mode, String instanceName, URI instanceHome) throws IOException { this.instanceName = instanceName; this.mode = mode; this.instanceHome = instanceHome; } /** * Returns the {@link NetworkConfigurator} for additional tuning * * @return the {@link NetworkConfigurator} for additional tuning * @throws java.io.IOException if an io error occurs */ public synchronized NetworkConfigurator getConfigurator() throws IOException { if (config == null) { configure(mode); } return config; } /** * Getter for property 'infrastructureID'. * * @return Value for property 'infrastructureID'. */ public PeerGroupID getInfrastructureID() { return infrastructureID; } /** * Setter for property 'infrastructureID'. * * @param infrastructureID Value to set for property 'infrastructureID'. */ public void setInfrastructureID(PeerGroupID infrastructureID) { this.infrastructureID = infrastructureID; if (config != null) { config.setInfrastructureID(infrastructureID); } } /** * Getter for property 'instanceName'. * * @return Value for property 'instanceName'. */ public String getInstanceName() { return instanceName; } /** * Setter for property 'instanceName'. * * @param instanceName Value to set for property 'instanceName'. */ public void setInstanceName(String instanceName) { this.instanceName = instanceName; } /** * Getter for property 'instanceHome'. * * @return Value for property 'instanceHome'. */ public URI getInstanceHome() { return instanceHome; } /** * Setter for property 'instanceHome'. * * @param instanceHome Value to set for property 'instanceHome'. */ public void setInstanceHome(URI instanceHome) { this.instanceHome = instanceHome; } /** * Getter for property node operating 'mode'. * * @return Value for property 'mode'. */ public ConfigMode getMode() { return mode; } /** * Setter for property 'mode'. * * @param mode Value to set for property 'mode'. * @throws IOException if an io error occurs */ public void setMode(ConfigMode mode) throws IOException { this.mode = mode; configure(mode); } /** * Getter for property 'peerID'. * * @return Value for property 'peerID'. */ public PeerID getPeerID() { return peerID; } /** * Setter for property 'peerID'. * * @param peerID Value to set for property 'peerID'. */ public void setPeerID(PeerID peerID) { this.peerID = peerID; } /** * Getter for property 'configPersistent'. * * @return Value for property 'configPersistent'. */ public boolean isConfigPersistent() { return configPersistent; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -