org.xsocket.connection
Class Server

Package class diagram package Server
java.lang.Object
  extended by org.xsocket.connection.Server
All Implemented Interfaces:
Closeable, Runnable, IServer

public class Server
extends Object
implements IServer

Implementation of a server. For more information see IServer

Author:
grro@xsocket.org

Field Summary
protected static int MIN_SIZE_WORKER_POOL
           
protected static int SIZE_WORKER_POOL
           
protected static int TASK_QUEUE_SIZE
           
 
Fields inherited from interface org.xsocket.connection.IServer
DEFAULT_CONNECTION_TIMEOUT_SEC, DEFAULT_IDLE_TIMEOUT_SEC, DEFAULT_READ_TRANSFER_PREALLOCATION_MIN_SIZE, DEFAULT_READ_TRANSFER_PREALLOCATION_SIZE, DEFAULT_READ_TRANSFER_USE_DIRECT, SO_RCVBUF, SO_REUSEADDR
 
Constructor Summary
  Server(IHandler handler)
          constructor

  Server(InetAddress address, int port, IHandler handler)
          constructor

  Server(InetAddress address, int port, IHandler handler, SSLContext sslContext, boolean sslOn)
          constructor

  Server(InetAddress address, int port, Map<String,Object> options, IHandler handler, SSLContext sslContext, boolean sslOn)
          constructor

  Server(InetAddress address, int port, Map<String,Object> options, IHandler handler, SSLContext sslContext, boolean sslOn, int backlog)
          constructor

  Server(InetAddress address, int port, Map<String,Object> options, IHandler handler, SSLContext sslContext, boolean sslOn, int backlog, int minPoolsize, int maxPoolsize)
          constructor

protected Server(InetSocketAddress address, Map<String,Object> options, IHandler handler, SSLContext sslContext, boolean sslOn, int backlog)
          constructor
protected Server(InetSocketAddress address, Map<String,Object> options, IHandler handler, SSLContext sslContext, boolean sslOn, int backlog, int minPoolsize, int maxPoolsize)
          constructor
protected Server(InetSocketAddress address, Map<String,Object> options, IHandler handler, SSLContext sslContext, boolean sslOn, int backlog, int minPoolsize, int maxPoolsize, int taskqueueSize)
          constructor
  Server(int port, IHandler handler)
          constructor

  Server(int port, IHandler handler, int backlog)
          constructor

  Server(int port, IHandler handler, int minPoolsize, int maxPoolsize)
          constructor

  Server(int port, IHandler handler, SSLContext sslContext, boolean sslOn)
          constructor

  Server(int port, IHandler handler, SSLContext sslContext, boolean sslOn, int minPoolsize, int maxPoolsize)
          constructor

  Server(int port, Map<String,Object> options, IHandler handler)
          constructor

  Server(int port, Map<String,Object> options, IHandler handler, SSLContext sslContext, boolean sslOn)
          constructor

  Server(Map<String,Object> options, IHandler handler)
          constructor

  Server(String ipAddress, int port, IHandler handler)
          constructor

  Server(String ipAddress, int port, IHandler handler, SSLContext sslContext, boolean sslOn)
          constructor

  Server(String ipAddress, int port, Map<String,Object> options, IHandler handler)
          constructor

  Server(String ipAddress, int port, Map<String,Object> options, IHandler handler, SSLContext sslContext, boolean sslOn)
          constructor

 
Method Summary
 void addListener(IServerListener listener)
          adds a listener
 void close()
          
 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.
 String getImplementationDate()
          returns the implementation date
 String getImplementationVersion()
          returns the implementation version
 InetAddress getLocalAddress()
          get the local address
 int getLocalPort()
          get the server port
 Set<INonBlockingConnection> getOpenConnections()
           
 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 getServerName()
          return the server name
 String getStartUpLogMessage()
          returns the startUp log message
 Executor getWorkerpool()
          return the worker pool
 boolean isOpen()
          signals, if service is running
protected  void onClosed()
           
protected  void onPreRejectConnection(NonBlockingConnection connection)
           
 boolean removeListener(IServerListener listener)
          removes a listener
 void run()
          
 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 setHandler(IHandler handler)
          set the handler
 void setIdleTimeoutMillis(long timeoutMillis)
          sets the idle timeout in millis
 void setMaxConcurrentConnections(int maxConcurrentConnections)
          sets the max number of concurrent connections
 void setMaxReadBufferThreshold(int maxSize)
          set the max app read buffer threshold
 void setServerName(String name)
          the the server name.
 void setStartUpLogMessage(String message)
          set the log message, which will be printed out during the start up
 void setWorkerpool(Executor executor)
          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.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SIZE_WORKER_POOL

protected static final int SIZE_WORKER_POOL

MIN_SIZE_WORKER_POOL

protected static final int MIN_SIZE_WORKER_POOL

TASK_QUEUE_SIZE

protected static final int TASK_QUEUE_SIZE
Constructor Detail

Server

public Server(IHandler handler)
       throws UnknownHostException,
              IOException
constructor

Parameters:
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
Throws:
IOException - If some other I/O error occurs
UnknownHostException - if the local host cannot determined

Server

public Server(Map<String,Object> options,
              IHandler handler)
       throws UnknownHostException,
              IOException
constructor

Parameters:
options - the socket options
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
Throws:
IOException - If some other I/O error occurs
UnknownHostException - if the local host cannot determined

Server

public Server(int port,
              IHandler handler)
       throws UnknownHostException,
              IOException
constructor

Parameters:
port - the local port
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(int port,
              IHandler handler,
              int minPoolsize,
              int maxPoolsize)
       throws UnknownHostException,
              IOException
constructor

Parameters:
port - the local port
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
minPoolsize - the min workerpool size
maxPoolsize - the max workerpool size
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(int port,
              IHandler handler,
              int backlog)
       throws UnknownHostException,
              IOException
constructor

Parameters:
port - the local port
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
backlog - The maximum number number of pending connections. If has the value 0, or a negative value, then an implementation specific default is used.
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(int port,
              Map<String,Object> options,
              IHandler handler)
       throws UnknownHostException,
              IOException
constructor

Parameters:
port - the local port
options - the acceptor socket options
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(InetAddress address,
              int port,
              IHandler handler)
       throws UnknownHostException,
              IOException
constructor

Parameters:
address - the local address
port - the local port
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(String ipAddress,
              int port,
              IHandler handler)
       throws UnknownHostException,
              IOException
constructor

Parameters:
ipAddress - the local ip address
port - the local port
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(String ipAddress,
              int port,
              Map<String,Object> options,
              IHandler handler)
       throws UnknownHostException,
              IOException
constructor

Parameters:
ipAddress - the local ip address
port - the local port
options - the socket options
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(int port,
              IHandler handler,
              SSLContext sslContext,
              boolean sslOn)
       throws UnknownHostException,
              IOException
constructor

Parameters:
port - local port
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(int port,
              IHandler handler,
              SSLContext sslContext,
              boolean sslOn,
              int minPoolsize,
              int maxPoolsize)
       throws UnknownHostException,
              IOException
constructor

Parameters:
port - local port
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
minPoolsize - the min workerpool size
maxPoolsize - the max workerpool size
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(int port,
              Map<String,Object> options,
              IHandler handler,
              SSLContext sslContext,
              boolean sslOn)
       throws UnknownHostException,
              IOException
constructor

Parameters:
port - local port
options - the acceptor socket options
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(String ipAddress,
              int port,
              IHandler handler,
              SSLContext sslContext,
              boolean sslOn)
       throws UnknownHostException,
              IOException
constructor

Parameters:
ipAddress - local ip address
port - local port
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(String ipAddress,
              int port,
              Map<String,Object> options,
              IHandler handler,
              SSLContext sslContext,
              boolean sslOn)
       throws UnknownHostException,
              IOException
constructor

Parameters:
ipAddress - local ip address
port - local port
options - the acceptor socket options
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(InetAddress address,
              int port,
              IHandler handler,
              SSLContext sslContext,
              boolean sslOn)
       throws UnknownHostException,
              IOException
constructor

Parameters:
address - local address
port - local port
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(InetAddress address,
              int port,
              Map<String,Object> options,
              IHandler handler,
              SSLContext sslContext,
              boolean sslOn)
       throws UnknownHostException,
              IOException
constructor

Parameters:
address - local address
port - local port
options - the socket options
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(InetAddress address,
              int port,
              Map<String,Object> options,
              IHandler handler,
              SSLContext sslContext,
              boolean sslOn,
              int backlog)
       throws UnknownHostException,
              IOException
constructor

Parameters:
address - local address
port - local port
options - the socket options
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
backlog - The maximum number number of pending connections. If has the value 0, or a negative value, then an implementation specific default is used.
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

public Server(InetAddress address,
              int port,
              Map<String,Object> options,
              IHandler handler,
              SSLContext sslContext,
              boolean sslOn,
              int backlog,
              int minPoolsize,
              int maxPoolsize)
       throws UnknownHostException,
              IOException
constructor

Parameters:
address - local address
port - local port
options - the socket options
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
backlog - The maximum number number of pending connections. If has the value 0, or a negative value, then an implementation specific default is used.
minPoolsize - The min workerpool size
maxPoolsize - The max workerpool size
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

protected Server(InetSocketAddress address,
                 Map<String,Object> options,
                 IHandler handler,
                 SSLContext sslContext,
                 boolean sslOn,
                 int backlog)
          throws UnknownHostException,
                 IOException
constructor

Parameters:
address - local address
options - the socket options
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
backlog - The maximum number number of pending connections. If has the value 0, or a negative value, then an implementation specific default is used.
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

protected Server(InetSocketAddress address,
                 Map<String,Object> options,
                 IHandler handler,
                 SSLContext sslContext,
                 boolean sslOn,
                 int backlog,
                 int minPoolsize,
                 int maxPoolsize)
          throws UnknownHostException,
                 IOException
constructor

Parameters:
address - local address
options - the socket options
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
backlog - The maximum number number of pending connections. If has the value 0, or a negative value, then an implementation specific default is used.
minPoolsize - The min workerpool size
maxPoolsize - The max workerpool size
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs

Server

protected Server(InetSocketAddress address,
                 Map<String,Object> options,
                 IHandler handler,
                 SSLContext sslContext,
                 boolean sslOn,
                 int backlog,
                 int minPoolsize,
                 int maxPoolsize,
                 int taskqueueSize)
          throws UnknownHostException,
                 IOException
constructor

Parameters:
address - local address
options - the socket options
handler - the handler to use (supported: IConnectHandler, IDisconnectHandler, IDataHandler, IIdleTimeoutHandler, IConnectionTimeoutHandler, IConnectionScoped, ILifeCycle)
sslOn - true, is SSL should be activated
sslContext - the ssl context to use
backlog - The maximum number number of pending connections. If has the value 0, or a negative value, then an implementation specific default is used.
minPoolsize - The min workerpool size
maxPoolsize - The max workerpool size
taskqueueSize - The taskqueue size
Throws:
UnknownHostException - if the local host cannot determined
IOException - If some other I/O error occurs
Method Detail

setHandler

public void setHandler(IHandler handler)
set the handler

Parameters:
handler - the handler

setServerName

public final void setServerName(String name)
the the server name. The server name will be used to print out the start log message.
E.g.
   IServer cacheServer = new Server(port, new CacheHandler());
   ConnectionUtils.start(server);
   server.setServerName("CacheServer");


   // prints out
   // 01::52::42,756 10 INFO [Server$AcceptorCallback#onConnected] CacheServer listening on 172.25.34.33/172.25.34.33:9921 (xSocket 2.0)
 

Parameters:
name - the server name

getServerName

public final String getServerName()
return the server name

Returns:
the server name

getStartUpLogMessage

public String getStartUpLogMessage()
returns the startUp log message

Specified by:
getStartUpLogMessage in interface IServer
Returns:
the startUp log message

setStartUpLogMessage

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

Specified by:
setStartUpLogMessage in interface IServer
Parameters:
message - the startUp log message

run

public void run()

Specified by:
run in interface Runnable

start

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

Specified by:
start in interface IServer
Throws:
SocketTimeoutException - is the timeout has been reached
IOException

getOption

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

Specified by:
getOption in interface IServer
Parameters:
name - the name of the option
Returns:
the value of the option
Throws:
IOException - In an I/O error occurs

getHandler

public IHandler getHandler()
Description copied from interface: IServer
gets the handler

Specified by:
getHandler in interface IServer
Returns:
the handler

getOptions

public final 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.

Specified by:
getOptions in interface IServer
Returns:
An unmodifiable map of the options supported by this channel

close

public final void close()

Specified by:
close in interface Closeable

onClosed

protected void onClosed()
                 throws IOException
Throws:
IOException

onPreRejectConnection

protected void onPreRejectConnection(NonBlockingConnection connection)
                              throws IOException
Throws:
IOException

addListener

public final void addListener(IServerListener listener)
adds a listener

Specified by:
addListener in interface IServer
Parameters:
listener - gthe listener to add

removeListener

public final boolean removeListener(IServerListener listener)
removes a listener

Specified by:
removeListener in interface IServer
Parameters:
listener - the listener to remove
Returns:
true, is the listener has been removed

getWorkerpool

public final Executor getWorkerpool()
return the worker pool

Specified by:
getWorkerpool in interface IServer
Returns:
the worker pool

setWorkerpool

public final void setWorkerpool(Executor executor)
sets the worker pool

Specified by:
setWorkerpool in interface IServer
Parameters:
executor - the workerpool

isOpen

public final boolean isOpen()
signals, if service is running

Specified by:
isOpen in interface IServer
Returns:
true, if the server is running

setMaxConcurrentConnections

public final void setMaxConcurrentConnections(int maxConcurrentConnections)
sets the max number of concurrent connections

Parameters:
maxConcurrentConnections - the max number of concurrent connections

setMaxReadBufferThreshold

public void setMaxReadBufferThreshold(int maxSize)
set the max app read buffer threshold

Parameters:
maxSize - the max read buffer threshold

getLocalPort

public final int getLocalPort()
get the server port

Specified by:
getLocalPort in interface IServer
Returns:
the server port

getLocalAddress

public final InetAddress getLocalAddress()
get the local address

Specified by:
getLocalAddress in interface IServer
Returns:
the local address

getOpenConnections

public Set<INonBlockingConnection> getOpenConnections()

getFlushmode

public final IConnection.FlushMode getFlushmode()
return the flush mode for new connections

Specified by:
getFlushmode in interface IServer
Returns:
the flush mode

setFlushmode

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

Specified by:
setFlushmode in interface IServer
Parameters:
flusmode - the flush mode

setAutoflush

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

Specified by:
setAutoflush in interface IServer
Parameters:
autoflush - true if autoflush should be activated

getAutoflush

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

Specified by:
getAutoflush in interface IServer
Returns:
true, if autoflush is activated

setConnectionTimeoutMillis

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

Specified by:
setConnectionTimeoutMillis in interface IServer

setWriteTransferRate

public 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))

Specified by:
setWriteTransferRate in interface IServer
Parameters:
bytesPerSecond - the transfer rate of the outgoing data
Throws:
IOException - If some other I/O error occurs

setIdleTimeoutMillis

public void setIdleTimeoutMillis(long timeoutMillis)
sets the idle timeout in millis

Specified by:
setIdleTimeoutMillis in interface IServer

getConnectionTimeoutMillis

public final long getConnectionTimeoutMillis()
gets the connection timeout

Specified by:
getConnectionTimeoutMillis in interface IServer
Returns:
connection timeout

getIdleTimeoutMillis

public final long getIdleTimeoutMillis()
returns the idle timeout in millis.

Specified by:
getIdleTimeoutMillis in interface IServer
Returns:
idle timeout in millis

getImplementationVersion

public String getImplementationVersion()
returns the implementation version

Returns:
the implementation version

getImplementationDate

public String getImplementationDate()
returns the implementation date

Returns:
the implementation date

toString

public String toString()
Overrides:
toString in class Object


Copyright 2010 xSocket.org