mirrormap.nio
Class TcpConnectionEndPoint

java.lang.Object
  extended by mirrormap.lifecycle.AbstractLifeCycle
      extended by mirrormap.nio.TcpConnectionEndPoint
All Implemented Interfaces:
IDestroyable, ILifeCycle, IConnectionEndPoint, ISelectionKeyTask

public final class TcpConnectionEndPoint
extends AbstractLifeCycle
implements IConnectionEndPoint, ISelectionKeyTask

Handles the socket processing for a TCP end point.

Author:
Ramon Servadei

Constructor Summary
TcpConnectionEndPoint(IConnectionEndPointReceiver receiver, SelectorTasks selectorTasks, Executor writer, String host, int port)
          Standard constructor for a TCP end point.
 
Method Summary
protected  void doDestroy()
          Destroy the internal TCP I/O components.
protected  void doStart()
          Overridden in subclasses to perform custom logic on activation.
 IConnectionEndPointReceiver getReceiver()
          The object handling the inbound data for the connection
protected  SocketChannel getSocketChannel()
           
 boolean isConnected()
           
 boolean isOutbound()
          Indicates the connecting direction.
 void run()
          Run the task associated with the operation and selection key
 void send(byte[] data)
          Add the data to the pending messages list
 void setSelectionKey(SelectionKey selectionKey)
          Receives the selection key returned from the SelectableChannel.register(java.nio.channels.Selector, int) method called during the SelectorTasks.register(int, SelectableChannel, ISelectionKeyTask) method.
 String toString()
           
 
Methods inherited from class mirrormap.lifecycle.AbstractLifeCycle
checkActive, destroy, finalize, isActive, start
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface mirrormap.lifecycle.ILifeCycle
isActive, start
 
Methods inherited from interface mirrormap.lifecycle.IDestroyable
destroy
 

Constructor Detail

TcpConnectionEndPoint

public TcpConnectionEndPoint(IConnectionEndPointReceiver receiver,
                             SelectorTasks selectorTasks,
                             Executor writer,
                             String host,
                             int port)
Standard constructor for a TCP end point.

Parameters:
receiver - the object that will handle data received from the remote end point
selectorTasks - used to register and unregister this end point against the SelectionKey of the underlying SelectableChannel backing this end point
writer - handles writing data, optional. If null then the Thread running the SelectorTasks.process() method will do the writing
host - the IP address or resolvable host name of the other end point
port - the TCP port for the socket connection of the other end point
Throws:
IllegalStateException - if the socket could not be constructed
Method Detail

send

public void send(byte[] data)
Add the data to the pending messages list

Specified by:
send in interface IConnectionEndPoint
Parameters:
data - the data to add to the pending messages

getReceiver

public IConnectionEndPointReceiver getReceiver()
Description copied from interface: IConnectionEndPoint
The object handling the inbound data for the connection

Specified by:
getReceiver in interface IConnectionEndPoint
Returns:
object handling the inbound data for the connection

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.

Specified by:
doStart in class AbstractLifeCycle

isConnected

public boolean isConnected()

isOutbound

public boolean isOutbound()
Indicates the connecting direction.

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

doDestroy

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

Specified by:
doDestroy in class AbstractLifeCycle

getSocketChannel

protected SocketChannel getSocketChannel()

toString

public String toString()
Overrides:
toString in class AbstractLifeCycle

run

public void run()
Description copied from interface: ISelectionKeyTask
Run the task associated with the operation and selection key

Specified by:
run in interface ISelectionKeyTask

setSelectionKey

public void setSelectionKey(SelectionKey selectionKey)
Description copied from interface: ISelectionKeyTask
Receives the selection key returned from the SelectableChannel.register(java.nio.channels.Selector, int) method called during the SelectorTasks.register(int, SelectableChannel, ISelectionKeyTask) method. This is the key associated with this task.

Specified by:
setSelectionKey in interface ISelectionKeyTask
Parameters:
selectionKey - the selection key associated with this task


Copyright © 2010. All Rights Reserved.