Class SFSExtension
- java.lang.Object
-
- com.smartfoxserver.v2.extensions.BaseSFSExtension
-
- com.smartfoxserver.v2.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 please check this doc-page
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
MULTIHANDLER_REQUEST_ID
-
Fields inherited from class com.smartfoxserver.v2.extensions.BaseSFSExtension
lagOscillation, lagSimulationMillis, logger, sfsApi
-
-
Constructor Summary
Constructors Constructor Description SFSExtension()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addEventHandler(SFSEventType eventType, IServerEventHandler handler)
protected void
addEventHandler(SFSEventType eventType, java.lang.Class<?> theClass)
Add a request handler for a specific event type.void
addFilter(java.lang.String filterName, SFSExtensionFilter filter)
Add a filter to the Extensionprotected void
addRequestHandler(java.lang.String requestId, IClientRequestHandler requestHandler)
protected void
addRequestHandler(java.lang.String requestId, java.lang.Class<?> theClass)
Add a request handler for a specific request id.protected void
clearAllHandlers()
Removes all event and request handlersvoid
clearFilters()
Removes all filters from the Extensionvoid
destroy()
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)void
handleClientRequest(java.lang.String requestId, User sender, ISFSObject params)
This method is called whenever a client sends a request to this Extensionvoid
handleServerEvent(ISFSEvent event)
Handle server eventprotected 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.protected void
removeEventHandler(SFSEventType eventType)
Remove an event handlervoid
removeFilter(java.lang.String filterName)
Removes a filter from the Extensionprotected void
removeRequestHandler(java.lang.String requestId)
Remove a request handler-
Methods inherited from class com.smartfoxserver.v2.extensions.BaseSFSExtension
addEventListener, getApi, getBuddyApi, getConfigProperties, getCurrentFolder, getExtensionFileName, getGameApi, getLevel, getLogger, getMMOApi, getName, getParentRoom, getParentZone, getPropertiesFileName, getReloadMode, getType, handleInternalMessage, isActive, removeEventListener, removeEventsForListener, send, send, send, send, setActive, setExtensionFileName, setLevel, setName, setParentRoom, setParentZone, setPropertiesFileName, setReloadMode, setType, toString, trace, trace
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.smartfoxserver.v2.extensions.ISFSExtension
init
-
-
-
-
Field Detail
-
MULTIHANDLER_REQUEST_ID
public static final java.lang.String MULTIHANDLER_REQUEST_ID
- See Also:
- Constant Field Values
-
-
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 asMultiHandler
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-syntaxtheClass
- the handler Class ( must implementIClientRequestHandler
)
-
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 eventtheClass
- the handler Class ( must implementIServerEventHandler
)- See Also:
addRequestHandler(String, Class)
-
addEventHandler
protected void addEventHandler(SFSEventType eventType, IServerEventHandler handler)
-
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 namesender
- the sender of the requestparams
- the custom parameters of the request
-
handleServerEvent
public void handleServerEvent(ISFSEvent event) throws java.lang.Exception
Handle server event- Specified by:
handleServerEvent
in interfaceISFSEventListener
- Overrides:
handleServerEvent
in classBaseSFSExtension
- Parameters:
event
- the Event- Throws:
java.lang.Exception
- See Also:
SFSEvent
-
addFilter
public final void addFilter(java.lang.String filterName, SFSExtensionFilter filter)
Add a filter to the Extension- Parameters:
filterName
- name of the filterfilter
- 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
-
-