org.xsocket.stream
Class HandlerChain

Package class diagram package HandlerChain
java.lang.Object
  extended by org.xsocket.stream.HandlerChain
All Implemented Interfaces:
java.lang.Cloneable, ILifeCycle, IConnectHandler, IConnectionScoped, IDataHandler, IDisconnectHandler, IHandler, ITimeoutHandler

public final class HandlerChain
extends java.lang.Object
implements IHandler, IConnectHandler, IDisconnectHandler, IDataHandler, ITimeoutHandler, IConnectionScoped, 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 remaing handlers will not be called.
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);
   ...
   
 


Constructor Summary
HandlerChain()
          constructor
HandlerChain(java.util.List<IHandler> handlers)
          constructor
 
Method Summary
 void addLast(IHandler handler)
          add a handler to the end og the chain
 java.lang.Object clone()
          
 boolean onConnect(INonBlockingConnection connection)
          handles a new incomming connection
 boolean onConnectionTimeout(INonBlockingConnection connection)
          handles the connection timeout.
 boolean onData(INonBlockingConnection connection)
          processes the incomming data based on the given connection.
 void onDestroy()
          signals 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()
          signals that the entity has been loaded and initialized
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HandlerChain

public HandlerChain()
constructor


HandlerChain

public HandlerChain(java.util.List<IHandler> handlers)
constructor

Parameters:
handlers - the initial handlers
Method Detail

addLast

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

Parameters:
handler - the handler to add

onInit

public void onInit()
signals that the entity has been loaded and initialized

Specified by:
onInit in interface ILifeCycle

onDestroy

public void onDestroy()
signals that the entity will be destroyed

Specified by:
onDestroy in interface ILifeCycle

onConnect

public boolean onConnect(INonBlockingConnection connection)
                  throws java.io.IOException
handles a new incomming connection

Specified by:
onConnect in interface IConnectHandler
Returns:
true for positive result of handling, false for negative result of handling
Throws:
java.io.IOException - If some other I/O error occurs. Throwing this exception causes that the underlying connection will be closed.

onDisconnect

public boolean onDisconnect(INonBlockingConnection connection)
                     throws java.io.IOException
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:
java.io.IOException - If some I/O error occurs.

onData

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

Please note, that the onData callback method could also be called for an already closed connection. This occurs when data has been received (and buffered internally) and the connection has been closed by the peer, immediately. In this case the isOpen call within the onData Method will return false. Reading of already received data wouldn`t fail. To detect if a connection has been closed the callback method onDisconnect should be implemented. The correct callback 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:
java.io.IOException - If some other I/O error occurs. Throwing this exception causes that the underlying connection will be closed.

onConnectionTimeout

public boolean onConnectionTimeout(INonBlockingConnection connection)
                            throws java.io.IOException
handles the connection timeout.

Specified by:
onConnectionTimeout in interface ITimeoutHandler
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:
java.io.IOException - if an error occurs. Throwing this exception causes that the underlying connection will be closed.

onIdleTimeout

public boolean onIdleTimeout(INonBlockingConnection connection)
                      throws java.io.IOException
handles the idle timeout.

Specified by:
onIdleTimeout in interface ITimeoutHandler
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:
java.io.IOException - if an error occurs. Throwing this exception causes that the underlying connection will be closed.

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException

Specified by:
clone in interface IConnectionScoped
Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException