org.xsocket.stream
Interface IMultithreadedServer

Package class diagram package IMultithreadedServer
All Superinterfaces:
java.io.Closeable, java.lang.Runnable
All Known Implementing Classes:
MultithreadedServer

public interface IMultithreadedServer
extends java.lang.Runnable, java.io.Closeable

A server accepts new incomming connections, and delegates the handling of the INonBlockingConnection to the assigned handler. The server includes dispatchers, which are responsible to perform the socket I/O operations. A connection is assigned to one dispatcher.
To handle application-relevant events like onData, onClose or onConnect the appropriated callback method of the assigned IHandler will be called. The supported callback methods of the handler will be analysed by using reflection during the server start-up phase. The callback method will be marked by implementing the specifc interface like IDataHandler or IConnectHandler. Often a handler will implement serveral handler interfaces.
If a handler implements the IConnectionScoped interface, a clone of the registered handler will be created for each new incomming connection.
E.g.

   ...
   IMultithreadedConnectionServer smtpServer = new MultithreadedTcpServer(port, new SmtpProtcolHandler());
   StreamUtils.start(server);
   ...
   
   
   // Handler definition
   class SmtpProtcolHandler implements IDataHandler, IConnectHandler, IConnectionScoped {
      private ArrayList rcptTos = new ArrayList();
      ...
   
      public boolean onConnect(INonBlockingConnection connection) throws IOException {
          connection.setAutoflush(false);
          connection.setFlushmode(FlushMode.ASYNC);
          
          connection.write("220 this is the example smtp server" + LINE_DELIMITER);
          connection.flush();
          return true;
      }
   
   
   
      public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException {
         switch (state) {
             case COMMAND_HANDLING:
                 handleCommand(connection);
                 break;
                 
             case MESSAGE_DATA_HANDLING:
                 handleMessageData(connection);
                 break;
         }
         return true;
      }   
      
      private void handleCommand(INonBlockingConnection connection) throws IOException, BufferUnderflowException {
      ...
      
      
      
      @Override
      public Object clone() throws CloneNotSupportedException {
          SmtpProtocolHandler copy = (SmtpProtocolHandler) super.clone();
          copy.rcptTos = new ArrayList(); // deep copy!
          return copy;
      }
   }
 


Field Summary
static int DEFAULT_CONNECTION_TIMEOUT_SEC
          the default connection timeout
static int DEFAULT_IDLE_TIMEOUT_SEC
          the default idle timeout
static int DEFAULT_RECEIVE_BUFFER_PREALLOCATION_SIZE
          Deprecated.  
static java.lang.String SO_RCVBUF
           
static java.lang.String SO_REUSEADDR
           
 
Method Summary
 void addListener(IMutlithreadedServerListener listener)
          adds a listener
 int getConnectionTimeoutSec()
          gets the connection timeout
 int getDispatcherPoolSize()
          get the dispatcher pool size
 int getIdleTimeoutSec()
          returns the idle timeout in sec.
 java.net.InetAddress getLocalAddress()
          get the local address
 int getLocalPort()
          get the server port
 java.lang.Object getOption(java.lang.String name)
          returns the vlaue of a option
 java.util.Map<java.lang.String,java.lang.Class> getOptions()
          Returns an unmodifiable map of the options supported by this endpont.
 int getReceiveBufferPreallocationSize()
          Deprecated. on replacement
 java.util.concurrent.Executor getWorkerpool()
          return the worker pool
 IWorkerPool getWorkerPool()
          Deprecated. use getWorkerpool() instead
 boolean isOpen()
          signals, if service is running
 boolean removeListener(IMutlithreadedServerListener listener)
          removes a listener
 void setConnectionTimeoutSec(int timeoutSec)
          sets the max time for a connections.
 void setDispatcherPoolSize(int size)
          Deprecated.  
 void setHandler(IHandler handler)
          set the handler.
 void setIdleTimeoutSec(int timeoutInSec)
          sets the idle timeout in sec
 void setReceiveBufferPreallocationSize(int size)
          Deprecated. use System.property instead. see IoProvider
 void setWorkerPool(IWorkerPool workerPool)
          Deprecated. worker pool should only be set by calling the constructor
 
Methods inherited from interface java.lang.Runnable
run
 
Methods inherited from interface java.io.Closeable
close
 

Field Detail

DEFAULT_RECEIVE_BUFFER_PREALLOCATION_SIZE

static final int DEFAULT_RECEIVE_BUFFER_PREALLOCATION_SIZE
Deprecated. 
the default receive buffer preallocation size

See Also:
Constant Field Values

DEFAULT_IDLE_TIMEOUT_SEC

static final int DEFAULT_IDLE_TIMEOUT_SEC
the default idle timeout

See Also:
Constant Field Values

DEFAULT_CONNECTION_TIMEOUT_SEC

static final int DEFAULT_CONNECTION_TIMEOUT_SEC
the default connection timeout

See Also:
Constant Field Values

SO_RCVBUF

static final java.lang.String SO_RCVBUF
See Also:
Constant Field Values

SO_REUSEADDR

static final java.lang.String SO_REUSEADDR
See Also:
Constant Field Values
Method Detail

isOpen

boolean isOpen()
signals, if service is running

Returns:
true, if the server is running

setHandler

void setHandler(IHandler handler)
set the handler. The server performs the initialzation of the given handler immediately.

Parameters:
handler - the handler. (supported: IConnectHandler, IDisconnectHandler, IDataHandler, ITimeoutHandler, IConnectionScoped, ILifeCycle)

setWorkerPool

void setWorkerPool(IWorkerPool workerPool)
Deprecated. worker pool should only be set by calling the constructor

replace the worker pool with the given one. By setting the WorkerPool with null the worker pool will be deactivated. The callback handler will be executed by the main dispatching thread. If the handler performs blocking operations, the disptaching will also be blocked!

By closing the endpoint, the close method of the worker pool will be called

Parameters:
workerPool - the worker pool to set or null to deactivate the worker pool

getWorkerPool

IWorkerPool getWorkerPool()
Deprecated. use getWorkerpool() instead

return the worker pool

Returns:
the worker pool

getWorkerpool

java.util.concurrent.Executor getWorkerpool()
return the worker pool

Returns:
the worker pool

setReceiveBufferPreallocationSize

void setReceiveBufferPreallocationSize(int size)
Deprecated. use System.property instead. see IoProvider

set the size of the preallocation buffer, for reading incomming data

Parameters:
size - preallocation buffer size

getReceiveBufferPreallocationSize

int getReceiveBufferPreallocationSize()
Deprecated. on replacement

get the size of the preallocation buffer, for reading incomming data

Returns:
preallocation buffer size

getIdleTimeoutSec

int getIdleTimeoutSec()
returns the idle timeout in sec.

Returns:
idle timeout in sec

setIdleTimeoutSec

void setIdleTimeoutSec(int timeoutInSec)
sets the idle timeout in sec

Parameters:
timeoutInSec - idle timeout in sec

addListener

void addListener(IMutlithreadedServerListener listener)
adds a listener

Parameters:
listener - gthe listener to add

removeListener

boolean removeListener(IMutlithreadedServerListener listener)
removes a listener

Parameters:
listener - the listener to remove
Returns:
true, is the listener has been removed

getConnectionTimeoutSec

int getConnectionTimeoutSec()
gets the connection timeout

Returns:
connection timeout

setConnectionTimeoutSec

void setConnectionTimeoutSec(int timeoutSec)
sets the max time for a connections. By exceeding this time the connection will be terminated

Parameters:
timeoutSec - the connection timeout in sec

getLocalPort

int getLocalPort()
get the server port

Returns:
the server port

getLocalAddress

java.net.InetAddress getLocalAddress()
get the local address

Returns:
the local address

setDispatcherPoolSize

void setDispatcherPoolSize(int size)
Deprecated. 

set the dispatcher pool size

Parameters:
size - the dispatcher pool size

getDispatcherPoolSize

int getDispatcherPoolSize()
get the dispatcher pool size

Returns:
the dispatcher pool size

getOption

java.lang.Object getOption(java.lang.String name)
                           throws java.io.IOException
returns the vlaue of a option

Parameters:
name - the name of the option
Returns:
the value of the option
Throws:
java.io.IOException - In an I/O error occurs

getOptions

java.util.Map<java.lang.String,java.lang.Class> getOptions()
Returns an unmodifiable map of the options supported by this endpont. The key in the returned map is the name of a option, and its value is the type of the option value. The returned map will never contain null keys or values.

Returns:
An unmodifiable map of the options supported by this channel