JXTA

net.jxta.impl.endpoint.tcp
Class TcpTransport

java.lang.Object
  extended by net.jxta.impl.endpoint.tcp.TcpTransport
All Implemented Interfaces:
MessageReceiver, MessageSender, MessageTransport, Module

public class TcpTransport
extends Object
implements Module, MessageSender, MessageReceiver

This class implements the TCP Message Transport.

See Also:
MessageTransport, MessagePropagater, MessageReceiver, MessageSender, EndpointService, JXTA Protocols Specification : Standard JXTA Transport Bindings

Field Summary
(package private) static int connectionTimeOut
          Connection timeout use the same system property defined by URLconnection, otherwise default to 10 seconds.
(package private)  EndpointService endpoint
           
(package private)  Executor executor
           
(package private)  PeerGroup group
           
(package private) static int LingerDelay
          The amount of time the socket "lingers" after we close it locally.
protected static int MAX_WRITE_SELECTORS
          The maximum number of write selectors we will maintain in our cache per transport instance.
(package private) static int MaxAcceptCnxBacklog
           
(package private)  Selector messengerSelector
           
(package private)  ThreadGroup myThreadGroup
          This is the thread group into which we will place all of the threads we create.
(package private) static int RecvBufferSize
          The TCP receive buffer size
(package private) static int SendBufferSize
          The TCP send buffer size.
(package private)  InetAddress usingInterface
           
 
Fields inherited from interface net.jxta.platform.Module
START_AGAIN_PROGRESS, START_AGAIN_STALLED, START_DISABLED, START_OK
 
Constructor Summary
TcpTransport()
          Construct a new TcpTransport instance
 
Method Summary
 boolean allowsRouting()
          Returns true if the Message Transport can be used by the EndpointRouter.
 boolean equals(Object target)
          
 long getBytesReceived()
          Gets the number of 'bytesReceived'.
 long getBytesSent()
          Gets the number of 'bytesSent'.
 long getConnectionsAccepted()
          Gets the number of 'connectionsAccepted'.
 EndpointService getEndpointService()
          Returns the endpoint service with which this MessageTransport is registered.
 long getMessagesReceived()
          Gets the number of 'messagesReceived'.
 long getMessagesSent()
          Gets the number of 'messagesSent'.
 Messenger getMessenger(EndpointAddress dst, Object hintIgnored)
          Return a Messenger for sending messages to the specified destination EndpointAddress.
 Messenger getMessenger(EndpointAddress dst, Object hintIgnored, boolean selfDestruct)
          
 String getProtocolName()
          Returns a String containing the name of the protocol used by this MessageTransport.
 EndpointAddress getPublicAddress()
          Returns the EndpointAddress which will be used as the source address for all messages sent by this message sender.
 Iterator<EndpointAddress> getPublicAddresses()
          Returns an Iterator of all of the EndpointAddresses by which this MessageReceiver is reachable.
(package private)  int getRestrictionPort()
          Getter for property 'restrictionPort'.
(package private)  Selector getSelector()
          Get a write selector from the cache.
(package private)  IncomingUnicastServer getServer()
          Getter for property 'server'.
(package private)  TransportBindingMeter getUnicastTransportBindingMeter(PeerID peerID, EndpointAddress destinationAddress)
           
 int hashCode()
          
 void incrementBytesReceived(long bytes)
          increment the number of bytes received
 void incrementBytesSent(long bytes)
          increment the number of bytes sent
 void incrementConnectionsAccepted()
          increment the number of connectionsAccepted sent by 1
 void incrementMessagesReceived()
          increment the number of messages received by 1
 void incrementMessagesSent()
          increment the number of messages sent by 1
 void init(PeerGroup group, ID assignedID, Advertisement impl)
          Initialize the module, passing it its peer group and advertisement.
 boolean isConnectionOriented()
          Returns true if the Message Transport is connection oriented (like TCP/IP).
(package private)  void messengerReadyEvent(Messenger newMessenger, EndpointAddress connAddr)
           
 boolean ping(EndpointAddress addr)
          Returns true if the specified destination address is reachable via this Message Transport otherwise returns false.
(package private)  void register(SocketChannel channel, TcpMessenger messenger)
          Registers the channel with the Read selector and attaches the messenger to the channel
(package private)  void returnSelector(Selector selector)
          Return the Selector to the cache
 int startApp(String[] arg)
          Complete any remaining initialization of the module.
 void stopApp()
          Stop a module.
 Object transportControl(Object operation, Object Value)
          Pass a transport control object to a message transport.
(package private)  void unregister(SocketChannel channel)
          Unregisters the channel with the Read selector
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SendBufferSize

static final int SendBufferSize
The TCP send buffer size. The size of the buffer used to store outgoing messages This should be set to the maximum message size (smaller is allowed).

See Also:
Constant Field Values

RecvBufferSize

static final int RecvBufferSize
The TCP receive buffer size

See Also:
Constant Field Values

LingerDelay

static final int LingerDelay
The amount of time the socket "lingers" after we close it locally. Linger enables the remote socket to finish receiving any pending data at its own rate. Note: LingerDelay time unit is seconds

See Also:
Constant Field Values

connectionTimeOut

static int connectionTimeOut
Connection timeout use the same system property defined by URLconnection, otherwise default to 10 seconds.


MaxAcceptCnxBacklog

static final int MaxAcceptCnxBacklog
See Also:
Constant Field Values

usingInterface

InetAddress usingInterface

group

PeerGroup group

endpoint

EndpointService endpoint

executor

Executor executor

messengerSelector

Selector messengerSelector

myThreadGroup

ThreadGroup myThreadGroup
This is the thread group into which we will place all of the threads we create. THIS HAS NO EFFECT ON SCHEDULING. Java thread groups are only for organization and naming.


MAX_WRITE_SELECTORS

protected static final int MAX_WRITE_SELECTORS
The maximum number of write selectors we will maintain in our cache per transport instance.

See Also:
Constant Field Values
Constructor Detail

TcpTransport

public TcpTransport()
Construct a new TcpTransport instance

Method Detail

getConnectionsAccepted

public long getConnectionsAccepted()
Gets the number of 'connectionsAccepted'.

Returns:
the number of 'connectionsAccepted'.

incrementConnectionsAccepted

public void incrementConnectionsAccepted()
increment the number of connectionsAccepted sent by 1


incrementMessagesSent

public void incrementMessagesSent()
increment the number of messages sent by 1


incrementMessagesReceived

public void incrementMessagesReceived()
increment the number of messages received by 1


incrementBytesSent

public void incrementBytesSent(long bytes)
increment the number of bytes sent

Parameters:
bytes - the number of bytes to be added

incrementBytesReceived

public void incrementBytesReceived(long bytes)
increment the number of bytes received

Parameters:
bytes - the number of bytes to be added

getMessagesSent

public long getMessagesSent()
Gets the number of 'messagesSent'.

Returns:
the number of 'messagesSent'.

getMessagesReceived

public long getMessagesReceived()
Gets the number of 'messagesReceived'.

Returns:
the number of 'messagesReceived'.

getBytesSent

public long getBytesSent()
Gets the number of 'bytesSent'.

Returns:
the number of 'bytesSent'.

getBytesReceived

public long getBytesReceived()
Gets the number of 'bytesReceived'.

Returns:
the number of 'bytesReceived'.

equals

public boolean equals(Object target)

Overrides:
equals in class Object

hashCode

public int hashCode()

Overrides:
hashCode in class Object

init

public void init(PeerGroup group,
                 ID assignedID,
                 Advertisement impl)
          throws PeerGroupException
Initialize the module, passing it its peer group and advertisement.

Note: when subclassing one of the existing PeerGroup implementations (which implement Module), it may not be recommended to overload the init method. See the documentation of the PeerGroup class being subclassed.

Specified by:
init in interface Module
Parameters:
group - The PeerGroup from which this Module can obtain services. If this module is a Service, this is also the PeerGroup of which this module is a service.
assignedID - Identity of Module within group. modules can use it as a the root of their namespace to create names that are unique within the group but predictable by the same module on another peer. This is normally the ModuleClassID which is also the name under which the module is known by other modules. For a group it is the PeerGroupID itself. The parameters of a service, in the Peer configuration, are indexed by the assignedID of that service, and a Service must publish its run-time parameters in the Peer Advertisement under its assigned ID.
impl - The implementation advertisement for this Module. It is permissible to pass null if no implementation advertisement is available. This may happen if the implementation was selected by explicit class name rather than by following an implementation advertisement. Modules are not required to support that style of loading, but if they do, then their documentation should mention it.
Throws:
PeerGroupException - This module failed to initialize.

startApp

public int startApp(String[] arg)
Complete any remaining initialization of the module. The module should be fully functional after startApp() is completed. That is also the opportunity to supply arbitrary arguments (mostly to applications).

If this module is a PeerGroup service, it may be invoked several times depending on its return value.

Specified by:
startApp in interface Module
Parameters:
arg - An array of Strings forming the parameters for this Module.
Returns:
int A status indication which may be one of Module.START_OK, Module.START_AGAIN_PROGRESS, Module.START_AGAIN_STALLED, which indicates partial or complete success, or any other value (negative values are recommended for future compatibility), which indicates failure.

stopApp

public void stopApp()
Stop a module. This may be called any time after init() completes and should not assume that startApp() has been called or completed.

The Module cannot be forced to comply, but in the future we might be able to deny it access to anything after some timeout.

Specified by:
stopApp in interface Module

getProtocolName

public String getProtocolName()
Returns a String containing the name of the protocol used by this MessageTransport. The value will match the "protocol" portion of all EndpointAddress.

Specified by:
getProtocolName in interface MessageTransport
Returns:
a String containing the name of the protocol used by this MessageTransport.

getPublicAddress

public EndpointAddress getPublicAddress()
Returns the EndpointAddress which will be used as the source address for all messages sent by this message sender. This is the "preferred" address to which replies should be sent. This address is not necessarily the best or only address by which the peer may be reached.

The public address may also be for a different message transport.

Specified by:
getPublicAddress in interface MessageSender
Returns:
an EndpointAddress containing the public address for this message receiver.

getEndpointService

public EndpointService getEndpointService()
Returns the endpoint service with which this MessageTransport is registered. If it is unregistered then null will be returned.

Specified by:
getEndpointService in interface MessageTransport
Returns:
the EndpointService with which this MessageTransport is registered.

transportControl

public Object transportControl(Object operation,
                               Object Value)
Pass a transport control object to a message transport.

Specified by:
transportControl in interface MessageTransport
Parameters:
operation - Object that specifies the type of control operation to be performed
Value - Object that specifies a value object associated with the control operation
Returns:
returned object

getPublicAddresses

public Iterator<EndpointAddress> getPublicAddresses()
Returns an Iterator of all of the EndpointAddresses by which this MessageReceiver is reachable. The list is in order of "preference" with the most "preferred" EndpointAddress being at the beginning of the list.

Specified by:
getPublicAddresses in interface MessageReceiver
Returns:
an Iterator of EndpointAddresses.

isConnectionOriented

public boolean isConnectionOriented()
Returns true if the Message Transport is connection oriented (like TCP/IP). Indicates that the Message Transport can provide efficient transport of a series of messages to the same destination.

Specified by:
isConnectionOriented in interface MessageSender
Returns:
true if the Message Transport is connection oriented.

allowsRouting

public boolean allowsRouting()
Returns true if the Message Transport can be used by the EndpointRouter. Indicates that the Message Transport can be used in the routing of messages to destinations which are not directly reachable via this transport.

More specifically, this Message Transport will be used to route messages who's final destination is not one of the endpoint addresses available from getReachableEndpointAddresses.

Specified by:
allowsRouting in interface MessageSender
Returns:
true if the protocol can be used by the EndpointRouter

getMessenger

public Messenger getMessenger(EndpointAddress dst,
                              Object hintIgnored)
Description copied from interface: net.jxta.endpoint.MessageSender
Return a Messenger for sending messages to the specified destination EndpointAddress.

Specified by:
getMessenger in interface MessageSender
Parameters:
dst - The destination address for which a messenger is requested.
hintIgnored - An optional hint for the transport to use when creating the messenger. The format of the hint is specific to each Message Transport and may be null if no hint is provided.
Returns:
a Messenger or null if the destination is not reachable.

getMessenger

public Messenger getMessenger(EndpointAddress dst,
                              Object hintIgnored,
                              boolean selfDestruct)


ping

public boolean ping(EndpointAddress addr)
Returns true if the specified destination address is reachable via this Message Transport otherwise returns false.

This implementation tries to open a connection, and after tests the result.

Specified by:
ping in interface MessageSender
Parameters:
addr - Address to ping
Returns:
true if the specified destination address is reachable via this Message Transport otherwise returns false.

getRestrictionPort

int getRestrictionPort()
Getter for property 'restrictionPort'.

Returns:
Value for property 'restrictionPort'.

getUnicastTransportBindingMeter

TransportBindingMeter getUnicastTransportBindingMeter(PeerID peerID,
                                                      EndpointAddress destinationAddress)

messengerReadyEvent

void messengerReadyEvent(Messenger newMessenger,
                         EndpointAddress connAddr)

getServer

IncomingUnicastServer getServer()
Getter for property 'server'.

Returns:
Value for property 'server'.

getSelector

Selector getSelector()
               throws InterruptedException
Get a write selector from the cache.

Returns:
A write selector.
Throws:
InterruptedException - If interrupted while waiting for a selector to become available.

returnSelector

void returnSelector(Selector selector)
Return the Selector to the cache

Parameters:
selector - the selector to put back into the pool

register

void register(SocketChannel channel,
              TcpMessenger messenger)
Registers the channel with the Read selector and attaches the messenger to the channel

Parameters:
channel - the socket channel.
messenger - the messenger to attach to the channel.

unregister

void unregister(SocketChannel channel)
Unregisters the channel with the Read selector

Parameters:
channel - the socket channel.

JXSE