java.lang.Object
net.i2p.router.transport.udp.MessageReceiver
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).
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classThread taking messages off the queue.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicIntegerReceiver threads currently reading.private booleanTrue while the receiver threads keep running.private final BanLoggerLogger recording the banned peers.private final BlockingQueue<InboundMessageState> List of messages (InboundMessageState) fully received but not interpreted yet.private final RouterContextThe router context.private final LogLogger for the message receiver.private final AtomicIntegerMessages being processed right now.private static intNumber of receiver threads.private static final AtomicIntegerSequence number of the receiver threads.private final UDPTransportThe transport the messages belong to.private static final intMost receiver threads allowed.private static final intFewest receiver threads allowed.private static final longQueue entry that stops a receiver thread.Constructor Summary
ConstructorsConstructorDescriptionMessageReceiver(RouterContext ctx, UDPTransport transport) Creates the completed-message queue, sized from available memory, and registers its rate stats.Method Summary
Modifier and TypeMethodDescription(package private) voidDynamically adjust thread count.intReturns the number of runners actively processing messages (not parked on take()).intReturns the maximum capacity of the message receiver queue.intReturns the current queue depth of the message receiver.static intReturns the current target thread count.doubleMessage receiver pool utilization as a ratio (0.0-1.0).(package private) voidloop(I2NPMessageHandler handler) Main processing loop.private I2NPMessagereadMessage(ByteArray buf, InboundMessageState state, I2NPMessageHandler handler) Assemble all the fragments into an I2NP message.voidThis queues the message for processing.static voidsetThreadCount(int count) The target thread count.voidshutdown()Shut down the receiver threads.private voidStarts one more receiver thread if needed.voidstartup()Start the receiver threads.
Field Details
_context
The router context._log
Logger for the message receiver._transport
The transport the messages belong to._banLogger
Logger recording the banned peers._completeMessages
List of messages (InboundMessageState) fully received but not interpreted yet._alive
private volatile boolean _aliveTrue while the receiver threads keep running._threadCount
private static volatile int _threadCountNumber of receiver threads._activeRunners
Receiver threads currently reading._processingCount
Messages being processed right now.POISON_IMS
private static final long POISON_IMSQueue entry that stops a receiver thread.- See Also:
MIN_THREADS
MAX_THREADS
_threadNum
Sequence number of the receiver threads.
Constructor Details
MessageReceiver
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 banningtransport- 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
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
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 conveniencestate- the fragments to assemble, released by this callhandler- the handler the assembled message is delivered to- Returns:
- null on error