public class ServerSocketAccept extends AsynchronousChannelStep<java.lang.Void,java.lang.Void>
AsynchronousServerSocketChannel
. The step will suspend
execution until a request is accepted and then spawn another coroutine for
the handling of the request. The handling coroutine will receive the AsynchronousSocketChannel
for the communication with the client. It could
then store the channel as AsynchronousSocketStep.SOCKET_CHANNEL
and
process the request with SocketReceive
and SocketSend
steps.
After the request handling coroutine has been spawned this step will
resume execution of it's own coroutine. A typical server accept loop
therefore needs to be implemented in that coroutine, e.g. inside a
conditional Loop
step. Handling coroutines will run in parallel in
separate continuations but in the same scope. Interdependencies between the
main coroutine and the request handling should be avoided to prevent the risk
of deadlocks.
Modifier and Type | Class and Description |
---|---|
protected static class |
ServerSocketAccept.AcceptCallback
A
CompletionHandler implementation that receives the result of an
asynchronous accept and processes the request with an asynchronous
coroutine execution. |
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.AsynchronousServerSocketChannel> |
SERVER_SOCKET_CHANNEL
State: an
AsynchronousServerSocketChannel that has been openened
and connected by an asynchronous execution. |
CHANNEL_GROUP
Constructor and Description |
---|
ServerSocketAccept(java.util.function.Function<Continuation<?>,java.net.SocketAddress> fGetSocketAddress,
Coroutine<java.nio.channels.AsynchronousSocketChannel,?> rRequestHandler)
Creates a new instance that accepts a single server request and processes
it asynchronously in a coroutine.
|
Modifier and Type | Method and Description |
---|---|
static ServerSocketAccept |
acceptRequestOn(java.util.function.Function<Continuation<?>,java.net.SocketAddress> fGetSocketAddress,
Coroutine<java.nio.channels.AsynchronousSocketChannel,?> rRequestHandler)
Accepts an incoming request on the given socket address and then handles
it by executing the given coroutine with the client socket channel as
it's input.
|
protected java.lang.Void |
execute(java.lang.Void rData,
Continuation<?> rContinuation)
This method must be implemented by subclasses to provide the actual
functionality of this step.
|
protected java.nio.channels.AsynchronousServerSocketChannel |
getServerSocketChannel(Continuation<?> rContinuation)
Returns the channel to be used by 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.
|
void |
runAsync(java.util.concurrent.CompletableFuture<java.lang.Void> fPreviousExecution,
CoroutineStep<java.lang.Void,?> 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.AsynchronousServerSocketChannel> SERVER_SOCKET_CHANNEL
AsynchronousServerSocketChannel
that has been openened
and connected by an asynchronous execution.public ServerSocketAccept(java.util.function.Function<Continuation<?>,java.net.SocketAddress> fGetSocketAddress, Coroutine<java.nio.channels.AsynchronousSocketChannel,?> rRequestHandler)
SERVER_SOCKET_CHANNEL
.fGetSocketAddress
- A function that provides the socket address to
connect to from the current continuationrRequestHandler
- A coroutine that processes a single server
requestpublic static ServerSocketAccept acceptRequestOn(java.util.function.Function<Continuation<?>,java.net.SocketAddress> fGetSocketAddress, Coroutine<java.nio.channels.AsynchronousSocketChannel,?> rRequestHandler)
fGetSocketAddress
- A function that produces the address of the
local address to accept the request fromrRequestHandler
- The coroutine to process the next request withpublic void runAsync(java.util.concurrent.CompletableFuture<java.lang.Void> fPreviousExecution, CoroutineStep<java.lang.Void,?> 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.lang.Void,java.lang.Void>
fPreviousExecution
- The future of the previous code executionrNextStep
- The next step to execute or NULL for nonerContinuation
- The continuation of the executionprotected java.lang.Void execute(java.lang.Void rData, Continuation<?> rContinuation)
execute
in class CoroutineStep<java.lang.Void,java.lang.Void>
rData
- The input valuerContinuation
- The continuation of the executionprotected java.nio.channels.AsynchronousServerSocketChannel getServerSocketChannel(Continuation<?> rContinuation) throws java.io.IOException
SERVER_SOCKET_CHANNEL
relation. If that doesn't exists
or if it contains a closed channel a new AsynchronousServerSocketChannel
will be opened and stored in the state
object.rContinuation
- The continuation to query for an existing channeljava.io.IOException
- If opening the channel failsprotected java.net.SocketAddress getSocketAddress(Continuation<?> rContinuation)
rContinuation
- The current continuationprotected java.util.function.Function<Continuation<?>,java.net.SocketAddress> getSocketAddressFactory()