networking.base
Class SocketThread

java.lang.Object
  extended by java.lang.Thread
      extended by networking.base.ForkedThread
          extended by networking.base.SocketThread
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
ClientThread, Server

public abstract class SocketThread
extends ForkedThread

SocketThread - this class represents a thread which owns a socket and maintains an additional message queue


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
private  boolean m_exit
          Exit flag
private  java.io.InputStream m_in
          Input socket stream
private  java.io.PipedInputStream m_innerIn
          Input stream for internal queue processing
private  java.io.PipedOutputStream m_innerOut
          Output stream for internal queue processing
private  java.io.OutputStream m_out
          Output socket stream
private  java.net.Socket m_socket
          Socket
private  long m_timeOut
          Maximum idle time (milliseconds) after which the thread exits
static long s_timeSleep
          Sleep value used in wait message loop (milliseconds)
 
Fields inherited from class networking.base.ForkedThread
CHILD, m_fork, NONE, PARENT
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
SocketThread()
          Construct new SocketThread object
SocketThread(java.net.Socket socket)
          Construct new SocketThread object
SocketThread(SocketThread t)
          Copy constructor
 
Method Summary
protected  void deinit()
          Deallocate resources
 void enque(Message msg)
           
protected abstract  void error(java.lang.Exception e)
          Report error
protected  void exit()
          Exit message loop
protected  java.io.InputStream getIn()
          Get socket input stream
protected  java.io.OutputStream getOut()
          Get socket output stream
protected  void init()
          Allocate any required resources
private  void init1()
          Init inner queue
protected  void processMessage(Message msg)
          Process a message
protected  Message recv()
           
protected  void run1()
          Convenience routine
protected  void send(Message msg)
           
protected  void setSocket(java.net.Socket socket)
          Attach socket after construction
 void setTimeOut(long millis)
          Set time-out value
protected  void wait(java.io.InputStream in)
          Wait for InputStream become ready for reading, or exit was called or time-out has expired
private  java.io.InputStream waitOneOf()
          Wait for one of the InputStreams become ready for reading, or exit was called or time-out has expired
 
Methods inherited from class networking.base.ForkedThread
run
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, 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

s_timeSleep

public static final long s_timeSleep
Sleep value used in wait message loop (milliseconds)

See Also:
Constant Field Values

m_socket

private java.net.Socket m_socket
Socket


m_out

private java.io.OutputStream m_out
Output socket stream


m_in

private java.io.InputStream m_in
Input socket stream


m_innerOut

private java.io.PipedOutputStream m_innerOut
Output stream for internal queue processing


m_innerIn

private java.io.PipedInputStream m_innerIn
Input stream for internal queue processing


m_exit

private boolean m_exit
Exit flag


m_timeOut

private long m_timeOut
Maximum idle time (milliseconds) after which the thread exits

Constructor Detail

SocketThread

public SocketThread()
Construct new SocketThread object


SocketThread

public SocketThread(java.net.Socket socket)
Construct new SocketThread object

Parameters:
socket - socket

SocketThread

public SocketThread(SocketThread t)
Copy constructor

Parameters:
st - SocketThread object
Method Detail

setSocket

protected void setSocket(java.net.Socket socket)
Attach socket after construction

Parameters:
socket - socket

processMessage

protected void processMessage(Message msg)
                       throws java.lang.Exception
Process a message

Parameters:
msg - message
Throws:
java.lang.Exception

exit

protected final void exit()
Exit message loop


setTimeOut

public final void setTimeOut(long millis)
Set time-out value

Parameters:
millis - time-out value (milliseconds)

wait

protected final void wait(java.io.InputStream in)
                   throws java.lang.Exception
Wait for InputStream become ready for reading, or exit was called or time-out has expired

Parameters:
in - InputStream
Throws:
java.lang.Exception

waitOneOf

private java.io.InputStream waitOneOf()
                               throws java.lang.Exception
Wait for one of the InputStreams become ready for reading, or exit was called or time-out has expired

Throws:
java.lang.Exception

run1

protected void run1()
             throws java.lang.Exception
Convenience routine

Overrides:
run1 in class ForkedThread
Throws:
java.lang.Exception

init1

private void init1()
            throws java.lang.Exception
Init inner queue

Throws:
java.lang.Exception

init

protected void init()
             throws java.lang.Exception
Description copied from class: ForkedThread
Allocate any required resources

Overrides:
init in class ForkedThread
Throws:
java.lang.Exception

deinit

protected void deinit()
               throws java.lang.Exception
Description copied from class: ForkedThread
Deallocate resources

Overrides:
deinit in class ForkedThread
Throws:
java.lang.Exception

enque

public final void enque(Message msg)
                 throws java.lang.Exception
Throws:
java.lang.Exception

send

protected final void send(Message msg)
                   throws java.lang.Exception
Throws:
java.lang.Exception

recv

protected final Message recv()
                      throws java.lang.Exception
Throws:
java.lang.Exception

getOut

protected final java.io.OutputStream getOut()
Get socket output stream

Returns:
socket output stream

getIn

protected final java.io.InputStream getIn()
Get socket input stream

Returns:
socket input stream

error

protected abstract void error(java.lang.Exception e)
Report error

Specified by:
error in class ForkedThread
Parameters:
e - exception


Copyright © 2009 Alexey Veremenko. All rights reserved.