Class Window

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel, ChannelHolder

    public class Window
    extends AbstractLoggingBean
    implements java.nio.channels.Channel, ChannelHolder
    A Window for a given channel. Windows are used to not overflow the client or server when sending datas. Both clients and servers have a local and remote window and won't send anymore data until the window has been expanded. When the local window is
    • Field Detail

      • SPACE_AVAILABLE_PREDICATE

        public static final java.util.function.Predicate<Window> SPACE_AVAILABLE_PREDICATE
        Default Predicate used to test if space became available
      • closed

        private final java.util.concurrent.atomic.AtomicBoolean closed
      • initialized

        private final java.util.concurrent.atomic.AtomicBoolean initialized
      • lock

        private final java.lang.Object lock
      • suffix

        private final java.lang.String suffix
      • size

        private long size
      • maxSize

        private long maxSize
      • packetSize

        private long packetSize
    • Constructor Detail

      • Window

        public Window​(AbstractChannel channel,
                      java.lang.Object lock,
                      boolean client,
                      boolean local)
    • Method Detail

      • getSize

        public long getSize()
      • getMaxSize

        public long getMaxSize()
      • getPacketSize

        public long getPacketSize()
      • init

        public void init​(long size,
                         long packetSize,
                         PropertyResolver resolver)
      • expand

        public void expand​(int window)
      • consume

        public void consume​(long len)
      • consumeAndCheck

        public void consumeAndCheck​(long len)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • check

        public void check​(long maxFree)
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • waitAndConsume

        public void waitAndConsume​(long len,
                                   long maxWaitTime)
                            throws java.lang.InterruptedException,
                                   WindowClosedException,
                                   java.net.SocketTimeoutException
        Waits for enough data to become available to consume the specified size
        Parameters:
        len - Size of data to consume
        maxWaitTime - Max. time (millis) to wait for enough data to become available
        Throws:
        java.lang.InterruptedException - If interrupted while waiting
        WindowClosedException - If window closed while waiting
        java.net.SocketTimeoutException - If timeout expired before enough data became available
        See Also:
        waitForCondition(Predicate, Duration), consume(long)
      • waitAndConsume

        public void waitAndConsume​(long len,
                                   java.time.Duration maxWaitTime)
                            throws java.lang.InterruptedException,
                                   WindowClosedException,
                                   java.net.SocketTimeoutException
        Waits for enough data to become available to consume the specified size
        Parameters:
        len - Size of data to consume
        maxWaitTime - Max. time to wait for enough data to become available
        Throws:
        java.lang.InterruptedException - If interrupted while waiting
        WindowClosedException - If window closed while waiting
        java.net.SocketTimeoutException - If timeout expired before enough data became available
        See Also:
        waitForCondition(Predicate, Duration), consume(long)
      • waitForSpace

        public long waitForSpace​(long maxWaitTime)
                          throws java.lang.InterruptedException,
                                 WindowClosedException,
                                 java.net.SocketTimeoutException
        Waits until some data becomes available or timeout expires
        Parameters:
        maxWaitTime - Max. time (millis) to wait for space to become available
        Returns:
        Amount of available data - always positive
        Throws:
        java.lang.InterruptedException - If interrupted while waiting
        WindowClosedException - If window closed while waiting
        java.net.SocketTimeoutException - If timeout expired before space became available
        See Also:
        waitForCondition(Predicate, Duration)
      • waitForSpace

        public long waitForSpace​(java.time.Duration maxWaitTime)
                          throws java.lang.InterruptedException,
                                 WindowClosedException,
                                 java.net.SocketTimeoutException
        Waits until some data becomes available or timeout expires
        Parameters:
        maxWaitTime - Max. time to wait for space to become available
        Returns:
        Amount of available data - always positive
        Throws:
        java.lang.InterruptedException - If interrupted while waiting
        WindowClosedException - If window closed while waiting
        java.net.SocketTimeoutException - If timeout expired before space became available
        See Also:
        waitForCondition(Predicate, Duration)
      • waitForCondition

        protected void waitForCondition​(java.util.function.Predicate<? super Window> predicate,
                                        java.time.Duration maxWaitTime)
                                 throws WindowClosedException,
                                        java.lang.InterruptedException,
                                        java.net.SocketTimeoutException
        Waits up to a specified amount of time for a condition to be satisfied and signaled via the lock. Note: assumes that lock is acquired when this method is called.
        Parameters:
        predicate - The Predicate to check if the condition has been satisfied - the argument to the predicate is this reference
        maxWaitTime - Max. time to wait for the condition to be satisfied
        Throws:
        WindowClosedException - If window closed while waiting
        java.lang.InterruptedException - If interrupted while waiting
        java.net.SocketTimeoutException - If timeout expired before condition was satisfied
        See Also:
        isOpen()
      • updateSize

        protected void updateSize​(long size)
      • checkInitialized

        protected void checkInitialized​(java.lang.String location)
      • isOpen

        public boolean isOpen()
        Specified by:
        isOpen in interface java.nio.channels.Channel
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object