net.vattp.data
Class ListenThread

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--net.vattp.data.ListenThread
All Implemented Interfaces:
Runnable

class ListenThread
extends Thread


Field Summary
private  VatTPMgr myConnMgr
          The VatTPMgr to report new Sockets to
private  ServerSocket myListenServerSocket
           
private  InetAddress myOptIP
           
private  boolean mySuspended
           
private  boolean myTerminateFlag
           
private  UserThread myUserThread
           
private  Object myUserThreadLock
          An object to synchronize communication with the user thread
private  Vat myVat
          A vat for synchronization when calling the VatTPMgr
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
(package private) ListenThread(String optLocalAddr, VatTPMgr connMgr, Vat vat)
          Construct a new object to run in its own thread and listen on a ServerSocket for new incoming connections.
 
Method Summary
private  void callMgr(DataCommThunk thunk)
          Call a method in the VatTPMgr
(package private)  NetAddr listenAddress()
          The address on which this thread is listening, or null if there was a problem
private  void noticeProblem(Throwable t)
           
 void run()
          The actual body of the listener thread.
private  void setupNewConnection(Socket clientSocket)
          Create a new connection to handle the new socket.
(package private)  void shutdown()
          Shutdown the thread.
(package private)  void startup()
          Start the listener thread.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

myTerminateFlag

private boolean myTerminateFlag

myUserThread

private final UserThread myUserThread

myOptIP

private InetAddress myOptIP

myListenServerSocket

private final ServerSocket myListenServerSocket

myConnMgr

private VatTPMgr myConnMgr
The VatTPMgr to report new Sockets to


myVat

private final Vat myVat
A vat for synchronization when calling the VatTPMgr


myUserThreadLock

private final Object myUserThreadLock
An object to synchronize communication with the user thread


mySuspended

private final boolean mySuspended
Constructor Detail

ListenThread

ListenThread(String optLocalAddr,
             VatTPMgr connMgr,
             Vat vat)
       throws UnknownHostException,
              IOException
Construct a new object to run in its own thread and listen on a ServerSocket for new incoming connections.

Parameters:
optLocalAddr - The local address we will listen on.
connMgr - the VatTPMgr to be notified about interesting events (new connections and errors) that happen while listening.
Method Detail

callMgr

private void callMgr(DataCommThunk thunk)
Call a method in the VatTPMgr

Parameters:
thunk - a Thunk that will perform the call. The thunk will be called after the Vat lock is obtained.

noticeProblem

private void noticeProblem(Throwable t)

listenAddress

NetAddr listenAddress()
The address on which this thread is listening, or null if there was a problem


run

public void run()
The actual body of the listener thread.

Opens a ServerSocket and accepts connections on it. Each connection spawns a new ByteConnection object that is handed off to the innerListener to be dealt with.

Loops until told to stop by somebody calling 'shutdown()' (which sets an internal flag).

Specified by:
run in interface Runnable
Overrides:
run in class Thread
See Also:
java.lang.Thread#start(), java.lang.Thread#stop(), java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String), java.lang.Runnable#run()

setupNewConnection

private void setupNewConnection(Socket clientSocket)
Create a new connection to handle the new socket. Tell the VatTPMgr about the new Socket.


shutdown

void shutdown()
Shutdown the thread.

The listener thread itself will die the next time it returns from 'accept' and notices that the terminate flag is set. What is more likely, however, is that the process will quiesce and we will go gently into the night, taking the daemon listener thread away with us when we go. We kill the listener user thread here so that that can happen.


startup

void startup()
Start the listener thread.

HACK: The listener thread needs to be a daemon thread, because if it were a user thread it would be uninteruptible while it was off waiting on an accept() (this is due to a flaw in Solaris, actually). However, if it's a daemon thread the app can exit even if the thread is still running. However, if we're just sitting there waiting for connections to arrive over the network, we don't want to exit, we want to keep running. Thus we have the net.vattp.data.UserThread, which does NOTHING but wait. Since it's a user thread it keeps the app from exiting (and thus allows the listener thread to keep running waiting for a connection) and since it's not waiting on an accept we can kill it. When we tell the listener thread to shutdown (which we now can do since it's a daemon thread), it sends a notify() to the listener user thread whereupon *it* shuts down too. Hallelujah, amen.



comments?