nu.dll.io
Class QueuedSender

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--nu.dll.io.QueuedSender
All Implemented Interfaces:
java.lang.Runnable

public class QueuedSender
extends java.lang.Thread

Experimental class that handles asynchronous sending of Message objects to different ObjectOutputStreams. When using the enqueue() methods, this class has a 200 ms delay before actually writing the data to the streams. flush() can be called to force the data to be written immediately. When broadcasting identical messages to a list of clients, or when sending a bulk of several messages to one client, one of the enqueueAll() may be used to ensure efficient bulk sending.


Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
QueuedSender()
          Creates a queued sender with no default output stream.
QueuedSender(java.io.ObjectOutputStream stream)
          Creates a queued sender with a default output stram.
QueuedSender(java.io.ObjectOutputStream stream, java.util.List queue)
          Creates a queued sender with a default output stream and using the specified List as a queue.
 
Method Summary
 void enqueue(Message m)
          Enqueues the supplies messages to be written to the default output stream.
 void enqueue(java.io.ObjectOutputStream istream, Message m)
          Enqueues the supplied message to be written to the supplied output stream.
 void enqueueAll(java.lang.Object[] istream, Message message)
          Enqueues the supplied message to all the given output streams.
 void enqueueAll(java.io.ObjectOutputStream istream, java.lang.Object[] messages)
          Enqueues all supplied messages to the given outputstream.
 void flush()
          Causes the sender to write all pending messages to their destinations, unless it isn't already doing so.
 void halt()
          Causes the thread to terminate as soon as it has written the remaining objects on its queue to their streams.
 void run()
          Runs until halt() is called or the thread is interrupted.
 
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, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

QueuedSender

public QueuedSender()
Creates a queued sender with no default output stream.

QueuedSender

public QueuedSender(java.io.ObjectOutputStream stream)
Creates a queued sender with a default output stram.

QueuedSender

public QueuedSender(java.io.ObjectOutputStream stream,
                    java.util.List queue)
Creates a queued sender with a default output stream and using the specified List as a queue.
Method Detail

run

public void run()
Runs until halt() is called or the thread is interrupted. The main loop waits 200 ms or until flush() is called, and then writes any queued objects to their respective streams.
Overrides:
run in class java.lang.Thread

halt

public void halt()
Causes the thread to terminate as soon as it has written the remaining objects on its queue to their streams.

enqueueAll

public void enqueueAll(java.io.ObjectOutputStream istream,
                       java.lang.Object[] messages)
Enqueues all supplied messages to the given outputstream. Useful for sending a batch of data to one client.

enqueueAll

public void enqueueAll(java.lang.Object[] istream,
                       Message message)
Enqueues the supplied message to all the given output streams. Useful for broadcasting the same message to a number of clients.

enqueue

public void enqueue(java.io.ObjectOutputStream istream,
                    Message m)
Enqueues the supplied message to be written to the supplied output stream.

enqueue

public void enqueue(Message m)
Enqueues the supplies messages to be written to the default output stream. If this sender lacks a default output stream, an IllegalStateException is thrown.
Throws:
java.lang.IllegalStateException - thrown if this sender lacks a default output stream

flush

public void flush()
Causes the sender to write all pending messages to their destinations, unless it isn't already doing so.