Project JXTA

net.jxta.impl.endpoint.router
Class EndpointRouter

java.lang.Object
  extended by net.jxta.impl.endpoint.router.EndpointRouter
All Implemented Interfaces:
EventListener, EndpointListener, MessageReceiver, MessageSender, MessageTransport, MessengerEventListener, Module

public class EndpointRouter
extends Object
implements EndpointListener, MessageReceiver, MessageSender, MessengerEventListener, Module


Nested Class Summary
protected  class EndpointRouter.ClearPendingQuery
           
 
Field Summary
static long ASYNC_MESSENGER_WAIT
          how long we are willing to wait for a response from an async getMessenger.
(package private)  EndpointService endpoint
          endpointservice handle
static Integer GET_ROUTE_CONTROL
          MessageTransport Control operation
protected  EndpointAddress localPeerAddr
          local peer ID as a endpointAddress.
protected  Map pendingQueries
          We record queries when first started and keep them pending for a while.
static int RouteControlOp
           
static String routerSName
          Router Service Name
 
Fields inherited from interface net.jxta.platform.Module
START_AGAIN_PROGRESS, START_AGAIN_STALLED, START_OK
 
Constructor Summary
EndpointRouter()
          Default constructor
 
Method Summary
protected static PeerID addr2pid(EndpointAddress addr)
          convert an endpointRouterAddress into a PeerID
(package private)  EndpointAddress addressMessage(Message message, EndpointAddress dstAddress)
          Updates the router element of a message and returns the pid address of the next hop (where to send the message).
 boolean allowsRouting()
          
protected  List cleanupAnyEdges(String src, List results)
          cleanup any edge peers when trying to forward an SRDI query so we are guaranteed to the best of our knowledge that the peer is a rendezvous.
protected  Messenger ensureLocalRoute(EndpointAddress pId, Object hint)
          Ensure there is a local route for a given peer id if it can at all be done.
(package private)  Messenger findBestReachableEndpoint(EndpointAddress dest, List mightWork, boolean exist)
          Given a list of addresses, find the best reachable endpoint.
(package private)  Messenger findReachableEndpoint(EndpointAddress destPeer, boolean exist)
          Read the route advertisement for a peer and find a suitable transport endpoint for sending to that peere either directly or via one of the advertised peer router
(package private)  Messenger findReachableEndpoint(EndpointAddress destPeer, boolean exist, Object hint)
           
protected  Iterator getAllCachedMessengerDestinations()
          Get all direct route destinations
protected  Iterator getAllRoutedRouteAddresses()
          Get all long route destination addresses
protected  BadRoute getBadRoute(EndpointAddress addr)
          get bad route entry
protected  Messenger getCachedMessenger(EndpointAddress addr)
          Snoop if we have a messenger
 EndpointService getEndpointService()
          
(package private)  EndpointAddress getGatewayAddress(EndpointAddress dest, boolean seekRoute)
          Given a peer id, return an address to reach that peer.
(package private)  EndpointAddress getGatewayAddress(EndpointAddress dest, boolean seekRoute, Object hint)
           
 Messenger getMessenger(EndpointAddress addr, Object hint)
          
protected  RouteAdvertisement getMyLocalRoute()
           
protected  Iterator getPendingQueriesAllDestinations()
          Get all pendingRouteQuery destinations
protected  EndpointRouter.ClearPendingQuery getPendingRouteQuery(EndpointAddress addr)
          get a pending route query info
 String getProtocolName()
          
 EndpointAddress getPublicAddress()
          
 Iterator getPublicAddresses()
          
protected  RouteAdvertisement getRoute(EndpointAddress pId, boolean seekRoute)
          Get the routed route, if any, for a given peer id.
protected  RouteCM getRouteCM()
          Get the route CM cache Manager
protected  Iterator getRoutedRouteAllDestinations()
          Get all long route destinations
protected  RouteResolver getRouteResolver()
          Get the route resolver manager
 void init(PeerGroup g, ID assignedID, Advertisement impl)
          
 boolean isConnectionOriented()
          
protected  boolean isLocalRoute(EndpointAddress pId)
          isLocalRoute is a shalow test.
protected  boolean isPendingRouteQuery(EndpointAddress addr)
          is there a pending route query for that destination
 boolean isPropagateEnabled()
          (@inheritdoc}
 boolean isPropagationSupported()
          (@inheritdoc}
protected  boolean isRoutedRoute(EndpointAddress addr)
          Do we have a longue route for that destination
 boolean messengerReady(MessengerEvent event)
          Receives notifications of new messengers being generated by the underlying network transports.
 boolean newMessenger(MessengerEvent event)
          call when an asynchronous new messenger is ready.
 void noMessenger(EndpointAddress logDest)
          call when an asynchronous new messenger could not be obtained.
protected static EndpointAddress pid2addr(PeerID pid)
          convert a PeerID into an EndpointRouter Address
 boolean ping(EndpointAddress addr)
          Returns true if the target address is reachable.
 void processIncomingMessage(Message msg, EndpointAddress srcAddr, EndpointAddress dstAddr)
          
 void propagate(Message srcMsg, String pName, String pParam, String prunePeer)
          
protected  void removeRoute(EndpointAddress pId)
          This method is used to remove a route
(package private)  void sendOnLocalRoute(EndpointAddress destination, Message message)
          Send a message to a given logical destination if it maps to some messenger in our messenger pool or if such a mapping can be found and added.
protected  void setBadRoute(EndpointAddress addr, BadRoute badRoute)
          set bad route entry
protected  boolean setRoute(RouteAdvertisement r, boolean force)
          set new route info
 int startApp(String[] arg)
          
 void stopApp()
          

Careful that stopApp() could in theory be called before startApp().

 Object transportControl(Object operation, Object value)
          
protected  void updateRouteAdv(RouteAdvertisement route)
          check if it is a new route adv
protected  void updateRouteAdv(RouteAdvertisement route, boolean force)
          check if it is a new route adv
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

localPeerAddr

protected EndpointAddress localPeerAddr
local peer ID as a endpointAddress.


endpoint

EndpointService endpoint
endpointservice handle


routerSName

public static final String routerSName
Router Service Name

See Also:
Constant Field Values

pendingQueries

protected Map pendingQueries
We record queries when first started and keep them pending for a while. Threads coming in the meanwhile wait for a result without initiating a query. Thus threads may wait passed the point where the query is no-longer pending, and, although they could initiate a new one, they do not. However, other threads coming later may initiate a new query. So a query is not re-initiated systematically on a fixed schedule. This mechanism also serves to avoid infinite recursions if we're looking for the route to a rendezvous (route queries will try to go there themselves). FIXME: jice@jxta.org 20020903 this is approximate. We can do cleaner/better than that, but it's an inexpensive improvement over what was there before. FIXME: tra@jxta.org 20030818 the pending hashmap should be moved in the routeResolver class as this will allow to only synchronize on the routeResolver object rather than the router object.


GET_ROUTE_CONTROL

public static final Integer GET_ROUTE_CONTROL
MessageTransport Control operation


RouteControlOp

public static final int RouteControlOp
See Also:
Constant Field Values

ASYNC_MESSENGER_WAIT

public static final long ASYNC_MESSENGER_WAIT
how long we are willing to wait for a response from an async getMessenger. We do not wait long at all because it is non-critical that we get the answer synchronously. The goal is to avoid starting a route discovery if there's a chance to get a direct connection. However, we will still take advantage of the direct route if it is found while we wait for the route discovery result. If that happens, the only wrong is that we used some bandwidth doing a route discovery that wasn't needed after all.

See Also:
Constant Field Values
Constructor Detail

EndpointRouter

public EndpointRouter()
Default constructor

Method Detail

getMyLocalRoute

protected RouteAdvertisement getMyLocalRoute()

isLocalRoute

protected boolean isLocalRoute(EndpointAddress pId)
isLocalRoute is a shalow test. It tells you that there used to be a local route that worked the last time it was tried.


ensureLocalRoute

protected Messenger ensureLocalRoute(EndpointAddress pId,
                                     Object hint)
Ensure there is a local route for a given peer id if it can at all be done.

Parameters:
pId - the peer who's route is desired.
hint - specify a specific route hint to use
Returns:
Messenger for local route. null if none could be found or created.

sendOnLocalRoute

void sendOnLocalRoute(EndpointAddress destination,
                      Message message)
                throws IOException
Send a message to a given logical destination if it maps to some messenger in our messenger pool or if such a mapping can be found and added.

Parameters:
destination - peer-based address to send the message to.
message - the message to be sent.
Throws:
IOException

init

public void init(PeerGroup g,
                 ID assignedID,
                 Advertisement impl)
          throws PeerGroupException

Specified by:
init in interface Module
Throws:
PeerGroupException

startApp

public int startApp(String[] arg)

Specified by:
startApp in interface Module

stopApp

public void stopApp()

Careful that stopApp() could in theory be called before startApp().

Specified by:
stopApp in interface Module

isConnectionOriented

public boolean isConnectionOriented()

Specified by:
isConnectionOriented in interface MessageSender

allowsRouting

public boolean allowsRouting()

Specified by:
allowsRouting in interface MessageSender

getEndpointService

public EndpointService getEndpointService()

Specified by:
getEndpointService in interface MessageTransport

getPublicAddress

public EndpointAddress getPublicAddress()

Specified by:
getPublicAddress in interface MessageSender

getPublicAddresses

public Iterator getPublicAddresses()

Specified by:
getPublicAddresses in interface MessageReceiver

getProtocolName

public String getProtocolName()

Specified by:
getProtocolName in interface MessageTransport

isPropagateEnabled

public boolean isPropagateEnabled()
(@inheritdoc}

Specified by:
isPropagateEnabled in interface MessageSender

isPropagationSupported

public boolean isPropagationSupported()
(@inheritdoc}

Specified by:
isPropagationSupported in interface MessageSender

propagate

public void propagate(Message srcMsg,
                      String pName,
                      String pParam,
                      String prunePeer)
               throws IOException

Specified by:
propagate in interface MessageSender
Throws:
IOException

getGatewayAddress

EndpointAddress getGatewayAddress(EndpointAddress dest,
                                  boolean seekRoute)
Given a peer id, return an address to reach that peer. The address may be for a directly reachable peer, or for the first gateway along a route to reach the peer. If we do not have a route to the peer, we will use the Peer Routing Protocol to try to discover one. We will wait up to 30 seconds for a route to be discovered.

Parameters:
dest - the peer we are trying to reach.
seekRoute - whether to go as far as issuing a route query, or just fish in our cache. when forwarding a message we allow ourselves to mend a broken source-issued route but we won't go as far as seeking one from other peers. When originating a message, on the other end we will aggressively try to find route.
hint - whether we are passed a route hint to be used, in that case that route hint should be used
Returns:
an EndpointAddress at which that peer should be reachable.

getGatewayAddress

EndpointAddress getGatewayAddress(EndpointAddress dest,
                                  boolean seekRoute,
                                  Object hint)

ping

public boolean ping(EndpointAddress addr)
Returns true if the target address is reachable. Otherwise returns false.

Specified by:
ping in interface MessageSender

messengerReady

public boolean messengerReady(MessengerEvent event)
Receives notifications of new messengers being generated by the underlying network transports.

IMPORTANT: Incoming messengers only. If/when this is used for outgoing, some things have to change:

For example we do not need to send the welcome msg, but for outgoing messengers, we would need to.

Currently, newMessenger handles the outgoing side.

Specified by:
messengerReady in interface MessengerEventListener
Parameters:
event - the new messenger event.

noMessenger

public void noMessenger(EndpointAddress logDest)
call when an asynchronous new messenger could not be obtained.

Parameters:
logDest - the failed logical destination

newMessenger

public boolean newMessenger(MessengerEvent event)
call when an asynchronous new messenger is ready. (name is not great).

Parameters:
event - the new messenger event.

getRoute

protected RouteAdvertisement getRoute(EndpointAddress pId,
                                      boolean seekRoute)
Get the routed route, if any, for a given peer id.

Parameters:
pId - the peer who's route is desired.
seekRoute - boolean to indicate if we should search for a route if we don't have one
Returns:
a route advertisement describing the direct route to the peer.

setRoute

protected boolean setRoute(RouteAdvertisement r,
                           boolean force)
set new route info

Parameters:
r - new route to learn
force - true if the route was optained by receiving a message

removeRoute

protected void removeRoute(EndpointAddress pId)
This method is used to remove a route

Parameters:
pId - route to peerid to be removed

processIncomingMessage

public void processIncomingMessage(Message msg,
                                   EndpointAddress srcAddr,
                                   EndpointAddress dstAddr)

Specified by:
processIncomingMessage in interface EndpointListener

findBestReachableEndpoint

Messenger findBestReachableEndpoint(EndpointAddress dest,
                                    List mightWork,
                                    boolean exist)
Given a list of addresses, find the best reachable endpoint.

Parameters:
addrs - A list of addresses to evaulate reachability.
exist - true if there already are existing messengers for the given destinations but we want one more. It may lead us to reject certain addresses that we would otherwise accept.
Returns:
The endpoint address for which we found a local route otherwise null

findReachableEndpoint

Messenger findReachableEndpoint(EndpointAddress destPeer,
                                boolean exist)
Read the route advertisement for a peer and find a suitable transport endpoint for sending to that peere either directly or via one of the advertised peer router


findReachableEndpoint

Messenger findReachableEndpoint(EndpointAddress destPeer,
                                boolean exist,
                                Object hint)

getMessenger

public Messenger getMessenger(EndpointAddress addr,
                              Object hint)

Specified by:
getMessenger in interface MessageSender

addressMessage

EndpointAddress addressMessage(Message message,
                               EndpointAddress dstAddress)
Updates the router element of a message and returns the pid address of the next hop (where to send the message). Currently, address message is only called for messages that we originate. As a result we will always agressively seek a route if needed.

Parameters:
message - the message for which to compute/update a route.
destAddress - the final destination of the route which the message be set to follow.
Returns:
EndpointAddress The address (logical) where to send the message next. Null if there is nowhere to send it to.

transportControl

public Object transportControl(Object operation,
                               Object value)

Specified by:
transportControl in interface MessageTransport

addr2pid

protected static PeerID addr2pid(EndpointAddress addr)
convert an endpointRouterAddress into a PeerID


pid2addr

protected static EndpointAddress pid2addr(PeerID pid)
convert a PeerID into an EndpointRouter Address


updateRouteAdv

protected void updateRouteAdv(RouteAdvertisement route)
check if it is a new route adv


updateRouteAdv

protected void updateRouteAdv(RouteAdvertisement route,
                              boolean force)
check if it is a new route adv


cleanupAnyEdges

protected List cleanupAnyEdges(String src,
                               List results)
cleanup any edge peers when trying to forward an SRDI query so we are guaranteed to the best of our knowledge that the peer is a rendezvous. This is not perfect, as it may take time for the peerview to converge but at least we can remove any peers that is not a rendezvous.


isPendingRouteQuery

protected boolean isPendingRouteQuery(EndpointAddress addr)
is there a pending route query for that destination

Parameters:
addr - destination address
Returns:
true or false

getPendingRouteQuery

protected EndpointRouter.ClearPendingQuery getPendingRouteQuery(EndpointAddress addr)
get a pending route query info

Parameters:
addr - destination address
Returns:
pending route query info

isRoutedRoute

protected boolean isRoutedRoute(EndpointAddress addr)
Do we have a longue route for that destination

Parameters:
addr - destination address
Returns:
true or false

getCachedMessenger

protected Messenger getCachedMessenger(EndpointAddress addr)
Snoop if we have a messenger

Parameters:
addr - destination address
Returns:
Messenger

getAllCachedMessengerDestinations

protected Iterator getAllCachedMessengerDestinations()
Get all direct route destinations

Returns:
Iterator iterations of all endpoint destinations

getRoutedRouteAllDestinations

protected Iterator getRoutedRouteAllDestinations()
Get all long route destinations

Returns:
Iterator iterations of all routed route destinations

getAllRoutedRouteAddresses

protected Iterator getAllRoutedRouteAddresses()
Get all long route destination addresses

Returns:
Iterator iterations of all routed route addresses

getPendingQueriesAllDestinations

protected Iterator getPendingQueriesAllDestinations()
Get all pendingRouteQuery destinations

Returns:
Iterator iterations of all pending route query destinations

getRouteCM

protected RouteCM getRouteCM()
Get the route CM cache Manager


getRouteResolver

protected RouteResolver getRouteResolver()
Get the route resolver manager


setBadRoute

protected void setBadRoute(EndpointAddress addr,
                           BadRoute badRoute)
set bad route entry

Parameters:
addr - of the bad route
badRoute - bad route info

getBadRoute

protected BadRoute getBadRoute(EndpointAddress addr)
get bad route entry

Parameters:
addr - of the bad route
Returns:
BadRoute bad route info

JXTA J2SE