au.com.terabit.jproactor
Class Acceptor

java.lang.Object
  extended by au.com.terabit.jproactor.Acceptor
All Implemented Interfaces:
AsynchAcceptHandler, AsynchChannelHandler

public class Acceptor
extends java.lang.Object
implements AsynchAcceptHandler, AsynchChannelHandler

The Acceptor class allows to start user defined server-side protocol servers.
Acceptor opens java.net.ServerSocketChannel and requests Demultiplexor to execute accept operation, once accept is completed registered AsynchChannelHandlerFactory instantiates a new Protocol object and starts execution using the same multiplexor instance.

The following code fragment shows how to start test EchoServer using acceptor:

 // Create and run Demultiplexor instance for EchoServer on port 9999  
 Demultiplexor m = new Demultiplexor();
 m.start();

 // Create acceptor instance for EchoServer on port 9999  
 Acceptor a = new Acceptor( m, new EchoServerProtocolFactory(), 9999 );
 // start listening for incoming connections
 a.start();
 

Version:
$Revision$ $Date$
Author:
Alexander Libman, Yevgeny Libman

Field Summary
static int NUM_INITIAL_ACCEPTS
          How may accepts to perform, default value
 
Constructor Summary
Acceptor(Demultiplexor m, int port, AsynchChannelHandlerFactory factory)
          Creates acceptor for the specified AsynchHandlerFactory and on the specified tcp/ip port.
Acceptor(Demultiplexor m, int port, java.lang.String host, AsynchChannelHandlerFactory factory)
          Creates acceptor for the specified AsynchHandlerFactory and on the specified tcp/ip port and host.
Acceptor(Demultiplexor m, java.net.SocketAddress address, AsynchChannelHandlerFactory factory)
          Creates acceptor for the specified AsynchHandlerFactory and on the specified Socket Address.
 
Method Summary
 void acceptCompleted(OpAccept op)
          Implementation of AsynchChannelHandler#acceptCompleted(OpAccept op).
 void channelAttached(AsynchChannel channel)
          Notifies that this handler is associated with the TCPAsynchChannel instance.
 void channelClosed(AsynchChannel channel)
          Notifies that cnannel is closed.
 void start()
          Starts acceptor, using NUM_INITIAL_ACCEPTS default number of accept operations.
 void start(int numInitialAccepts)
          Starts accept operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NUM_INITIAL_ACCEPTS

public static final int NUM_INITIAL_ACCEPTS
How may accepts to perform, default value

See Also:
Constant Field Values
Constructor Detail

Acceptor

public Acceptor(Demultiplexor m,
                int port,
                AsynchChannelHandlerFactory factory)
         throws java.net.UnknownHostException
Creates acceptor for the specified AsynchHandlerFactory and on the specified tcp/ip port.

Parameters:
m - - multiplexor instance
factory - - protocol factory
port - - tcp/ip port to listen on
Throws:
java.net.UnknownHostException

Acceptor

public Acceptor(Demultiplexor m,
                int port,
                java.lang.String host,
                AsynchChannelHandlerFactory factory)
Creates acceptor for the specified AsynchHandlerFactory and on the specified tcp/ip port and host.

Parameters:
m - - multiplexor instance
factory - - protocol factory
port - - tcp/ip port to listen on
host - - host name

Acceptor

public Acceptor(Demultiplexor m,
                java.net.SocketAddress address,
                AsynchChannelHandlerFactory factory)
Creates acceptor for the specified AsynchHandlerFactory and on the specified Socket Address.

Parameters:
m - - multiplexor instance
factory - - protocol factory
address - - socket address to listen on
Method Detail

acceptCompleted

public void acceptCompleted(OpAccept op)
                     throws java.lang.Exception
Implementation of AsynchChannelHandler#acceptCompleted(OpAccept op). Executes accept operation, non-blocking, instantiates protocol on the accepted channel, starts protocol using acceptor's multiplexor instance.

Specified by:
acceptCompleted in interface AsynchAcceptHandler
Throws:
java.lang.Exception - if this method is called

channelAttached

public void channelAttached(AsynchChannel channel)
                     throws java.lang.Exception
Description copied from interface: AsynchChannelHandler
Notifies that this handler is associated with the TCPAsynchChannel instance.

One AsynchHandler can be shared with multiple socket channels, e.g. chat server. This mehtod is called by TCPAsynchChannel.

Specified by:
channelAttached in interface AsynchChannelHandler
Parameters:
channel - a new channel that is associated with this protocol.
Throws:
java.lang.Exception

channelClosed

public void channelClosed(AsynchChannel channel)
                   throws java.lang.Exception
Description copied from interface: AsynchChannelHandler
Notifies that cnannel is closed.

Specified by:
channelClosed in interface AsynchChannelHandler
Parameters:
channel - asynchronous channel that was closed
Throws:
java.lang.Exception

start

public void start()
           throws java.lang.Exception
Starts acceptor, using NUM_INITIAL_ACCEPTS default number of accept operations.
This method can be called more than once.

Throws:
java.lang.Exception
See Also:
au.com.terabit.multiplexor.AsynchChannelHandler#start(), start(int numInitialAccepts)

start

public void start(int numInitialAccepts)
           throws java.lang.Exception
Starts accept operation.
This method can be called more than once.

Throws:
java.io.IOException - if error occurs, the operation won't be started in this case.
java.lang.Exception