Class EndPoint
SessionTask. It serves as the bridge between the SessionTask and the NioSelectorScheduler, using
a pair of mailboxes for exchanging socket registration and socket ready events.
The other purpose of this class is to provide convenience methods that read from a socket into a bytebuffer, or write
from a bytebuffer to the socket. If the socket is not ready for business, the endpoint (and hence the task) simply
yields, without registering with the NioSelectorScheduler. The idea is to give the other runnable
tasks a chance to run before retrying the operation (on resumption); this avoids waking up the selector -- an
expensive operation -- as much as possible, and introduces a delay between retries. If, after a fixed number of times
(YIELD_COUNT), the operation still hasn't succeeded, the endpoint registers itself with the
NioSelectorScheduler, and waits for a socket-ready event from the selector.
This scheme is adaptive to load, in that the delay between retries is proportional to the number of runnable tasks.
Busy sockets tend to get serviced more often as the socket is always ready.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate NioSelectorSchedulerThe socket channel wrapped by the EndPoint.(package private) static final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclose()Deprecated.in a future version, this method will no longer print the stack trace(package private) IOExceptionclose2()Close the endpointfill(ByteBuffer buf, int atleastN) ReadatleastNbytes more into the buffer if there's space.fillMessage(ByteBuffer bb, int lengthLength, boolean lengthIncludesItself) Reads a length-prefixed message in its entirety.voidvoidvoidvoidwrite(ByteBuffer buf) Write buf.remaining() bytes to dataChannel().voidwrite(FileChannel fc, long start, long length) Write a file to the endpoint usingFileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel)
-
Field Details
-
YIELD_COUNT
static final int YIELD_COUNT -
sockch
The socket channel wrapped by the EndPoint. See #dataChannel() -
box
-
sched
-
-
Constructor Details
-
EndPoint
-
-
Method Details
-
dataChannel
-
write
Write buf.remaining() bytes to dataChannel().- Throws:
IOExceptionPausable
-
fill
ReadatleastNbytes more into the buffer if there's space. Otherwise, allocate a bigger buffer that'll accomodate the earlier contents and atleastN more bytes.- Parameters:
buf- ByteBuffer to be filledatleastN- At least this many bytes to be read.- Throws:
IOExceptionPausable
-
fillMessage
public ByteBuffer fillMessage(ByteBuffer bb, int lengthLength, boolean lengthIncludesItself) throws IOException, Pausable Reads a length-prefixed message in its entirety.- Parameters:
bb- The bytebuffer to fill, assuming there is sufficient space (including the bytes for the length). Otherwise, the contents are copied into a sufficiently big buffer, and the new buffer is returned.lengthLength- The number of bytes occupied by the length. Must be 1, 2 or 4, assumed to be in big-endian order.lengthIncludesItself- true if the packet length includes lengthLength- Returns:
- the buffer bb passed in if the message fits or a new buffer. Either way, the buffer returned has the entire message including the initial length prefix.
- Throws:
IOExceptionPausable
-
pauseUntilReadable
- Throws:
PausableIOException
-
pauseUntilWritable
- Throws:
PausableIOException
-
pauseUntilAcceptable
- Throws:
PausableIOException
-
write
Write a file to the endpoint usingFileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel)- Parameters:
fc- FileChannel to copy to endpointstart- Start offsetlength- Number of bytes to be written- Throws:
IOExceptionPausable
-
close2
IOException close2()Close the endpoint -
close
Deprecated.in a future version, this method will no longer print the stack traceClose the endpoint, printing and returning and any IOException
-