org.xsocket.connection
Class HandlerChain

Package class diagram package HandlerChain
java.lang.Object
  extended by org.xsocket.connection.HandlerChain
All Implemented Interfaces:
IConnectHandler, IConnectionTimeoutHandler, IDataHandler, IDisconnectHandler, IHandler, IIdleTimeoutHandler, ILifeCycle

public final class HandlerChain
extends Object
implements IHandler, IConnectHandler, IDataHandler, IDisconnectHandler, IConnectionTimeoutHandler, IIdleTimeoutHandler, ILifeCycle

Implements a handler chain. Each handler of the chain will be called (in the registering order), until one handler signal by the return value true, that the event has been handled. In this case the remaining handlers will not be called.

Nested chains is not supported yet
E.g.

   ...
   HandlerChain tcpBasedSpamfilter = new HandlerChain();
   tcpBasedSpamfilter.addLast(new BlackIPFilter()); 
   tcpBasedSpamfilter.addLast(new FirstConnectRefuseFilter());
   
   HandlerChain mainChain = new HandlerChain();
   mainChain.addLast(tcpBasedSpamfilter);
   mainChain.addLast(new SmtpProtocolHandler());
   
   IMultithreadedServer smtpServer = new MultithreadedServer(port, mainChain);
   StreamUtils.start(server);
   ...
   
 

Author:
grro@xsocket.org

Field Summary
 
Fields inherited from interface org.xsocket.connection.IHandler
DEFAULT_EXECUTION_MODE
 
Constructor Summary
HandlerChain()
          constructor
HandlerChain(List<IHandler> handlers)
          constructor
 
Method Summary
 void addLast(IHandler handler)
          add a handler to the end og the chain
 boolean onConnect(INonBlockingConnection connection)
          handles a new incoming connection
 boolean onConnectionTimeout(INonBlockingConnection connection)
          handles the connection timeout.
 boolean onData(INonBlockingConnection connection)
          processes the incoming data based on the given connection.
 void onDestroy()
          notifies that the entity will be destroyed
 boolean onDisconnect(INonBlockingConnection connection)
          handles disconnecting of a connection
 boolean onIdleTimeout(INonBlockingConnection connection)
          handles the idle timeout.
 void onInit()
          notifies that the entity has been loaded and initialized
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HandlerChain

public HandlerChain()
constructor


HandlerChain

public HandlerChain(List<IHandler> handlers)
constructor

Parameters:
handlers - the initial handlers
Method Detail

onInit

public void onInit()
Description copied from interface: ILifeCycle
notifies that the entity has been loaded and initialized

Specified by:
onInit in interface ILifeCycle

onDestroy

public void onDestroy()
               throws IOException
Description copied from interface: ILifeCycle
notifies that the entity will be destroyed

Specified by:
onDestroy in interface ILifeCycle
Throws:
IOException - if an exception occurs. The exception will be (logged and) swallowed

addLast

public void addLast(IHandler handler)
add a handler to the end og the chain

Parameters:
handler - the handler to add

onConnect

public boolean onConnect(INonBlockingConnection connection)
                  throws IOException,
                         BufferUnderflowException,
                         MaxReadSizeExceededException
handles a new incoming connection

Specified by:
onConnect in interface IConnectHandler
Returns:
true for positive result of handling, false for negative result of handling
Throws:
IOException - if some other I/O error occurs. Throwing this exception causes that the underlying connection will be closed.
MaxReadSizeExceededException - if the max read size has been reached (e.g. by calling method IDataSource.readStringByDelimiter(String, int)). Throwing this exception causes that the underlying connection will be closed.
BufferUnderflowException - if more incoming data is required to process (e.g. delimiter hasn't yet received -> readByDelimiter methods or size of the available, received data is smaller than the required size -> readByLength). The BufferUnderflowException will be swallowed by the framework

onData

public boolean onData(INonBlockingConnection connection)
               throws IOException
processes the incoming data based on the given connection.

Please note, that the onData call back method can also be called for if an connection will be closed. In this case the isOpen call within the onData Method will return false. Reading of already received data will not fail. To detect if a connection has been closed the callback method onDisconnect should be implemented. The correct call back order will be managed by the xSocket.

Specified by:
onData in interface IDataHandler
Parameters:
connection - the underlying connection
Returns:
true for positive result of handling, false for negative result of handling. The return value will be used by the HandlerChain to interrupted the chaining (if result is true)
Throws:
IOException - If some other I/O error occurs. Throwing this exception causes that the underlying connection will be closed.
ClosedChannelException - if the connection is closed
MaxReadSizeExceededException - if the max read size has been reached (e.g. by calling method IDataSource.readStringByDelimiter(String, int)). Throwing this exception causes that the underlying connection will be closed.

onDisconnect

public boolean onDisconnect(INonBlockingConnection connection)
                     throws IOException,
                            BufferUnderflowException,
                            MaxReadSizeExceededException
handles disconnecting of a connection

Specified by:
onDisconnect in interface IDisconnectHandler
Parameters:
connection - the closed connection
Returns:
true for positive result of handling, false for negative result of handling
Throws:
IOException - If some I/O error occurs.
BufferUnderflowException
MaxReadSizeExceededException

onIdleTimeout

public boolean onIdleTimeout(INonBlockingConnection connection)
                      throws IOException,
                             BufferUnderflowException,
                             MaxReadSizeExceededException
handles the idle timeout.

Specified by:
onIdleTimeout in interface IIdleTimeoutHandler
Parameters:
connection - the underlying connection
Returns:
true if the timeout event has been handled (in case of false the connection will be closed by the server)
Throws:
IOException - if an error occurs. Throwing this exception causes that the underlying connection will be closed.
BufferUnderflowException
MaxReadSizeExceededException

onConnectionTimeout

public boolean onConnectionTimeout(INonBlockingConnection connection)
                            throws IOException,
                                   BufferUnderflowException,
                                   MaxReadSizeExceededException
handles the connection timeout.

Specified by:
onConnectionTimeout in interface IConnectionTimeoutHandler
Parameters:
connection - the underlying connection
Returns:
true if the timeout event has been handled (in case of false the connection will be closed by the server)
Throws:
IOException - if an error occurs. Throwing this exception causes that the underlying connection will be closed.
BufferUnderflowException
MaxReadSizeExceededException


Copyright 2008 xSocket.org