Class 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 a AsynchronousSocketChannel.
    • 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 with until(BiPredicate) that checks whether a byte buffer contains the complete content of an HTTP response.
      protected boolean performAsyncOperation​(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 void performBlockingOperation​(java.nio.channels.AsynchronousSocketChannel channel, java.nio.ByteBuffer data)
      Must be implemented for the blocking execution of a step.
      static SocketReceive receiveFrom​(java.net.SocketAddress socketAddress)  
      static SocketReceive receiveFrom​(java.util.function.Function<Continuation<?>,​java.net.SocketAddress> getSocketAddress)
      Suspends until data has been received from a network socket.
      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.
      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.
      • Methods inherited from class org.obrel.core.RelatedObject

        deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface de.esoco.lib.property.Fluent

        _with
      • Methods inherited from interface org.obrel.core.FluentRelatable

        with, with, with
      • Methods inherited from interface org.obrel.core.Relatable

        deleteRelation, deleteRelation, deleteRelations, get, getAll, getOption, getRelation, getRelationCount, getRelations, getRelations, hasFlag, hasRelation, hasRelations, init, set, set, set, set, set, setOption, streamRelations, transform
    • 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 continuation
        checkFinished - 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 with until(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 input ByteBuffer of the step. If the capacity of the buffer is reached before the EOF signal is received the coroutine will be terminated with a CoroutineException.

        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
      • 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
      • performBlockingOperation

        protected void performBlockingOperation​(java.nio.channels.AsynchronousSocketChannel channel,
                                                java.nio.ByteBuffer data)
                                         throws java.lang.Exception
        Must be implemented for the blocking execution of a step. It receives an AsynchronousSocketChannel which must be accessed through the blocking API (like Future.get()).
        Specified by:
        performBlockingOperation in class AsynchronousSocketStep
        Parameters:
        channel - The channel to perform the operation on
        data - The byte buffer for the operation data
        Throws:
        java.lang.Exception - Any kind of exception may be thrown