Class SFSExtension

  • All Implemented Interfaces:
    ISFSEventListener, ISFSExtension

    public abstract class SFSExtension
    extends BaseSFSExtension
    The SFSExtension class provides an invocation mechanism that promotes clear separation between the main extension class and each request and event handlers.

    Developers can create each request/event handlers as separate classes and then register them in the SFSExtension which will take care of the invocation details.

    SFSExtension also provides a servlet-like Filter system. Extension Filters are added to a filter chain and executed in the order of insertion. They can be used to log, filter, or handle specific requests or events before they get to the Extension itself. The advantage of pluggable Filters is that they don't get in the way of your Extension code, their execution order can be altered and they can be used to stop the execution flow, if necessary.

    An example of this could be a custom ban filter where User credentials are checked against a black list before the request is passed to your Login Handler. Another example of usage would be logging or filtering public and private messages.

    For a more detailed overview of Extensions please check this doc-page

    • Field Detail

      • MULTIHANDLER_REQUEST_ID

        public static final java.lang.String MULTIHANDLER_REQUEST_ID
        See Also:
        Constant Field Values
    • Constructor Detail

      • SFSExtension

        public SFSExtension()
    • Method Detail

      • destroy

        public void destroy()
        Description copied from interface: ISFSExtension
        This method is called once by the Server when the Extension must be shut down (e.g. before a server restart or before the Extension code is reloaded)

        Here you can put all the necessary code to release any resources that was acquired/started in the init() method. (e.g. event listeners, threads, scheduled tasks, files etc...)

      • addRequestHandler

        protected void addRequestHandler​(java.lang.String requestId,
                                         java.lang.Class<?> theClass)
        Add a request handler for a specific request id.

        Dot syntax

        In order to properly organize request names in complex application we have established a convention similar to Java package naming that uses a "dot syntax". This will also allow you to group multiple requests under the same handler class.

        Examples:

           addRequestHandler("myapp.chessGame", ChessGameHandler.class);
         
        If the ChessGameHandler is annotated with as MultiHandler it will receive all commands starting with myapp.ChessGame or in other words myapp.ChessGame.*

        NOTE: multiple handlers for the same request is not supported.

        Parameters:
        requestId - the requestId, optionally using dot-syntax
        theClass - the handler Class ( must implement IClientRequestHandler )
      • addRequestHandler

        protected void addRequestHandler​(java.lang.String requestId,
                                         IClientRequestHandler requestHandler)
      • addEventHandler

        protected void addEventHandler​(SFSEventType eventType,
                                       java.lang.Class<?> theClass)
        Add a request handler for a specific event type. Each event can have only one handler.

        NOTE:Each event can have only one handler.

        Parameters:
        eventType - the type of event
        theClass - the handler Class ( must implement IServerEventHandler )
        See Also:
        addRequestHandler(String, Class)
      • removeRequestHandler

        protected void removeRequestHandler​(java.lang.String requestId)
        Remove a request handler
        Parameters:
        requestId - name of the request
      • removeEventHandler

        protected void removeEventHandler​(SFSEventType eventType)
        Remove an event handler
        Parameters:
        eventType - the type of event
      • clearAllHandlers

        protected void clearAllHandlers()
        Removes all event and request handlers
      • handleClientRequest

        public void handleClientRequest​(java.lang.String requestId,
                                        User sender,
                                        ISFSObject params)
        This method is called whenever a client sends a request to this Extension
        Parameters:
        requestId - the request command name
        sender - the sender of the request
        params - the custom parameters of the request
      • addFilter

        public final void addFilter​(java.lang.String filterName,
                                    SFSExtensionFilter filter)
        Add a filter to the Extension
        Parameters:
        filterName - name of the filter
        filter - the filter
      • removeFilter

        public void removeFilter​(java.lang.String filterName)
        Removes a filter from the Extension
        Parameters:
        filterName - name of the filter
      • clearFilters

        public void clearFilters()
        Removes all filters from the Extension
      • initFloodFilter

        protected void initFloodFilter​(com.smartfoxserver.v2.extensions.ExtensionFloodFilterConfig cfg)

        Initializes the Extension anti-flood filter, which allows to configure a maximum request rate (req/s) for every custom Extension call.

        For more information see the external documentation

        Parameters:
        cfg - the filter's configuration object
        See Also:
        ExtensionFloodFilterConfig