org.caleigo.toolkit.tunnel
Class AbstractTunnel

java.lang.Object
  extended byorg.caleigo.toolkit.tunnel.AbstractTunnel
All Implemented Interfaces:
ITunnel
Direct Known Subclasses:
HTTPTunnel, SocketTunnel

public abstract class AbstractTunnel
extends java.lang.Object
implements ITunnel

Abstract class that implements most of the functinallity needed by an ITunnel. Only to methods need to be implemented by subclsses: getOutStream and getInStream. Optionally the method createPackage can be overridden to provide custom implementations of the IPackage interface.

Version:
1.00
Author:
Mattias Hagstrand

Nested Class Summary
static class AbstractTunnel.DefaultConnectionMessage
           
static class AbstractTunnel.DefaultPackage
           
static class AbstractTunnel.DefaultStatusMessage
           
static class AbstractTunnel.ErrorMessage
           
static interface AbstractTunnel.IConnectionMessage
           
static interface AbstractTunnel.IFinalizeMessage
           
protected  class AbstractTunnel.InputThread
           
static interface AbstractTunnel.IPackage
          An IPackage represents a package that is sent by an ITunnel.
static interface AbstractTunnel.IStatusMessage
           
protected  class AbstractTunnel.OutputThread
           
protected  class AbstractTunnel.WorkerThread
           
 
Field Summary
static int DEBUG
           
static int DEFAULT_NBR_OF_WORKER_THREADS
           
static int ERROR
           
static int INFO
           
protected  ITunnelCodec mCodec
           
protected  int mCurrentPackageID
           
protected  AbstractTunnel.InputThread mInputThread
           
protected  java.util.List mInQueue
           
protected  int mLogLevel
           
protected  java.util.List mMessageConsumers
           
protected  AbstractTunnel.OutputThread mOutputThread
           
protected  java.util.List mOutQueue
           
protected  java.util.List mPackers
           
protected  java.util.Map mReplyMessages
           
protected  java.util.List mStatusCallbacks
           
protected  java.util.List mTunnelListeners
           
protected  AbstractTunnel.WorkerThread[] mWorkerThreads
           
protected static int sNbrOfWorkerThreads
           
static int WARNING
           
 
Constructor Summary
AbstractTunnel()
          Default constructor.
AbstractTunnel(ITunnelCodec codec)
           
 
Method Summary
 void addMessageConsumer(IMessageConsumer consumer)
          Adds the consumer to the end of this ITunnels message consumer list.
 void addPacker(ITunnelPacker packer)
          Adds the packer to the end of this ITunnels packer list.
 void addStatusCallback(IStatusCallback callback)
          Adds the callback to the list of IStatusCallbacks that receives status messages from this ITunnel.
protected  void addToInQueue(AbstractTunnel.IPackage inPackage)
           
protected  void addToOutQueue(AbstractTunnel.IPackage outPackage)
           
 void addTunnelListener(ITunnelListener listener)
           
 java.lang.Object ask(java.lang.Object message)
          Sends a request/reply message over this ITunnel.
protected  AbstractTunnel.IConnectionMessage createConnectionMessage()
           
 AbstractTunnel.IPackage createPackage(java.lang.Object message, int sessionID)
          Creates an IPackage that contains the provided message.
protected  void doOnConnectionLost()
           
protected  void doOnTunnelClose()
           
 void finalize()
          Performs cleanup for this ITunnel.
protected  void finalizeTunnel()
           
protected  void fireConnectionLostEvent()
           
protected  void fireTunnelClosedEvent()
           
 java.lang.String getDescription()
          Returns a humanly readably text that describes the ITunnel.
protected abstract  java.io.InputStream getInputStream()
          Returns an InputStream that should be used to receive messages.
 int getLogLevel()
          Gets the log level for this ITunnel.
protected  AbstractTunnel.IPackage getNextFromInQueue()
           
protected  AbstractTunnel.IPackage getNextFromOutQueue()
           
protected  int getNextPackageID()
           
protected abstract  java.io.OutputStream getOutputStream()
          Returns an OutputStream that should be used to send messages.
 void initialize()
          Initializes the ITunnel.
 boolean isActive()
          Returns true if this tunnel is active, that is, if it is correctly initialized.
protected  void log(java.lang.Object source, int logLevel, java.lang.String message)
           
protected  void log(java.lang.Object source, int logLevel, java.lang.String message, java.lang.Throwable e)
           
 int ping()
          Sends a ping message over this ITunnel.
 void remomveMessageConsumer(IMessageConsumer consumer)
          Removes the consumer from this ITunnels message consumer list.
 void remomvePacker(ITunnelPacker packer)
          Removes the packer from this ITunnels packer list.
 void removeStatusCallback(IStatusCallback callback)
          Removes the callback from the list of IStatusCallbacks.
 void removeTunnelListener(ITunnelListener listener)
           
 void send(java.lang.Object message)
          Sends a message over this ITunnel.
 void setCodec(ITunnelCodec codec)
          Sets the ITunnelCodec that should be used by this ITunnel.
 void setLogLevel(int level)
          Sets the log level for this ITunnel.
static void setNbrOfWorkerThreads(int nbrOfWorkerThreads)
          Sets the number of worker threads for all tunnels that are initilized after this methods has been called.
protected  java.lang.Object waitForReply(AbstractTunnel.IPackage requestPackage)
           
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

public static final int DEBUG
See Also:
Constant Field Values

INFO

public static final int INFO
See Also:
Constant Field Values

WARNING

public static final int WARNING
See Also:
Constant Field Values

ERROR

public static final int ERROR
See Also:
Constant Field Values

DEFAULT_NBR_OF_WORKER_THREADS

public static final int DEFAULT_NBR_OF_WORKER_THREADS
See Also:
Constant Field Values

sNbrOfWorkerThreads

protected static int sNbrOfWorkerThreads

mMessageConsumers

protected java.util.List mMessageConsumers

mCodec

protected ITunnelCodec mCodec

mPackers

protected java.util.List mPackers

mStatusCallbacks

protected java.util.List mStatusCallbacks

mLogLevel

protected int mLogLevel

mOutQueue

protected java.util.List mOutQueue

mInQueue

protected java.util.List mInQueue

mTunnelListeners

protected java.util.List mTunnelListeners

mCurrentPackageID

protected int mCurrentPackageID

mWorkerThreads

protected AbstractTunnel.WorkerThread[] mWorkerThreads

mOutputThread

protected AbstractTunnel.OutputThread mOutputThread

mInputThread

protected AbstractTunnel.InputThread mInputThread

mReplyMessages

protected java.util.Map mReplyMessages
Constructor Detail

AbstractTunnel

public AbstractTunnel()
Default constructor. Uses a SerializeCodec as codec.


AbstractTunnel

public AbstractTunnel(ITunnelCodec codec)
Method Detail

setNbrOfWorkerThreads

public static void setNbrOfWorkerThreads(int nbrOfWorkerThreads)
Sets the number of worker threads for all tunnels that are initilized after this methods has been called. Calling this method has no effect on tunnels that have allready been initilized.


initialize

public void initialize()
                throws java.lang.IllegalStateException,
                       TunnelException
Initializes the ITunnel. This method must be called before any messages can be sent. After this method has been called it is not possible to set the ITunnelCodec or to add or remove ITunnelPackers. Subclasses of AbstractTunnel must call super.initialize(). Successful initialization recuires that the methods getInStream and getOutStream does not return null.

Specified by:
initialize in interface ITunnel
Throws:
java.lang.IllegalStateException
TunnelException

finalize

public void finalize()
              throws java.lang.IllegalStateException,
                     TunnelException
Performs cleanup for this ITunnel. No messages can be sent after this method has been called. This method will send all messages in the message queue before closing down. Subclasses of AbstractTunnel must call super.finalize() before they do any finalization of their own.

Specified by:
finalize in interface ITunnel
Throws:
java.lang.IllegalStateException
TunnelException

isActive

public boolean isActive()
Returns true if this tunnel is active, that is, if it is correctly initialized. If this method returns true then a call to finalize must not throw an IllegalStateException.

Specified by:
isActive in interface ITunnel

ping

public int ping()
         throws TunnelException
Sends a ping message over this ITunnel. This method is typically used to check that the communication is working.

Specified by:
ping in interface ITunnel
Returns:
the ping time in milli seconds.
Throws:
TunnelException

send

public void send(java.lang.Object message)
          throws TunnelException
Sends a message over this ITunnel. This method returns after the message has been sent.

Specified by:
send in interface ITunnel
Throws:
TunnelException

ask

public java.lang.Object ask(java.lang.Object message)
                     throws TunnelException
Sends a request/reply message over this ITunnel. This method blocks until the reply is received.

Specified by:
ask in interface ITunnel
Throws:
TunnelException

addMessageConsumer

public void addMessageConsumer(IMessageConsumer consumer)
Adds the consumer to the end of this ITunnels message consumer list.

Specified by:
addMessageConsumer in interface ITunnel

remomveMessageConsumer

public void remomveMessageConsumer(IMessageConsumer consumer)
Removes the consumer from this ITunnels message consumer list.

Specified by:
remomveMessageConsumer in interface ITunnel

setCodec

public void setCodec(ITunnelCodec codec)
              throws java.lang.IllegalStateException
Sets the ITunnelCodec that should be used by this ITunnel.

Specified by:
setCodec in interface ITunnel
Throws:
java.lang.IllegalStateException

addPacker

public void addPacker(ITunnelPacker packer)
               throws java.lang.IllegalStateException
Adds the packer to the end of this ITunnels packer list.

Specified by:
addPacker in interface ITunnel
Throws:
java.lang.IllegalStateException

remomvePacker

public void remomvePacker(ITunnelPacker packer)
                   throws java.lang.IllegalStateException
Removes the packer from this ITunnels packer list.

Specified by:
remomvePacker in interface ITunnel
Throws:
java.lang.IllegalStateException

addStatusCallback

public void addStatusCallback(IStatusCallback callback)
Adds the callback to the list of IStatusCallbacks that receives status messages from this ITunnel.

Specified by:
addStatusCallback in interface ITunnel

removeStatusCallback

public void removeStatusCallback(IStatusCallback callback)
Removes the callback from the list of IStatusCallbacks.

Specified by:
removeStatusCallback in interface ITunnel

setLogLevel

public void setLogLevel(int level)
Sets the log level for this ITunnel.

Specified by:
setLogLevel in interface ITunnel

getLogLevel

public int getLogLevel()
Gets the log level for this ITunnel.

Specified by:
getLogLevel in interface ITunnel

addTunnelListener

public void addTunnelListener(ITunnelListener listener)
Specified by:
addTunnelListener in interface ITunnel

removeTunnelListener

public void removeTunnelListener(ITunnelListener listener)
Specified by:
removeTunnelListener in interface ITunnel

getDescription

public java.lang.String getDescription()
Returns a humanly readably text that describes the ITunnel.

Specified by:
getDescription in interface ITunnel

createPackage

public AbstractTunnel.IPackage createPackage(java.lang.Object message,
                                             int sessionID)
Creates an IPackage that contains the provided message.


getOutputStream

protected abstract java.io.OutputStream getOutputStream()
Returns an OutputStream that should be used to send messages.


getInputStream

protected abstract java.io.InputStream getInputStream()
Returns an InputStream that should be used to receive messages.


createConnectionMessage

protected AbstractTunnel.IConnectionMessage createConnectionMessage()

log

protected void log(java.lang.Object source,
                   int logLevel,
                   java.lang.String message)

log

protected void log(java.lang.Object source,
                   int logLevel,
                   java.lang.String message,
                   java.lang.Throwable e)

fireTunnelClosedEvent

protected void fireTunnelClosedEvent()

fireConnectionLostEvent

protected void fireConnectionLostEvent()

getNextPackageID

protected int getNextPackageID()

addToOutQueue

protected void addToOutQueue(AbstractTunnel.IPackage outPackage)

getNextFromOutQueue

protected AbstractTunnel.IPackage getNextFromOutQueue()

addToInQueue

protected void addToInQueue(AbstractTunnel.IPackage inPackage)

getNextFromInQueue

protected AbstractTunnel.IPackage getNextFromInQueue()

waitForReply

protected java.lang.Object waitForReply(AbstractTunnel.IPackage requestPackage)

doOnTunnelClose

protected void doOnTunnelClose()

doOnConnectionLost

protected void doOnConnectionLost()

finalizeTunnel

protected void finalizeTunnel()


Copyright © 2005 Caleigo. All Rights Reserved.