/* * 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 java.io.File; import java.io.IOException; import java.net.URI; import javax.security.cert.CertificateException; import net.jxta.credential.AuthenticationCredential; import net.jxta.credential.Credential; import net.jxta.exception.ConfiguratorException; import net.jxta.exception.PeerGroupException; import net.jxta.exception.ProtocolNotSupportedException; import net.jxta.impl.membership.pse.StringAuthenticator; import net.jxta.logging.Logger; import net.jxta.logging.Logging; import net.jxta.membership.InteractiveAuthenticator; import net.jxta.membership.MembershipService; import net.jxta.peer.PeerID; import net.jxta.peergroup.IModuleDefinitions; import net.jxta.peergroup.NetPeerGroupFactory; import net.jxta.peergroup.PeerGroup; import net.jxta.peergroup.PeerGroupID; import net.jxta.protocol.PeerGroupAdvertisement; import net.jxta.rendezvous.RendezVousService; import net.jxta.rendezvous.RendezvousEvent; import net.jxta.rendezvous.RendezvousListener; /** * 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 * SUPER: provide the functionality of a Rendezvous, Relay, Proxy node. */ public class NetworkManager implements RendezvousListener { private final static transient Logger LOG = Logging.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 * * @since 2.6 Will be removed in a future release */ @Deprecated PROXY, /** * A Rendezvous, Relay, and JXME Proxy node */ SUPER } private final Object networkConnectLock = "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 = null; private NetworkConfigurator config; private boolean configPersistent = true; // private boolean useDefaultSeeds; static final String DEFAULT_INSTANCE_HOME = ".jxta/"; /** * 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 of the node {@link ConfigMode} * @param instanceName Node name * @throws IOException if an io error occurs */ NetworkManager(ConfigMode mode, String instanceName) throws IOException { this(mode, instanceName, new File(DEFAULT_INSTANCE_HOME).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 */ 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'. */ private 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'. */ private 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; } /** * Setter for property 'configPersistent'. If enabled, the PlatformConfig is persisted * when {@code startNetwork()} is called. * * @param persisted Value to set for property 'configPersistent'. */ public void setConfigPersistent(boolean persisted) { this.configPersistent = persisted; } private void configure(ConfigMode mode) throws IOException { switch (mode) { case ADHOC: config = NetworkConfigurator.newAdHocConfiguration(instanceHome); break; case EDGE: config = NetworkConfigurator.newEdgeConfiguration(instanceHome); break; case RENDEZVOUS: config = NetworkConfigurator.newRdvConfiguration(instanceHome); break; case RELAY: config = NetworkConfigurator.newRelayConfiguration(instanceHome); break; case RENDEZVOUS_RELAY: config = NetworkConfigurator.newRdvRelayConfiguration(instanceHome); break; case SUPER: config = NetworkConfigurator.newRdvRelayConfiguration(instanceHome); break; default: config = NetworkConfigurator.newAdHocConfiguration(instanceHome); } if (!config.exists()) { Logging.logCheckedInfo(LOG, "Created new configuration. mode = ", mode); config.setDescription("Created by NetworkManager"); config.setPeerID(peerID); config.setInfrastructureID(infrastructureID); config.setName(instanceName); // if (useDefaultSeeds) { // config.addRdvSeedingURI(publicSeedingRdvURI); // config.addRelaySeedingURI(publicSeedingRelayURI); // } } else { Logging.logCheckedInfo(LOG, "Loading existing configuration. mode = ", mode); File pc = new File(config.getHome(), "PlatformConfig"); try { config.load(pc.toURI()); } catch (CertificateException pseFailed) { throw new IOException("Failure reading membership service certificates.", pseFailed); } // XXX 20070524 bondolo Aren't we completely ignoring the mode? What if it changed? // 20070614 hamada Good question, this feature is postponed due to the difficulty of comparing a stored/requested modes. } } /** * Creates and starts the JXTA infrastructure peer group (aka NetPeerGroup) based on the specified mode * template. This class also registers a listener for rendezvous events. * * @return The Net Peer Group * @throws net.jxta.exception.PeerGroupException * if the group fails to initialize * @throws java.io.IOException if an io error occurs * @throws net.jxta.exception.ConfiguratorException if platform is not configured properly */ public synchronized PeerGroup startNetwork() throws PeerGroupException, IOException, ConfiguratorException { if (started) { return netPeerGroup; } if (config == null) { configure(mode); } Logging.logCheckedInfo(LOG, "Starting JXTA Network! MODE = ", mode, ", HOME = ", instanceHome); // create, and Start the default jxta NetPeerGroup NetPeerGroupFactory factory = new NetPeerGroupFactory(config.getPlatformConfig(), instanceHome); netPeerGroup = factory.getNetPeerGroup(); // Saving if config is persistent if (configPersistent) { config.save(); } rendezvous = netPeerGroup.getRendezVousService(); rendezvous.addListener(this); started = true; stopped = false; Logging.logCheckedInfo(LOG, "Started JXTA Network!"); return netPeerGroup; } /** * Establishes group credential. This is a required step when planning to * to utilize TLS messengers or secure pipes * * @param group peer group to establish credentials in * @param keystorePassword The passphrase for the keystore. This is a * char[] rather than a String so that it can be blanked after use. * @param principalPassword The passphrase for the identity. This is a * char[] rather than a String so that it can be blanked after use. * @throws net.jxta.exception.PeerGroupException * if group credentials were rejected * @throws net.jxta.exception.ProtocolNotSupportedException * if authenticator rejected the credential */ public static void login(PeerGroup group, char[] keystorePassword, char[] principalPassword) throws PeerGroupException, ProtocolNotSupportedException { StringAuthenticator auth; MembershipService membership = group.getMembershipService(); Credential cred = membership.getDefaultCredential(); if (cred == null) { AuthenticationCredential authCred = new AuthenticationCredential(group, "StringAuthentication", null); auth = (StringAuthenticator) membership.apply(authCred); if (auth != null) { auth.setAuth1_KeyStorePassword(keystorePassword); auth.setAuth2Identity(group.getPeerID()); auth.setAuth3_IdentityPassword(principalPassword); if (auth.isReadyForJoin()) { membership.join(auth); } } } cred = membership.getDefaultCredential(); if (null == cred) { AuthenticationCredential authCred = new AuthenticationCredential(group, "InteractiveAuthentication", null); InteractiveAuthenticator iAuth = (InteractiveAuthenticator) membership.apply(authCred); if (iAuth.interact() && iAuth.isReadyForJoin()) { membership.join(iAuth); } } } /** * Stops NetPeerGroup */ public synchronized void stopNetwork() { if (stopped || !started) { return; } Logging.logCheckedInfo(LOG, "Stopping JXTA Network!"); stopped = true; synchronized (networkConnectLock) { connected = false; networkConnectLock.notifyAll(); } rendezvous.removeListener(this); PeerGroup worldPeerGroup = netPeerGroup.getParentGroup(); //mindarchitect 16052014 //Check parent peer group should not be null if (worldPeerGroup != null) { PeerGroupAdvertisement worldPeerGroupAdvertisement = worldPeerGroup.getPeerGroupAdvertisement(); //mindarchitect 16052014 //Should be platform specification ID //Unfortunately cannot check by ID as WorldPeerGroupID is package level visible only if (worldPeerGroupAdvertisement.getModuleSpecID().equals(IModuleDefinitions.refPlatformSpecID)) { //Stop peer group worldPeerGroup.stopApp(); } } netPeerGroup.stopApp(); // Permit restart. started = false; Logging.logCheckedInfo(LOG, "Stopped JXTA Network!"); } /** * Gets the netPeerGroup object * * @return The netPeerGroup value */ public PeerGroup getNetPeerGroup() { return netPeerGroup; } /** * Blocks only, if not connected to a rendezvous, or until a connection to rendezvous node occurs. * * @param timeout timeout in milliseconds, a zero timeout of waits forever * @return true if connected to a rendezvous, false otherwise */ public boolean waitForRendezvousConnection(long timeout) { // Are we an EDGE? if (this.getMode().compareTo(ConfigMode.EDGE)!=0) { Logging.logCheckedWarning(LOG, "Trying to wait for RendezVous connection while not being an EDGE."); } if (0 == timeout) timeout = Long.MAX_VALUE; long timeoutAt = System.currentTimeMillis() + timeout; if (timeoutAt <= 0) { // handle overflow. timeoutAt = Long.MAX_VALUE; } while (started && !stopped && !rendezvous.isConnectedToRendezVous() && !rendezvous.isRendezVous()) { try { long waitFor = timeoutAt - System.currentTimeMillis(); if (waitFor > 0) { synchronized (networkConnectLock) { networkConnectLock.wait(timeout); } } else { // all done with waiting. break; } } catch (InterruptedException e) { Thread.interrupted(); break; } } return rendezvous.isConnectedToRendezVous() || rendezvous.isRendezVous(); } /** * rendezvousEvent the rendezvous event * * @param event rendezvousEvent */ public void rendezvousEvent(RendezvousEvent event) { if (event.getType() == RendezvousEvent.RDVCONNECT || event.getType() == RendezvousEvent.RDVRECONNECT || event.getType() == RendezvousEvent.BECAMERDV) { synchronized (networkConnectLock) { connected = true; networkConnectLock.notifyAll(); } } } // /** // * if true uses the public rendezvous seeding service // * // * @param useDefaultSeeds if true uses the default development seeding service // */ // public void setUseDefaultSeeds(boolean useDefaultSeeds) { // this.useDefaultSeeds = useDefaultSeeds; // } // // /** // * Returns true if useDefaultSeeds is set to true // * // * @return true if useDefaultSeeds is set to true // */ // public boolean getUseDefaultSeeds() { // return useDefaultSeeds; // } /** * Registers a Runtime shutdown hook to cleanly shutdown the JXTA platform */ public synchronized void registerShutdownHook() { if (shutdownHook != null) { return; } shutdownHook = new NetworkManager.ShutdownHook(); Runtime.getRuntime().addShutdownHook(shutdownHook); } /** * Unregisters a Runtime shutdown hook to cleanly shutdown the JXTA platform */ public synchronized void unregisterShutdownHook() { if (shutdownHook == null) { return; } Runtime.getRuntime().removeShutdownHook(shutdownHook); shutdownHook = null; } private class ShutdownHook extends Thread { /** * {@inheritDoc} */ @Override public void run() { stopNetwork(); } } /** * Returns true if started * * @return true if started */ public boolean isStarted() { return (started && (!stopped)); } /** * Recursively deletes the content of a directory directory. This can be usefull * to clean-up persisted cache content. * * @param TheFile the directory to delete */ public static void RecursiveDelete(File TheFile) { // Checking parameter if ( TheFile == null ) Logging.logCheckedError(LOG, "Attempting to recursively delete a NULL directoy"); if ( TheFile.isDirectory() ) { try { Logging.logCheckedDebug(LOG, "Recursively deleting: ", TheFile.getCanonicalPath()); } catch (IOException ex) { Logging.logCheckedError(LOG, "Cannot retrieve canonical path:", ex); } } File[] SubFiles = TheFile.listFiles(); if (SubFiles!=null) { for(int i=0;i<SubFiles.length;i++) { if (SubFiles[i].isDirectory()) { RecursiveDelete(SubFiles[i]); } SubFiles[i].delete(); } TheFile.delete(); } } }