public abstract class AsynchronousSocketStep extends AsynchronousChannelStep<java.nio.ByteBuffer,java.nio.ByteBuffer>
AsynchronousSocketChannel
. The channel will be opened and connected
as necessary. It may also be provided before the step is invoked in a state
relation with the type SOCKET_CHANNEL
. If the channel is opened by
this step it will have the MetaTypes.MANAGED
flag so that it will be
automatically closed when the CoroutineScope
finishes.AsynchronousChannelStep.ChannelCallback<V,C extends java.nio.channels.AsynchronousChannel>, AsynchronousChannelStep.ChannelOperation<C extends java.nio.channels.AsynchronousChannel>
Modifier and Type | Field and Description |
---|---|
static org.obrel.core.RelationType<java.nio.channels.AsynchronousSocketChannel> |
SOCKET_CHANNEL
State: the
AsynchronousSocketChannel that the steps in a
coroutine operate on. |
CHANNEL_GROUP
Constructor and Description |
---|
AsynchronousSocketStep(java.util.function.Function<Continuation<?>,java.net.SocketAddress> fGetSocketAddress)
Creates a new instance that connects to the socket with the address
provided by the given factory.
|
Modifier and Type | Method and Description |
---|---|
protected java.nio.ByteBuffer |
execute(java.nio.ByteBuffer rData,
Continuation<?> rContinuation)
This method must be implemented by subclasses to provide the actual
functionality of this step.
|
protected java.net.SocketAddress |
getSocketAddress(Continuation<?> rContinuation)
Returns the address of the socket to connect to.
|
protected java.util.function.Function<Continuation<?>,java.net.SocketAddress> |
getSocketAddressFactory()
Returns the socket address factory of this step.
|
protected java.nio.channels.AsynchronousSocketChannel |
getSocketChannel(Continuation<?> rContinuation)
Returns the channel to be used by this step.
|
protected abstract boolean |
performAsyncOperation(int nBytesProcessed,
java.nio.channels.AsynchronousSocketChannel rChannel,
java.nio.ByteBuffer rData,
AsynchronousChannelStep.ChannelCallback<java.lang.Integer,java.nio.channels.AsynchronousSocketChannel> rCallback)
Implementation of the ChannelOperation functional interface method
signature.
|
protected abstract void |
performBlockingOperation(java.nio.channels.AsynchronousSocketChannel aChannel,
java.nio.ByteBuffer rData)
Must be implemented for the blocking execution of a step.
|
void |
runAsync(java.util.concurrent.CompletableFuture<java.nio.ByteBuffer> fPreviousExecution,
CoroutineStep<java.nio.ByteBuffer,?> rNextStep,
Continuation<?> rContinuation)
Runs this execution step asynchronously as a continuation of a previous
code execution in a
CompletableFuture and proceeds to the next
step afterwards. |
getChannelGroup
fail, runBlocking, terminateCoroutine, toString
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
public static final org.obrel.core.RelationType<java.nio.channels.AsynchronousSocketChannel> SOCKET_CHANNEL
AsynchronousSocketChannel
that the steps in a
coroutine operate on.public AsynchronousSocketStep(java.util.function.Function<Continuation<?>,java.net.SocketAddress> fGetSocketAddress)
SOCKET_CHANNEL
.fGetSocketAddress
- A function that provides the socket address to
connect to from the current continuationpublic void runAsync(java.util.concurrent.CompletableFuture<java.nio.ByteBuffer> fPreviousExecution, CoroutineStep<java.nio.ByteBuffer,?> rNextStep, Continuation<?> rContinuation)
CompletableFuture
and proceeds to the next
step afterwards.
Subclasses that need to suspend the invocation of the next step until
some condition is met (e.g. sending or receiving data has finished) need
to override this method and create a Suspension
by invoking
Continuation.suspend(CoroutineStep, CoroutineStep)
on the next
step. If the condition that caused the suspension resolves the coroutine
execution can be resumed by calling Suspension.resume(Object)
.
Subclasses that override this method also need to handle errors by
terminating any further execution (i.e. not resuming a suspension if such
exists) and forwarding the causing exception to Continuation.fail(Throwable)
.
runAsync
in class CoroutineStep<java.nio.ByteBuffer,java.nio.ByteBuffer>
fPreviousExecution
- The future of the previous code executionrNextStep
- The next step to execute or NULL for nonerContinuation
- The continuation of the executionprotected abstract boolean performAsyncOperation(int nBytesProcessed, java.nio.channels.AsynchronousSocketChannel rChannel, java.nio.ByteBuffer rData, AsynchronousChannelStep.ChannelCallback<java.lang.Integer,java.nio.channels.AsynchronousSocketChannel> rCallback) throws java.lang.Exception
java.lang.Exception
AsynchronousChannelStep.ChannelOperation.execute(int,java.nio.channels.AsynchronousChannel,
ByteBuffer, AsynchronousChannelStep.ChannelCallback)
protected abstract void performBlockingOperation(java.nio.channels.AsynchronousSocketChannel aChannel, java.nio.ByteBuffer rData) throws java.lang.Exception
AsynchronousSocketChannel
which must be accessed through the
blocking API (like Future.get()
).aChannel
- The channel to perform the operation onrData
- The byte buffer for the operation datajava.lang.Exception
- Any kind of exception may be thrownprotected java.nio.ByteBuffer execute(java.nio.ByteBuffer rData, Continuation<?> rContinuation)
execute
in class CoroutineStep<java.nio.ByteBuffer,java.nio.ByteBuffer>
rData
- The input valuerContinuation
- The continuation of the executionprotected java.net.SocketAddress getSocketAddress(Continuation<?> rContinuation)
rContinuation
- The current continuationprotected java.util.function.Function<Continuation<?>,java.net.SocketAddress> getSocketAddressFactory()
protected java.nio.channels.AsynchronousSocketChannel getSocketChannel(Continuation<?> rContinuation) throws java.io.IOException
SOCKET_CHANNEL
relation. If that doesn't exists or the
channel is closed a new AsynchronousSocketChannel
will be opened
and stored in the coroutine relation. Using the coroutine to store the
channel allows coroutines to be structured so that multiple subroutines
perform communication on different channels.rContinuation
- The continuation to query for an existing channeljava.io.IOException
- If opening the channel fails