📄 relaytransport.java
字号:
/* * 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. * * $Id: RelayTransport.java,v 1.34 2004/11/03 23:45:45 bondolo Exp $ * */package net.jxta.impl.endpoint.relay;import java.util.Enumeration;import java.util.NoSuchElementException;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.StructuredDocument;import net.jxta.document.XMLDocument;import net.jxta.document.XMLElement;import net.jxta.endpoint.EndpointAddress;import net.jxta.endpoint.EndpointListener;import net.jxta.endpoint.EndpointService;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.StringMessageElement;import net.jxta.id.ID;import net.jxta.peergroup.PeerGroup;import net.jxta.protocol.ConfigParams;import net.jxta.protocol.ModuleImplAdvertisement;import net.jxta.protocol.PeerAdvertisement;import net.jxta.protocol.RouteAdvertisement;import net.jxta.platform.Module;import net.jxta.exception.PeerGroupException;import net.jxta.impl.util.TimeUtils;import net.jxta.impl.protocol.RelayConfigAdv;/** * * The Relay Server supports the following commands: * * CONNECT - message contains PEERID, optional LEASE * DISCONNECT - message contains PEERID. * GETSERVER - message contains PEERID. */public final class RelayTransport implements EndpointListener, Module { /** * Log4J Logger **/ private final static Logger LOG = Logger.getLogger(RelayTransport.class.getName()); // // constants //// static final String protocolName = "relay"; static final String RELAY_NS = "relay"; static final String REQUEST_ELEMENT = "request"; static final String RESPONSE_ELEMENT = "response"; static final String PEERID_ELEMENT = "peerid"; static final String LEASE_ELEMENT = "lease"; static final String RELAY_ADV_ELEMENT = "relayAdv"; static final String CONNECT_REQUEST = "connect"; static final MessageElement CONNECT_REQUEST_ELEMENT = new StringMessageElement(REQUEST_ELEMENT, CONNECT_REQUEST, null); static final String DISCONNECT_REQUEST = "disconnect"; static final MessageElement DISCONNECT_REQUEST_ELEMENT = new StringMessageElement(REQUEST_ELEMENT, DISCONNECT_REQUEST, null); static final String PID_REQUEST = "pid"; static final MessageElement PID_REQUEST_ELEMENT = new StringMessageElement(REQUEST_ELEMENT, PID_REQUEST, null); static final String CONNECTED_RESPONSE = "connected"; static final MessageElement CONNECTED_RESPONSE_ELEMENT = new StringMessageElement(RESPONSE_ELEMENT, CONNECTED_RESPONSE, null); static final String DISCONNECTED_RESPONSE = "disconnected"; static final MessageElement DISCONNECTED_RESPONSE_ELEMENT = new StringMessageElement(RESPONSE_ELEMENT, DISCONNECTED_RESPONSE, null); static final String PID_RESPONSE = "pid"; static final MessageElement PID_RESPONSE_ELEMENT = new StringMessageElement(RESPONSE_ELEMENT, PID_RESPONSE, null); static final int DEFAULT_MAX_CLIENTS = 150; static final int DEFAULT_MAX_SERVERS = 1; // Note the weird time below can be decreased but should not be increased // otherwise there will not be enough traffic for the other side to // keep the connection open. static final long DEFAULT_LEASE = TimeUtils.ANHOUR; static final long DEFAULT_STALL_TIMEOUT = 15 * TimeUtils.ASECOND; static final long DEFAULT_POLL_INTERVAL = 15 * TimeUtils.ASECOND; // (the poll costs very little) static final long DEFAULT_BROADCAST_INTERVAL = 10 * TimeUtils.AMINUTE; static final int DEFAULT_CLIENT_QUEUE_SIZE = 20; private PeerGroup group = null; private ID assignedID = null; private ModuleImplAdvertisement implAdvertisement = null; private String serviceName = null; private RelayClient relayClient = null; private RelayServer relayServer = null; /** * {@inheritDoc} **/ public void init(PeerGroup group, ID assignedID, Advertisement implAdv) throws PeerGroupException { this.group = group; this.assignedID = assignedID; this.implAdvertisement = (ModuleImplAdvertisement) implAdv; this.serviceName = assignedID.getUniqueValue().toString(); ConfigParams confAdv = (ConfigParams) group.getConfigAdvertisement(); // Get the config. If we do not have a config, we're done; we just keep // the defaults (edge peer/no auto-rdv) RelayConfigAdv relayConfigAdv = null; if (confAdv != null) { Advertisement adv = null; try { XMLDocument configDoc = (XMLDocument) confAdv.getServiceParam(assignedID); // XXX bondolo 20041025 For backwards compatibility configDoc.addAttribute( "type", RelayConfigAdv.getAdvertisementType() ); if (null != configDoc) { adv = AdvertisementFactory.newAdvertisement(configDoc); } } catch (NoSuchElementException failed) { ; } catch (IllegalArgumentException failed) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("Error in relay advertisement", failed); } throw failed; } if (adv instanceof RelayConfigAdv) { relayConfigAdv = (RelayConfigAdv) adv; } else { relayConfigAdv = (RelayConfigAdv) AdvertisementFactory.newAdvertisement(RelayConfigAdv.getAdvertisementType()); } } // XXX bondolo 20041030 I'd like to move these to startApp so that we // can pass endpointService and share the instance. if (relayConfigAdv.isServerEnabled()) { relayServer = new RelayServer(group, serviceName, relayConfigAdv); } if (relayConfigAdv.isClientEnabled()) { relayClient = new RelayClient(group, serviceName, relayConfigAdv); } if (LOG.isEnabledFor(Level.INFO)) { StringBuffer configInfo = new StringBuffer("Configuring Relay Message Transport : " + assignedID); if (implAdvertisement != null) { configInfo.append("\n\tImplementation :"); configInfo.append("\n\t\tModule Spec ID: " + implAdvertisement.getModuleSpecID()); configInfo.append("\n\t\tImpl Description : " + implAdvertisement.getDescription()); configInfo.append("\n\t\tImpl URI : " + implAdvertisement.getUri()); configInfo.append("\n\t\tImpl Code : " + implAdvertisement.getCode()); } 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\tisServer : " + relayConfigAdv.isServerEnabled()); configInfo.append("\n\t\tisClient : " + relayConfigAdv.isClientEnabled()); LOG.info(configInfo);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -