Package kilim.nio

Class EndPoint


  • public class EndPoint
    extends java.lang.Object
    The EndPoint represents an open socket connection. It is a wrapper over a non-blocking socket (channel) and belongs to a 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 
      Modifier and Type Class Description
      private static class  EndPoint.Sched  
    • 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.IOException close()
      Deprecated.
      in a future version, this method will no longer print the stack trace
      (package private) java.io.IOException close2()
      Close the endpoint
      java.nio.channels.SocketChannel dataChannel()  
      java.nio.ByteBuffer fill​(java.nio.ByteBuffer buf, int atleastN)
      Read atleastN bytes more into the buffer if there's space.
      java.nio.ByteBuffer fillMessage​(java.nio.ByteBuffer bb, int lengthLength, boolean lengthIncludesItself)
      Reads a length-prefixed message in its entirety.
      void pauseUntilAcceptable()  
      void pauseUntilReadable()  
      void pauseUntilWritable()  
      void write​(java.nio.ByteBuffer buf)
      Write buf.remaining() bytes to dataChannel().
      void write​(java.nio.channels.FileChannel fc, long start, long length)
      Write a file to the endpoint using FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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()
    • 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,
                          Pausable
        Write buf.remaining() bytes to dataChannel().
        Throws:
        java.io.IOException
        Pausable
      • fill

        public java.nio.ByteBuffer fill​(java.nio.ByteBuffer buf,
                                        int atleastN)
                                 throws java.io.IOException,
                                        Pausable
        Read atleastN bytes 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 filled
        atleastN - At least this many bytes to be read.
        Throws:
        java.io.IOException
        Pausable
      • fillMessage

        public java.nio.ByteBuffer fillMessage​(java.nio.ByteBuffer bb,
                                               int lengthLength,
                                               boolean lengthIncludesItself)
                                        throws java.io.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:
        java.io.IOException
        Pausable
      • pauseUntilReadable

        public void pauseUntilReadable()
                                throws Pausable,
                                       java.io.IOException
        Throws:
        Pausable
        java.io.IOException
      • pauseUntilWritable

        public void pauseUntilWritable()
                                throws Pausable,
                                       java.io.IOException
        Throws:
        Pausable
        java.io.IOException
      • pauseUntilAcceptable

        public void pauseUntilAcceptable()
                                  throws Pausable,
                                         java.io.IOException
        Throws:
        Pausable
        java.io.IOException
      • write

        public void write​(java.nio.channels.FileChannel fc,
                          long start,
                          long length)
                   throws java.io.IOException,
                          Pausable
        Write a file to the endpoint using FileChannel.transferTo(long, long, java.nio.channels.WritableByteChannel)
        Parameters:
        fc - FileChannel to copy to endpoint
        start - Start offset
        length - Number of bytes to be written
        Throws:
        java.io.IOException
        Pausable
      • 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 trace
        Close the endpoint, printing and returning and any IOException