📄 beeptransport.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: BeepTransport.java,v 1.14 2002/01/03 01:18:42 bondolo Exp $
*/
package net.jxta.impl.endpoint.beep;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Enumeration;
import java.net.InetAddress;
import java.io.IOException;
import java.net.UnknownHostException;
import org.apache.log4j.Priority;
import org.apache.log4j.Category;
import org.beepcore.beep.core.ProfileRegistry;
import org.beepcore.beep.core.SessionTuningProperties;
import org.beepcore.beep.core.Session;
import org.beepcore.beep.profile.ProfileConfiguration;
import org.beepcore.beep.transport.tcp.AutomatedTCPSessionCreator;
import org.beepcore.beep.util.ConsoleLog;
import org.beepcore.beep.util.Log;
import org.beepcore.beep.core.BEEPException;
import net.jxta.document.Advertisement;
import net.jxta.document.Element;
import net.jxta.document.TextElement;
import net.jxta.exception.PeerGroupException;
import net.jxta.id.ID;
import net.jxta.peergroup.PeerGroup;
import net.jxta.endpoint.EndpointService;
import net.jxta.endpoint.EndpointAddress;
import net.jxta.endpoint.EndpointMessenger;
import net.jxta.endpoint.EndpointProtocol;
import net.jxta.endpoint.Message;
import net.jxta.protocol.EndpointAdvertisement;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.protocol.TransportAdvertisement;
import net.jxta.impl.endpoint.Address;
import net.jxta.impl.protocol.BeepAdv;
/***
* This class implements the Beep Transport Protocol
**/
public class BeepTransport implements EndpointProtocol {
private static final Category LOG = Category.getInstance(BeepTransport.class.getName());
private static final long PINGDELAY = 2000; // 2 seconds
/**
* Peer EndpointService on whose behalf we are doing the protocol thing.
**/
private EndpointService endpoint;
/**
* Advertismeent of the Transport EndpointService
**/
private Advertisement implAdv;
/**
* Beep specific portion.
**/
private BeepAdv beepAdv;
/**
* The IP address we will try to bind to locally.
**/
private String localAddress;
/**
* The network interface we are bound to locally.
**/
private InetAddress usingInterface;
/**
* The port we are using.
**/
private int useTcpPort;
/**
* If not specified in the advertisement then constructed locally.
* This is the address we are known to the world as. Because of NAT
* port forwarding, this may not be the same address as our local
* address.
**/
private String publicAddress;
/**
* Our publicAddress as an EndpointAddress
**/
private Address ourAddress;
/**
* Registry of profiles supported for this transport.
**/
ProfileRegistry reg;
/**
* The active sessions
**/
HashMap activeSessions = new HashMap();
/**
* Constructor for BeepTransport.
*
**/
public BeepTransport() {
}
public boolean allowOverLoad() {
return false;
}
// Temporary
public int startApp(String[] args) { return 1; }
public void stopApp() {}
public void init(PeerGroup group, ID assignedID, Advertisement impl)
throws PeerGroupException {
ModuleImplAdvertisement implAdv = (ModuleImplAdvertisement) impl;
PeerAdvertisement configAdv = (PeerAdvertisement)
group.getConfigAdvertisement();
// Get our peer-defined parameters in the configAdv
Element param = configAdv.getServiceParam(assignedID);
// FIXME 20011220 bondolo@jxta.org Temporarily accept both nodes of
// type TCPAdv and TransportAdvertisement.
Enumeration beepChilds = param.getChildren(
TransportAdvertisement.getAdvertisementType());
// get the TransportAdv from either TransportAdv or HttpAdv
if( beepChilds.hasMoreElements() ) {
beepAdv = (BeepAdv) beepChilds.nextElement();
} else {
beepChilds = param.getChildren(
BeepAdv.getAdvertisementType());
if( beepChilds.hasMoreElements() ) {
beepAdv = (BeepAdv) beepChilds.nextElement();
}
}
useTcpPort = beepAdv.getPort();
localAddress = beepAdv.getLocalAddress();
publicAddress = beepAdv.getPublicAddress();
try {
if ((localAddress == null) || localAddress.equalsIgnoreCase("TBD")) {
// This causes us to use a "random" TCP Interface
usingInterface = InetAddress.getLocalHost();
} else {
usingInterface = InetAddress.getByName( localAddress );
}
} catch( UnknownHostException iDontExist ) {
throw new RuntimeException( "Could not find IP address for local Host!" );
}
if ((publicAddress == null) || publicAddress.equalsIgnoreCase("TBD")) {
publicAddress = usingInterface.getHostAddress() + ":" + useTcpPort;
}
ourAddress = new Address();
ourAddress.setProtocolName( getProtocolName() );
ourAddress.setProtocolAddress( publicAddress );
if (LOG.isEnabledFor(Priority.INFO)) LOG.info("Configured Beep Transport");
if (LOG.isEnabledFor(Priority.INFO)) LOG.info(" Using Intf:" + usingInterface.getHostAddress() );
if (LOG.isEnabledFor(Priority.INFO)) LOG.info(" Using Port:"+ useTcpPort );
if (LOG.isEnabledFor(Priority.INFO)) LOG.info(" Local Addr:"+ ourAddress );
// Get the BEEP logging going.
if( LOG.getChainedPriority().equals( Priority.DEBUG ) ) {
ConsoleLog log = new ConsoleLog();
log.setSeverity( Log.SEV_DEBUG_VERBOSE );
Log.setLogService( log );
}
endpoint = group.getEndpointService();
String classname = "net.jxta.impl.endpoint.beep.JxtaBeepProfile";
reg = new ProfileRegistry();
SessionTuningProperties tuning = new SessionTuningProperties();
ProfileConfiguration profileConfig = new ProfileConfiguration();
// load the profile class
JxtaBeepProfile p;
try {
try {
p = (JxtaBeepProfile) Class.forName(classname).newInstance();
} catch (IllegalAccessException e) {
throw new Exception("Class " + classname + " cannot be constructed.");
} catch (InstantiationException e) {
throw new Exception("Class " + classname + " returned an error");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -