Interface MessageReader<H extends MessageHeader,​M extends Message>

All Known Implementing Classes:
DebugBufferingPacketReader, MultiPacketReader, SimplePacketReader, SyncMessageReader, TimeTrackingPacketReader, TracingPacketReader

public interface MessageReader<H extends MessageHeader,​M extends Message>
  • Method Summary

    Modifier and Type Method Description
    default byte getMessageSequence()
    Get last message sequence number, as it was stored by readHeader().
    default void pushMessageListener​(MessageListener<M> l)
    Queue a MessageListener to receive messages delivered asynchronously.
    H readHeader()
    Read the next message header from server, possibly blocking indefinitely until the message is received.
    default M readMessage​(java.util.Optional<M> reuse, int expectedType)
    Read message from server into to the given Message instance or into the new one if not present.
    M readMessage​(java.util.Optional<M> reuse, H header)
    Read message from server into to the given Message instance or into the new one if not present.
    default void resetMessageSequence()
    Set stored message sequence number to 0.
    default void start()
    Start reading messages reader from the provided channel.
    default void stopAfterNextMessage()
    Signal to the reader that it should stop reading messages after reading the next message.
    default MessageReader<H,​M> undecorate()
    Return the previous MessageReader instance from the decorators chain or the current MessageReader if it is the first entry in a chain.
    default MessageReader<H,​M> undecorateAll()
    Return a MessageReader instance free of decorators.
  • Method Details

    • readHeader

      H readHeader() throws java.io.IOException
      Read the next message header from server, possibly blocking indefinitely until the message is received.
      Returns:
      MessageHeader of the next message
      Throws:
      java.io.IOException - if an error occurs
    • readMessage

      M readMessage​(java.util.Optional<M> reuse, H header) throws java.io.IOException
      Read message from server into to the given Message instance or into the new one if not present. For asynchronous channel it synchronously reads the next message in the stream, blocking until the message is read fully. Could throw CJCommunicationsException wrapping an IOException during read or parse
      Parameters:
      reuse - Message object to reuse. May be ignored by implementation.
      header - MessageHeader instance
      Returns:
      Message instance
      Throws:
      java.io.IOException - if an error occurs
    • readMessage

      default M readMessage​(java.util.Optional<M> reuse, int expectedType) throws java.io.IOException
      Read message from server into to the given Message instance or into the new one if not present. For asynchronous channel it synchronously reads the next message in the stream, blocking until the message is read fully. Could throw WrongArgumentException if the expected message type is not the next message (exception will be thrown in *caller* context).
      Parameters:
      reuse - Message object to reuse. May be ignored by implementation.
      expectedType - Expected type of message.
      Returns:
      Message instance
      Throws:
      java.io.IOException - if an error occurs
    • pushMessageListener

      default void pushMessageListener​(MessageListener<M> l)
      Queue a MessageListener to receive messages delivered asynchronously.
      Parameters:
      l - MessageListener
    • getMessageSequence

      default byte getMessageSequence()
      Get last message sequence number, as it was stored by readHeader().
      Returns:
      number
    • resetMessageSequence

      default void resetMessageSequence()
      Set stored message sequence number to 0.
    • undecorateAll

      default MessageReader<H,​M> undecorateAll()
      Return a MessageReader instance free of decorators.
      Returns:
      MessageReader
    • undecorate

      default MessageReader<H,​M> undecorate()
      Return the previous MessageReader instance from the decorators chain or the current MessageReader if it is the first entry in a chain.
      Returns:
      MessageReader
    • start

      default void start()
      Start reading messages reader from the provided channel.
    • stopAfterNextMessage

      default void stopAfterNextMessage()
      Signal to the reader that it should stop reading messages after reading the next message.