Spring Integration

org.springframework.integration.ip.tcp
Class AbstractSocketReader

java.lang.Object
  extended by org.springframework.integration.ip.tcp.AbstractSocketReader
All Implemented Interfaces:
MessageFormats, SocketReader
Direct Known Subclasses:
NetSocketReader, NioSocketReader

public abstract class AbstractSocketReader
extends Object
implements SocketReader, MessageFormats

Abstract SocketReader that handles data in 3 standard, and one custom format. The default format is MessageFormats.FORMAT_LENGTH_HEADER in which the message consists of a 4 byte integer (in network byte order) containing the length of data that follows. MessageFormats.FORMAT_STX_ETX indicates a message where the data begins with STX (0x02) and ends with ETX (0x03); the STX and ETX are not part of the data. MessageFormats.FORMAT_CRLF indicates a message followed by carriage return and line feed '\r\n'. FORMAT_LENGTH_HEADER can be used for Socket and SocketChannel implementations are provided for the standard formats. Users requiring other formats should subclass the appropriate implementation, and provide an implementation for assembleDataCustomFormat() which is invoked by assembleData() when the format is MessageFormats.FORMAT_CUSTOM.

Since:
2.0
Author:
Gary Russell

Field Summary
protected  Object assembledData
          The assembled data; must contain a reference when assembleData() returns true; will be set to null when getAssembledData() is called.
protected  int maxMessageSize
           
protected  int messageFormat
           
 
Fields inherited from interface org.springframework.integration.ip.tcp.SocketReader
MESSAGE_COMPLETE, MESSAGE_INCOMPLETE, SOCKET_CLOSED
 
Fields inherited from interface org.springframework.integration.ip.tcp.MessageFormats
ETX, FORMAT_CRLF, FORMAT_CUSTOM, FORMAT_IMPLICIT, FORMAT_JAVA_SERIALIZED, FORMAT_LENGTH_HEADER, FORMAT_STX_ETX, STX
 
Constructor Summary
AbstractSocketReader()
           
 
Method Summary
 int assembleData()
          Reads the data the socket and assembles packets of data into a complete message, depending on the format of that data.
protected abstract  int assembleDataCrLfFormat()
          Assembles data in format MessageFormats.FORMAT_CRLF.
protected abstract  int assembleDataCustomFormat()
          Assembles data in format MessageFormats.FORMAT_CUSTOM.
protected abstract  int assembleDataLengthFormat()
          Assembles data in format MessageFormats.FORMAT_LENGTH_HEADER.
protected abstract  int assembleDataSerializedFormat()
          Assembles data in format MessageFormats.FORMAT_JAVA_SERIALIZED
protected abstract  int assembleDataStxEtxFormat()
          Assembles data in format MessageFormats.FORMAT_STX_ETX.
protected abstract  void doClose()
          Called after an exception; close the transport.
 Object getAssembledData()
          Retrieves the assembled tcp data or null if the data is not yet assembled.
 void setMaxMessageSize(int maxMessageSize)
           
 void setMessageFormat(int messageFormat)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.integration.ip.tcp.SocketReader
getAddress, getSocket
 

Field Detail

messageFormat

protected int messageFormat

assembledData

protected Object assembledData
The assembled data; must contain a reference when assembleData() returns true; will be set to null when getAssembledData() is called.


maxMessageSize

protected int maxMessageSize
Constructor Detail

AbstractSocketReader

public AbstractSocketReader()
Method Detail

assembleDataLengthFormat

protected abstract int assembleDataLengthFormat()
                                         throws IOException
Assembles data in format MessageFormats.FORMAT_LENGTH_HEADER.

Returns:
SocketReader.MESSAGE_COMPLETE when message is assembled, otherwise SocketReader.MESSAGE_IMCOMPLETE, or < 0 if socket closed before any data for a message is received.
Throws:
IOException

assembleDataStxEtxFormat

protected abstract int assembleDataStxEtxFormat()
                                         throws IOException
Assembles data in format MessageFormats.FORMAT_STX_ETX.

Returns:
SocketReader.MESSAGE_COMPLETE when message is assembled, otherwise SocketReader.MESSAGE_IMCOMPLETE, or < 0 if socket closed before any data for a message is received.
Throws:
IOException

assembleDataCrLfFormat

protected abstract int assembleDataCrLfFormat()
                                       throws IOException
Assembles data in format MessageFormats.FORMAT_CRLF.

Returns:
SocketReader.MESSAGE_COMPLETE when message is assembled, otherwise SocketReader.MESSAGE_IMCOMPLETE, or < 0 if socket closed before any data for a message is received.
Throws:
IOException

assembleDataSerializedFormat

protected abstract int assembleDataSerializedFormat()
                                             throws IOException
Assembles data in format MessageFormats.FORMAT_JAVA_SERIALIZED

Returns:
SocketReader.MESSAGE_COMPLETE when message is assembled, otherwise SocketReader.MESSAGE_IMCOMPLETE, or < 0 if socket closed before any data for a message is received.
Throws:
IOException

assembleDataCustomFormat

protected abstract int assembleDataCustomFormat()
                                         throws IOException
Assembles data in format MessageFormats.FORMAT_CUSTOM. Implementations must return false until the message is completely assembled, at which time the implementation must update assembledData to reference the assembled message.

Returns:
True when a message is completely assembled.
Throws:
IOException

assembleData

public int assembleData()
                 throws IOException
Description copied from interface: SocketReader
Reads the data the socket and assembles packets of data into a complete message, depending on the format of that data.

Specified by:
assembleData in interface SocketReader
Returns:
MESSAGE_COMPLETE when message is assembled, otherwise MESSAGE_IMCOMPLETE, or < 0 if socket closed before any data for a message is received.
Throws:
IOException

getAssembledData

public Object getAssembledData()
Description copied from interface: SocketReader
Retrieves the assembled tcp data or null if the data is not yet assembled. Once this method is called, the assembled data is again null until a new assembly is completed.

Specified by:
getAssembledData in interface SocketReader
Returns:
The assembled data or null.

doClose

protected abstract void doClose()
Called after an exception; close the transport.


setMessageFormat

public void setMessageFormat(int messageFormat)
Parameters:
messageFormat - the messageFormat to set,

setMaxMessageSize

public void setMaxMessageSize(int maxMessageSize)

Spring Integration

Copyright © 2010. All Rights Reserved.