fulmine.distribution.connection.tcp
Class AbstractSocketChannelConnection

java.lang.Object
  extended by fulmine.AbstractLifeCycle
      extended by fulmine.distribution.connection.tcp.TcpConnectionParameters
          extended by fulmine.distribution.connection.tcp.AbstractSocketChannelConnection
All Implemented Interfaces:
IConnection, IConnectionParameters, IEventSource, IAddressable, IDescriptor, IDestroyable, ILifeCycle
Direct Known Subclasses:
TcpConnection

public abstract class AbstractSocketChannelConnection
extends TcpConnectionParameters
implements IConnection

A SocketChannel connection to a remote IFrameworkContext instance. This uses the Java NIO framework.

Author:
Ramon Servadei

Field Summary
protected  boolean destroyWhenMessagesProcessed
          A flag to inform the MessageConsumedEventHandler that the connection should be destroyed when all messages have been consumed.
protected  IEventSource eventSourceDelegate
          The delegate for the IEventSource operations of this
protected  int messagesToProcess
          Tracks the number of messages received and not yet processed.
 
Constructor Summary
AbstractSocketChannelConnection(IFrameworkContext context, String identity, String address, int port, int connectionHashCode)
          Standard constructor for the parameters of the connection.
 
Method Summary
 void addEvent(IEvent event)
          Add the event to the source and notify any registered IEventListener instances with the event.
 boolean addListener(IEventListener listener)
          Add the listener to the end of the list of listeners registered against this.
protected  void connectionHandshakeComplete()
          This method is called when all relevant details of the connection have been established.
protected  void doComponentDestroy()
           
protected  void doDestroy()
          Destroy the internal TCP I/O components.
protected  void doStart()
          Overridden in subclasses to perform custom logic on activation.
protected  IFrameworkContext getContext()
           
 byte getEventSourceGroupId()
          A byte identifying the event source group id.
 List<IEventListener> getListeners()
          Get the list of IEventListener instances observing this.
protected  SocketChannel getSocketChannel()
           
 boolean isConnected()
           
protected  boolean isConnectionHandshakeComplete()
          Determine if the application level connection handshake has been established.
 boolean isOutbound()
          Indicates the connecting direction.
 boolean removeListener(IEventListener listener)
          Remove the listener from the list of listeners registered against this.
 List<IEventListener> removeListeners()
          Remove all listeners registered for receiving IEvent events originating from this.
 String toDetailedString()
          Provides a formatted string describing this object in detail.
 String toIdentityString()
          Get a string that provides the identification for this object.
 String toString()
           
 
Methods inherited from class fulmine.distribution.connection.tcp.TcpConnectionParameters
equals, getAddress, getDomain, getIdentity, getRemoteContextHashCode, getRemoteContextIdentity, getRemoteHostAddress, getRemoteHostTcpPort, getType, hashCode, isEqual, setIdentity, setRemoteContextHashCode
 
Methods inherited from class fulmine.AbstractLifeCycle
checkActive, destroy, finalize, getLog, isActive, start
 
Methods inherited from class java.lang.Object
clone, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface fulmine.distribution.connection.IConnection
send
 
Methods inherited from interface fulmine.ILifeCycle
isActive, start
 
Methods inherited from interface fulmine.IDestroyable
destroy
 
Methods inherited from interface fulmine.IAddressable
getAddress, getDomain, getIdentity, getType
 
Methods inherited from interface fulmine.distribution.connection.IConnectionParameters
getRemoteContextHashCode, getRemoteContextIdentity, isEqual
 

Field Detail

eventSourceDelegate

protected IEventSource eventSourceDelegate
The delegate for the IEventSource operations of this


destroyWhenMessagesProcessed

protected volatile boolean destroyWhenMessagesProcessed
A flag to inform the MessageConsumedEventHandler that the connection should be destroyed when all messages have been consumed.

See Also:
messagesToProcess

messagesToProcess

protected int messagesToProcess
Tracks the number of messages received and not yet processed. When the message is processed, a MessageConsumedEvent is raised that is intercepted by the MessageConsumedEventHandler which decrements this.

Access must be synchronized on the AbstractSocketChannelConnection instance.

Constructor Detail

AbstractSocketChannelConnection

public AbstractSocketChannelConnection(IFrameworkContext context,
                                       String identity,
                                       String address,
                                       int port,
                                       int connectionHashCode)
Standard constructor for the parameters of the connection.

Parameters:
context - the context this connection services
identity - the connection identity
address - the IP address or resolvable host name of the remote context
port - the port for the socket connection to the remote context
connectionHashCode - the hashcode of the connection to the remote context
Throws:
IllegalStateException - if the socket could not be constructed
Method Detail

doComponentDestroy

protected void doComponentDestroy()

getContext

protected IFrameworkContext getContext()

doStart

protected void doStart()
Description copied from class: AbstractLifeCycle
Overridden in subclasses to perform custom logic on activation. Any exceptions should be thrown as a RuntimeException or subclass thereof. When this method is called, the AbstractLifeCycle.isActive() method will return true.

Overrides:
doStart in class TcpConnectionParameters

isConnected

public boolean isConnected()

isOutbound

public boolean isOutbound()
Indicates the connecting direction.

Specified by:
isOutbound in interface IConnection
Returns:
true if this socket connects to a server socket, false if it was an inbound connection from another context

doDestroy

protected void doDestroy()
Destroy the internal TCP I/O components.

Overrides:
doDestroy in class TcpConnectionParameters

addEvent

public void addEvent(IEvent event)
Description copied from interface: IEventSource
Add the event to the source and notify any registered IEventListener instances with the event. The manner of activating listeners with the event may be synchronous or asynchronous; it is the implementation that decides this.

Specified by:
addEvent in interface IEventSource
Parameters:
event - the event to pass on to the registered listeners

addListener

public boolean addListener(IEventListener listener)
Description copied from interface: IEventSource
Add the listener to the end of the list of listeners registered against this. If the listener is an IPriorityEventListener, it is added to the beginning of the list.

Listeners are reference counted. Adding the same listener multiple times simply increments a reference count of the listener instance; if a listener is added twice, it must be removed twice for the listener instance reference to be removed from the internal list.

Specified by:
addListener in interface IEventSource
Parameters:
listener - the listener to add
Returns:
true if the listener instance was added (there were no reference counts for it), false if it already existed (the reference count was incremented)

getEventSourceGroupId

public byte getEventSourceGroupId()
Description copied from interface: IEventSource
A byte identifying the event source group id. This will be the id of the EventProcessor that will distribute the events from this source.

Specified by:
getEventSourceGroupId in interface IEventSource
Returns:
a byte for the EventProcessor servicing this.

getListeners

public List<IEventListener> getListeners()
Description copied from interface: IEventSource
Get the list of IEventListener instances observing this. The list is the order of registration and the notification order for any IEvent raised by this event source.

The list is not modifiable.

Specified by:
getListeners in interface IEventSource
Returns:
an unmodifiable list of listeners, or null if there are no listeners

removeListener

public boolean removeListener(IEventListener listener)
Description copied from interface: IEventSource
Remove the listener from the list of listeners registered against this. If this is the last listener being removed, the list of listeners is set to null.

Listeners are reference counted. When removing a listener, this method decrements the reference count until there are no more references, at which point the listener instance reference is actually removed.

Specified by:
removeListener in interface IEventSource
Parameters:
listener - the listener to remove
Returns:
true if the listener was found and the reference count was 0 and it was removed, false otherwise

removeListeners

public List<IEventListener> removeListeners()
Description copied from interface: IEventSource
Remove all listeners registered for receiving IEvent events originating from this.

Specified by:
removeListeners in interface IEventSource
Returns:
the list of listeners removed

toDetailedString

public String toDetailedString()
Description copied from interface: IDescriptor
Provides a formatted string describing this object in detail. This has the same or more information than the standard Object.toString() method.

Specified by:
toDetailedString in interface IDescriptor
Returns:
a formatted string describing this object in detail

toIdentityString

public String toIdentityString()
Description copied from interface: IDescriptor
Get a string that provides the identification for this object. This should have no more detail than the Object.toString() method.

Specified by:
toIdentityString in interface IDescriptor
Returns:
a string that provides the identification for this object

getSocketChannel

protected SocketChannel getSocketChannel()

isConnectionHandshakeComplete

protected boolean isConnectionHandshakeComplete()
Determine if the application level connection handshake has been established. When a transport connection is made, the context making the connection sends out a 'con' message which the other context responds to with an 'ack'. This sequence completes the application level handshake for the connection and exchanges details about each context.

Each context marks its end of the connection as established as per the following;

Returns:
true if the connection handshake has been completed and the details about the remote context are known.

connectionHandshakeComplete

protected void connectionHandshakeComplete()
This method is called when all relevant details of the connection have been established.

See Also:
isConnectionHandshakeComplete()

toString

public String toString()
Overrides:
toString in class TcpConnectionParameters


Copyright © 2007-2009. All Rights Reserved.