Package io.sisu.nng.aio
Class Aio
- java.lang.Object
-
- io.sisu.nng.aio.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
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.concurrent.atomic.AtomicIntegercreatedstatic java.util.concurrent.atomic.AtomicIntegerfreedstatic intTIMEOUT_DEFAULT_DURATIONstatic intTIMEOUT_INFINITE
-
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 voidassertSuccessful()Check that the latest Aio operation was successful.booleanbegin()voidcancel()Abort the current Aio operation, calling theAioCallbackif present.voidfinish(int err)protected voidfree()Free this Aio and set it invalid.AioPointergetAioPointer()MessagegetMessage()Get aMessageset on the Aio, most likely as part of a receive operation.java.lang.StringgetOutputAsString(int index)intgetResult()voidreceive(Socket socket)Perform an asynchronous receiving operation on the given Socket.voidsend(Socket socket)Perform an asynchronous sending operation on the given Socket.voidsetMessage(Message msg)Set aMessageon the Aio, usually used prior to a send operation.voidsetOutput(int index, java.nio.ByteBuffer buffer)voidsetTimeoutMillis(int timeoutMillis)Set a timeout for asynchronous operations by the Aio.voidsleep(int millis)Perform an asynchronous delay, causing the Aio'sAioCallbackto fire after the provided timeout.voidwaitForFinish()Wait for the current Aio operation to complete.
-
-
-
Field Detail
-
created
public static java.util.concurrent.atomic.AtomicInteger created
-
freed
public static java.util.concurrent.atomic.AtomicInteger freed
-
TIMEOUT_INFINITE
public static final int TIMEOUT_INFINITE
- See Also:
- Constant Field Values
-
TIMEOUT_DEFAULT_DURATION
public static final int TIMEOUT_DEFAULT_DURATION
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Aio
public Aio() throws NngExceptionAllocate 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)
-
setTimeoutMillis
public void setTimeoutMillis(int timeoutMillis)
Set a timeout for asynchronous operations by the Aio. Two special timeouts are available:TIMEOUT_INFINITE- no timeout will be usedTIMEOUT_DEFAULT_DURATION- the "default" socket timeout will be used
- Parameters:
timeoutMillis- the timeout in milliseconds.
-
cancel
public void cancel()
Abort the current Aio operation, calling theAioCallbackif 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 anAioCallbackis 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 NngExceptionCheck that the latest Aio operation was successful. If not, throw an exception with the error.- Specified by:
assertSuccessfulin interfaceAioProxy- Throws:
NngException- if the latest Aio operation failed
-
getAioPointer
public AioPointer getAioPointer()
-
setMessage
public void setMessage(Message msg)
Set aMessageon the Aio, usually used prior to a send operation.- Specified by:
setMessagein interfaceAioProxy- Parameters:
msg- theMessageto set
-
getMessage
public Message getMessage()
Get aMessageset on the Aio, most likely as part of a receive operation.- Specified by:
getMessagein interfaceAioProxy- Returns:
- the
Message, null if noMessageis set
-
sleep
public void sleep(int millis)
Perform an asynchronous delay, causing the Aio'sAioCallbackto fire after the provided timeout.
-
send
public void send(Socket socket)
Perform an asynchronous sending operation on the given Socket. The Message to send must have been previously set viasetMessage(Message). Note: if you're looking for asynchronous Socket usage, use a Context instead.
-
receive
public void receive(Socket socket)
Perform an asynchronous receiving operation on the given Socket.
-
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.
-
-