Package org.astrogrid.samp.client
Class AbstractMessageHandler
- java.lang.Object
-
- org.astrogrid.samp.client.AbstractMessageHandler
-
- All Implemented Interfaces:
MessageHandler
- Direct Known Subclasses:
ClientTracker
,MetaQueryMessageHandler
,PingMessageHandler
public abstract class AbstractMessageHandler extends java.lang.Object implements 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 theprocessCall(org.astrogrid.samp.client.HubConnection, java.lang.String, org.astrogrid.samp.Message)
method.- Since:
- 16 Jul 2008
- Author:
- Mark Taylor
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.logging.Logger
logger_
private Subscriptions
subscriptions_
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractMessageHandler(java.lang.String mtype)
Constructor using a single subscribed MType.protected
AbstractMessageHandler(java.lang.String[] mtypes)
Constructor using a given list of subscribed MTypes.protected
AbstractMessageHandler(java.util.Map subscriptions)
Constructor using a given subscriptions map.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected Response
createResponse(java.util.Map processOutput)
Invoked byreceiveCall
to create a success response from the result of callingprocessCall
.java.util.Map
getSubscriptions()
Returns a Subscriptions map corresponding to the messages handled by this object.abstract java.util.Map
processCall(HubConnection connection, java.lang.String senderId, Message message)
Implements message processing.void
receiveCall(HubConnection connection, java.lang.String senderId, java.lang.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.void
receiveNotification(HubConnection connection, java.lang.String senderId, Message message)
CallsprocessCall(org.astrogrid.samp.client.HubConnection, java.lang.String, org.astrogrid.samp.Message)
and discards the result.void
setSubscriptions(java.util.Map subscriptions)
Sets the subscriptions map.
-
-
-
Field Detail
-
subscriptions_
private Subscriptions subscriptions_
-
logger_
private final java.util.logging.Logger logger_
-
-
Constructor Detail
-
AbstractMessageHandler
protected AbstractMessageHandler(java.util.Map subscriptions)
Constructor using a given subscriptions map.- Parameters:
subscriptions
-Subscriptions
-like map defining which MTypes this handler can process
-
AbstractMessageHandler
protected AbstractMessageHandler(java.lang.String[] mtypes)
Constructor using a given list of subscribed MTypes.- Parameters:
mtypes
- list of MTypes which this handler can process
-
AbstractMessageHandler
protected AbstractMessageHandler(java.lang.String mtype)
Constructor using a single subscribed MType.- Parameters:
mtype
- single MType which this handler can process
-
-
Method Detail
-
processCall
public abstract java.util.Map processCall(HubConnection connection, java.lang.String senderId, Message message) throws java.lang.Exception
Implements message processing. Implementations should normally return a map which contains thesamp.result
part 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
createResponse
implementation - Throws:
java.lang.Exception
-
createResponse
protected Response createResponse(java.util.Map processOutput)
Invoked byreceiveCall
to 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
public void setSubscriptions(java.util.Map subscriptions)
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
public java.util.Map getSubscriptions()
Description copied from interface:MessageHandler
Returns 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:
getSubscriptions
in interfaceMessageHandler
- Returns:
Subscriptions
-like map
-
receiveNotification
public void receiveNotification(HubConnection connection, java.lang.String senderId, Message message)
CallsprocessCall(org.astrogrid.samp.client.HubConnection, java.lang.String, org.astrogrid.samp.Message)
and discards the result.- Specified by:
receiveNotification
in interfaceMessageHandler
- Parameters:
connection
- hub connectionsenderId
- public ID of client which sent the messagemessage
- message
-
receiveCall
public void receiveCall(HubConnection connection, java.lang.String senderId, java.lang.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:
receiveCall
in interfaceMessageHandler
- Parameters:
connection
- hub connectionsenderId
- public ID of client which sent the messagemsgId
- message IDmessage
- message- Throws:
SampException
-
-