com.echomine.net
Class PerpetualSocketAcceptor

java.lang.Object
  extended by com.echomine.net.Connection
      extended by com.echomine.net.TimeableConnection
          extended by com.echomine.net.SocketAcceptor
              extended by com.echomine.net.PerpetualSocketAcceptor

public class PerpetualSocketAcceptor
extends SocketAcceptor

Accepts connections indefinitely. Once a connection is accepted, it is immediately handled in a spawned thread. The acceptor will go back to receiving more connections.

The acceptor offers both a synchronous and asynchronous version of accept. Running perpetually, this means that (1) synchronous accept will accept and handle one connection at a time and (2) asynchronous accept will accept, hand it to a handler in a different thread, then go back to accept more connections (ie. a daemon). Depending on which suits your situation, use the appropriate method.

Perpetual Acceptor also support firiing off of connection events.

NOTE: Using this class requires that the handler is multi-threading safe if asynchronous accept is used (doesn't apply to synchronous accept). This means for async accepts, the handler cannot contain any per-connection data. Otherwise, you'll get into data corruption because of multi-threading issues.


Field Summary
 
Fields inherited from class com.echomine.net.SocketAcceptor
socket
 
Fields inherited from class com.echomine.net.Connection
listenerList
 
Constructor Summary
PerpetualSocketAcceptor()
           
PerpetualSocketAcceptor(ConnectionModel model)
           
PerpetualSocketAcceptor(ConnectionModel model, int backlog)
           
 
Method Summary
 void aaccept(SocketHandler socketHandler)
          Accepts a connection,hands it over to the handler for processing, and immediately goes back to accepting more connections.
 void accept(SocketHandler socketHandler)
          Accepts a connection and hands it over to the handler for processing one at a time.
 void close()
          Closes the server socket, unbind from the port, and shuts down the perpetual loops.
 
Methods inherited from class com.echomine.net.SocketAcceptor
getConnectionModel, open, setConnectionModel, setConnectionModel
 
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
 

Constructor Detail

PerpetualSocketAcceptor

public PerpetualSocketAcceptor()

PerpetualSocketAcceptor

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

PerpetualSocketAcceptor

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

accept

public void accept(SocketHandler socketHandler)

Accepts a connection and hands it over to the handler for processing one at a time.

To stop the daemon from accepting more connections, just call the close method.

The method will return immediately and run the daemon in a background thread.

Overrides:
accept in class SocketAcceptor

aaccept

public void aaccept(SocketHandler socketHandler)

Accepts a connection,hands it over to the handler for processing, and immediately goes back to accepting more connections. This differs from the regular accept method in that it does not process connections one at a time.

The async accept is a little more intricate than the sync accept since it can't just use SocketAcceptor.aaccept().

The method returns immediately and runs the daemon in a background thread.

Overrides:
aaccept in class SocketAcceptor

close

public void close()

Closes the server socket, unbind from the port, and shuts down the perpetual loops. 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.

Overrides:
close in class SocketAcceptor


Copyright © 2001-2005 Echomine. All Rights Reserved.