Class SFSExtension

java.lang.Object
com.smartfoxserver.extensions.BaseSFSExtension
com.smartfoxserver.extensions.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 check the relevant section in our main documentation site.

  • Field Details

  • Constructor Details

    • SFSExtension

      public SFSExtension()
  • Method Details

    • 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(String requestId, 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(String requestId, IClientRequestHandler requestHandler)
    • addEventHandler

      protected void addEventHandler(SFSEventType eventType, 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:
    • addEventHandler

      protected void addEventHandler(SFSEventType eventType, IServerEventHandler handler)
    • removeRequestHandler

      protected void removeRequestHandler(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(String requestId, User sender, ISFSObject params, TransportType txType)
      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
    • handleServerEvent

      public void handleServerEvent(ISFSEvent event) throws Exception
      Handle server event
      Specified by:
      handleServerEvent in interface ISFSEventListener
      Overrides:
      handleServerEvent in class BaseSFSExtension
      Parameters:
      event - the Event
      Throws:
      Exception
      See Also:
    • addFilter

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

      public void removeFilter(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.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