|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.net.ServerSocket
org.sapia.ubik.net.mplex.MultiplexServerSocket
public class MultiplexServerSocket
This class is the main server socket that multiplexes the traditionnal
java.net.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 the MultiplexServerSocket was 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| Nested Class Summary | |
|---|---|
class |
MultiplexServerSocket.SelectorTask
This class is responsible of the selection logic of the multiplex. |
| Field Summary | |
|---|---|
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 Summary | |
|---|---|
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,
java.net.InetAddress bindAddr)
Creates a new MultiplexServerSocket instance. |
|
| Method Summary | |
|---|---|
java.net.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. |
java.lang.String |
toString()
Returns the implementation address and implementation port of this socket as a String. |
| Methods inherited from class java.net.ServerSocket |
|---|
bind, bind, getChannel, getInetAddress, getLocalPort, getLocalSocketAddress, getReceiveBufferSize, getReuseAddress, getSoTimeout, implAccept, isBound, isClosed, setPerformancePreferences, setReceiveBufferSize, setReuseAddress, setSocketFactory, setSoTimeout |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final short DEFAULT_READ_AHEAD_BUFFER_SIZE
public static final short DEFAULT_ACCEPTOR_DAEMON_THREAD
public static final short DEFAULT_SELECTOR_DAEMON_THREAD
| Constructor Detail |
|---|
public MultiplexServerSocket()
throws java.io.IOException
java.io.IOException - If an error occurs opening the socket.
public MultiplexServerSocket(int port)
throws java.io.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.
java.io.IOException - If an error occurs when opening the socket.
public MultiplexServerSocket(int port,
int backlog)
throws java.io.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.
java.io.IOException - If an error occurs when opening the socket.
public MultiplexServerSocket(int port,
int backlog,
java.net.InetAddress bindAddr)
throws java.io.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.
java.io.IOException - If an error occurs when opening the socket.| Method Detail |
|---|
public int getReadAheadBufferSize()
public void setReadAheadBufferSize(int aSize)
accept().
aSize - The new size.
java.lang.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.
java.lang.IllegalStateException - If the server is already running.public void setSelectorDaemonThread(int maxThread)
maxThread - The new numbe of running daemon.
java.lang.IllegalStateException - If the server is already running.public MultiplexSocketConnector createSocketConnector(StreamSelector aSelector)
aSelector - The stream selector to assign to the created socket connector.
java.lang.IllegalStateException - If this server socket is closed.public void removeSocketConnector(MultiplexSocketConnector aConnector)
aConnector - The socket connector to remove from this server socket.
public java.net.Socket accept()
throws java.io.IOException
accept in class java.net.ServerSocketjava.io.IOException - If an I/O error occurs when waiting for a connection.
java.net.SocketTimeoutException - if a timeout was previously set with setSoTimeout and
the timeout has been reached.
public void close()
throws java.io.IOException
close in class java.net.ServerSocketjava.io.IOException - If an I/O error occurs when closing the socket.public java.lang.String toString()
String.
toString in class java.net.ServerSocketpublic void run()
run in interface java.lang.Runnable
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||