Module convex.peer
Package convex.net

Class MessageReceiver

java.lang.Object
convex.net.MessageReceiver

public class MessageReceiver extends Object
Class responsible for buffered accumulation of data received over a single connection. Data received must be passed in via @receiveFromChannel Passes any successfully received Messages to a specified Consumer, using the same thread on which the MessageReceiver was called.

"There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery"

- attributed to Mathias Verraes
  • Field Details

    • INITIAL_RECEIVE_BUFFER_SIZE

      public static final int INITIAL_RECEIVE_BUFFER_SIZE
      See Also:
  • Constructor Details

  • Method Details

    • getReceivedCount

      public long getReceivedCount()
      Get the number of messages received in total by this Receiver
      Returns:
      Count of messages received
    • receiveFromChannel

      public int receiveFromChannel(ReadableByteChannel chan) throws BadFormatException, convex.net.impl.HandlerException, IOException
      Handles receipt of bytes from a channel. Should be called with a ReadableByteChannel containing bytes received. May be called multiple times during receipt of a single message, i.e. can handle partial message receipt. Will consume enough bytes from channel to handle exactly one message. Bytes will be left unconsumed on the channel if more are available. This hopefully creates sufficient backpressure on clients sending a lot of messages.
      Parameters:
      chan - Byte channel
      Returns:
      The number of bytes read from the channel, or -1 if EOS
      Throws:
      IOException - If IO error occurs
      BadFormatException - If a bad encoding is received
      convex.net.impl.HandlerException - If the message handler throws an unexpected Exception
    • setHook

      public void setHook(Consumer<Message> hook)
      Sets an optional additional message receiver hook (for debugging / observability purposes)
      Parameters:
      hook - Hook to call when a message is received