de.unkrig.commons.net
Class NioTcpServer

java.lang.Object
  extended by de.unkrig.commons.net.NioTcpServer
All Implemented Interfaces:
Stoppable

public class NioTcpServer
extends java.lang.Object
implements Stoppable

A runnable that accepts TCP connections on a given interface/port and passes them to a NioTcpServer.ConnectionHandler. Does not supprt SSL (yet).

This implementation, opposed to TcpServer, does use java.nio.channels, i.e. connections are handled asynchronously and do not block one thread each. In other words, it scales well for thousands of client connections.


Nested Class Summary
static interface NioTcpServer.ConnectionHandler
          Represents a TCP connection to a remote peer (e.g. a server or a client).
 
Constructor Summary
NioTcpServer()
           
 
Method Summary
 java.net.InetSocketAddress addServer(java.net.InetSocketAddress endpoint, int backlog, NioTcpServer.ConnectionHandler clientConnectionHandler)
          Create a server with the specified endpoint and listen backlog.
 void start(int multiplexerThreadCount)
          Starts this server.
 void stop()
          Returns when this Stoppable has completely stopped.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NioTcpServer

public NioTcpServer()
Method Detail

addServer

public java.net.InetSocketAddress addServer(java.net.InetSocketAddress endpoint,
                                            int backlog,
                                            NioTcpServer.ConnectionHandler clientConnectionHandler)
                                     throws java.io.IOException
Create a server with the specified endpoint and listen backlog.

The address of the endpoint can be used on a multi-homed host for a server that will only accept connect requests to one of its addresses. If address of the endpoint is the wildcard address, it will default accepting connections on any/all local addresses.

The port of the endpoint must be between 0 and 65535, inclusive. If it is zero, then an ephemoral port will be picked for the server socket.

The backlog argument must be a positive value greater than 0. If the value passed if equal or less than 0, then the default value will be assumed.

Parameters:
endpoint - The local port and InetAddress the server will bind to. If null, then the system will pick up an ephemeral port and a valid local address to bind the socket.
backlog - The listen backlog
Returns:
The address and port of the actual endpoint, which may differ from the given endpoint.
Throws:
java.io.IOException

start

public void start(int multiplexerThreadCount)
Starts this server.

Parameters:
multiplexerThreadCount - Number of threads accepting connections. Notice that accepted connections are handled by additional threads, so a value of 1 is appropriate in many cases.

stop

public void stop()
Description copied from interface: Stoppable
Returns when this Stoppable has completely stopped.

Specified by:
stop in interface Stoppable