Interface Selectable

  • All Superinterfaces:
    Extendable, ReactorChild
    All Known Implementing Classes:
    SelectableImpl

    public interface Selectable
    extends ReactorChild, Extendable
    An entity that can be multiplexed using a Selector.

    Every selectable is associated with exactly one SelectableChannel. Selectables may be interested in three kinds of events: read events, write events, and timer events. A selectable will express its interest in these events through the isReading(), isWriting(), and getDeadline() methods.

    When a read, write, or timer event occurs, the selectable must be notified by calling readable(), writeable(), or expired() as appropriate. Once a selectable reaches a terminal state (see isTerminal(), it will never be interested in events of any kind. When this occurs it should be removed from the Selector and discarded using free().

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static interface  Selectable.Callback
      A callback that can be passed to the various "on" methods of the selectable - to allow code to be run when the selectable becomes ready for the associated operation.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void error()
      Notify the selectable that an error has occurred.
      void expired()
      Notify the selectable that it has expired.
      void free()
      Notify the selectable that it has been free'd.
      java.nio.channels.SelectableChannel getChannel()  
      long getDeadline()  
      Reactor getReactor()  
      boolean isReading()  
      boolean isRegistered()
      Check if a selectable is registered.
      boolean isTerminal()  
      boolean isWriting()  
      void onError​(Selectable.Callback runnable)
      Registers a callback that will be run when the selectable is notified of an error.
      void onExpired​(Selectable.Callback runnable)
      Registers a callback that will be run when the selectable expires.
      void onFree​(Selectable.Callback runnable)
      Registers a callback that will be run when the selectable is notified that it has been free'd.
      void onReadable​(Selectable.Callback runnable)
      Registers a callback that will be run when the selectable becomes ready for reading.
      void onRelease​(Selectable.Callback runnable)
      Registers a callback that will be run when the selectable is notified that it has been released.
      void onWritable​(Selectable.Callback runnable)
      Registers a callback that will be run when the selectable becomes ready for writing.
      void readable()
      Notify the selectable that the underlying SelectableChannel is ready for a read operation.
      void release()
      Notify the selectable that it has been released.
      void setChannel​(java.nio.channels.SelectableChannel channel)
      Associates a SelectableChannel with this selector.
      void setCollector​(Collector collector)
      Configure a selectable with a set of callbacks that emit readable, writable, and expired events into the supplied collector.
      void setDeadline​(long deadline)
      Sets the value that will be returned by getDeadline().
      void setReading​(boolean reading)
      Sets the value that will be returned by isReading().
      void setRegistered​(boolean registered)
      Set the registered flag for a selectable.
      void setWriting​(boolean writing)
      Sets the value that will be returned by isWriting().
      void terminate()
      Terminates the selectable.
      void writeable()
      Notify the selectable that the underlying SelectableChannel is ready for a write operation.
    • Method Detail

      • isReading

        boolean isReading()
        Returns:
        true if the selectable is interested in receiving notification (via the readable() method that indicate that the associated SelectableChannel has data ready to be read from it.
      • isWriting

        boolean isWriting()
        Returns:
        true if the selectable is interested in receiving notifications (via the writeable() method that indicate that the associated SelectableChannel is ready to be written to.
      • getDeadline

        long getDeadline()
        Returns:
        a deadline after which this selectable can expect to receive a notification (via the expired() method that indicates that the deadline has past. The deadline is expressed in the same format as System.currentTimeMillis(). Returning a deadline of zero (or a negative number) indicates that the selectable does not wish to be notified of expiry.
      • setReading

        void setReading​(boolean reading)
        Sets the value that will be returned by isReading().
        Parameters:
        reading -
      • setWriting

        void setWriting​(boolean writing)
        Sets the value that will be returned by isWriting().
        Parameters:
        writing -
      • setDeadline

        void setDeadline​(long deadline)
        Sets the value that will be returned by getDeadline().
        Parameters:
        deadline -
      • onReadable

        void onReadable​(Selectable.Callback runnable)
        Registers a callback that will be run when the selectable becomes ready for reading.
        Parameters:
        runnable - the callback to register. Any previously registered callback will be replaced.
      • onWritable

        void onWritable​(Selectable.Callback runnable)
        Registers a callback that will be run when the selectable becomes ready for writing.
        Parameters:
        runnable - the callback to register. Any previously registered callback will be replaced.
      • onExpired

        void onExpired​(Selectable.Callback runnable)
        Registers a callback that will be run when the selectable expires.
        Parameters:
        runnable - the callback to register. Any previously registered callback will be replaced.
      • onError

        void onError​(Selectable.Callback runnable)
        Registers a callback that will be run when the selectable is notified of an error.
        Parameters:
        runnable - the callback to register. Any previously registered callback will be replaced.
      • onRelease

        void onRelease​(Selectable.Callback runnable)
        Registers a callback that will be run when the selectable is notified that it has been released.
        Parameters:
        runnable - the callback to register. Any previously registered callback will be replaced.
      • onFree

        void onFree​(Selectable.Callback runnable)
        Registers a callback that will be run when the selectable is notified that it has been free'd.
        Parameters:
        runnable - the callback to register. Any previously registered callback will be replaced.
      • readable

        void readable()
        Notify the selectable that the underlying SelectableChannel is ready for a read operation.
      • writeable

        void writeable()
        Notify the selectable that the underlying SelectableChannel is ready for a write operation.
      • expired

        void expired()
        Notify the selectable that it has expired.
      • error

        void error()
        Notify the selectable that an error has occurred.
      • release

        void release()
        Notify the selectable that it has been released.
      • free

        void free()
        Notify the selectable that it has been free'd.
        Specified by:
        free in interface ReactorChild
      • setChannel

        void setChannel​(java.nio.channels.SelectableChannel channel)
        Associates a SelectableChannel with this selector.
        Parameters:
        channel -
      • getChannel

        java.nio.channels.SelectableChannel getChannel()
        Returns:
        the SelectableChannel associated with this selector.
      • isRegistered

        boolean isRegistered()
        Check if a selectable is registered. This can be used for tracking whether a given selectable has been registerd with an external event loop.

        Note: the reactor code, currently, does not use this flag.

        Returns:
        trueif the selectable is registered.
      • setRegistered

        void setRegistered​(boolean registered)
        Set the registered flag for a selectable.

        Note: the reactor code, currently, does not use this flag.

        Parameters:
        registered - the value returned by isRegistered()
      • setCollector

        void setCollector​(Collector collector)
        Configure a selectable with a set of callbacks that emit readable, writable, and expired events into the supplied collector.
        Parameters:
        collector -
      • getReactor

        Reactor getReactor()
        Returns:
        the reactor to which this selectable is a child.
      • terminate

        void terminate()
        Terminates the selectable. Once a selectable reaches a terminal state it will never be interested in events of any kind.
      • isTerminal

        boolean isTerminal()
        Returns:
        true if the selectable has reached a terminal state.