Class AbstractMessageDelegator<S extends MessageConnection>
java.lang.Object
com.jme3.network.util.AbstractMessageDelegator<S>
- Type Parameters:
S- the message connection type handled by this delegator
- All Implemented Interfaces:
MessageListener<S>
- Direct Known Subclasses:
ObjectMessageDelegator, SessionDataDelegator
public abstract class AbstractMessageDelegator<S extends MessageConnection>
extends Object
implements MessageListener<S>
A MessageListener implementation that will forward messages to methods
of a delegate object. These methods can be automapped or manually
specified. Subclasses provide specific implementations for how to
find the actual delegate object.
- Author:
- Paul Speed
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractMessageDelegator(Class delegateType, boolean automap) Creates an AbstractMessageDelegator that will forward received messages to methods of the specified delegate type. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanReturns true if the specified method name is allowed.protected final voidautomap()Calls the map(Set) method with a null argument causing all available matching methods to mapped to message types.protected MethodfindDelegate(String name, Class messageType) Goes through all of the delegate type's methods to find a method of the specified name that may take the specified message type.protected ClassConvenience method that returns the message type as reflectively determined for a particular method.Class[]Returns the array of messages known to be handled by this message delegator.protected MethodReturns the mapped method for the specified message type.protected abstract ObjectgetSourceDelegate(S source) Implemented by subclasses to provide the actual delegate object against which the mapped message type methods will be called.protected booleanisValidMethod(Method m, Class messageType) Returns true if the specified method is valid for the specified message type.Manually maps a specified method to the specified message type.Specifically maps the specified methods names, autowiring the parameters.protected voidGoes through all of the delegate type's declared methods mapping methods that match the current constraints.voidmessageReceived(S source, Message msg) Implementation of the MessageListener's messageReceived() method that will use the current message type mapping to find an appropriate message handling method and call it on the delegate returned by getSourceDelegate().
-
Constructor Details
-
AbstractMessageDelegator
Creates an AbstractMessageDelegator that will forward received messages to methods of the specified delegate type. If automap is true then reflection is used to lookup probably message handling methods.- Parameters:
delegateType- the delegate class that contains handler methodsautomap- true to scan the delegate type for handler methods immediately
-
-
Method Details
-
getMessageTypes
Returns the array of messages known to be handled by this message delegator.- Returns:
- the message types currently mapped to delegate methods
-
isValidMethod
Returns true if the specified method is valid for the specified message type. This is used internally during automapping to provide implementation specific filtering of methods. This implementation checks for methods that take either the connection and message type arguments (in that order) or just the message type.- Parameters:
m- the method to testmessageType- the expected message type, or null to accept anyMessage- Returns:
- true if the method can handle the specified message type
-
getMessageType
Convenience method that returns the message type as reflectively determined for a particular method. This only works with methods that actually have arguments. This implementation returns the last element of the method's getParameterTypes() array, thus supporting both method(connection, messageType) and method(messageType) calling forms.- Parameters:
m- the method to inspect- Returns:
- the message type accepted by the method
-
findDelegate
Goes through all of the delegate type's methods to find a method of the specified name that may take the specified message type.- Parameters:
name- the method name to search formessageType- the message type the delegate method should accept- Returns:
- the matching method, or null if none is found
-
allowName
Returns true if the specified method name is allowed. This is used by automapping to determine if a method should be rejected purely on name. Default implementation always returns true.- Parameters:
name- the method name to evaluate- Returns:
- true if the name is eligible for automapping
-
automap
protected final void automap()Calls the map(Set) method with a null argument causing all available matching methods to mapped to message types. -
map
Specifically maps the specified methods names, autowiring the parameters.- Parameters:
methodNames- the delegate method names to map- Returns:
- this delegator for chaining
-
map
Goes through all of the delegate type's declared methods mapping methods that match the current constraints. If the constraints set is null then allowName() is checked for names otherwise only names in the constraints set are allowed. For each candidate method that passes the above checks, isValidMethod() is called with a null message type argument. All methods are made accessible thus supporting non-public methods as well as public methods.- Parameters:
constraints- the allowed method names, or null to useallowName(String)
-
map
Manually maps a specified method to the specified message type.- Parameters:
messageType- the message type to mapmethodName- the delegate method name to invoke for that type- Returns:
- this delegator for chaining
-
getMethod
-
getSourceDelegate
Implemented by subclasses to provide the actual delegate object against which the mapped message type methods will be called.- Parameters:
source- the source connection associated with the message- Returns:
- the delegate object to invoke, or null to ignore the message
-
messageReceived
Implementation of the MessageListener's messageReceived() method that will use the current message type mapping to find an appropriate message handling method and call it on the delegate returned by getSourceDelegate().- Specified by:
messageReceivedin interfaceMessageListener<S extends MessageConnection>- Parameters:
source- the connection or endpoint that delivered the messagemsg- the received message
-