📄 relaytransport.java
字号:
/* * Copyright (c) 2001-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.impl.endpoint.relay;import java.util.NoSuchElementException;import java.util.logging.Level;import net.jxta.logging.Logging;import java.util.logging.Logger;import net.jxta.discovery.DiscoveryService;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.XMLDocument;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.platform.Module;import net.jxta.exception.PeerGroupException;import net.jxta.impl.util.TimeUtils;import net.jxta.impl.protocol.RelayConfigAdv;import net.jxta.pipe.PipeService;/** * 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 { /** * 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 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; ModuleImplAdvertisement implAdvertisement = (ModuleImplAdvertisement) implAdv; this.serviceName = assignedID.getUniqueValue().toString(); ConfigParams confAdv = 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); if (null != configDoc) { adv = AdvertisementFactory.newAdvertisement(configDoc); } } catch (NoSuchElementException failed) { //ignored } catch (IllegalArgumentException failed) { if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) { LOG.log(Level.SEVERE, "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 (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) { StringBuilder configInfo = new StringBuilder("Configuring Relay Message Transport : " + assignedID); if (implAdvertisement != null) { configInfo.append("\n\tImplementation :"); configInfo.append("\n\t\tModule Spec ID: ").append(implAdvertisement.getModuleSpecID()); configInfo.append("\n\t\tImpl Description : ").append(implAdvertisement.getDescription()); configInfo.append("\n\t\tImpl URI : ").append(implAdvertisement.getUri()); configInfo.append("\n\t\tImpl Code : ").append(implAdvertisement.getCode()); } configInfo.append("\n\tGroup Params :"); configInfo.append("\n\t\tGroup : ").append(group); configInfo.append("\n\t\tPeer ID : ").append(group.getPeerID()); configInfo.append("\n\tConfiguration :"); configInfo.append("\n\t\tService Name : ").append(serviceName); configInfo.append("\n\t\tisServer : ").append(relayConfigAdv.isServerEnabled()); configInfo.append("\n\t\tisClient : ").append(relayConfigAdv.isClientEnabled()); LOG.config(configInfo.toString()); } } /** * {@inheritDoc} */ public int startApp(String[] args) { EndpointService endpoint = group.getEndpointService(); if (null == endpoint) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -