Project JXTA

net.jxta.impl.endpoint
Class LoopbackMessenger

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.LoopbackMessenger
All Implemented Interfaces:
Messenger, SimpleSelectable
Direct Known Subclasses:
TlsTransport.TlsLoopbackMessenger

public class LoopbackMessenger
extends BlockingMessenger

This class implements local delivery of messages ( for example when the InputPipe and the OutputPipe are located on the same peer)

The reason this class is useful is that it may not always be possible to connect to oneself without actually going to a relay. If your peer is an http client, it is not able to connect to self through the normal http transport.

Since transports cannot be relied on to perform a loopback, some layer above has to figure out that a message is looping back. Since peerid loopback does not explicitly request to go through a real transport, and since peerid addressing is the job of the router, it is the router that performs loopback.

The router could probably perform the lookback by delivering the message to its own input queue, that would take a special transport instead of a special messenger, which is the same kind of deal but would imply some incoming message processing by the router for every message. In contrast, the loopback messenger is setup once and the router will never sees the messages. That's a good optimization.

Alternatively, the endpoint service itself could figure out the loopback, but since the API wants to give a messenger to the requestor rather than just sending a message, the endpoint would have to setup a loopback messenger anyway. So it is pretty much the same.

Anyone with a better way, speak up. J-C


Nested Class Summary
 
Nested classes/interfaces inherited from interface net.jxta.util.SimpleSelectable
SimpleSelectable.IdentityReference
 
Field Summary
(package private)  EndpointService endpoint
          The endpoint we are working for, ie. that we will loop back to.
 
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
LoopbackMessenger(EndpointService ep, EndpointAddress src, EndpointAddress dest, EndpointAddress logicalDest)
          Create a new loopback messenger.
 
Method Summary
 void closeImpl()
          Close connection.
 EndpointAddress getLogicalDestinationImpl()
          Obtain the logical destination address from the implementer (a transport for example).
 boolean isIdleImpl()
          return true if this messenger has not been used for a long time.
 boolean sendMessageBImpl(Message message, String service, String serviceParam)
          Sends a message to the destination
 
Methods inherited from class net.jxta.impl.endpoint.BlockingMessenger
close, getChannelMessenger, getDestAddressToUse, getLogicalDestinationAddress, getState, isClosed, 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

endpoint

EndpointService endpoint
The endpoint we are working for, ie. that we will loop back to.

Constructor Detail

LoopbackMessenger

public LoopbackMessenger(EndpointService ep,
                         EndpointAddress src,
                         EndpointAddress dest,
                         EndpointAddress logicalDest)
Create a new loopback messenger.

Parameters:
ep - where messages go
src - who messages should be addressed from
dest - who messages should be addressed to
Method Detail

getLogicalDestinationImpl

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

Specified by:
getLogicalDestinationImpl in class BlockingMessenger

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.

Specified by:
isIdleImpl in class BlockingMessenger

closeImpl

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

Specified by:
closeImpl 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:
message - the message to send.
service - Optionally replaces the service in the destination address. If null then the destination address's default service will be used.
serviceParam - 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