edu.cuny.cat.comm
Class SocketBasedConnection<M extends Message>

java.lang.Object
  extended by edu.cuny.cat.comm.SocketBasedConnection<M>
Type Parameters:
M - the type of messages that can be transmitted through the connection.
All Implemented Interfaces:
Connection<M>, ProactiveConnection<M>
Direct Known Subclasses:
SocketBasedCatpConnection, TelnetConnection

public abstract class SocketBasedConnection<M extends Message>
extends java.lang.Object
implements ProactiveConnection<M>

An abstract implementation of Connection when the communication is socket-based. Child classes should be defined to support concrete message parsing, e.g., SocketBasedCatpConnection and TelnetConnection .

Version:
$Revision: 1.3 $
Author:
Jinzhong Niu

Field Summary
protected static int BUFFER_SIZE
           
protected static org.apache.log4j.Logger logger
           
protected  java.nio.ByteBuffer readBuffer
           
protected  boolean skipLF
           
protected  java.nio.channels.SocketChannel socketChannel
           
 
Constructor Summary
SocketBasedConnection(java.nio.channels.SocketChannel socketChannel)
           
 
Method Summary
 void close()
          closes this connection.
 java.lang.String getLocalAddressInfo()
           
 java.lang.String getRemoteAddressInfo()
           
protected  void initBuffer()
           
 boolean isClosed()
          checks whether the connection is closed or not.
 void open()
          opens the connection before sending or receiving any message.
protected  java.lang.String readLine()
          read a plain-text line from the socket channel.
 void sendMessage(M msg)
          sends a Message through this connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface edu.cuny.cat.comm.ProactiveConnection
getMessage
 

Field Detail

logger

protected static org.apache.log4j.Logger logger

socketChannel

protected java.nio.channels.SocketChannel socketChannel

BUFFER_SIZE

protected static final int BUFFER_SIZE
See Also:
Constant Field Values

readBuffer

protected java.nio.ByteBuffer readBuffer

skipLF

protected boolean skipLF
Constructor Detail

SocketBasedConnection

public SocketBasedConnection(java.nio.channels.SocketChannel socketChannel)
Method Detail

initBuffer

protected void initBuffer()

sendMessage

public void sendMessage(M msg)
                 throws CatException
Description copied from interface: Connection
sends a Message through this connection.

Specified by:
sendMessage in interface Connection<M extends Message>
Parameters:
msg - message to be sent.
Throws:
CatException

readLine

protected java.lang.String readLine()
                             throws java.io.IOException
read a plain-text line from the socket channel. The implementation is based on the code of BufferedReader.readLine().

Returns:
a non-null string if successful or null if EOF was met.
Throws:
java.io.IOException

open

public void open()
          throws ConnectionException
Description copied from interface: Connection
opens the connection before sending or receiving any message.

Specified by:
open in interface Connection<M extends Message>
Throws:
ConnectionException

close

public void close()
           throws ConnectionException
Description copied from interface: Connection
closes this connection.

Specified by:
close in interface Connection<M extends Message>
Throws:
ConnectionException

isClosed

public boolean isClosed()
Description copied from interface: Connection
checks whether the connection is closed or not.

Specified by:
isClosed in interface Connection<M extends Message>
Returns:
true if closed; false otherwise.

getLocalAddressInfo

public java.lang.String getLocalAddressInfo()
Specified by:
getLocalAddressInfo in interface Connection<M extends Message>
Returns:
a string describing the address of the local end of this connection

getRemoteAddressInfo

public java.lang.String getRemoteAddressInfo()
Specified by:
getRemoteAddressInfo in interface Connection<M extends Message>
Returns:
a string describing the address of the remote end of this connection