TopoWeb Commons API

org.topoweb.net.server
Class SessionReader

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

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

An org.topoweb.util.concurrent.SingleRun that reads from sockets. Receives new Sessions from a queue and registers them with an internally managed selector that detects the readiness of sockets for read operations. Reads from sockets until the completion of an interpretable request is detected, and then enqueue's the socket's associated Session for processing by some other agent.

Note:

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
          The logger used to log messages.
 
Constructor Summary
SessionReader(org.topoweb.util.concurrent.Takable readPendingQueue, org.topoweb.util.concurrent.Puttable readCompletedQueue, RequestBoundaryDetector requestDetector, SessionPool sessionPool)
          Creates a new instance with the given parameters.
 
Method Summary
protected  boolean enqueue(Session session)
          Enqueue's the given session in the read-completed queue.
 void init()
          Initializes the instance.
 boolean isAlive()
           
 void join(int timeout)
           
 void run()
          Runs the session reader 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
The logger used to log messages. This logger uses this class's package namespace.

Constructor Detail

SessionReader

public SessionReader(org.topoweb.util.concurrent.Takable readPendingQueue,
                     org.topoweb.util.concurrent.Puttable readCompletedQueue,
                     RequestBoundaryDetector requestDetector,
                     SessionPool sessionPool)
              throws java.lang.IllegalArgumentException
Creates a new instance with the given parameters.

Parameters:
readPendingQueue - the queue from which Sessions arrive
readCompletedQueue - the queue on which Sessions are sent once a request has been completely read
requestDetector - the request boundary detector used to tell whether enough bytes have been read to form an interpretable request
sessionPool - the pool to which cancelled Session instances are returned
Throws:
java.lang.IllegalArgumentException - if any of the arguments is null
Method Detail

init

public void init()
          throws java.lang.IllegalStateException,
                 java.io.IOException
Initializes the instance. A new "read-op" selector is opened.

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 session reader loop.

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 read-completed 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.

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.