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

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractMessageDelegator(Class delegateType, boolean automap)
    Creates an AbstractMessageDelegator that will forward received messages to methods of the specified delegate type.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected boolean
    Returns true if the specified method name is allowed.
    protected final void
    Calls the map(Set) method with a null argument causing all available matching methods to mapped to message types.
    protected Method
    findDelegate(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 Class
    Convenience method that returns the message type as reflectively determined for a particular method.
    Returns the array of messages known to be handled by this message delegator.
    protected Method
    Returns the mapped method for the specified message type.
    protected abstract Object
    Implemented by subclasses to provide the actual delegate object against which the mapped message type methods will be called.
    protected boolean
    isValidMethod(Method m, Class messageType)
    Returns true if the specified method is valid for the specified message type.
    map(Class messageType, String methodName)
    Manually maps a specified method to the specified message type.
    map(String... methodNames)
    Specifically maps the specified methods names, autowiring the parameters.
    protected void
    map(Set<String> constraints)
    Goes through all of the delegate type's declared methods mapping methods that match the current constraints.
    void
    messageReceived(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().

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AbstractMessageDelegator

      protected AbstractMessageDelegator(Class delegateType, boolean automap)
      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 methods
      automap - true to scan the delegate type for handler methods immediately
  • Method Details

    • getMessageTypes

      public Class[] getMessageTypes()
      Returns the array of messages known to be handled by this message delegator.
      Returns:
      the message types currently mapped to delegate methods
    • isValidMethod

      protected boolean isValidMethod(Method m, Class messageType)
      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 test
      messageType - the expected message type, or null to accept any Message
      Returns:
      true if the method can handle the specified message type
    • getMessageType

      protected Class getMessageType(Method m)
      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

      protected Method findDelegate(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.
      Parameters:
      name - the method name to search for
      messageType - the message type the delegate method should accept
      Returns:
      the matching method, or null if none is found
    • allowName

      protected boolean allowName(String name)
      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

      public AbstractMessageDelegator<S> map(String... methodNames)
      Specifically maps the specified methods names, autowiring the parameters.
      Parameters:
      methodNames - the delegate method names to map
      Returns:
      this delegator for chaining
    • map

      protected void map(Set<String> constraints)
      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 use allowName(String)
    • map

      public AbstractMessageDelegator<S> map(Class messageType, String methodName)
      Manually maps a specified method to the specified message type.
      Parameters:
      messageType - the message type to map
      methodName - the delegate method name to invoke for that type
      Returns:
      this delegator for chaining
    • getMethod

      protected Method getMethod(Class c)
      Returns the mapped method for the specified message type.
      Parameters:
      c - the message type
      Returns:
      the mapped method, or null if none is registered
    • getSourceDelegate

      protected abstract Object getSourceDelegate(S source)
      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

      public void messageReceived(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().
      Specified by:
      messageReceived in interface MessageListener<S extends MessageConnection>
      Parameters:
      source - the connection or endpoint that delivered the message
      msg - the received message