org.exolab.core.mipc
Class MultiplexConnection

java.lang.Object
  extended byjava.lang.Thread
      extended byorg.exolab.core.mipc.MultiplexConnection
All Implemented Interfaces:
MultiplexConnectionIfc, java.lang.Runnable
Direct Known Subclasses:
MultiplexSSLConnection

public class MultiplexConnection
extends java.lang.Thread
implements MultiplexConnectionIfc

The MultiplexConnection encapsulates a single network connection. It allows for multiple channels or multiplexed message streams. The object is thread safe and uses threads within it to do its job. The design for the Multiplexed streaming borrows concepts from the book "Java Network Programming : A Complete Guide to Networking, Streams, and Distributed Computing", by Merlin Hughes, Michael Shoffner, Derek Hamner The MultiplexConnection is designed as follows: - users of the connection register themselves as channels. each channel is given a stream to place outbound messages on, and gives the MC a stream to write inbound messages on. Typically this "inbound" stream is a Queue Stream and the user sucks messages off of the queue on a separate thread. That way the multiplex delivery mechanism does not have to wait for a channel handler to process a message. - As channel producers send messages, they are multiplexed and placed on a queue for outbound delivery. A separate MessageCopier thread takes messages from this queue and sends them onto to the "raw" outbound stream. - Inbound messages are handled by Demultiplixer. The Demux manages a list of channels. It unwraps a packet and uses the name encoded in the packet to pass the message onto an output stream. This outputstream is the one passed into "register" by the channel consumer/producer. The Demux runs within the same thread as MultiplexConnection. It is important to note: All activity on the "raw" inbound and outbound streams are synchronized, as well as the objects used to build the MultiplexConnection. So everything is thread-safe.

Version:
$Revision: 1.6 $
Author:
Art Whitten
See Also:
MultiplexConnectionServer, MessageCopier, Demultiplexer

Field Summary
protected  boolean _finished
          Used for stopping the thread
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
MultiplexConnection(java.net.Socket socket)
          Creates a MultiplexConnection on top of an existing Socket
MultiplexConnection(java.lang.String hostname, int port)
          Creates new MultiplexConnection on the specified host and port.
MultiplexConnection(java.lang.ThreadGroup group, java.net.Socket socket)
          Creates a MultiplexConnection on top of an existing Socket
 
Method Summary
 void deregister(java.lang.String label)
          Deregister a multiplex channel.
 void disconnected()
          This is called by the demultiplexer when the channel has been abnormally terminated.
 void finish()
          Gracefully stop the MultiplexConnection thread.
 java.lang.String getHost()
          Return the hostname that this connection bound to
 int getPort()
          Return the port that this connection is bound to
protected  void init(java.net.Socket socket)
           
 org.exolab.core.mipc.MessageOutput register(java.lang.String label, org.exolab.core.mipc.MessageOutput out)
          Register a new multiplex channel.
 void run()
          The workhorse for the MultiplexConnection.
 void setDisconnectionEventListener(DisconnectionEventListener listener)
          Set the listener that will be called back when the multiplex connection is dropped
 
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

_finished

protected boolean _finished
Used for stopping the thread

Constructor Detail

MultiplexConnection

public MultiplexConnection(java.lang.String hostname,
                           int port)
                    throws java.net.UnknownHostException,
                           java.io.IOException
Creates new MultiplexConnection on the specified host and port.

Parameters:
hostname - the hostname to create a socket connection to
port - the port to connect to

MultiplexConnection

public MultiplexConnection(java.net.Socket socket)
                    throws java.io.IOException
Creates a MultiplexConnection on top of an existing Socket

Parameters:
socket - The existing socket to wrap.

MultiplexConnection

public MultiplexConnection(java.lang.ThreadGroup group,
                           java.net.Socket socket)
                    throws java.io.IOException
Creates a MultiplexConnection on top of an existing Socket

Parameters:
group - the thread group that the connection belongs to
socket - the existing socket to wrap
Method Detail

finish

public void finish()
Gracefully stop the MultiplexConnection thread.

Specified by:
finish in interface MultiplexConnectionIfc

setDisconnectionEventListener

public void setDisconnectionEventListener(DisconnectionEventListener listener)
Set the listener that will be called back when the multiplex connection is dropped

Specified by:
setDisconnectionEventListener in interface MultiplexConnectionIfc
Parameters:
listener - - the disconnection event listener

run

public void run()
The workhorse for the MultiplexConnection. Typically this method is invoked as a result of calling 'start' on the MultiplexConnection. MultiplexConnection derives from Thread.

Specified by:
run in interface MultiplexConnectionIfc

register

public org.exolab.core.mipc.MessageOutput register(java.lang.String label,
                                                   org.exolab.core.mipc.MessageOutput out)
Register a new multiplex channel. 'label' is the identifier for the channel and 'messageOutput' is where inbound messages are placed. If there is an existing channel, it is overwritten. The return value is a MessageOutput that is used by the channel producer to send messages. The interface encapsulates a MutliplexOutputStream.

Specified by:
register in interface MultiplexConnectionIfc
Parameters:
label - The channel name to register for
out - Inbound messages are placed here
Returns:
MessageOuput Channel producers place outbound message on this stream.

deregister

public void deregister(java.lang.String label)
Deregister a multiplex channel.

Specified by:
deregister in interface MultiplexConnectionIfc
Parameters:
label - the name of the channel to unregister

disconnected

public void disconnected()
This is called by the demultiplexer when the channel has been abnormally terminated. It will call finish on the connection and then notify any registered listener

Specified by:
disconnected in interface MultiplexConnectionIfc

getHost

public java.lang.String getHost()
Return the hostname that this connection bound to

Specified by:
getHost in interface MultiplexConnectionIfc
Returns:
the host name

getPort

public int getPort()
Return the port that this connection is bound to

Specified by:
getPort in interface MultiplexConnectionIfc
Returns:
the port number

init

protected void init(java.net.Socket socket)
             throws java.io.IOException
Throws:
java.io.IOException


Copyright © 1999-2005 The Exolab Group. All Rights Reserved.