jvm.entity
Class TCPIPReadThread

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--jclass.SleepingThread
              |
              +--jvm.entity.TCPIPReadThread

public class TCPIPReadThread
extends SleepingThread

(please read also bellow !) A thread that reads messages from the network and tries to put them into a queue. It blocks if the queue becomes full and waits for some queue entries to free up.

(The above was the original stuff)
Now, there appears the case in which we might just NOT want to fill up the queue but to do something fast and useful with any new incomming message. This is why we have to provide a mean by which every time the thread reads a message it also calls another external object's (some sort of observer) method with the just received message as parameter. This other object is a MessageHandler.

That means that a correct description of this would be something like: A thread that reads messages from the network and for each new message it calls the MessageHandler.newmsg() method.

Written: Radu Sion
Version: 1.19
Source: TCPIPReadThread.java

Visit Smart Software 

See Also:
Message, MessageHandler, SimpleQueue, FullException, EmptyException, TCPIPWriteThread, SleepingThread

Fields inherited from class jclass.SleepingThread
DEFAULT_SLEEP
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
TCPIPReadThread(EntityID id, SmartObjectInputStream is, MessageHandler msgh, int max_messages, long sleep)
          Constructs a new TCPIPReadThread.
 
Method Summary
protected  void finalize()
           
 boolean getActive()
          Gets the thread's current status.
 void run()
           
 void setActive(boolean how)
          Sets the thread's status.
 void setEntityID(EntityID id)
          Sets the internal used entity id.
 boolean setMessageHandler(MessageHandler msgh)
          This sets the incomming message handler.
 
Methods inherited from class jclass.SleepingThread
getSleep, mysleep, pause, pause, setSleep
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, 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, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TCPIPReadThread

public TCPIPReadThread(EntityID id,
                       SmartObjectInputStream is,
                       MessageHandler msgh,
                       int max_messages,
                       long sleep)
Constructs a new TCPIPReadThread. Normally exits after reading max_messages messages
Parameters:
id - EntityID of the current entity. This is needed in the call to MessageHandler.newmsg()
is - ObjectInputstream from which to read
msgh - MessageHandler to use
max_messages - Number of messages to read. (if -1 then read in loop)
sleep - Internal sleep delay
Method Detail

setActive

public void setActive(boolean how)
Sets the thread's status. If active it runs ok. If not active run exits gracefully, after finishing the last action it was involved in. It is NOT equal to stop() which breaks into the thread's actions and exits run rightaway.

getActive

public boolean getActive()
Gets the thread's current status.
See Also:
setActive(boolean)

setMessageHandler

public boolean setMessageHandler(MessageHandler msgh)
This sets the incomming message handler. (which is the handler passed as argument in the constructor) This should be done at the beginning BEFORE starting the thread.
Note: Be aware of the fact that if you change the handler during operation, that is AFTER calling start() you might loose messages that where handled by the old handler ... This method tries to avoid this by calling the old MessageHandler's down() method which should flush all messages etc.
Parameters:
mh - New MessageHandler to set
Returns:
True most of the time, false if unable to start (and set) new handler

setEntityID

public void setEntityID(EntityID id)
Sets the internal used entity id. The EntityID is used when calling the messagehandler's newmsg() method. No parameter checkings are performed.

run

public void run()
Overrides:
run in class java.lang.Thread

finalize

protected void finalize()
Overrides:
finalize in class java.lang.Object