Project JXTA

net.jxta.impl.endpoint.tcp
Class TcpMessenger

java.lang.Object
  extended by net.jxta.util.AbstractSimpleSelectable
      extended by net.jxta.endpoint.AbstractMessenger
          extended by net.jxta.impl.endpoint.BlockingMessenger
              extended by net.jxta.impl.endpoint.tcp.TcpMessenger
All Implemented Interfaces:
Messenger, SimpleSelectable

 class TcpMessenger
extends BlockingMessenger

Implements a messenger which sends messages via raw TCP sockets.

FIXME jice@jxta.org 20021007: Although in theory not too clean, we could merge connection and messenger. there is a one-to-one mapping between them except that for incoming connections we sometimes throw the messenger away. Merging them would add only a message element and an endpoint address to the connection object and would simplify close() isclosed() and GC's life quite a bit. (Look Ma, no synch ! All synchronized() have been removed. With the help of a volatile reference to the TcpConnection, this is no longer necessary this optimizes at least one critical function: isClosed().


Nested Class Summary
 
Nested classes/interfaces inherited from interface net.jxta.util.SimpleSelectable
SimpleSelectable.IdentityReference
 
Field Summary
(package private)  TcpConnection conn
          The connection
(package private)  boolean incoming
          If this is an incoming connection we must not close it when this messenger disapears.
 
Fields inherited from class net.jxta.endpoint.AbstractMessenger
DEFAULT_MTU, dstAddress
 
Fields inherited from class net.jxta.util.AbstractSimpleSelectable
identityReference
 
Fields inherited from interface net.jxta.endpoint.Messenger
ANYSTATE, BREAKING, BROKEN, CLOSED, CLOSING, CONNECTED, DISCONNECTED, DISCONNECTING, IDLE, RECONCLOSING, RECONNECTING, RECONSATURATED, RESOLCLOSING, RESOLPENDING, RESOLSATURATED, RESOLVED, RESOLVING, SATURATED, SENDING, SENDINGSATURATED, TERMINAL, UNRESOLVABLE, UNRESOLVED, UNRESOLVING, USABLE
 
Constructor Summary
TcpMessenger(EndpointAddress destaddr, TcpConnection conn, TcpTransport p)
          Create a new TcpNonBlockingMessenger for the specified address.
TcpMessenger(EndpointAddress destaddr, TcpTransport p)
          Create a new TcpNonBlockingMessenger for the specified address.
 
Method Summary
 void closeImpl()
          Close connection.
 EndpointAddress getLogicalDestinationImpl()
          Obtain the logical destination address from the implementer (a transport for example).
(package private)  TransportBindingMeter getTransportBindingMeter()
           
 boolean isClosed()
          We overload isClosed because many messengers still invoke super.isClosed() for their own implementation and they expect it to be true only when all is shutdown; not while we're closing gently.
 boolean isIdleImpl()
          return true if this messenger has not been used for a long time. Since we probe the connection status, we'll keep a messenger as long as the connection is active, even if only on the incoming side.
 boolean sendMessageBImpl(Message message, String service, String serviceParam)
          Sends a message to the destination
protected  void start()
          Starts the underlying connection receive thread if any.
 
Methods inherited from class net.jxta.impl.endpoint.BlockingMessenger
close, getChannelMessenger, getDestAddressToUse, getLogicalDestinationAddress, getState, resolve, sendMessageB, sendMessageN, setOwner, shutdown
 
Methods inherited from class net.jxta.endpoint.AbstractMessenger
flush, getDestinationAddress, getDestinationAddressObject, getMTU, isIdle, isSynchronous, itemChanged, sendMessage, sendMessage, sendMessage, setStateLock, waitState
 
Methods inherited from class net.jxta.util.AbstractSimpleSelectable
getIdentityReference, haveListeners, notifyChange, register, registerListener, unregister, unregisterListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.jxta.util.SimpleSelectable
getIdentityReference, register, unregister
 

Field Detail

conn

volatile TcpConnection conn
The connection


incoming

boolean incoming
If this is an incoming connection we must not close it when this messenger disapears. It has many reasons to disappear while the connection must keep receiving messages. This is causing some problems for incoming messengers that are managed by some entity, such as the router or the relay. These two do call close explicitly when they discard a messenger, and their intent is truely to nuke the connection. So basically we need to distinguish between incoming messengers that are abandonned without closing (for these we must protect the input side because that's the only reason for the connection being there) and incoming messengers that are explicitly closed (in which case we must let the entire connection be closed).

Constructor Detail

TcpMessenger

TcpMessenger(EndpointAddress destaddr,
             TcpConnection conn,
             TcpTransport p)
       throws IOException
Create a new TcpNonBlockingMessenger for the specified address.

Parameters:
destaddr - the destination of the messenger
p - the tcp MessageSender we are working for.
Throws:
IOException

TcpMessenger

TcpMessenger(EndpointAddress destaddr,
             TcpTransport p)
       throws IOException
Create a new TcpNonBlockingMessenger for the specified address.

Parameters:
destaddr - the destination of the messenger
p - the tcp MessageSender we are working for.
Throws:
IOException
Method Detail

start

protected void start()
Starts the underlying connection receive thread if any.


closeImpl

public void closeImpl()
Close connection. May fail current send.

Specified by:
closeImpl in class BlockingMessenger

isClosed

public boolean isClosed()
Description copied from class: BlockingMessenger
We overload isClosed because many messengers still invoke super.isClosed() for their own implementation and they expect it to be true only when all is shutdown; not while we're closing gently. FIXME - jice@jxta.org 20040413: transports should get a deeper retrofit eventually.

Specified by:
isClosed in interface Messenger
Overrides:
isClosed in class BlockingMessenger

getTransportBindingMeter

TransportBindingMeter getTransportBindingMeter()

isIdleImpl

public boolean isIdleImpl()
return true if this messenger has not been used for a long time. The definition of long time is: "so long that closing it is worth the risk of having to re-open". A messenger should self close if it thinks it meets the definition of idle. BlockingMessenger leaves the evaluation to the transport but does the work automatically. Important: if self destruction is used, this method must work; not just return false. See the constructor. In general, if closeImpl does not need to do anyhing, then self destruction is not needed. Since we probe the connection status, we'll keep a messenger as long as the connection is active, even if only on the incoming side. So we're being a bit nice to the other side. Anyway, incoming connections do not go away when the messenger does. There's a receive timeout for that.

Specified by:
isIdleImpl in class BlockingMessenger

getLogicalDestinationImpl

public EndpointAddress getLogicalDestinationImpl()
Obtain the logical destination address from the implementer (a transport for example).

Specified by:
getLogicalDestinationImpl in class BlockingMessenger

sendMessageBImpl

public boolean sendMessageBImpl(Message message,
                                String service,
                                String serviceParam)
                         throws IOException
Sends a message to the destination

Specified by:
sendMessageBImpl in class BlockingMessenger
Parameters:
msg - the message to send.
destService - Optionally replaces the service in the destination address. If null then the destination address's default service will be used.
destServiceParam - Optionally replaces the service param in the destination address. If null then the destination address's default service parameter will be used.
Throws:
IOException

JXTA J2SE