Class Aio

  • All Implemented Interfaces:
    AioProxy

    public class Aio
    extends java.lang.Object
    implements AioProxy
    Asynchronous IO (AIO) handle used primarily by Contexts. This class can be used directly, but is designed to be most useful as part of a Context. Most developers will interact with the Aio instance via its AioProxy interface and not the Aio instance direactly. In general, most methods are simply wrappers around the native NNG aio functions for manipulating the aio. However, the introduction of the AioCallback approach provides a Java-friendly way to provide callback event handlers. @see io.sisu.nng.aio.Context
    • Constructor Summary

      Constructors 
      Constructor Description
      Aio()
      Allocate a new Aio with a no-op callback.
      Aio​(AioCallback cb)
      Allocate a new Aio with the given AioCallback for processing events.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void assertSuccessful()
      Check that the latest Aio operation was successful.
      boolean begin()  
      void cancel()
      Abort the current Aio operation, calling the AioCallback if present.
      void finish​(int err)  
      protected void free()
      Free this Aio and set it invalid.
      AioPointer getAioPointer()  
      Message getMessage()
      Get a Message set on the Aio, most likely as part of a receive operation.
      java.lang.String getOutputAsString​(int index)  
      int getResult()  
      void receive​(Socket socket)
      Perform an asynchronous receiving operation on the given Socket.
      void send​(Socket socket)
      Perform an asynchronous sending operation on the given Socket.
      void setMessage​(Message msg)
      Set a Message on the Aio, usually used prior to a send operation.
      void setOutput​(int index, java.nio.ByteBuffer buffer)  
      void setTimeoutMillis​(int timeoutMillis)
      Set a timeout for asynchronous operations by the Aio.
      void sleep​(int millis)
      Perform an asynchronous delay, causing the Aio's AioCallback to fire after the provided timeout.
      void waitForFinish()
      Wait for the current Aio operation to complete.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • created

        public static java.util.concurrent.atomic.AtomicInteger created
      • freed

        public static java.util.concurrent.atomic.AtomicInteger freed
      • TIMEOUT_DEFAULT_DURATION

        public static final int TIMEOUT_DEFAULT_DURATION
        See Also:
        Constant Field Values
    • Constructor Detail

      • Aio

        public Aio()
            throws NngException
        Allocate a new Aio with a no-op callback.
        Throws:
        NngException - if allocation fails
      • Aio

        public Aio​(AioCallback cb)
            throws NngException
        Allocate a new Aio with the given AioCallback for processing events.
        Parameters:
        cb - an instance of AioCallback for handling events on this Aio
        Throws:
        NngException - if allocation fails
    • Method Detail

      • setOutput

        public void setOutput​(int index,
                              java.nio.ByteBuffer buffer)
      • getOutputAsString

        public java.lang.String getOutputAsString​(int index)
      • begin

        public boolean begin()
        Specified by:
        begin in interface AioProxy
      • setTimeoutMillis

        public void setTimeoutMillis​(int timeoutMillis)
        Set a timeout for asynchronous operations by the Aio. Two special timeouts are available: The value is persistent and will be used for all subsequent asynchronous operations until changed.
        Parameters:
        timeoutMillis - the timeout in milliseconds.
      • finish

        public void finish​(int err)
        Specified by:
        finish in interface AioProxy
      • cancel

        public void cancel()
        Abort the current Aio operation, calling the AioCallback if present. (If so, the callback will be notified the operation completed with a cancellation error.)
      • getResult

        public int getResult()
      • waitForFinish

        public void waitForFinish()
        Wait for the current Aio operation to complete. If an AioCallback is in use by the Aio, this call will return after the callback completes execution. If there's no pending operation, it will return immediately.
      • assertSuccessful

        public void assertSuccessful()
                              throws NngException
        Check that the latest Aio operation was successful. If not, throw an exception with the error.
        Specified by:
        assertSuccessful in interface AioProxy
        Throws:
        NngException - if the latest Aio operation failed
      • getAioPointer

        public AioPointer getAioPointer()
      • setMessage

        public void setMessage​(Message msg)
        Set a Message on the Aio, usually used prior to a send operation.
        Specified by:
        setMessage in interface AioProxy
        Parameters:
        msg - the Message to set
      • sleep

        public void sleep​(int millis)
        Perform an asynchronous delay, causing the Aio's AioCallback to fire after the provided timeout.
        Specified by:
        sleep in interface AioProxy
        Parameters:
        millis - time to sleep the Aio in milliseconds
      • send

        public void send​(Socket socket)
        Perform an asynchronous sending operation on the given Socket. The Message to send must have been previously set via setMessage(Message). Note: if you're looking for asynchronous Socket usage, use a Context instead.
        Specified by:
        send in interface AioProxy
        Parameters:
        socket - the Socket to attempt the send operation on
      • receive

        public void receive​(Socket socket)
        Perform an asynchronous receiving operation on the given Socket.
        Specified by:
        receive in interface AioProxy
        Parameters:
        socket - the Socket to attempt the receive operation on
      • free

        protected void free()
        Free this Aio and set it invalid. If an operation is in progress, the operation will be cancelled and this method will block until it's completely canceled. The backing native memory for the nng_aio will be freed. The Aio must not be reused at this point.