java.lang.Object
net.i2p.router.transport.udp.InboundMessageState
All Implemented Interfaces:
CDQEntry

class InboundMessageStateextends Object implements CDQEntry
Hold the raw data fragments of an inbound message.
  • Field Details

    • _context

      private final RouterContext _context
      The router context.
    • _log

      private final Log _log
      Logger for the inbound message.
    • _messageId

      private final long _messageId
      The message id being reassembled.
    • _from

      private final Hash _from
      The peer the message came from.
    • _fragments

      private ByteArray[] _fragments
      Fragments received, by fragment number.
    • _lastFragment

      private int _lastFragment
      Highest fragment number seen.
    • _receiveBegin

      private final long _receiveBegin
      When the first fragment arrived.
    • _enqueueTime

      private long _enqueueTime
      When the complete message is enqueued.
    • _completeSize

      private int _completeSize
      Total size of the reassembled message.
    • _released

      private volatile boolean _released
      True once the message was released to the job queue.
    • _receivedCount

      private int _receivedCount
      Fragments received so far.
    • lock

      private final Object lock
      Lock for the reassembly state.
    • MAX_RECEIVE_TIME

      private static final long MAX_RECEIVE_TIME
      Time allowed to reassemble a message.
      See Also:
    • MAX_FRAGMENTS

      public static final int MAX_FRAGMENTS
      Maximum number of fragments per message.
      See Also:
    • MAX_FRAGMENT_SIZE

      private static final int MAX_FRAGMENT_SIZE
      Largest fragment size, in bytes.
      See Also:
    • _fragmentCache

      private static final ByteCache _fragmentCache
      Pooled buffers for the fragments.
  • Constructor Details

    • InboundMessageState

      public InboundMessageState(RouterContext ctx, long messageId, Hash from)
      Creates a new InboundMessageState with no fragments.
      Parameters:
      ctx - the router context
      messageId - the ID of the message the fragments belong to
      from - the peer the fragments arrive from
    • InboundMessageState

      public InboundMessageState(RouterContext ctx, long messageId, Hash from, byte[] data, int off, int len, int fragmentNum, boolean isLast) throws DataFormatException
      Creates an InboundMessageState and stores the first received fragment.
      Parameters:
      ctx - the router context
      messageId - the ID of the message the fragments belong to
      from - the peer the fragments arrive from
      data - buffer holding the fragment payload
      off - index in data at which this fragment starts
      len - number of fragment bytes to copy out of data
      fragmentNum - zero-based position of this fragment in the message
      isLast - true if this fragment completes the message, so its total size is now known
      Throws:
      DataFormatException - if fragment is invalid
  • Method Details

    • receiveFragment

      public boolean receiveFragment(byte[] data, int off, int len, int fragmentNum, boolean isLast)
      Receives and stores a fragment.
      Parameters:
      data - the buffer holding the fragment payload
      off - index in data at which this fragment starts
      len - number of fragment bytes to copy out of data
      fragmentNum - zero-based position of this fragment in the message
      isLast - true if this fragment completes the message
      Returns:
      true if successful, false if corrupt or invalid
    • hasFragment

      public boolean hasFragment(int fragmentNum)
      Returns whether the specified fragment has been received.
      Parameters:
      fragmentNum - zero-based position of the fragment in the message
      Returns:
      whether fragment is present
    • isComplete

      public boolean isComplete()
      Returns true if all fragments up to last have been received.
      Returns:
      true if every fragment up to the last one has arrived
    • isExpired

      public boolean isExpired()
      Returns true if message has expired (received more than 10s ago).
      Returns:
      true if more than MAX_RECEIVE_TIME has elapsed since the first fragment
    • getLifetime

      public long getLifetime()
      Returns the message lifetime in milliseconds.
      Returns:
      milliseconds elapsed since the first fragment arrived
    • setEnqueueTime

      public void setEnqueueTime(long now)
      The enqueue time for queueing.
      Specified by:
      setEnqueueTime in interface CDQEntry
      Parameters:
      now - the enqueue timestamp
    • getEnqueueTime

      public long getEnqueueTime()
      The enqueue time.
      Specified by:
      getEnqueueTime in interface CDQEntry
      Returns:
      ms since epoch when the message was enqueued
    • drop

      public void drop()
      Drops and releases resources.
      Specified by:
      drop in interface CDQEntry
    • getFrom

      public Hash getFrom()
      The Hash of the sender.
      Returns:
      hash of the sending peer, as taken from the session
    • getMessageId

      public long getMessageId()
      The message ID.
      Returns:
      the 64-bit SSU2 message id
    • getCompleteSize

      public int getCompleteSize()
      The total size of the complete message in bytes.
      Returns:
      the complete size
      Throws:
      IllegalStateException - if message incomplete or released
    • createACKBitfield

      public ACKBitfield createACKBitfield()
      Creates a bitfield representing received fragments.
      Returns:
      a bitfield marking every fragment received so far
    • releaseResources

      public void releaseResources()
      Releases all cached fragments and marks this state as released.
    • isReleased

      public boolean isReleased()
      Whether the pooled fragment buffers have been returned already.
      Returns:
      true once releaseResources() has run on this state
    • getFragments

      public ByteArray[] getFragments()
      The array of fragments, throws if already released.
      Returns:
      the fragment array, indexed by fragment number
    • getFragmentCount

      public int getFragmentCount()
      Number of fragments received or expected.
      Returns:
      the fragment count
    • toString

      public String toString()
      A string summary of this message state.
      Overrides:
      toString in class Object