Package de.esoco.coroutine.step.nio
Class SocketReceive
- java.lang.Object
-
- org.obrel.core.RelatedObject
-
- de.esoco.coroutine.CoroutineStep<I,O>
-
- de.esoco.coroutine.step.nio.AsynchronousChannelStep<java.nio.ByteBuffer,java.nio.ByteBuffer>
-
- de.esoco.coroutine.step.nio.AsynchronousSocketStep
-
- de.esoco.coroutine.step.nio.SocketReceive
-
- All Implemented Interfaces:
de.esoco.lib.property.Fluent<CoroutineStep<java.nio.ByteBuffer,java.nio.ByteBuffer>>,org.obrel.core.FluentRelatable<CoroutineStep<java.nio.ByteBuffer,java.nio.ByteBuffer>>,org.obrel.core.Relatable
public class SocketReceive extends AsynchronousSocketStep
Implements asynchronous reading from aAsynchronousSocketChannel.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class de.esoco.coroutine.step.nio.AsynchronousChannelStep
AsynchronousChannelStep.ChannelCallback<V,C extends java.nio.channels.AsynchronousChannel>, AsynchronousChannelStep.ChannelOperation<C extends java.nio.channels.AsynchronousChannel>
-
-
Field Summary
-
Fields inherited from class de.esoco.coroutine.step.nio.AsynchronousSocketStep
SOCKET_CHANNEL
-
Fields inherited from class de.esoco.coroutine.step.nio.AsynchronousChannelStep
CHANNEL_GROUP
-
-
Constructor Summary
Constructors Constructor Description SocketReceive(java.util.function.Function<Continuation<?>,java.net.SocketAddress> getSocketAddress, java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> checkFinished)Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer>contentFullyRead()Returns a new predicate to be used withuntil(BiPredicate)that checks whether a byte buffer contains the complete content of an HTTP response.protected booleanperformAsyncOperation(int bytesProcessed, java.nio.channels.AsynchronousSocketChannel channel, java.nio.ByteBuffer data, AsynchronousChannelStep.ChannelCallback<java.lang.Integer,java.nio.channels.AsynchronousSocketChannel> callback)Implementation of the ChannelOperation functional interface method signature.protected voidperformBlockingOperation(java.nio.channels.AsynchronousSocketChannel channel, java.nio.ByteBuffer data)Must be implemented for the blocking execution of a step.static SocketReceivereceiveFrom(java.net.SocketAddress socketAddress)static SocketReceivereceiveFrom(java.util.function.Function<Continuation<?>,java.net.SocketAddress> getSocketAddress)Suspends until data has been received from a network socket.static SocketReceivereceiveUntil(java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> checkFinished)Suspends until data has been received from a previously connected channel stored in the currently executed coroutine.SocketReceiveuntil(java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> checkFinished)Returns a new receive step instance the suspends until data has been received from a network socket and a certain condition on that data is met or an end-of-stream signal is received.-
Methods inherited from class de.esoco.coroutine.step.nio.AsynchronousSocketStep
execute, getSocketAddress, getSocketAddressFactory, getSocketChannel, runAsync
-
Methods inherited from class de.esoco.coroutine.step.nio.AsynchronousChannelStep
getChannelGroup
-
Methods inherited from class de.esoco.coroutine.CoroutineStep
runBlocking, terminateCoroutine, toString
-
Methods inherited from class org.obrel.core.RelatedObject
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
-
-
-
-
Constructor Detail
-
SocketReceive
public SocketReceive(java.util.function.Function<Continuation<?>,java.net.SocketAddress> getSocketAddress, java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> checkFinished)
Creates a new instance.- Parameters:
getSocketAddress- A function that provides the target socket address from the current continuationcheckFinished- A predicate that checks whether receiving is complete by evaluating the byte buffer after reading
-
-
Method Detail
-
contentFullyRead
public static java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> contentFullyRead()
Returns a new predicate to be used withuntil(BiPredicate)that checks whether a byte buffer contains the complete content of an HTTP response. The test is performed by calculating the full data size from the 'Content-Length' attribute in the response header and comparing it with the buffer position.- Returns:
- A new predicate instance
-
receiveFrom
public static SocketReceive receiveFrom(java.util.function.Function<Continuation<?>,java.net.SocketAddress> getSocketAddress)
Suspends until data has been received from a network socket. The data will be stored in the inputByteBufferof the step. If the capacity of the buffer is reached before the EOF signal is received the coroutine will be terminated with aCoroutineException.After the data has been fully received
ByteBuffer.flip()will be invoked on the buffer so that it can be used directly for subsequent reading from it.The returned step only receives the next block of data that is sent by the remote socket and then continues the coroutine execution. If data should be read until a certain condition is met a derived step needs to be created with
until(BiPredicate).- Parameters:
getSocketAddress- A function that provides the source socket address from the current continuation- Returns:
- A new step instance
-
receiveFrom
public static SocketReceive receiveFrom(java.net.SocketAddress socketAddress)
- See Also:
receiveFrom(Function)
-
receiveUntil
public static SocketReceive receiveUntil(java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> checkFinished)
Suspends until data has been received from a previously connected channel stored in the currently executed coroutine. If no such channel exists the execution will fail. This invocation is intended to be used for request-response communication where a receive is always preceded by a send operation.The predicate argument is the same as for the
until(BiPredicate)method.- Parameters:
checkFinished- A predicate that checks whether the data has been received completely- Returns:
- A new step instance
-
until
public SocketReceive until(java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> checkFinished)
Returns a new receive step instance the suspends until data has been received from a network socket and a certain condition on that data is met or an end-of-stream signal is received. If the capacity of the buffer is reached before the receiving is finished the coroutine will fail with an exception.- Parameters:
checkFinished- A predicate that checks whether the data has been received completely- Returns:
- A new step instance
-
performAsyncOperation
protected boolean performAsyncOperation(int bytesProcessed, java.nio.channels.AsynchronousSocketChannel channel, java.nio.ByteBuffer data, AsynchronousChannelStep.ChannelCallback<java.lang.Integer,java.nio.channels.AsynchronousSocketChannel> callback) throws java.io.IOExceptionImplementation of the ChannelOperation functional interface method signature.- Specified by:
performAsyncOperationin classAsynchronousSocketStep- Throws:
java.io.IOException- See Also:
AsynchronousChannelStep.ChannelOperation.execute(int, java.nio.channels.AsynchronousChannel, ByteBuffer, AsynchronousChannelStep.ChannelCallback)
-
performBlockingOperation
protected void performBlockingOperation(java.nio.channels.AsynchronousSocketChannel channel, java.nio.ByteBuffer data) throws java.lang.ExceptionMust be implemented for the blocking execution of a step. It receives anAsynchronousSocketChannelwhich must be accessed through the blocking API (likeFuture.get()).- Specified by:
performBlockingOperationin classAsynchronousSocketStep- Parameters:
channel- The channel to perform the operation ondata- The byte buffer for the operation data- Throws:
java.lang.Exception- Any kind of exception may be thrown
-
-