org.xsocket.connection
Interface IServer

Package class diagram package IServer
All Superinterfaces:
Closeable, Runnable
All Known Implementing Classes:
Server

public interface IServer
extends Runnable, Closeable

A server accepts new incoming 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 analyzed by using reflection during the server start-up phase. The callback method will be marked by implementing the specific interface like IDataHandler or IConnectHandler. Often a handler will implement several handler interfaces.

E.g.

   ...
   IServer smtpServer = new Server(25, new SmtpProtcolHandler());
   smtpServer.setFlushmode(FlushMode.ASYNC);
   
   smtpServer.start();
   ...


   // Handler definition
   class SmtpProtcolHandler implements IDataHandler, IConnectHandler {

      SessionData sessionData = (SessionData) connection.getAttachment();
      
      String cmd = connection.readStringByDelimiter("\r\n").toUpperCase();
      
      if (cmd.startsWith("HELO")) {
         connection.write("250 SMTP Service\r\n");
         
      } else if(cmd.equals("DATA")) {
         String msgId = connection.getId() + "." + sessionData.nextId();
         File msgFile = new File(msgFileDir + File.separator + msgId + ".msg");
         
         connection.setHandler(new DataHandler(msgFile, this));
         connection.write("354 Enter message, ending with \".\"\r\n");
         
      } else {
       
         ...         
      }
      
      ...
   }
 

Author:
grro@xsocket.org

Field Summary
static int DEFAULT_CONNECTION_TIMEOUT_SEC
          the default connection timeout
static String DEFAULT_HOST_ADDRESS
          the dafault host address
static int DEFAULT_IDLE_TIMEOUT_SEC
          the default idle timeout
static int DEFAULT_READ_TRANSFER_PREALLOCATION_MIN_SIZE
           
static int DEFAULT_READ_TRANSFER_PREALLOCATION_SIZE
           
static boolean DEFAULT_READ_TRANSFER_USE_DIRECT
           
static String SO_RCVBUF
           
static String SO_REUSEADDR
           
 
Method Summary
 void addListener(IServerListener listener)
          adds a listener
 boolean getAutoflush()
          get autoflush.
 long getConnectionTimeoutMillis()
          gets the connection timeout
 IConnection.FlushMode getFlushMode()
          return the flush mode for new connections
 IHandler getHandler()
          gets the handler
 long getIdleTimeoutMillis()
          returns the idle timeout in millis.
 InetAddress getLocalAddress()
          get the local address
 int getLocalPort()
          get the server port
 Object getOption(String name)
          returns the vlaue of a option
 Map<String,Class> getOptions()
          Returns an unmodifiable map of the options supported by this endpont.
 String getStartUpLogMessage()
          returns the startUp log message
 Executor getWorkerpool()
          return the worker pool
 boolean isOpen()
          signals, if service is running
 boolean removeListener(IServerListener listener)
          removes a listener
 void setAutoflush(boolean autoflush)
          set autoflush for new connections.
 void setConnectionTimeoutMillis(long timeoutMillis)
          sets the max time for a connections.
 void setFlushMode(IConnection.FlushMode flusmode)
          sets the flush mode for new connections.
 void setIdleTimeoutMillis(long timeoutInMillis)
          sets the idle timeout in millis
 void setStartUpLogMessage(String message)
          set the log message, which will be printed out during the start up
 void setWorkerpool(Executor workerpool)
          sets the worker pool
 void setWriteTransferRate(int bytesPerSecond)
          set the send delay time for a connection.
 void start()
          starts the given server within a dedicated thread.
 
Methods inherited from interface java.lang.Runnable
run
 
Methods inherited from interface java.io.Closeable
close
 

Field Detail

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

DEFAULT_HOST_ADDRESS

static final String DEFAULT_HOST_ADDRESS
the dafault host address

See Also:
Constant Field Values

SO_RCVBUF

static final String SO_RCVBUF
See Also:
Constant Field Values

SO_REUSEADDR

static final String SO_REUSEADDR
See Also:
Constant Field Values

DEFAULT_READ_TRANSFER_PREALLOCATION_SIZE

static final int DEFAULT_READ_TRANSFER_PREALLOCATION_SIZE
See Also:
Constant Field Values

DEFAULT_READ_TRANSFER_PREALLOCATION_MIN_SIZE

static final int DEFAULT_READ_TRANSFER_PREALLOCATION_MIN_SIZE
See Also:
Constant Field Values

DEFAULT_READ_TRANSFER_USE_DIRECT

static final boolean DEFAULT_READ_TRANSFER_USE_DIRECT
See Also:
Constant Field Values
Method Detail

isOpen

boolean isOpen()
signals, if service is running

Returns:
true, if the server is running

start

void start()
           throws IOException
starts the given server within a dedicated thread. This method blocks until the server is open.

Throws:
SocketTimeoutException - is the timeout has been reached
IOException

getIdleTimeoutMillis

long getIdleTimeoutMillis()
returns the idle timeout in millis.

Returns:
idle timeout in millis

setIdleTimeoutMillis

void setIdleTimeoutMillis(long timeoutInMillis)
sets the idle timeout in millis

Parameters:
timeoutInSec - idle timeout in millis

getConnectionTimeoutMillis

long getConnectionTimeoutMillis()
gets the connection timeout

Returns:
connection timeout

setConnectionTimeoutMillis

void setConnectionTimeoutMillis(long timeoutMillis)
sets the max time for a connections. By exceeding this time the connection will be terminated

Parameters:
timeoutSec - the connection timeout in millis

setWriteTransferRate

void setWriteTransferRate(int bytesPerSecond)
                          throws IOException
set the send delay time for a connection. Data to write will be buffered internally and be written to the underlying subsystem based on the given write rate. The write methods will not block for this time.
By default the write transfer rate is set with UNLIMITED

Reduced write transfer is only supported for FlushMode.ASYNC. see INonBlockingConnection#setFlushmode(org.xsocket.connection.IConnection.FlushMode))

Parameters:
bytesPerSecond - the transfer rate of the outgoing data
Throws:
IOException - If some other I/O error occurs

getLocalPort

int getLocalPort()
get the server port

Returns:
the server port

getWorkerpool

Executor getWorkerpool()
return the worker pool

Returns:
the worker pool

setWorkerpool

void setWorkerpool(Executor workerpool)
sets the worker pool

Parameters:
workerpool - the workerpool

getHandler

IHandler getHandler()
gets the handler

Returns:
the handler

setFlushMode

void setFlushMode(IConnection.FlushMode flusmode)
sets the flush mode for new connections. See INonBlockingConnection#setFlushmode(FlushMode) for more information

Parameters:
flusmode - the flush mode

getFlushMode

IConnection.FlushMode getFlushMode()
return the flush mode for new connections

Returns:
the flush mode

setAutoflush

void setAutoflush(boolean autoflush)
set autoflush for new connections. See IReadWriteableConnection#setAutoflush(boolean) for more information

Parameters:
autoflush - true if autoflush should be activated

getAutoflush

boolean getAutoflush()
get autoflush. See IReadWriteableConnection#setAutoflush(boolean) for more information

Returns:
true, if autoflush is activated

addListener

void addListener(IServerListener listener)
adds a listener

Parameters:
listener - gthe listener to add

removeListener

boolean removeListener(IServerListener listener)
removes a listener

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

getLocalAddress

InetAddress getLocalAddress()
get the local address

Returns:
the local address

getOption

Object getOption(String name)
                 throws IOException
returns the vlaue of a option

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

setStartUpLogMessage

void setStartUpLogMessage(String message)
set the log message, which will be printed out during the start up

Parameters:
message - the startUp log message

getStartUpLogMessage

String getStartUpLogMessage()
returns the startUp log message

Returns:
the startUp log message

getOptions

Map<String,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


Copyright 2008 xSocket.org