📄 networkmanager.java
字号:
* Setter for property 'configPersistent'. if disabled a PlatformConfig is not persisted. It assumed that * the PeerID is will be set, or a new PeerID will always be generated. * * @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 PROXY: config = NetworkConfigurator.newProxyConfiguration(instanceHome); break; case SUPER: config = NetworkConfigurator.newRdvRelayProxyConfiguration(instanceHome); break; default: config = NetworkConfigurator.newAdHocConfiguration(instanceHome); } if (!config.exists()) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.log(Level.INFO, "Created new configuration. mode = " + mode.toString()); } config.setDescription("Created by NetworkManager"); config.setPeerID(peerID); config.setInfrastructureID(infrastructureID); config.setName(instanceName); if (useDefaultSeeds) { config.addRdvSeedingURI(publicSeedingRdvURI); config.addRelaySeedingURI(publicSeedingRelayURI); } } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.log(Level.INFO, "Loading existing configuration. mode = " + mode.toString()); } File pc = new File(config.getHome(), "PlatformConfig"); try { config.load(pc.toURI()); } catch (CertificateException pseFailed) { IOException failure = new IOException("Failure reading membership service certificates."); failure.initCause(pseFailed); throw failure; } // 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 */ public synchronized PeerGroup startNetwork() throws PeerGroupException, IOException { if (started) { return netPeerGroup; } if (config == null) { configure(mode); } if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.log(Level.INFO, "Starting JXTA Network! MODE = " + mode.toString() + ", HOME = " + instanceHome); } // create, and Start the default jxta NetPeerGroup NetPeerGroupFactory factory = new NetPeerGroupFactory(config.getPlatformConfig(), instanceHome); netPeerGroup = factory.getInterface(); if (configPersistent) { config.save(); } rendezvous = netPeerGroup.getRendezVousService(); rendezvous.addListener(this); started = true; if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.log(Level.INFO, "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 keystore_password The passphrase for the keystore. This is a * char[] rather than a String so that it can be blanked after use. * @param principal_password 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[] keystore_password, char[] principal_password) 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(keystore_password); auth.setAuth2Identity(group.getPeerID()); auth.setAuth3_IdentityPassword(principal_password); 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 and unreferences the NetPeerGroup */ public synchronized void stopNetwork() { if (stopped || !started) { return; } if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.log(Level.INFO, "Stopping JXTA Network!"); } stopped = true; synchronized(networkConnectLock) { connected = false; networkConnectLock.notifyAll(); } rendezvous.removeListener(this); netPeerGroup.stopApp(); netPeerGroup.unref(); netPeerGroup = null; // permit restart. started = false; if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.log(Level.INFO, "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) { 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(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -