📄 relayclient.java
字号:
/* * * $Id: RelayClient.java,v 1.55 2006/07/24 18:04:23 hamada 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. */package net.jxta.impl.endpoint.relay;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.URI;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Enumeration;import java.util.Hashtable;import java.util.List;import java.util.Map;import java.util.Iterator;import java.util.Vector;import java.util.HashSet;import java.util.Set;import java.io.IOException;import java.net.URISyntaxException;import org.apache.log4j.Level;import org.apache.log4j.Logger;import net.jxta.discovery.DiscoveryService;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.Element;import net.jxta.document.MimeMediaType;import net.jxta.document.StructuredDocument;import net.jxta.document.StructuredDocumentFactory;import net.jxta.document.StructuredDocumentUtils;import net.jxta.document.StructuredTextDocument;import net.jxta.document.TextElement;import net.jxta.endpoint.EndpointAddress;import net.jxta.endpoint.EndpointService;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.MessageReceiver;import net.jxta.endpoint.MessageSender;import net.jxta.endpoint.MessageTransport;import net.jxta.endpoint.Messenger;import net.jxta.id.ID;import net.jxta.id.IDFactory;import net.jxta.peer.PeerID;import net.jxta.peergroup.PeerGroup;import net.jxta.protocol.AccessPointAdvertisement;import net.jxta.protocol.PeerAdvertisement;import net.jxta.protocol.RdvAdvertisement;import net.jxta.protocol.RouteAdvertisement;import net.jxta.impl.util.TimeUtils;import net.jxta.impl.protocol.RelayConfigAdv;/** * RelayClient manages the relationship with the RelayServer(s) * */public class RelayClient implements MessageReceiver, Runnable { /** * Log4J Logger **/ private final static Logger LOG = Logger.getLogger(RelayClient.class.getName()); private final static long DEFAULT_EXPIRATION = 20L * TimeUtils.AMINUTE; private final static long DAY_EXPIRATION = TimeUtils.ADAY; private final PeerGroup group; private final String serviceName; private EndpointService endpoint; private final EndpointAddress publicAddress; private final String groupName; private final String peerId; private final int maxServers; private final long leaseLengthToRequest; private final long messengerPollInterval; private Thread thread = null; private volatile boolean closed = false; /** * <ul> * <li>Values are {@link net.jxta.peergroup.PeerGroup}.</li> * </ul> **/ private final List activeRelayListeners = new ArrayList(); /** * <ul> * <li>Keys are {@link net.jxta.endpoint.EndpointAddress}.</li> * <li>Values are {@link net.jxta.protocol.RouteAdvertisement}.</li> * </ul> **/ private final Map activeRelays = new Hashtable(); /** * <ul> * <li>Values are {@link net.jxta.endpoint.EndpointAddress}.</li> * </ul> **/ private final Set seedRelays = new HashSet(); /** * URIs from which we will load seeds. * * <ul> * <li>Values are {@link java.net.URI}.</li> * </ul> **/ private final Set seedingURIs = new HashSet(); /** * Seeds loaded from the seeding URIs. * * <ul> * <li>Values are {@link net.jxta.endpoint.EndpointAddress}.</li> * </ul> **/ private final Set seededRelays = new HashSet(); protected RelayServerConnection currentServer = null; public RelayClient(PeerGroup group, String serviceName, RelayConfigAdv relayConfig) { this.group = group; this.groupName = group.getPeerGroupID().getUniqueValue().toString(); this.serviceName = serviceName; maxServers = (-1 != relayConfig.getMaxRelays()) ? relayConfig.getMaxRelays() : RelayTransport.DEFAULT_MAX_SERVERS; leaseLengthToRequest = (-1 != relayConfig.getClientLeaseDuration()) ? relayConfig.getClientLeaseDuration() : RelayTransport.DEFAULT_LEASE; messengerPollInterval = (-1 != relayConfig.getMessengerPollInterval()) ? relayConfig.getMessengerPollInterval() : RelayTransport.DEFAULT_POLL_INTERVAL; seedRelays.addAll( Arrays.asList(relayConfig.getSeedRelays()) ); seedingURIs.addAll( Arrays.asList(relayConfig.getSeedingURIs()) ); // sanity check peerId = group.getPeerID().getUniqueValue().toString(); publicAddress = new EndpointAddress(RelayTransport.protocolName, peerId, null, null); if (LOG.isEnabledFor(Level.INFO)) { StringBuffer configInfo = new StringBuffer("Configuring Relay Client"); configInfo.append("\n\tGroup Params :"); configInfo.append("\n\t\tGroup : " + group.getPeerGroupName()); configInfo.append("\n\t\tGroup ID : " + group.getPeerGroupID()); configInfo.append("\n\t\tPeer ID : " + group.getPeerID()); configInfo.append("\n\tConfiguration :"); configInfo.append("\n\t\tService Name : " + serviceName); configInfo.append("\n\t\tPublic Address : " + publicAddress); configInfo.append("\n\t\tMax Relay Servers : " + maxServers); configInfo.append("\n\t\tMax Lease Length : " + leaseLengthToRequest + "ms."); configInfo.append("\n\t\tMessenger Poll Interval : " + messengerPollInterval + "ms."); configInfo.append("\n\t\tSeed Relays : "); Iterator eachRelay = Arrays.asList(seedRelays.toArray()).iterator(); if (!eachRelay.hasNext()) { configInfo.append("\n\t\t\t(none defined)"); } while (eachRelay.hasNext()) { configInfo.append("\n\t\t\t" + eachRelay.next()); } LOG.info(configInfo); } } public synchronized boolean startClient() { endpoint = group.getEndpointService(); if (endpoint.addMessageTransport(this) == null) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("Transport registration refused"); } return false; } // start the client thread thread = new Thread(group.getHomeThreadGroup(), this, "Relay Client Worker Thread for " + publicAddress); thread.setDaemon(true); thread.start(); if (LOG.isEnabledFor(Level.INFO)) { LOG.info("Started client : " + publicAddress.toString()); } return true; } public synchronized void stopClient() { if( closed ) { return; } closed = true; endpoint.removeMessageTransport(this); // make sure the thread is not running Thread tempThread = thread; thread = null; if (tempThread != null) { tempThread.interrupt(); } if (LOG.isEnabledFor(Level.INFO)) { LOG.info("Stopped client : " + publicAddress.toString()); } } /** * {@inheritDoc} **/ public Iterator getPublicAddresses() { EndpointAddress addr = null; if (publicAddress != null) { addr = (EndpointAddress) publicAddress.clone(); } return Collections.singletonList(addr).iterator(); } /** * {@inheritDoc} **/ public String getProtocolName() { return RelayTransport.protocolName; } /** * {@inheritDoc} **/ public EndpointService getEndpointService() { return endpoint; } /** * {@inheritDoc} **/ public Object transportControl(Object operation, Object Value) { return null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -