java.lang.Object
net.i2p.router.transport.udp.MessageReceiver

class MessageReceiverextends Object
Pull fully completed fragments off the InboundMessageFragments queue, parse 'em into I2NPMessages, and stick them on the InNetMessagePool by way of the UDPTransport.

Supports dynamic thread count adjustment via setThreadCount(int).

  • Field Details

    • _context

      private final RouterContext _context
      The router context.
    • _log

      private final Log _log
      Logger for the message receiver.
    • _transport

      private final UDPTransport _transport
      The transport the messages belong to.
    • _banLogger

      private final BanLogger _banLogger
      Logger recording the banned peers.
    • _completeMessages

      private final BlockingQueue<InboundMessageState> _completeMessages
      List of messages (InboundMessageState) fully received but not interpreted yet.
    • _alive

      private volatile boolean _alive
      True while the receiver threads keep running.
    • _threadCount

      private static volatile int _threadCount
      Number of receiver threads.
    • _activeRunners

      private final AtomicInteger _activeRunners
      Receiver threads currently reading.
    • _processingCount

      private final AtomicInteger _processingCount
      Messages being processed right now.
    • POISON_IMS

      private static final long POISON_IMS
      Queue entry that stops a receiver thread.
      See Also:
    • MIN_THREADS

      private static final int MIN_THREADS
      Fewest receiver threads allowed.
      See Also:
    • MAX_THREADS

      private static final int MAX_THREADS
      Most receiver threads allowed.
      See Also:
    • _threadNum

      private static final AtomicInteger _threadNum
      Sequence number of the receiver threads.
  • Constructor Details

    • MessageReceiver

      public MessageReceiver(RouterContext ctx, UDPTransport transport)
      Creates the completed-message queue, sized from available memory, and registers its rate stats.
      Parameters:
      ctx - the router context, used for the log, the rate stats, and peer banning
      transport - the UDP transport that parsed messages are handed back to
  • Method Details

    • getThreadCount

      public static int getThreadCount()
      Returns the current target thread count.
      Returns:
      the thread count
    • getQueueSize

      public int getQueueSize()
      Returns the current queue depth of the message receiver.
      Returns:
      the queue size
    • getProcessingCount

      public int getProcessingCount()
      Returns the number of runners actively processing messages (not parked on take()).
      Returns:
      the processing count
    • getUtilization

      public double getUtilization()
      Message receiver pool utilization as a ratio (0.0-1.0). Returns NaN if not started.
      Returns:
      in-flight messages divided by receiver threads, or NaN before any thread is running
    • getQueueCapacity

      public int getQueueCapacity()
      Returns the maximum capacity of the message receiver queue.
      Returns:
      the queue capacity
    • setThreadCount

      public static void setThreadCount(int count)
      The target thread count. Takes effect immediately — excess threads will exit, new threads will be started if needed.
      Parameters:
      count - the requested number of processing threads, clamped into the range 2 to 16
    • startup

      public void startup()
      Start the receiver threads.
    • startRunner

      private void startRunner()
      Starts one more receiver thread if needed.
    • shutdown

      public void shutdown()
      Shut down the receiver threads.
    • adjustThreads

      void adjustThreads()
      Dynamically adjust thread count. If target is higher than current, start new runners. If lower, poison excess runners.
    • receiveMessage

      public void receiveMessage(InboundMessageState state)
      This queues the message for processing. Processing will call state.releaseResources(), do not access state after calling this. BLOCKING if queue is full.
      Parameters:
      state - the parsed message state; ownership passes to the receiving thread
    • loop

      void loop(I2NPMessageHandler handler)
      Main processing loop. Pulls completed messages from the queue, parses them into I2NPMessages, and delivers them to the transport.
      Parameters:
      handler - the message reader that turns queued bytes into I2NPMessage objects
    • readMessage

      private I2NPMessage readMessage(ByteArray buf, InboundMessageState state, I2NPMessageHandler handler)
      Assemble all the fragments into an I2NP message. This calls state.releaseResources(), do not access state after calling this.
      Parameters:
      buf - temp buffer for convenience
      state - the fragments to assemble, released by this call
      handler - the handler the assembled message is delivered to
      Returns:
      null on error