java.lang.Object
net.i2p.router.InNetMessagePool
All Implemented Interfaces:
Service

public class InNetMessagePoolextends Object implements Service
Receives and dispatches inbound network messages with configurable processing modes. Handles message routing, job queuing, and concurrent processing for efficient inbound traffic management.

Dispatching can operate in different modes configured by transport layer: - Direct dispatch on caller thread (low latency), - Queued dispatch with threaded processing for higher concurrency.

Thread-safe queues from java.util.concurrent provide efficient, lock-free concurrency, minimizing blocking and wake overhead.

  • Field Details

    • _log

      private final Log _log
      Logger for the message pool.
    • _context

      private final RouterContext _context
      The router context.
    • _handlerJobBuilders

      private final HandlerJobBuilder[] _handlerJobBuilders
      Job builders, indexed by message type.
    • _pendingDataMessages

      private final LinkedBlockingQueue<InNetMessagePool.QueuedMessage> _pendingDataMessages
      Concurrent queue for pending tunnel data messages with their sender hashes. Used only when dispatching is not direct. Uses QueuedMessage wrapper to ensure message/sender pairing is preserved.
    • _pendingGatewayMessages

      private final LinkedBlockingQueue<I2NPMessage> _pendingGatewayMessages
      Messages waiting for a gateway job.
    • _shortCircuitDataJob

      private InNetMessagePool.SharedShortCircuitDataJob _shortCircuitDataJob
      Job handling messages on the caller's thread.
    • _shortCircuitGatewayJob

      private InNetMessagePool.SharedShortCircuitGatewayJob _shortCircuitGatewayJob
      Gateway job handling messages directly.
    • _alive

      private final AtomicBoolean _alive
      True while the pool is running.
    • _dispatchThreaded

      private final boolean _dispatchThreaded
      True to handle messages on job threads.
    • PROP_DISPATCH_THREADED

      private static final String PROP_DISPATCH_THREADED
      Config flags for dispatch mode.
      See Also:
    • DEFAULT_DISPATCH_THREADED

      private static final boolean DEFAULT_DISPATCH_THREADED
      Default for threaded dispatch.
      See Also:
    • PROP_DISPATCH_DIRECT

      private static final String PROP_DISPATCH_DIRECT
      Config key for direct dispatch.
      See Also:
    • DEFAULT_DISPATCH_DIRECT

      private static final boolean DEFAULT_DISPATCH_DIRECT
      Default for direct dispatch.
      See Also:
    • DISPATCH_DIRECT

      private final boolean DISPATCH_DIRECT
      True to handle some messages without a job.
    • MAX_I2NP_MESSAGE_TYPE

      private static final int MAX_I2NP_MESSAGE_TYPE
      Maximum valid I2NP message type.
      See Also:
    • MAX_DELIVERY_SKEW

      private static final int MAX_DELIVERY_SKEW
      Clock latitude allowed for delivery times.
      See Also:
    • RATES

      private static final long[] RATES
      Rate stat periods for the pool statistics.
  • Constructor Details

    • InNetMessagePool

      public InNetMessagePool(RouterContext context)
      Message pool for processing inbound I2NP messages. Initializes message queues according to configuration properties.
      Parameters:
      context - router context for environment access
  • Method Details

    • registerHandlerJobBuilder

      public HandlerJobBuilder registerHandlerJobBuilder(int i2npMessageType, HandlerJobBuilder builder)
      Registers or replaces a HandlerJobBuilder for a given message type.
      Parameters:
      i2npMessageType - I2NP message type (0 to MAX_I2NP_MESSAGE_TYPE)
      builder - handler job builder for this type
      Returns:
      previous builder registered for this type, or null if none
      Throws:
      ArrayIndexOutOfBoundsException - if message type out of bounds
    • add

      public int add(I2NPMessage messageBody, RouterIdentity fromRouter, Hash fromRouterHash)
      Adds a new inbound message to be processed.

      The message is either dispatched directly or queued for processing, depending on configuration. It supports detection of duplicates, expired messages, and dispatches messages to appropriate handlers or jobs.

      Parameters:
      messageBody - the inbound message to add, not null
      fromRouter - the router identity the message is from, may be null
      fromRouterHash - the hash of the router it is from, may be null (will be computed if so)
      Returns:
      -1 if message dropped due to validation or errors, 0 otherwise
    • add

      public int add(I2NPMessage messageBody, RouterIdentity fromRouter, Hash fromRouterHash, long msgIDBloomXor)
      Adds a new inbound message with optional bloom filter XOR id.
      Parameters:
      messageBody - message to add (non-null)
      fromRouter - source router identity (may be null)
      fromRouterHash - source router hash (may be null, computed if null)
      msgIDBloomXor - XOR value for message unique ID in bloom filter
      Returns:
      -1 if invalid or duplicate message, 0 otherwise
    • handleInvalidMessage

      private void handleInvalidMessage(I2NPMessage messageBody, String invalidReason, RouterIdentity fromRouter, Hash fromRouterHash, boolean doHistory, long msgIDBloomXor)
      Handles invalid or duplicate messages by logging, statistics updates, and history recording.
      Parameters:
      messageBody - the message being dropped
      invalidReason - lower-case reason, capitalized in the log; containing "expire" counts the drop as expired rather than duplicate
      fromRouter - the sender's identity, or null if unknown
      fromRouterHash - the sender's hash, used when fromRouter is null
      doHistory - true to record the drop in the message history
      msgIDBloomXor - the XOR applied to the message id for the history entry
    • processDatabaseStoreMessage

      private boolean processDatabaseStoreMessage(I2NPMessage messageBody, RouterIdentity fromRouter, Hash fromRouterHash, boolean doHistory)
      Processes DatabaseStoreMessage types by running or queueing corresponding jobs.
      Parameters:
      messageBody - the DatabaseStoreMessage to process
      fromRouter - the sender's identity, or null if unknown
      fromRouterHash - the sender's hash
      doHistory - whether to record in the message history, currently unused
      Returns:
      always true, a job was run or queued
    • handleDroppedUnmatchedMessage

      private void handleDroppedUnmatchedMessage(I2NPMessage messageBody, RouterIdentity fromRouter, Hash fromRouterHash, boolean doHistory)
      Handles dropping messages that are not matched or handled.
      Parameters:
      messageBody - the unmatched message being dropped
      fromRouter - the sender's identity, or null if unknown
      fromRouterHash - the sender's hash, used when fromRouter is null
      doHistory - true to record the drop in the message history
    • handleReplies

      public int handleReplies(I2NPMessage messageBody)
      Handles replies for outbound messages that correspond to this inbound message.
      Parameters:
      messageBody - inbound message for which replies are handled
      Returns:
      count of original messages replied to
    • shortCircuitTunnelGateway

      private void shortCircuitTunnelGateway(I2NPMessage messageBody)
      Shortcut dispatch tunnel gateway message either directly or queued.
      Parameters:
      messageBody - the tunnel gateway message
    • doShortCircuitTunnelGateway

      private void doShortCircuitTunnelGateway(I2NPMessage messageBody)
      Actual direct dispatch of a tunnel gateway message.
      Parameters:
      messageBody - the tunnel gateway message
    • shortCircuitTunnelData

      private void shortCircuitTunnelData(I2NPMessage messageBody, Hash from)
      Shortcut dispatch tunnel data message either directly or queued.
      Parameters:
      messageBody - the tunnel data message
      from - source router hash
    • doShortCircuitTunnelData

      private void doShortCircuitTunnelData(I2NPMessage messageBody, Hash from)
      Actual direct dispatch of a tunnel data message.
      Parameters:
      messageBody - the tunnel data message
      from - source router hash
    • renderStatusHTML

      public void renderStatusHTML(Writer out)
      Render the status of this message pool as HTML. Package-private for use by the router console.
      Specified by:
      renderStatusHTML in interface Service
      Parameters:
      out - the Writer to output HTML to
    • restart

      public void restart()
      Restart the message pool by shutting down and then starting up again. Clears all queued messages and reinitializes the dispatcher.
      Specified by:
      restart in interface Service
    • shutdown

      public void shutdown()
      Shutdown the message pool, stopping all dispatchers and clearing queued messages.
      Specified by:
      shutdown in interface Service
    • startup

      public void startup()
      Startup the message pool and, if configured, start dispatcher threads.
      Specified by:
      startup in interface Service