TopoWeb Commons API

org.topoweb.net.server
Class SessionAcceptor

java.lang.Object
  |
  +--org.topoweb.net.server.SessionAcceptor
All Implemented Interfaces:
java.lang.Runnable, org.topoweb.util.concurrent.SingleRun

public class SessionAcceptor
extends java.lang.Object
implements org.topoweb.util.concurrent.SingleRun

A org.topoweb.util.concurrent.SingleRun that accepts sockets and pushes them onto an event queue. This class should be run in a single thread and then only once.

Author:
Babak Farhang

Field Summary
protected static java.util.logging.Logger LOGGER
           
 
Constructor Summary
SessionAcceptor(java.net.InetSocketAddress address, SessionPool sessionPool, org.topoweb.util.concurrent.Puttable sessionQueue)
          Creates a new instance with the given parameters.
 
Method Summary
protected  void clean()
          Frees all the network resources of this instance.
protected  boolean enqueue(Session session)
          Enqueue's the given session in the queue.
 void init()
          Initializes the instance.
 boolean isAlive()
           
 void join(int timeout)
           
 void run()
          Runs the acceptor loop.
 void stop()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGGER

protected static final java.util.logging.Logger LOGGER
Constructor Detail

SessionAcceptor

public SessionAcceptor(java.net.InetSocketAddress address,
                       SessionPool sessionPool,
                       org.topoweb.util.concurrent.Puttable sessionQueue)
                throws java.lang.IllegalArgumentException
Creates a new instance with the given parameters.

Parameters:
address - the address specifying the host and port of the server this instance represents
sessionPool - a pool/factory for creating Session instances from accepted sockets
sessionQueue - a queue used to schedule the processing of Session instances
Method Detail

init

public void init()
          throws java.lang.IllegalStateException,
                 java.io.IOException
Initializes the instance. The server socket channel is opened and bound to the address passed in at instantiation. An internal selector is created and the server socket channel is registered with the selector for ACCEPT_OPs. Overriding subclasses must invoke super.init().

Throws:
java.lang.IllegalStateException - if this method has already been invoked
java.io.IOException - if an I/O error occurs

run

public void run()
         throws java.lang.IllegalStateException
Runs the acceptor loop. The loop waits on the internal selector for notification of "acceptable" sockets. It then accepts these new sockets and constructs Session instances from them using the SessionPool instance passed in at instantation. These Session instances are then pushed on to the event queue (the Puttable sessionQueue parameter passed in at instantation) for processing by another stage.

The acceptor loop can be signaled to stop through the stop method.

Specified by:
run in interface org.topoweb.util.concurrent.SingleRun
Throws:
java.lang.IllegalStateException - if the instance was not first initialized; if invoked more than once
See Also:
init(), stop()

stop

public void stop()
          throws java.lang.IllegalStateException
Specified by:
stop in interface org.topoweb.util.concurrent.SingleRun
java.lang.IllegalStateException

isAlive

public boolean isAlive()
Specified by:
isAlive in interface org.topoweb.util.concurrent.SingleRun

join

public void join(int timeout)
          throws java.lang.InterruptedException
Specified by:
join in interface org.topoweb.util.concurrent.SingleRun
java.lang.InterruptedException

enqueue

protected boolean enqueue(Session session)
Enqueue's the given session in the queue. The basic implementation waits roughly 1 second for the queue operation to succeed.

Parameters:
session - the session to be enqueued
Returns:
true, if successfully enqueued; false, o.w.

clean

protected void clean()
Frees all the network resources of this instance.


TopoWeb Commons API

Copyright (C) 2002 Babak Farhang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.