java.lang.Object
net.i2p.router.tunnel.TrivialPreprocessor
- All Implemented Interfaces:
TunnelGateway.QueuePreprocessor
- Direct Known Subclasses:
BatchedPreprocessor
Do the simplest thing possible for preprocessing - for each message available, turn it into the minimum number of fragmented preprocessed blocks, sending each of those out. This does not coalesce message fragments or delay for more optimal throughput.
See FragmentHandler Javadoc for tunnel message fragment format
Not instantiated directly except in unit tests; see BatchedPreprocessor.
Field Summary
Modifier and TypeFieldDescriptionprotected final RouterContextThe router context.protected static final ByteCacheHere in tunnels, we take from the cache but never add to it.protected final LogThe logger.protected static final intLength of the IV.private static final byteIs this the first fragment of a fragmented message?private static final byteIs this a follow-up byte?private static final byteType bits of a router fragmentprivate static final byteType bits of a tunnel fragmentstatic final intSize of preprocessed data blocks.Constructor Summary
ConstructorsConstructorDescriptionBinds the router context and initializes the log.Method Summary
Modifier and TypeMethodDescriptionprivate voidfillRandomNonZero(byte[] b, int off, int len) Efficiently fill with nonzero random data Don't waste too much entropy or call random() too often.longHow long to wait before flushing.protected static intgetInstructionAugmentationSize(PendingGatewayMessage msg, int offset, int instructionsSize) Get the augmentation size for instructions if fragmentation is needed.protected static intCalculate the size of the instructions for a pending message.protected voidnotePreprocessing(long messageId, int numFragments, int totalLength, List<Long> messageIds, String msg) Note preprocessing statistics.protected voidpreprocess(byte[] fragments, int fragmentLength) Wrap the preprocessed fragments with the necessary padding / checksums to act as a tunnel message.booleanpreprocessQueue(List<PendingGatewayMessage> pending, TunnelGateway.Sender sender, TunnelGateway.Receiver rec) Return true if there were messages remaining, and we should queue up a delayed flush to clear themprotected intwriteFirstFragment(PendingGatewayMessage msg, byte[] target, int offset) Write the first fragment of a tunnel message.protected intwriteSubsequentFragment(PendingGatewayMessage msg, byte[] target, int offset) Write a subsequent fragment of a tunnel message.
Field Details
_context
The router context._log
The logger.PREPROCESSED_SIZE
public static final int PREPROCESSED_SIZESize of preprocessed data blocks.- See Also:
IV_SIZE
_dataCache
Here in tunnels, we take from the cache but never add to it. In other words, we take advantage of other places in the router also using 1024-byte ByteCaches (since ByteCache only maintains once instance for each size) Used in BatchedPreprocessor; see add'l comments there.MASK_IS_SUBSEQUENT
MASK_FRAGMENTED
private static final byte MASK_FRAGMENTEDIs this the first fragment of a fragmented message?- See Also:
MASK_TUNNEL
MASK_ROUTER
Constructor Details
TrivialPreprocessor
Binds the router context and initializes the log.- Parameters:
ctx- the router context
Method Details
getDelayAmount
public long getDelayAmount()How long to wait before flushing.- Specified by:
getDelayAmountin interfaceTunnelGateway.QueuePreprocessor- Returns:
- delay in milliseconds
preprocessQueue
public boolean preprocessQueue(List<PendingGatewayMessage> pending, TunnelGateway.Sender sender, TunnelGateway.Receiver rec) Return true if there were messages remaining, and we should queue up a delayed flush to clear themNOTE: Unused here, see BatchedPreprocessor override, super is not called.
- Specified by:
preprocessQueuein interfaceTunnelGateway.QueuePreprocessor- Parameters:
pending- list of Pending objects for messages either unsent or partly sent. This list should be update with any values removed (the preprocessor owns the lock) Messages are not removed from the list until actually sent. The status of unsent and partially-sent messages is stored in the Pending structure.sender- the sender to hand the encrypted data torec- the receiver to accept encrypted data- Returns:
- true if we should delay before preprocessing again
notePreprocessing
protected void notePreprocessing(long messageId, int numFragments, int totalLength, List<Long> messageIds, String msg) Note preprocessing statistics. No-op - intentionally empty.- Parameters:
messageId- the message this fragment carries data fornumFragments- the fragment number within that messagetotalLength- the total length of the message the fragment belongs tomessageIds- the message ids of everything coalesced into this fragmentmsg- a description of the flush event, for debugging output
preprocess
protected void preprocess(byte[] fragments, int fragmentLength) Wrap the preprocessed fragments with the necessary padding / checksums to act as a tunnel message.- Parameters:
fragments- the fragment datafragmentLength- fragments[0:fragmentLength] is used
fillRandomNonZero
private void fillRandomNonZero(byte[] b, int off, int len) Efficiently fill with nonzero random data Don't waste too much entropy or call random() too often.- Parameters:
b- the buffer to filloff- the index in b of the first byte to filllen- the number of bytes to fill, none of which will be zero
writeFirstFragment
Write the first fragment of a tunnel message.- Parameters:
msg- the pending gateway messagetarget- the buffer to write the control byte and fragment body intooffset- the starting offset- Returns:
- the new offset after writing
writeSubsequentFragment
Write a subsequent fragment of a tunnel message.- Parameters:
msg- the pending gateway messagetarget- the buffer to write the control byte and fragment body intooffset- the starting offset- Returns:
- the new offset after writing
getInstructionsSize
Calculate the size of the instructions for a pending message. Does NOT include 4 for the message ID if the message will be fragmented; callgetInstructionAugmentationSize(PendingGatewayMessage, int, int)for that.- Parameters:
msg- the pending gateway message- Returns:
- generally 3 or 35 or 39 for first fragment, 7 for subsequent fragments.
getInstructionAugmentationSize
protected static int getInstructionAugmentationSize(PendingGatewayMessage msg, int offset, int instructionsSize) Get the augmentation size for instructions if fragmentation is needed.- Parameters:
msg- the pending gateway messageoffset- the current offsetinstructionsSize- the current instructions size- Returns:
- 0 or 4