Class EndPoint
- java.lang.Object
-
- kilim.nio.EndPoint
-
public class EndPoint extends java.lang.ObjectThe EndPoint represents an open socket connection. It is a wrapper over a non-blocking socket (channel) and belongs to aSessionTask. It serves as the bridge between the SessionTask and theNioSelectorScheduler, 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 theNioSelectorScheduler, 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 Modifier and Type Class Description private static classEndPoint.Sched
-
Field Summary
Fields Modifier and Type Field Description private Mailbox<SockEvent>boxprivate NioSelectorSchedulerschedjava.nio.channels.spi.AbstractSelectableChannelsockchThe socket channel wrapped by the EndPoint.(package private) static intYIELD_COUNT
-
Constructor Summary
Constructors Constructor Description EndPoint(NioSelectorScheduler nio, java.nio.channels.spi.AbstractSelectableChannel ch)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.io.IOExceptionclose()Deprecated.in a future version, this method will no longer print the stack trace(package private) java.io.IOExceptionclose2()Close the endpointjava.nio.channels.SocketChanneldataChannel()java.nio.ByteBufferfill(java.nio.ByteBuffer buf, int atleastN)ReadatleastNbytes more into the buffer if there's space.java.nio.ByteBufferfillMessage(java.nio.ByteBuffer bb, int lengthLength, boolean lengthIncludesItself)Reads a length-prefixed message in its entirety.voidpauseUntilAcceptable()voidpauseUntilReadable()voidpauseUntilWritable()voidwrite(java.nio.ByteBuffer buf)Write buf.remaining() bytes to dataChannel().voidwrite(java.nio.channels.FileChannel fc, long start, long length)Write a file to the endpoint usingFileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel)
-
-
-
Field Detail
-
YIELD_COUNT
static final int YIELD_COUNT
-
sockch
public java.nio.channels.spi.AbstractSelectableChannel sockch
The socket channel wrapped by the EndPoint. See #dataChannel()
-
sched
private NioSelectorScheduler sched
-
-
Constructor Detail
-
EndPoint
public EndPoint(NioSelectorScheduler nio, java.nio.channels.spi.AbstractSelectableChannel ch)
-
-
Method Detail
-
dataChannel
public java.nio.channels.SocketChannel dataChannel()
-
write
public void write(java.nio.ByteBuffer buf) throws java.io.IOException, PausableWrite buf.remaining() bytes to dataChannel().- Throws:
java.io.IOExceptionPausable
-
fill
public java.nio.ByteBuffer fill(java.nio.ByteBuffer buf, int atleastN) throws java.io.IOException, PausableReadatleastNbytes 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:
java.io.IOExceptionPausable
-
fillMessage
public java.nio.ByteBuffer fillMessage(java.nio.ByteBuffer bb, int lengthLength, boolean lengthIncludesItself) throws java.io.IOException, PausableReads 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:
java.io.IOExceptionPausable
-
pauseUntilReadable
public void pauseUntilReadable() throws Pausable, java.io.IOException- Throws:
Pausablejava.io.IOException
-
pauseUntilWritable
public void pauseUntilWritable() throws Pausable, java.io.IOException- Throws:
Pausablejava.io.IOException
-
pauseUntilAcceptable
public void pauseUntilAcceptable() throws Pausable, java.io.IOException- Throws:
Pausablejava.io.IOException
-
write
public void write(java.nio.channels.FileChannel fc, long start, long length) throws java.io.IOException, PausableWrite 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:
java.io.IOExceptionPausable
-
close2
java.io.IOException close2()
Close the endpoint
-
close
@Deprecated public java.io.IOException close()
Deprecated.in a future version, this method will no longer print the stack traceClose the endpoint, printing and returning and any IOException
-
-