com.devexperts.qd.qtp
Class AbstractByteArrayComposer

java.lang.Object
  extended by com.devexperts.qd.qtp.AbstractMessageVisitor
      extended by com.devexperts.qd.qtp.AbstractByteArrayComposer
All Implemented Interfaces:
DataVisitor, MessageVisitor, SubscriptionVisitor
Direct Known Subclasses:
BlobByteArrayComposer, ByteArrayComposer, TextByteArrayComposer

public abstract class AbstractByteArrayComposer
extends AbstractMessageVisitor
implements DataVisitor, SubscriptionVisitor

Base class for classes that compose QTP messages data into byte array.

It consolidates common logic for text and binary byte array composers. Most of it's logic was simply extracted from ByteArrayComposer.

Classes that are interested in extra-high performance can use ByteArrayComposer directly, and classes that are interested in opportunity to compose data both in text and binary formats can use this class.

NOTE: All protected methods declared to throw IOException actually never do this, because underlying ByteArrayDataOutput never do this. They are declared to throw IOException only in order not to process it in every subclass but to process it here. The only reason why IOException can be thrown is that there are errors in internal logic of a class. That's why AssertionError is always thrown in this case.

See Also:
ByteArrayComposer, TextByteArrayComposer, AbstractByteArrayParser

Field Summary
protected  boolean historyAddSubscription
           
protected  com.devexperts.io.ByteArrayOutput out
           
protected  byte[] pooledBuffer
           
protected  int processed
           
protected static byte RECORD_STATE_DESCRIBED
           
protected static byte RECORD_STATE_NEW
           
protected  byte[] recordState
           
protected  DataScheme scheme
           
protected  int threshold
           
 
Constructor Summary
protected AbstractByteArrayComposer(DataScheme scheme)
          Constructs composer with a specified scheme in a stateless mode (describe messages are not being composed).
protected AbstractByteArrayComposer(DataScheme scheme, boolean composeDescribeRecords)
          Constructs composer with a specified scheme and a describe records mode.
 
Method Summary
protected  void beginMessage(int messageTypeId)
          Invoked every time before processing a message.
protected abstract  void composeHistorySubscriptionTime(DataRecord record, long time)
           
protected abstract  void composeIntField(DataIntField field, int value)
           
protected abstract  void composeMessageHeader(int messageTypeId)
          Composes header of a message of specific type.
protected abstract  void composeObjField(DataObjField field, java.lang.Object value)
           
protected abstract  void composeOtherMessageBody(byte[] messageBytes, int offset, int length)
           
protected abstract  void composeRecord(DataRecord record, int cipher, java.lang.String symbol)
           
protected  void endMessage()
          Invoked every time after processing all message data.
protected abstract  void finishComposingMessage(int messageStart)
          Performs actions necessary to finish composing a message (for example, inserts message size, inserts records descriptions if necessary).
 byte[] getBuffer()
          Returns byte array buffer where data is stored.
 int getProcessed()
          Returns number of processed bytes ready to be sent over and removed.
 int getThreshold()
          Deprecated. No replacement. DEFAULT_THRESHOLD can be configured with system properties if needed.
 boolean hasCapacity()
          Returns whether visitor has capacity to efficiently visit next record.
protected  void moveDataForward(int oldPos, int newPos, int length)
          Accurately moves block of data of length length in out buffer form position oldPos into position newPos.
 void removeBytes(int n)
          Removes specified number of bytes from the start of the buffer.
 void resetSession()
          Resets session state for composer with describe records mode.
 void setThreshold(int threshold)
          Deprecated. No replacement. DEFAULT_THRESHOLD can be configured with system properties if needed.
 boolean visitData(DataProvider provider, MessageType type)
          This method consumes available data for data message from the given data provider.
 void visitIntField(DataIntField field, int value)
          Visits next Int-field within current record.
 void visitObjField(DataObjField field, java.lang.Object value)
          Visits next Obj-field within current record.
 boolean visitOtherMessage(int messageType, byte[] messageBytes, int offset, int length)
          This implementation simply skips all unknown messages.
 void visitRecord(DataRecord record, int cipher, java.lang.String symbol)
          Visits next record.
 void visitRecord(DataRecord record, int cipher, java.lang.String symbol, long time)
          Visits next record.
 boolean visitSubscription(SubscriptionProvider provider, MessageType type)
          This method consumes available subscription for subscription message from the given subscription provider.
 
Methods inherited from class com.devexperts.qd.qtp.AbstractMessageVisitor
visitDescribeProtocol, visitHistoryAddSubscription, visitHistoryData, visitHistoryRemoveSubscription, visitStreamAddSubscription, visitStreamData, visitStreamRemoveSubscription, visitTickerAddSubscription, visitTickerData, visitTickerRemoveSubscription
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

threshold

protected int threshold

pooledBuffer

protected byte[] pooledBuffer

RECORD_STATE_NEW

protected static final byte RECORD_STATE_NEW
See Also:
Constant Field Values

RECORD_STATE_DESCRIBED

protected static final byte RECORD_STATE_DESCRIBED
See Also:
Constant Field Values

historyAddSubscription

protected boolean historyAddSubscription

scheme

protected final DataScheme scheme

recordState

protected final byte[] recordState

out

protected final com.devexperts.io.ByteArrayOutput out

processed

protected int processed
Constructor Detail

AbstractByteArrayComposer

protected AbstractByteArrayComposer(DataScheme scheme)
Constructs composer with a specified scheme in a stateless mode (describe messages are not being composed). It is the same as AbstractByteArrayComposer(scheme, false).

Parameters:
scheme - Data scheme to use.

AbstractByteArrayComposer

protected AbstractByteArrayComposer(DataScheme scheme,
                                    boolean composeDescribeRecords)
Constructs composer with a specified scheme and a describe records mode.

Parameters:
scheme - The data scheme.
composeDescribeRecords - If true, then describe messages are composed right before records are used for the first time and this instance keeps its state and shall not be reused for different communication sessions.
Method Detail

removeBytes

public void removeBytes(int n)
Removes specified number of bytes from the start of the buffer.

Parameters:
n - Number of bytes to remove.
Throws:
java.lang.IllegalArgumentException - if specified number is negative or is larger than the number of processed bytes.

getThreshold

public int getThreshold()
Deprecated. No replacement. DEFAULT_THRESHOLD can be configured with system properties if needed.

Returns current threshold.

Returns:
current threshold.

setThreshold

public void setThreshold(int threshold)
Deprecated. No replacement. DEFAULT_THRESHOLD can be configured with system properties if needed.

Sets new threshold. The threshold regulates how much processed data is accumulated in the buffer. If accumulated data exceeds threshold, then message composition stops (in a normal way, it is not aborted).

Note that it is not guaranteed that accumulated data will never exceed threshold. The threshold is merely a hint and it is observed only by some methods.

Parameters:
threshold - New threshold.

hasCapacity

public boolean hasCapacity()
Description copied from interface: DataVisitor
Returns whether visitor has capacity to efficiently visit next record. This method may be used to advise data provider that it is desirable to stop current string of visiting and to keep remaining data. However, at present, data provider is not obliged to adhere to this method contract.

NOTE: data visitor must process all data that is passed to it via visitXXX calls no matter whether it has capacity to do it efficiently.

Specified by:
hasCapacity in interface DataVisitor
Specified by:
hasCapacity in interface SubscriptionVisitor

resetSession

public void resetSession()
Resets session state for composer with describe records mode. Resets the state of all described records so far, so that describe messages are started to being sent again, as if this composer was just created.

Throws:
java.lang.UnsupportedOperationException - when composer was constructed without describe records mode.
See Also:
AbstractByteArrayComposer(DataScheme, boolean)

getProcessed

public int getProcessed()
Returns number of processed bytes ready to be sent over and removed.

Returns:
number of processed bytes ready to be sent over and removed.

getBuffer

public byte[] getBuffer()
Returns byte array buffer where data is stored. Valid index range is from zero inclusive to the number of processed bytes exclusive.

Note: the array instance used for buffering may change if new messages are added or previous ones are removed.

Returns:
byte array buffer.

visitRecord

public void visitRecord(DataRecord record,
                        int cipher,
                        java.lang.String symbol)
Description copied from interface: DataVisitor
Visits next record.

Specified by:
visitRecord in interface DataVisitor

composeRecord

protected abstract void composeRecord(DataRecord record,
                                      int cipher,
                                      java.lang.String symbol)
                               throws java.io.IOException
Throws:
java.io.IOException

visitRecord

public void visitRecord(DataRecord record,
                        int cipher,
                        java.lang.String symbol,
                        long time)
Description copied from interface: SubscriptionVisitor
Visits next record.

Specified by:
visitRecord in interface SubscriptionVisitor

composeHistorySubscriptionTime

protected abstract void composeHistorySubscriptionTime(DataRecord record,
                                                       long time)
                                                throws java.io.IOException
Throws:
java.io.IOException

visitIntField

public void visitIntField(DataIntField field,
                          int value)
Description copied from interface: DataVisitor
Visits next Int-field within current record.

Specified by:
visitIntField in interface DataVisitor

composeIntField

protected abstract void composeIntField(DataIntField field,
                                        int value)
                                 throws java.io.IOException
Throws:
java.io.IOException

visitObjField

public void visitObjField(DataObjField field,
                          java.lang.Object value)
Description copied from interface: DataVisitor
Visits next Obj-field within current record.

Specified by:
visitObjField in interface DataVisitor

composeObjField

protected abstract void composeObjField(DataObjField field,
                                        java.lang.Object value)
                                 throws java.io.IOException
Throws:
java.io.IOException

visitData

public boolean visitData(DataProvider provider,
                         MessageType type)
Description copied from class: AbstractMessageVisitor
This method consumes available data for data message from the given data provider.

Specified by:
visitData in interface MessageVisitor
Specified by:
visitData in class AbstractMessageVisitor
Returns:
false if it had retrieved all available data and no data left, true if more data remains to be consumed

visitSubscription

public boolean visitSubscription(SubscriptionProvider provider,
                                 MessageType type)
Description copied from class: AbstractMessageVisitor
This method consumes available subscription for subscription message from the given subscription provider.

Specified by:
visitSubscription in interface MessageVisitor
Specified by:
visitSubscription in class AbstractMessageVisitor
Returns:
false if it had retrieved all available subscription and nothing left, true if more subscription remains to be consumed

visitOtherMessage

public boolean visitOtherMessage(int messageType,
                                 byte[] messageBytes,
                                 int offset,
                                 int length)
Description copied from class: AbstractMessageVisitor
This implementation simply skips all unknown messages.

Specified by:
visitOtherMessage in interface MessageVisitor
Overrides:
visitOtherMessage in class AbstractMessageVisitor
Parameters:
messageType - integer number representing a type of the message.
messageBytes - array containing message data.
offset - position of the first byte of message data in messageBytes array.
length - number of bytes starting from offset in messageBytes related to this message.
Returns:
true if the whole message was not processed because the visitor is full and false if the message was successfully processed.

composeOtherMessageBody

protected abstract void composeOtherMessageBody(byte[] messageBytes,
                                                int offset,
                                                int length)
                                         throws java.io.IOException
Throws:
java.io.IOException

beginMessage

protected void beginMessage(int messageTypeId)
Invoked every time before processing a message.

Parameters:
messageTypeId - id of message type (see MessageConstants).

composeMessageHeader

protected abstract void composeMessageHeader(int messageTypeId)
                                      throws java.io.IOException
Composes header of a message of specific type.

Parameters:
messageTypeId - id of message type (see MessageConstants).
Throws:
java.io.IOException - never. If it has been thrown then it means an internal error.

endMessage

protected void endMessage()
Invoked every time after processing all message data.


finishComposingMessage

protected abstract void finishComposingMessage(int messageStart)
                                        throws java.io.IOException
Performs actions necessary to finish composing a message (for example, inserts message size, inserts records descriptions if necessary). Used by ByteArrayComposer.

Parameters:
messageStart - position in buffer where message begins. The end of a message is at current buffer position (out.getPosition()).
Throws:
java.io.IOException - never.

moveDataForward

protected void moveDataForward(int oldPos,
                               int newPos,
                               int length)
Accurately moves block of data of length length in out buffer form position oldPos into position newPos.

Parameters:
oldPos - position to move from.
newPos - position to move into.
length - length of data block.