public class MultiplexServerSocket extends ServerSocket implements Runnable
ServerSocket. That means that it listens on the port of the
server socket and it provides a mechanism to register different handlers for
these new incoming socket connections. These handlers are called socket connectors
and they can be used to process multiple types of data streams over the same socket.
For instance, using this MultiplexServerSocket you could handle serialized
Java objects and HTTP requests using two different connectors (two distinct handling
logic), same host/port. This is achieved through a read-ahead on the incoming stream of data
from a new client socket connection. When a new incoming connection is requested by a client,
all the registered StreamSelectors are used to determine which socket connector
will handle the new connection.
The internals of this MultiplexServerSocket are simple: an instance of this class
works on an asynchronous process were new socket connections are first accepted, and then selected.
These two steps are described as follows:
MultiplexSocket
is created on the server-side; the acceptor adds that new connection/socket to the internal
pending queue.
MultiplexServerSocket contains two distinct pools of
threads that are configurable using the setAcceptorDaemonThread(int) and
setSelectorDaemonThread(int) methods.
The primary goal of this class is to keep the "standard" (non-NIO)
programming model of the JDK regarding socket handling. Whether you use this server socket
directly or you use a socket connector, the logic is still the same for the client that
receives new incoming connections: it calls an accept() method that blocks
until a new connection is made. For integration with actual running systems, an adapter
is available to make a socket connector look like a server socket. We used that strategy
with the open-source Simple HTTP server and it worked transparently and fluidly.
MultiplexSocket,
MultiplexSocketConnector,
ServerSocketAdapter,
StreamSelector| Modifier and Type | Class and Description |
|---|---|
class |
MultiplexServerSocket.SelectorTask
This class is responsible of the selection logic of the multiplex.
|
| Modifier and Type | Field and Description |
|---|---|
static short |
DEFAULT_ACCEPTOR_DAEMON_THREAD
The default number of acceptor daemon thread used to accept connections.
|
static short |
DEFAULT_READ_AHEAD_BUFFER_SIZE
The default number of bytes read ahead fom incoming socket connections.
|
static short |
DEFAULT_SELECTOR_DAEMON_THREAD
The default number of selector daemon thread used to accept connections.
|
| Constructor and Description |
|---|
MultiplexServerSocket()
Creates a new MultiplexServerSocket instance.
|
MultiplexServerSocket(int port)
Creates a new MultiplexServerSocket instance.
|
MultiplexServerSocket(int port,
int backlog)
Creates a new MultiplexServerSocket instance.
|
MultiplexServerSocket(int port,
int backlog,
InetAddress bindAddr)
Creates a new MultiplexServerSocket instance.
|
| Modifier and Type | Method and Description |
|---|---|
Socket |
accept()
Listens for a connection to be made to this socket and accepts it.
|
void |
close()
Closes this multiplex server socket.
|
MultiplexSocketConnector |
createSocketConnector(StreamSelector aSelector)
This factory method creates a socket connector through which a client will be able
to receive incoming socket connections.
|
int |
getAcceptorDaemonThread()
Returns the number of daemon threads used to accept incoming connections.
|
int |
getReadAheadBufferSize()
Returns the size of the buffer used to pre-read the incoming bytes of the
accepted connection.
|
int |
getSelectorDaemonThread()
Returns the number of daemon threads used to select an connector for incoming connections.
|
void |
removeSocketConnector(MultiplexSocketConnector aConnector)
Removes the socket connector passed in from the list of available connectors
associated with this server socket.
|
void |
run()
Implements the Runnable interface and it performs the asynchronous acceptor logic
of the multiplex.
|
void |
setAcceptorDaemonThread(int maxThread)
Changes the number of daemon threads used to accept incoming connections.
|
void |
setReadAheadBufferSize(int aSize)
Changes the size of the read ahead buffer size.
|
void |
setSelectorDaemonThread(int maxThread)
Changes the number of daemon threads used to select connectors for incoming connections.
|
void |
setSoTimeout(int timeout) |
String |
toString()
Returns the implementation address and implementation port of
this socket as a
String. |
bind, bind, getChannel, getInetAddress, getLocalPort, getLocalSocketAddress, getReceiveBufferSize, getReuseAddress, getSoTimeout, implAccept, isBound, isClosed, setPerformancePreferences, setReceiveBufferSize, setReuseAddress, setSocketFactorypublic static final short DEFAULT_READ_AHEAD_BUFFER_SIZE
public static final short DEFAULT_ACCEPTOR_DAEMON_THREAD
public static final short DEFAULT_SELECTOR_DAEMON_THREAD
public MultiplexServerSocket()
throws IOException
IOException - If an error occurs opening the socket.public MultiplexServerSocket(int port)
throws IOException
50. If a connection
indication arrives when the queue is full, the connection is refused.port - The port number to bind the server or 0 to use any port.IOException - If an error occurs when opening the socket.public MultiplexServerSocket(int port,
int backlog)
throws IOException
backlog parameter. If
a connection indication arrives when the queue is full, the
connection is refused.port - The port number to bind the server or 0 oi use any port.backlog - The maximum length of the queue.IOException - If an error occurs when opening the socket.public MultiplexServerSocket(int port,
int backlog,
InetAddress bindAddr)
throws IOException
bindAddr passed in.
The maximum queue length for incoming connection indications (a
request to connect) is set to the backlog parameter. If
a connection indication arrives when the queue is full, the
connection is refused.port - The port number to bind the server or 0 oi use any port.backlog - The maximum length of the queue.bindAddr - The local TCP address the server will bind to. If null
the server will accept connections on any/all local addresses.IOException - If an error occurs when opening the socket.public void setSoTimeout(int timeout)
throws SocketException
setSoTimeout in class ServerSocketSocketExceptionpublic int getReadAheadBufferSize()
public void setReadAheadBufferSize(int aSize)
accept().aSize - The new size.IllegalStateException - If the server is already running.public int getAcceptorDaemonThread()
public int getSelectorDaemonThread()
public void setAcceptorDaemonThread(int maxThread)
maxThread - The new numbe of running daemon.IllegalStateException - If the server is already running.public void setSelectorDaemonThread(int maxThread)
maxThread - The new numbe of running daemon.IllegalStateException - If the server is already running.public MultiplexSocketConnector createSocketConnector(StreamSelector aSelector)
aSelector - The stream selector to assign to the created socket connector.IllegalStateException - If this server socket is closed.public void removeSocketConnector(MultiplexSocketConnector aConnector)
aConnector - The socket connector to remove from this server socket.public Socket accept() throws IOException
accept in class ServerSocketIOException - If an I/O error occurs when waiting for a connection.SocketTimeoutException - if a timeout was previously set with setSoTimeout and
the timeout has been reached.public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class ServerSocketIOException - If an I/O error occurs when closing the socket.public String toString()
String.toString in class ServerSocketpublic void run()
Copyright © 2012 Sapia OSS. All Rights Reserved.