java.lang.Object
net.i2p.router.transport.udp.InboundMessageState
- All Implemented Interfaces:
CDQEntry
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classBitfield of the fragments received so far.Field Summary
Modifier and TypeFieldDescriptionprivate intTotal size of the reassembled message.private final RouterContextThe router context.private longWhen the complete message is enqueued.private static final ByteCachePooled buffers for the fragments.private ByteArray[]Fragments received, by fragment number.private final HashThe peer the message came from.private intHighest fragment number seen.private final LogLogger for the inbound message.private final longThe message id being reassembled.private final longWhen the first fragment arrived.private intFragments received so far.private booleanTrue once the message was released to the job queue.private final ObjectLock for the reassembly state.private static final intLargest fragment size, in bytes.static final intMaximum number of fragments per message.private static final longTime allowed to reassemble a message.Constructor Summary
ConstructorsConstructorDescriptionInboundMessageState(RouterContext ctx, long messageId, Hash from) Creates a new InboundMessageState with no fragments.InboundMessageState(RouterContext ctx, long messageId, Hash from, byte[] data, int off, int len, int fragmentNum, boolean isLast) Creates an InboundMessageState and stores the first received fragment.Method Summary
All MethodsModifier and TypeMethodDescriptionCreates a bitfield representing received fragments.voiddrop()Drops and releases resources.intThe total size of the complete message in bytes.longThe enqueue time.intNumber of fragments received or expected.The array of fragments, throws if already released.getFrom()The Hash of the sender.longReturns the message lifetime in milliseconds.longThe message ID.booleanhasFragment(int fragmentNum) Returns whether the specified fragment has been received.booleanReturns true if all fragments up to last have been received.booleanReturns true if message has expired (received more than 10s ago).booleanWhether the pooled fragment buffers have been returned already.booleanreceiveFragment(byte[] data, int off, int len, int fragmentNum, boolean isLast) Receives and stores a fragment.voidReleases all cached fragments and marks this state as released.voidsetEnqueueTime(long now) The enqueue time for queueing.toString()A string summary of this message state.
Field Details
_context
The router context._log
Logger for the inbound message._messageId
private final long _messageIdThe message id being reassembled._from
The peer the message came from._fragments
Fragments received, by fragment number._lastFragment
private int _lastFragmentHighest fragment number seen._receiveBegin
private final long _receiveBeginWhen the first fragment arrived._enqueueTime
private long _enqueueTimeWhen the complete message is enqueued._completeSize
private int _completeSizeTotal size of the reassembled message._released
private volatile boolean _releasedTrue once the message was released to the job queue._receivedCount
private int _receivedCountFragments received so far.lock
Lock for the reassembly state.MAX_RECEIVE_TIME
private static final long MAX_RECEIVE_TIMETime allowed to reassemble a message.- See Also:
MAX_FRAGMENTS
public static final int MAX_FRAGMENTSMaximum number of fragments per message.- See Also:
MAX_FRAGMENT_SIZE
private static final int MAX_FRAGMENT_SIZELargest fragment size, in bytes.- See Also:
_fragmentCache
Pooled buffers for the fragments.
Constructor Details
InboundMessageState
Creates a new InboundMessageState with no fragments.- Parameters:
ctx- the router contextmessageId- the ID of the message the fragments belong tofrom- 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 contextmessageId- the ID of the message the fragments belong tofrom- the peer the fragments arrive fromdata- buffer holding the fragment payloadoff- index in data at which this fragment startslen- number of fragment bytes to copy out of datafragmentNum- zero-based position of this fragment in the messageisLast- 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 payloadoff- index in data at which this fragment startslen- number of fragment bytes to copy out of datafragmentNum- zero-based position of this fragment in the messageisLast- 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:
setEnqueueTimein interfaceCDQEntry- Parameters:
now- the enqueue timestamp
getEnqueueTime
public long getEnqueueTime()The enqueue time.- Specified by:
getEnqueueTimein interfaceCDQEntry- Returns:
- ms since epoch when the message was enqueued
drop
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
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
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