com.crankuptheamps.client
Class MessageRouter

java.lang.Object
  extended by com.crankuptheamps.client.MessageRouter

public class MessageRouter
extends Object

MessageRouter is used to register and manage a list of handler objects for messages, and then to route messages to those handlers as messages arrive. MessageRouter also "knows" about the meaning of AMPS acks and can use them to automatically clean up routes as acks arrive.


Constructor Summary
MessageRouter()
           
 
Method Summary
 void addRoute(CommandId commandId_, MessageHandler messageHandler_, int requestedAcks_, int systemAcks_, boolean isSubscribe_)
          Adds a route to self.
 void clear()
          Removes all routes from self.
 int deliverAck(Message ackMessage_, int ackType_)
          Deliver a message that is known already to be an Ack.
 int deliverData(Message dataMessage_)
          Delivers a data message (not an Ack) to the registered route.
 int deliverData(Message dataMessage_, CommandId commandId_)
          Delivers a data message using a specific command ID from the message.
 MessageHandler findRoute(CommandId commandId_)
          Find and return a route
 boolean removeRoute(CommandId commandId_)
          Remove a route from self.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageRouter

public MessageRouter()
Method Detail

addRoute

public void addRoute(CommandId commandId_,
                     MessageHandler messageHandler_,
                     int requestedAcks_,
                     int systemAcks_,
                     boolean isSubscribe_)
Adds a route to self.

Parameters:
commandId_ - The command, query, or subid used for this route.
messageHandler_ - The message handler to route to
requestedAcks_ - The actual acks requested from amps for this command
systemAcks_ - The acks not requested by the end user, but requested by AMPS. These will not be delivered to the message handler, but are still processed for auto-removal.
isSubscribe_ - True if this route is for an ongoing subscription

removeRoute

public boolean removeRoute(CommandId commandId_)
Remove a route from self.

Parameters:
commandId_ - The route to remove
Returns:
true if the route was removed.

findRoute

public MessageHandler findRoute(CommandId commandId_)
Find and return a route

Parameters:
commandId_ - The command id for this route
Returns:
The MessageHandler registered for this route, or null if none is registered.

clear

public void clear()
Removes all routes from self.


deliverAck

public int deliverAck(Message ackMessage_,
                      int ackType_)
               throws Exception
Deliver a message that is known already to be an Ack. Coordinates the removal of routes based on the ack received and the original message type.

Parameters:
ackMessage_ - The Message to deliver.
ackType_ - The ack type from that message.
Returns:
The number of message deliveries that occurred
Throws:
Any - exception from user message handlers.
Exception

deliverData

public int deliverData(Message dataMessage_)
                throws Exception
Delivers a data message (not an Ack) to the registered route. Uses the commandID, subID, and queryID to deliver find a route and deliver to the first one found. This method is optimized for speed and does not attempt to examine ack types for removal of routes.

Parameters:
dataMessage_ - The non-ack message to deliver.
Returns:
The number of deliveries performed
Throws:
Exception - Any exception thrown by the user message handler.

deliverData

public int deliverData(Message dataMessage_,
                       CommandId commandId_)
                throws Exception
Delivers a data message using a specific command ID from the message. Optimized for speed and does not attempt to examine the message for auto-removal of routes

Parameters:
dataMessage_ - The message to deliver.
commandId_ - The command ID which will be used to lookup the delivery route
Returns:
The number of deliveries performed
Throws:
Exception - Any exception returned thrown by the message handler.