org.szegedi.nioserver
Class NioServer

java.lang.Object
  |
  +--org.szegedi.nioserver.NioServer
All Implemented Interfaces:
Log

public class NioServer
extends java.lang.Object
implements Log

The runtime class for non-blocking servers. Every NioServer manages a number of threads for accepting connections, reading, and writing. In the basic case, you will have a single thread for accepting connections and another single thread for processing read and write operations. However, if you are running the code on a multiprocessor machine, you can create more initial threads to improve CPU utilization. Also, when running on Windows, the server will spawn new threads when all current threads service 63 channels - the maximum number of channels a single thread (more precisely "the maximum number of keys a single selector") can service under this OS family. Note that any number of different services (that is, server sockets) bound to any IP address and any port on the local machine can be serviced by a single server instance - the non-blocking nature allows accepting connections from multiple server sockets on a single thread.

An implementation note: the server by default operates in "half-duplex" mode. This means that read and write notifications for each connection are serviced on a single thread, therefore the server will never both read and write a connection concurrently - if the connection is ready for both reading and writing, read will occur before write. You can force the server into a "full-duplex" mode where read and write operations are serviced on separate threads by seting the org.szegedi.nio.fullDuplex system property to true. Note however, that some combinations of a JVM and OS cause erroneous operation in full-duplex mode, so test for yourself. Since most protocols follow some kind of request-response semantics, this is usually not a big problem. Also note that half-duplex applies only to a single connection; if you have multiple read-write threads, then the connections on separate threads are still serviced concurrently. The term half-duplex applies exclusively to serialization of read and write operations in a context of single connection.

Version:
$Id : $
Author:
Attila Szegedi, szegedia at freemail dot hu

Field Summary
 
Fields inherited from interface org.szegedi.nioserver.Log
DEBUG, ERROR, INFO, LEVELNAMES, WARNING
 
Constructor Summary
NioServer()
          Creates a new non-blocking server that will use one permanent thread for accepting connections and two permanent thread for read/write operations.
NioServer(int permanentAcceptThreads, int permanentTransferThreads)
          Creates a new non-blocking server that will use the specified number of permanent threads for accepting connections and for read/write operations.
 
Method Summary
 void log(java.lang.String message, int level)
           
 void log(java.lang.String message, java.lang.Throwable t, int level)
           
 Service registerService(ProtocolHandlerFactory factory, ServerSocketConfiguration ssconf)
          Adds a service to this server.
 void stop()
          Stops this server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NioServer

public NioServer()
          throws java.io.IOException
Creates a new non-blocking server that will use one permanent thread for accepting connections and two permanent thread for read/write operations.


NioServer

public NioServer(int permanentAcceptThreads,
                 int permanentTransferThreads)
          throws java.io.IOException
Creates a new non-blocking server that will use the specified number of permanent threads for accepting connections and for read/write operations.

Parameters:
permanentAcceptThreads - number of threads that are always available for accepting connections, regardless of the server load. One thread will usually be sufficient, however on a SMP machine with several network interfaces you can achieve better throughput with more threads.
permanentTransferThreads - number of threads that are always available for servicing socket reads and writes, regardless of server load. Using two threads per CPU should yield satisfactory results.
Method Detail

stop

public void stop()
Stops this server. All threads are terminated and all services are stopped and unregistered.


registerService

public Service registerService(ProtocolHandlerFactory factory,
                               ServerSocketConfiguration ssconf)
                        throws java.io.IOException
Adds a service to this server. A service is a coupling of a protocol handler factory and a server socket configuration. After a service is added, the server will accept connections on the socket address specified by the server configuration and hand over accepted connections to the protocol handler factory. The protocol handlers created by the factory will then be called whenever there is a pending read or write operation on their associated connection.

Parameters:
factory - the protocol handler factory that will create protocol handlers for servicing connections
ssconf - a ServerSocketConfiguration object that specifies server socket address, port, timeout behavior, etc.
Returns:
a service object that represents the registered network service.
java.io.IOException

log

public void log(java.lang.String message,
                int level)
Specified by:
log in interface Log

log

public void log(java.lang.String message,
                java.lang.Throwable t,
                int level)
Specified by:
log in interface Log