📄 tcpconfig.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: TcpConfig.java,v 1.2 2002/03/04 21:42:56 echtcherbina Exp $
*/
package net.jxta.impl.config;
import net.jxta.document.*;
import java.util.Enumeration;
import net.jxta.endpoint.EndpointAddress;
import net.jxta.impl.endpoint.Address;
import java.io.IOException;
/**
* Sub-configuration for TCP-specific settings. Instances of this class
* are normally contained within a Config instance.
*
**/
public class TcpConfig {
/** The default TCP server port **/
private static final int DEFAULT_PORT = 6001;
/** The default multicast hostname **/
private static final String MCAST_HOST = "224.0.1.85";
/** The default multicast port **/
private static final int MCAST_PORT = 1234;
/** The default multicast datagram size **/
private static final int MCAST_DGRAM_SIZE = 8192;
private static final String DEFAULT_IMPL =
"net.jxta.impl.endpoint.tcp.TcpTransport";
/** Server listening address **/
private EndpointAddress mServerAddress;
/** multicast address for mcast discovery **/
private EndpointAddress mMulticastAddress;
/** multicast bufferlen for mcast discovery datagrams **/
private int mMulticastBufferLen;
/** tells whether to use the public address **/
private boolean mUsePublicAddress;
/** The address I want to expose publicly. **/
private EndpointAddress mPublicAddress;
/** The local address the interface must be bound to **/
private EndpointAddress localAddress;
/** Classname for the TCP transport implementation **/
private String mImplClassname;
// XML tag names
private static final String KEY_SERVER = "Server";
private static final String KEY_MCAST = "Multicast";
private static final String KEY_MCAST_BUFF = "MulticastBufferLen";
private static final String KEY_IMPL = "Impl";
/** Writes the configuration to XML **/
public void writeToXML( StructuredTextDocument doc ) {
Element parent = doc.createElement( ConfigConstants.KEY_TCP_TRANSPORT );
doc.appendChild( parent );
ConfigUtil.writeAddress( doc, parent, mServerAddress, KEY_SERVER );
ConfigUtil.writeAddress( doc, parent, mMulticastAddress, KEY_MCAST );
Element e = doc.createElement( KEY_MCAST_BUFF, String.valueOf(
mMulticastBufferLen ) );
parent.appendChild( e );
ConfigUtil.writeAddress( doc, parent, mPublicAddress, ConfigConstants.KEY_PUBLICADDR );
e = doc.createElement(ConfigConstants.KEY_USE_PUBLICADDR,
String.valueOf( mUsePublicAddress ) );
parent.appendChild( e );
ConfigUtil.writeAddress( doc, parent, localAddress, ConfigConstants.KEY_LOCALADDR );
e = doc.createElement( KEY_IMPL, mImplClassname );
parent.appendChild( e );
}
/** Creates a TcpConfig with the default settings **/
public static TcpConfig createDefaultConfig() {
TcpConfig conf = new TcpConfig();
conf.setServerAddress( new Address( "tcp://localhost:"+DEFAULT_PORT));
conf.setMulticastAddress( new Address( "tcp://"+MCAST_HOST+":"+MCAST_PORT ));
conf.setMulticastBufferLen( MCAST_DGRAM_SIZE );
conf.setLocalAddress( null );
conf.setPublicAddress( null );
conf.setUsePublicAddress( false );
conf.setImplClassname( DEFAULT_IMPL );
return conf;
}
/** Creates a new HttpConfig instance given a parent "Http" element **/
public static TcpConfig newFromXML( TextElement parent ) {
Enumeration enum = parent.getChildren();
if ( enum == null ) return null;
TcpConfig conf = new TcpConfig();
while ( enum.hasMoreElements() ) {
TextElement e = (TextElement) enum.nextElement();
String elname = e.getName();
if ( elname.equals( KEY_IMPL ) )
conf.mImplClassname = e.getTextValue();
if ( elname.equals( KEY_MCAST_BUFF ) )
conf.mMulticastBufferLen = Integer.valueOf( e.getTextValue() ).intValue();
else if ( elname.equals( KEY_SERVER ) )
conf.mServerAddress = new Address( e.getTextValue() );
else if ( elname.equals( KEY_MCAST ) )
conf.mMulticastAddress = new Address( e.getTextValue() );
else if ( elname.equals( ConfigConstants.KEY_PUBLICADDR ) )
conf.mPublicAddress = new Address( e.getTextValue() );
else if ( elname.equals( ConfigConstants.KEY_USE_PUBLICADDR ) )
conf.mUsePublicAddress = Boolean.valueOf( e.getTextValue() ).booleanValue();
else if ( elname.equals( ConfigConstants.KEY_LOCALADDR ) )
conf.localAddress = new Address ( e.getTextValue() );
}
return conf;
}
/** Gets the EndpointAddress for the local TCP server **/
public EndpointAddress getServerAddress() {
return mServerAddress;
}
/** Sets the EndpointAddress for the local TCP server **/
public void setServerAddress( EndpointAddress aAddress ) {
mServerAddress = aAddress;
}
/** Gets the EndpointAddress for the multicast channel **/
public EndpointAddress getMulticastAddress() {
return mMulticastAddress;
}
/** Sets the EndpointAddress for the multicast channel **/
public void setMulticastAddress( EndpointAddress aMulticastAddress ) {
mMulticastAddress = aMulticastAddress;
}
/** Gets the buffer length for multicast datagrams **/
public int getMulticastBufferLen() {
return mMulticastBufferLen;
}
/** Sets the buffer length for multicast datagrams **/
public void setMulticastBufferLen( int aBufferLen ) {
mMulticastBufferLen = aBufferLen;
}
/** Gets the EndpointAddress for the local address.
May be null. **/
public EndpointAddress getLocalAddress() {
return localAddress;
}
/** Sets the EndpointAddress for the local IP address I want to expose. **/
public void setLocalAddress( EndpointAddress aLocalAddress ) {
localAddress = aLocalAddress;
}
/** Gets the EndpointAddress for the public TCP address I want to expose.
May be null, unless getUsePublicAddres()==true. **/
public EndpointAddress getPublicAddress() {
return mPublicAddress;
}
/** Sets the EndpointAddress for the public TCP address I want to expose. This
must be set if getUsePublicAddres()==true **/
public void setPublicAddress( EndpointAddress aPublicAddress ) {
mPublicAddress = aPublicAddress;
}
/** Tells whether to use the PublicAddress settings. **/
public boolean getUsePublicAddress() {
return mUsePublicAddress;
}
/** Sets whether to use the PublicAddress settings. **/
public void setUsePublicAddress( boolean aUsePublicAddress ) {
mUsePublicAddress = aUsePublicAddress;
}
/** Sets the classname for the Tcp transport implementation class **/
public String getImplClassname() {
return mImplClassname;
}
/** Sets the classname for the Tcp transport implementation class **/
public void setImplClassname( String aImplClassname ) {
mImplClassname = aImplClassname;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -