net.officefloor.plugin.socket.server.tcp.protocol
Class TcpConnectionHandler

java.lang.Object
  extended by net.officefloor.plugin.socket.server.tcp.protocol.TcpConnectionHandler
All Implemented Interfaces:
AsynchronousManagedObject, ManagedObject, ConnectionHandler, ServerTcpConnection, WriteBufferReceiver

public class TcpConnectionHandler
extends Object
implements ConnectionHandler, AsynchronousManagedObject, WriteBufferReceiver, ServerTcpConnection

TCP ConnectionHandler.

Author:
Daniel Sagenschneider

Constructor Summary
TcpConnectionHandler(TcpCommunicationProtocol protocol, Connection connection, int sendBufferSize, long maxIdleTime)
          Initiate.
 
Method Summary
 void close()
           Flags to close the WriteBufferReceiver.
 WriteBuffer createWriteBuffer(byte[] data, int length)
          Creates a WriteBuffer for the data.
 WriteBuffer createWriteBuffer(ByteBuffer buffer)
          Creates the WriteBuffer for the ByteBuffer.
 ServerInputStream getInputStream()
          Obtains the ServerInputStream that provides access to the data sent from the client.
 Object getLock()
          Obtains the lock to synchronize for using this WriteBufferReceiver.
 Object getObject()
          Obtains the object being managed.
 ServerOutputStream getOutputStream()
           Obtains the ServerOutputStream to write data back to the client.
 void handleHeartbeat(HeartBeatContext context)
           Handles a heart beat on Connection.
 void handleRead(ReadContext context)
          Handles a read from the SocketChannel.
 boolean isClosed()
          Indicates if the WriteBufferReceiver is closed.
 void registerAsynchronousCompletionListener(AsynchronousListener listener)
          Provides the AsynchronousListener to the AsynchronousManagedObject to enable call back to notify state and completion of asynchronous processing.
 boolean waitOnClientData()
           Flags for the ManagedObject to not execute another Task until further data is received from the client.
 void writeData(WriteBuffer[] data)
          Writes data to client of this Connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TcpConnectionHandler

public TcpConnectionHandler(TcpCommunicationProtocol protocol,
                            Connection connection,
                            int sendBufferSize,
                            long maxIdleTime)
Initiate.

Parameters:
protocol - TcpCommunicationProtocol.
connection - Connection.
sendBufferSize - Send buffer size.
maxIdleTime - Maximum idle time for the Connection measured in milliseconds.
Method Detail

handleHeartbeat

public void handleHeartbeat(HeartBeatContext context)
                     throws IOException
Description copied from interface: ConnectionHandler

Handles a heart beat on Connection.

Typical use of the heart beat is to allow the ConnectionHandler to close an idle Connection.

Specified by:
handleHeartbeat in interface ConnectionHandler
Parameters:
context - HeartBeatContext.
Throws:
IOException - If fails to handle heart beat. Possibly from attempting to close it.

handleRead

public void handleRead(ReadContext context)
                throws IOException
Description copied from interface: ConnectionHandler
Handles a read from the SocketChannel.

Specified by:
handleRead in interface ConnectionHandler
Parameters:
context - ReadContext.
Throws:
IOException - If fails to obtain data from the ReadContext.

getLock

public Object getLock()
Description copied from interface: WriteBufferReceiver
Obtains the lock to synchronize for using this WriteBufferReceiver.

Specified by:
getLock in interface WriteBufferReceiver
Returns:
Lock for this WriteBufferReceiver.

createWriteBuffer

public WriteBuffer createWriteBuffer(byte[] data,
                                     int length)
Description copied from interface: WriteBufferReceiver
Creates a WriteBuffer for the data.

Specified by:
createWriteBuffer in interface WriteBufferReceiver
Parameters:
data - Data.
length - Length of data.
Returns:
WriteBuffer.

createWriteBuffer

public WriteBuffer createWriteBuffer(ByteBuffer buffer)
Description copied from interface: WriteBufferReceiver
Creates the WriteBuffer for the ByteBuffer.

Specified by:
createWriteBuffer in interface WriteBufferReceiver
Parameters:
buffer - ByteBuffer.
Returns:
WriteBuffer.

writeData

public void writeData(WriteBuffer[] data)
               throws IOException
Description copied from interface: WriteBufferReceiver
Writes data to client of this Connection.

Specified by:
writeData in interface WriteBufferReceiver
Parameters:
data - Data to be written.
Throws:
IOException - If fails to write data.

close

public void close()
           throws IOException
Description copied from interface: WriteBufferReceiver

Flags to close the WriteBufferReceiver.

Close occurs after all data has been written.

Specified by:
close in interface WriteBufferReceiver
Throws:
IOException - If fails to close.

isClosed

public boolean isClosed()
Description copied from interface: WriteBufferReceiver
Indicates if the WriteBufferReceiver is closed.

Specified by:
isClosed in interface WriteBufferReceiver
Returns:
true if the WriteBufferReceiver is closed.

registerAsynchronousCompletionListener

public void registerAsynchronousCompletionListener(AsynchronousListener listener)
Description copied from interface: AsynchronousManagedObject
Provides the AsynchronousListener to the AsynchronousManagedObject to enable call back to notify state and completion of asynchronous processing.

Specified by:
registerAsynchronousCompletionListener in interface AsynchronousManagedObject
Parameters:
listener - AsynchronousListener.

getObject

public Object getObject()
Description copied from interface: ManagedObject
Obtains the object being managed.

Specified by:
getObject in interface ManagedObject
Returns:
Object being managed.

waitOnClientData

public boolean waitOnClientData()
                         throws IOException
Description copied from interface: ServerTcpConnection

Flags for the ManagedObject to not execute another Task until further data is received from the client.

On calling this the next time a Task is invoked using this ManagedObject, data will be available from the ServerInputStream.

Specified by:
waitOnClientData in interface ServerTcpConnection
Returns:
true indicating if will wait on client data. false if client data is available and therefore will not wait.
Throws:
IOException - If fails to initiate waiting on client.

getInputStream

public ServerInputStream getInputStream()
Description copied from interface: ServerTcpConnection
Obtains the ServerInputStream that provides access to the data sent from the client.

Specified by:
getInputStream in interface ServerTcpConnection
Returns:
ServerInputStream.

getOutputStream

public ServerOutputStream getOutputStream()
Description copied from interface: ServerTcpConnection

Obtains the ServerOutputStream to write data back to the client.

Closing the ServerOutputStream will result in closing the Connection.

Specified by:
getOutputStream in interface ServerTcpConnection
Returns:
ServerOutputStream.


Copyright © 2005-2013. All Rights Reserved.