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.AtomicInteger
created
static java.util.concurrent.atomic.AtomicInteger
freed
static int
TIMEOUT_DEFAULT_DURATION
static int
TIMEOUT_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 void
assertSuccessful()
Check that the latest Aio operation was successful.boolean
begin()
void
cancel()
Abort the current Aio operation, calling theAioCallback
if present.void
finish(int err)
protected void
free()
Free this Aio and set it invalid.AioPointer
getAioPointer()
Message
getMessage()
Get aMessage
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 aMessage
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'sAioCallback
to fire after the provided timeout.void
waitForFinish()
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 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)
-
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 theAioCallback
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 anAioCallback
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 interfaceAioProxy
- Throws:
NngException
- if the latest Aio operation failed
-
getAioPointer
public AioPointer getAioPointer()
-
setMessage
public void setMessage(Message msg)
Set aMessage
on the Aio, usually used prior to a send operation.- Specified by:
setMessage
in interfaceAioProxy
- Parameters:
msg
- theMessage
to set
-
getMessage
public Message getMessage()
Get aMessage
set on the Aio, most likely as part of a receive operation.- Specified by:
getMessage
in interfaceAioProxy
- Returns:
- the
Message
, null if noMessage
is set
-
sleep
public void sleep(int millis)
Perform an asynchronous delay, causing the Aio'sAioCallback
to 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.
-
-