Package org.astrogrid.samp.client
Class AbstractMessageHandler
java.lang.Object
org.astrogrid.samp.client.AbstractMessageHandler
- All Implemented Interfaces:
MessageHandler
Partial implementation of MessageHandler interface which helps to ensure
correct client behaviour.
Concrete subclasses just need to specify the MTypes they subscribe to
and implement the
processCall(org.astrogrid.samp.client.HubConnection, java.lang.String, org.astrogrid.samp.Message) method.- Since:
- 16 Jul 2008
- Author:
- Mark Taylor
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractMessageHandler(String mtype) Constructor using a single subscribed MType.protectedAbstractMessageHandler(String[] mtypes) Constructor using a given list of subscribed MTypes.protectedAbstractMessageHandler(Map subscriptions) Constructor using a given subscriptions map. -
Method Summary
Modifier and TypeMethodDescriptionprotected ResponsecreateResponse(Map processOutput) Invoked byreceiveCallto create a success response from the result of callingprocessCall.Returns a Subscriptions map corresponding to the messages handled by this object.abstract MapprocessCall(HubConnection connection, String senderId, Message message) Implements message processing.voidreceiveCall(HubConnection connection, String senderId, String msgId, Message message) CallsprocessCall(org.astrogrid.samp.client.HubConnection, java.lang.String, org.astrogrid.samp.Message), generates a response from the result usingcreateResponse(java.util.Map), and sends the resulting response as a reply to the hub.voidreceiveNotification(HubConnection connection, String senderId, Message message) CallsprocessCall(org.astrogrid.samp.client.HubConnection, java.lang.String, org.astrogrid.samp.Message)and discards the result.voidsetSubscriptions(Map subscriptions) Sets the subscriptions map.
-
Constructor Details
-
AbstractMessageHandler
Constructor using a given subscriptions map.- Parameters:
subscriptions-Subscriptions-like map defining which MTypes this handler can process
-
AbstractMessageHandler
Constructor using a given list of subscribed MTypes.- Parameters:
mtypes- list of MTypes which this handler can process
-
AbstractMessageHandler
Constructor using a single subscribed MType.- Parameters:
mtype- single MType which this handler can process
-
-
Method Details
-
processCall
public abstract Map processCall(HubConnection connection, String senderId, Message message) throws Exception Implements message processing. Implementations should normally return a map which contains thesamp.resultpart of the call response, that is the MType-specific return value name->value map. As a special case, returning null is equivalent to returning an empty map. However, ifcreateResponse(java.util.Map)is overridden, the return value semantics may be different.- Parameters:
connection- hub connectionsenderId- public ID of sender clientmessage- message with MType this handler is subscribed to- Returns:
- result of handling this message; exact semantics determined
by
createResponseimplementation - Throws:
Exception
-
createResponse
Invoked byreceiveCallto create a success response from the result of callingprocessCall.The default implementation calls
Response.createSuccessResponse(java.util.Map)(processOutput), first transforming a null value to an empty map for convenience. However, it may be overridden for more flexibility (for instance in order to return non-OK responses).- Parameters:
processOutput- a Map returned byprocessCall
-
setSubscriptions
Sets the subscriptions map. Usually this is called by the constructor, but it may be reset manually.- Parameters:
subscriptions-Subscriptions-like map defining which MTypes this handler can process
-
getSubscriptions
Description copied from interface:MessageHandlerReturns a Subscriptions map corresponding to the messages handled by this object. Only messages with MTypes which match the keys of this map will be passed to this object.- Specified by:
getSubscriptionsin interfaceMessageHandler- Returns:
Subscriptions-like map
-
receiveNotification
CallsprocessCall(org.astrogrid.samp.client.HubConnection, java.lang.String, org.astrogrid.samp.Message)and discards the result.- Specified by:
receiveNotificationin interfaceMessageHandler- Parameters:
connection- hub connectionsenderId- public ID of client which sent the messagemessage- message
-
receiveCall
public void receiveCall(HubConnection connection, String senderId, String msgId, Message message) throws SampException CallsprocessCall(org.astrogrid.samp.client.HubConnection, java.lang.String, org.astrogrid.samp.Message), generates a response from the result usingcreateResponse(java.util.Map), and sends the resulting response as a reply to the hub. In case of an exception, a suitable error response is sent instead.- Specified by:
receiveCallin interfaceMessageHandler- Parameters:
connection- hub connectionsenderId- public ID of client which sent the messagemsgId- message IDmessage- message- Throws:
SampException
-