com.echomine.net
Class SocketAcceptor

java.lang.Object
  extended by com.echomine.net.Connection
      extended by com.echomine.net.TimeableConnection
          extended by com.echomine.net.SocketAcceptor
Direct Known Subclasses:
PerpetualSocketAcceptor

public class SocketAcceptor
extends TimeableConnection

Default Acceptor class for TCP protocols. It contains event firing methods necessary for status observation. The code contains both synchronous and asynchronous acceptance of socket code. Use the accept method for synchronous accepts and the aaccept method for asynchronous accepts (control returns immediately to caller).

The Connection Model basically contains information for creating a new server socket. If the host returned is null, then acceptor will bind to all interfaces. Otherwise, the acceptor will bind only to the specified interface IP.

A side note regarding acceptor handlers. Usually acceptor handlers are multi-threaded safe and contains no per-connection instance variables. The handlers can usually create another object that keeps per-connection data.


Field Summary
protected  java.net.ServerSocket socket
           
 
Fields inherited from class com.echomine.net.Connection
listenerList
 
Constructor Summary
SocketAcceptor()
           
SocketAcceptor(ConnectionModel model)
           
SocketAcceptor(ConnectionModel model, int backlog)
           
 
Method Summary
 void aaccept(SocketHandler socketHandler)
          The asynchronous version of the accept method.
 void accept(SocketHandler socketHandler)
          Accepts a connection synchronously and hands it over to the handler for processing.
 void close()
          Closes the server socket and unbind from the port.
 ConnectionModel getConnectionModel()
           
 void open(int backlog)
           
 void setConnectionModel(ConnectionModel connectionModel)
          use a new connection model.
 void setConnectionModel(ConnectionModel connectionModel, int backlog)
          sets the connection model plus the backlog for the port that the listener should bind to and listen on.
 
Methods inherited from class com.echomine.net.TimeableConnection
fireConnectionClosed, fireConnectionEstablished
 
Methods inherited from class com.echomine.net.Connection
addConnectionListener, fireConnectionStarting, fireConnectionStartingWithoutVeto, removeConnectionListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

socket

protected java.net.ServerSocket socket
Constructor Detail

SocketAcceptor

public SocketAcceptor()

SocketAcceptor

public SocketAcceptor(ConnectionModel model)
               throws java.io.IOException
Throws:
java.io.IOException

SocketAcceptor

public SocketAcceptor(ConnectionModel model,
                      int backlog)
               throws java.io.IOException
Throws:
java.io.IOException
Method Detail

open

public void open(int backlog)
          throws java.io.IOException
Throws:
java.io.IOException

accept

public void accept(SocketHandler socketHandler)

Accepts a connection synchronously and hands it over to the handler for processing. The caller of this method will wait until data processing is finished before regaining control. This is meant for occassions such as HTTP requests where retrieval of data occurs once and data is returned for further processing.

Notice that the server socket is NOT closed after handler returns. The connection should be closed explicitly by the application or inside the handler.


aaccept

public void aaccept(SocketHandler socketHandler)

The asynchronous version of the accept method. The connection is passed to a thread for execution. The server socket is not automatically closed at the end. You must explicitly close the server socket when you're done. Failure to do so will result in a port that's binded until the end of the application.


close

public void close()

Closes the server socket and unbind from the port. The close does NOT close all the currently accepted (and processing) handlers. It only close the server socket so that additional connections are refused. It is up to you to keep track of the handlers and also close them if you want all connections to be shutdown.


getConnectionModel

public ConnectionModel getConnectionModel()

setConnectionModel

public void setConnectionModel(ConnectionModel connectionModel)
                        throws java.io.IOException

use a new connection model. The current server socket will be closed and then a new server socket will be created. Once a connection model is passed in through the constructor, it's not usually changed. However, the Acceptor can be reused in this way if the situation requires it.

Throws:
java.io.IOException

setConnectionModel

public void setConnectionModel(ConnectionModel connectionModel,
                               int backlog)
                        throws java.io.IOException
sets the connection model plus the backlog for the port that the listener should bind to and listen on.

Throws:
java.io.IOException


Copyright © 2001-2005 Echomine. All Rights Reserved.