src/java.net.http/share/classes/jdk/internal/net/http/SocketTube.java
author mbaesken
Thu, 28 Nov 2019 13:02:39 +0100
changeset 59323 ae2eb76c486d
parent 52499 768b1c612100
permissions -rw-r--r--
8234821: remove unused functions from libjli Reviewed-by: clanger, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     1
/*
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     4
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    10
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    15
 * accompanied this code).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    16
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    20
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    23
 * questions.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    24
 */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    25
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    26
package jdk.internal.net.http;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    27
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    28
import java.io.IOException;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    29
import java.nio.ByteBuffer;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    30
import java.util.List;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    31
import java.util.Objects;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    32
import java.util.concurrent.Flow;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    33
import java.util.concurrent.atomic.AtomicLong;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    34
import java.util.concurrent.atomic.AtomicReference;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    35
import java.nio.channels.SelectableChannel;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    36
import java.nio.channels.SelectionKey;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    37
import java.nio.channels.SocketChannel;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    38
import java.util.ArrayList;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    39
import java.util.function.Consumer;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    40
import java.util.function.Supplier;
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
    41
import jdk.internal.net.http.common.BufferSupplier;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    42
import jdk.internal.net.http.common.Demand;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    43
import jdk.internal.net.http.common.FlowTube;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
    44
import jdk.internal.net.http.common.Log;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    45
import jdk.internal.net.http.common.Logger;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    46
import jdk.internal.net.http.common.SequentialScheduler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    47
import jdk.internal.net.http.common.SequentialScheduler.DeferredCompleter;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    48
import jdk.internal.net.http.common.SequentialScheduler.RestartableTask;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    49
import jdk.internal.net.http.common.Utils;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    50
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    51
/**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    52
 * A SocketTube is a terminal tube plugged directly into the socket.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    53
 * The read subscriber should call {@code subscribe} on the SocketTube before
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    54
 * the SocketTube is subscribed to the write publisher.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    55
 */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    56
final class SocketTube implements FlowTube {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    57
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    58
    final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    59
    static final AtomicLong IDS = new AtomicLong();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    60
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    61
    private final HttpClientImpl client;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    62
    private final SocketChannel channel;
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
    63
    private final SliceBufferSource sliceBuffersSource;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    64
    private final Object lock = new Object();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    65
    private final AtomicReference<Throwable> errorRef = new AtomicReference<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    66
    private final InternalReadPublisher readPublisher;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    67
    private final InternalWriteSubscriber writeSubscriber;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    68
    private final long id = IDS.incrementAndGet();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    69
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    70
    public SocketTube(HttpClientImpl client, SocketChannel channel,
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
    71
                      Supplier<ByteBuffer> buffersFactory) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    72
        this.client = client;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    73
        this.channel = channel;
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
    74
        this.sliceBuffersSource = new SliceBufferSource(buffersFactory);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
    75
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    76
        this.readPublisher = new InternalReadPublisher();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    77
        this.writeSubscriber = new InternalWriteSubscriber();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    78
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    79
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    80
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    81
     * Returns {@code true} if this flow is finished.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    82
     * This happens when this flow internal read subscription is completed,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    83
     * either normally (EOF reading) or exceptionally  (EOF writing, or
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    84
     * underlying socket closed, or some exception occurred while reading or
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    85
     * writing to the socket).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    86
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    87
     * @return {@code true} if this flow is finished.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    88
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    89
    public boolean isFinished() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    90
        InternalReadPublisher.InternalReadSubscription subscription =
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    91
                readPublisher.subscriptionImpl;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    92
        return subscription != null && subscription.completed
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    93
                || subscription == null && errorRef.get() != null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    94
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    95
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    96
    // ===================================================================== //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    97
    //                       Flow.Publisher                                  //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    98
    // ======================================================================//
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    99
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   100
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   101
     * {@inheritDoc }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   102
     * @apiNote This method should be called first. In particular, the caller
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   103
     *          must ensure that this method must be called by the read
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   104
     *          subscriber before the write publisher can call {@code onSubscribe}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   105
     *          Failure to adhere to this contract may result in assertion errors.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   106
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   107
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   108
    public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   109
        Objects.requireNonNull(s);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   110
        assert s instanceof TubeSubscriber : "Expected TubeSubscriber, got:" + s;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   111
        readPublisher.subscribe(s);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   112
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   113
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   114
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   115
    // ===================================================================== //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   116
    //                       Flow.Subscriber                                 //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   117
    // ======================================================================//
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   118
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   119
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   120
     * {@inheritDoc }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   121
     * @apiNote The caller must ensure that {@code subscribe} is called by
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   122
     *          the read subscriber before {@code onSubscribe} is called by
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   123
     *          the write publisher.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   124
     *          Failure to adhere to this contract may result in assertion errors.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   125
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   126
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   127
    public void onSubscribe(Flow.Subscription subscription) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   128
        writeSubscriber.onSubscribe(subscription);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   129
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   130
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   131
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   132
    public void onNext(List<ByteBuffer> item) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   133
        writeSubscriber.onNext(item);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   134
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   135
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   136
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   137
    public void onError(Throwable throwable) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   138
        writeSubscriber.onError(throwable);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   139
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   140
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   141
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   142
    public void onComplete() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   143
        writeSubscriber.onComplete();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   144
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   145
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   146
    // ===================================================================== //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   147
    //                           Events                                      //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   148
    // ======================================================================//
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   149
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   150
    void signalClosed() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   151
        // Ensures that the subscriber will be terminated and that future
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   152
        // subscribers will be notified when the connection is closed.
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   153
        if (Log.channel()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   154
            Log.logChannel("Connection close signalled: connection closed locally ({0})",
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   155
                    channelDescr());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   156
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   157
        readPublisher.subscriptionImpl.signalError(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   158
                new IOException("connection closed locally"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   159
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   160
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   161
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   162
     * A restartable task used to process tasks in sequence.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   163
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   164
    private static class SocketFlowTask implements RestartableTask {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   165
        final Runnable task;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   166
        private final Object monitor = new Object();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   167
        SocketFlowTask(Runnable task) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   168
            this.task = task;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   169
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   170
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   171
        public final void run(DeferredCompleter taskCompleter) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   172
            try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   173
                // non contentious synchronized for visibility.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   174
                synchronized(monitor) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   175
                    task.run();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   176
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   177
            } finally {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   178
                taskCompleter.complete();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   179
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   180
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   181
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   182
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   183
    // This is best effort - there's no guarantee that the printed set of values
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   184
    // is consistent. It should only be considered as weakly accurate - in
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   185
    // particular in what concerns the events states, especially when displaying
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   186
    // a read event state from a write event callback and conversely.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   187
    void debugState(String when) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   188
        if (debug.on()) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   189
            StringBuilder state = new StringBuilder();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   190
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   191
            InternalReadPublisher.InternalReadSubscription sub =
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   192
                    readPublisher.subscriptionImpl;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   193
            InternalReadPublisher.ReadEvent readEvent =
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   194
                    sub == null ? null : sub.readEvent;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   195
            Demand rdemand = sub == null ? null : sub.demand;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   196
            InternalWriteSubscriber.WriteEvent writeEvent =
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   197
                    writeSubscriber.writeEvent;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   198
            Demand wdemand = writeSubscriber.writeDemand;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   199
            int rops = readEvent == null ? 0 : readEvent.interestOps();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   200
            long rd = rdemand == null ? 0 : rdemand.get();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   201
            int wops = writeEvent == null ? 0 : writeEvent.interestOps();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   202
            long wd = wdemand == null ? 0 : wdemand.get();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   203
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   204
            state.append(when).append(" Reading: [ops=")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   205
                    .append(rops).append(", demand=").append(rd)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   206
                    .append(", stopped=")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   207
                    .append((sub == null ? false : sub.readScheduler.isStopped()))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   208
                    .append("], Writing: [ops=").append(wops)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   209
                    .append(", demand=").append(wd)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   210
                    .append("]");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   211
            debug.log(state.toString());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   212
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   213
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   214
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   215
    /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   216
     * A repeatable event that can be paused or resumed by changing its
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   217
     * interestOps. When the event is fired, it is first paused before being
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   218
     * signaled. It is the responsibility of the code triggered by
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   219
     * {@code signalEvent} to resume the event if required.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   220
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   221
    private static abstract class SocketFlowEvent extends AsyncEvent {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   222
        final SocketChannel channel;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   223
        final int defaultInterest;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   224
        volatile int interestOps;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   225
        volatile boolean registered;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   226
        SocketFlowEvent(int defaultInterest, SocketChannel channel) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   227
            super(AsyncEvent.REPEATING);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   228
            this.defaultInterest = defaultInterest;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   229
            this.channel = channel;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   230
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   231
        final boolean registered() {return registered;}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   232
        final void resume() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   233
            interestOps = defaultInterest;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   234
            registered = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   235
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   236
        final void pause() {interestOps = 0;}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   237
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   238
        public final SelectableChannel channel() {return channel;}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   239
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   240
        public final int interestOps() {return interestOps;}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   241
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   242
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   243
        public final void handle() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   244
            pause();       // pause, then signal
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   245
            signalEvent(); // won't be fired again until resumed.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   246
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   247
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   248
        public final void abort(IOException error) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   249
            debug().log(() -> "abort: " + error);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   250
            pause();              // pause, then signal
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   251
            signalError(error);   // should not be resumed after abort (not checked)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   252
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   253
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   254
        protected abstract void signalEvent();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   255
        protected abstract void signalError(Throwable error);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   256
        abstract Logger debug();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   257
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   258
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   259
    // ===================================================================== //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   260
    //                              Writing                                  //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   261
    // ======================================================================//
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   262
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   263
    // This class makes the assumption that the publisher will call onNext
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   264
    // sequentially, and that onNext won't be called if the demand has not been
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   265
    // incremented by request(1).
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   266
    // It has a 'queue of 1' meaning that it will call request(1) in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   267
    // onSubscribe, and then only after its 'current' buffer list has been
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   268
    // fully written and current set to null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   269
    private final class InternalWriteSubscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   270
            implements Flow.Subscriber<List<ByteBuffer>> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   271
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   272
        volatile WriteSubscription subscription;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   273
        volatile List<ByteBuffer> current;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   274
        volatile boolean completed;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   275
        final AsyncTriggerEvent startSubscription =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   276
                new AsyncTriggerEvent(this::signalError, this::startSubscription);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   277
        final WriteEvent writeEvent = new WriteEvent(channel, this);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   278
        final Demand writeDemand = new Demand();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   279
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   280
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   281
        public void onSubscribe(Flow.Subscription subscription) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   282
            WriteSubscription previous = this.subscription;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   283
            if (debug.on()) debug.log("subscribed for writing");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   284
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   285
                boolean needEvent = current == null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   286
                if (needEvent) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   287
                    if (previous != null && previous.upstreamSubscription != subscription) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   288
                        previous.dropSubscription();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   289
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   290
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   291
                this.subscription = new WriteSubscription(subscription);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   292
                if (needEvent) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   293
                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   294
                        debug.log("write: registering startSubscription event");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   295
                    client.registerEvent(startSubscription);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   296
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   297
            } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   298
                signalError(t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   299
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   300
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   301
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   302
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   303
        public void onNext(List<ByteBuffer> bufs) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   304
            assert current == null : dbgString() // this is a queue of 1.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   305
                    + "w.onNext current: " + current;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   306
            assert subscription != null : dbgString()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   307
                    + "w.onNext: subscription is null";
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   308
            current = bufs;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   309
            tryFlushCurrent(client.isSelectorThread()); // may be in selector thread
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   310
            // For instance in HTTP/2, a received SETTINGS frame might trigger
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   311
            // the sending of a SETTINGS frame in turn which might cause
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   312
            // onNext to be called from within the same selector thread that the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   313
            // original SETTINGS frames arrived on. If rs is the read-subscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   314
            // and ws is the write-subscriber then the following can occur:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   315
            // ReadEvent -> rs.onNext(bytes) -> process server SETTINGS -> write
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   316
            // client SETTINGS -> ws.onNext(bytes) -> tryFlushCurrent
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   317
            debugState("leaving w.onNext");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   318
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   319
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   320
        // Don't use a SequentialScheduler here: rely on onNext() being invoked
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   321
        // sequentially, and not being invoked if there is no demand, request(1).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   322
        // onNext is usually called from within a user / executor thread.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   323
        // Initial writing will be performed in that thread. If for some reason,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   324
        // not all the data can be written, a writeEvent will be registered, and
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 50985
diff changeset
   325
        // writing will resume in the selector manager thread when the
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   326
        // writeEvent is fired.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   327
        //
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   328
        // If this method is invoked in the selector manager thread (because of
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   329
        // a writeEvent), then the executor will be used to invoke request(1),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   330
        // ensuring that onNext() won't be invoked from within the selector
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   331
        // thread. If not in the selector manager thread, then request(1) is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   332
        // invoked directly.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   333
        void tryFlushCurrent(boolean inSelectorThread) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   334
            List<ByteBuffer> bufs = current;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   335
            if (bufs == null) return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   336
            try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   337
                assert inSelectorThread == client.isSelectorThread() :
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   338
                       "should " + (inSelectorThread ? "" : "not ")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   339
                        + " be in the selector thread";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   340
                long remaining = Utils.remaining(bufs);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   341
                if (debug.on()) debug.log("trying to write: %d", remaining);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   342
                long written = writeAvailable(bufs);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   343
                if (debug.on()) debug.log("wrote: %d", written);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   344
                assert written >= 0 : "negative number of bytes written:" + written;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   345
                assert written <= remaining;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   346
                if (remaining - written == 0) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   347
                    current = null;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   348
                    if (writeDemand.tryDecrement()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   349
                        Runnable requestMore = this::requestMore;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   350
                        if (inSelectorThread) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   351
                            assert client.isSelectorThread();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   352
                            client.theExecutor().execute(requestMore);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   353
                        } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   354
                            assert !client.isSelectorThread();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   355
                            requestMore.run();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   356
                        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   357
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   358
                } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   359
                    resumeWriteEvent(inSelectorThread);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   360
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   361
            } catch (Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   362
                signalError(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   363
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   364
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   365
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   366
        // Kick off the initial request:1 that will start the writing side.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   367
        // Invoked in the selector manager thread.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   368
        void startSubscription() {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   369
            try {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   370
                if (debug.on()) debug.log("write: starting subscription");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   371
                if (Log.channel()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   372
                    Log.logChannel("Start requesting bytes for writing to channel: {0}",
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   373
                            channelDescr());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   374
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   375
                assert client.isSelectorThread();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   376
                // make sure read registrations are handled before;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   377
                readPublisher.subscriptionImpl.handlePending();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   378
                if (debug.on()) debug.log("write: offloading requestMore");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   379
                // start writing;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   380
                client.theExecutor().execute(this::requestMore);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   381
            } catch(Throwable t) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   382
                signalError(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   383
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   384
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   385
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   386
        void requestMore() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   387
           WriteSubscription subscription = this.subscription;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   388
           subscription.requestMore();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   389
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   390
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   391
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   392
        public void onError(Throwable throwable) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   393
            signalError(throwable);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   394
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   395
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   396
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   397
        public void onComplete() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   398
            completed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   399
            // no need to pause the write event here: the write event will
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   400
            // be paused if there is nothing more to write.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   401
            List<ByteBuffer> bufs = current;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   402
            long remaining = bufs == null ? 0 : Utils.remaining(bufs);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   403
            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   404
                debug.log( "write completed, %d yet to send", remaining);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   405
            debugState("InternalWriteSubscriber::onComplete");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   406
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   407
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   408
        void resumeWriteEvent(boolean inSelectorThread) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   409
            if (debug.on()) debug.log("scheduling write event");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   410
            resumeEvent(writeEvent, this::signalError);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   411
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   412
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   413
        void signalWritable() {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   414
            if (debug.on()) debug.log("channel is writable");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   415
            tryFlushCurrent(true);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   416
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   417
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   418
        void signalError(Throwable error) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   419
            debug.log(() -> "write error: " + error);
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   420
            if (Log.channel()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   421
                Log.logChannel("Failed to write to channel ({0}: {1})",
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   422
                        channelDescr(), error);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   423
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   424
            completed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   425
            readPublisher.signalError(error);
50985
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   426
            Flow.Subscription subscription = this.subscription;
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   427
            if (subscription != null) subscription.cancel();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   428
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   429
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   430
        // A repeatable WriteEvent which is paused after firing and can
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   431
        // be resumed if required - see SocketFlowEvent;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   432
        final class WriteEvent extends SocketFlowEvent {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   433
            final InternalWriteSubscriber sub;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   434
            WriteEvent(SocketChannel channel, InternalWriteSubscriber sub) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   435
                super(SelectionKey.OP_WRITE, channel);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   436
                this.sub = sub;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   437
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   438
            @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   439
            protected final void signalEvent() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   440
                try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   441
                    client.eventUpdated(this);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   442
                    sub.signalWritable();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   443
                } catch(Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   444
                    sub.signalError(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   445
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   446
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   447
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   448
            @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   449
            protected void signalError(Throwable error) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   450
                sub.signalError(error);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   451
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   452
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   453
            @Override
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   454
            Logger debug() { return debug; }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   455
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   456
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   457
        final class WriteSubscription implements Flow.Subscription {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   458
            final Flow.Subscription upstreamSubscription;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   459
            volatile boolean cancelled;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   460
            WriteSubscription(Flow.Subscription subscription) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   461
                this.upstreamSubscription = subscription;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   462
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   463
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   464
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   465
            public void request(long n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   466
                if (cancelled) return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   467
                upstreamSubscription.request(n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   468
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   469
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   470
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   471
            public void cancel() {
50985
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   472
                if (cancelled) return;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   473
                if (debug.on()) debug.log("write: cancel");
50985
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   474
                if (Log.channel()) {
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   475
                    Log.logChannel("Cancelling write subscription");
cd41f34e548c 8206001: Enable TLS1.3 by default in Http Client
michaelm
parents: 50681
diff changeset
   476
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   477
                dropSubscription();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   478
                upstreamSubscription.cancel();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   479
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   480
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   481
            void dropSubscription() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   482
                synchronized (InternalWriteSubscriber.this) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   483
                    cancelled = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   484
                    if (debug.on()) debug.log("write: resetting demand to 0");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   485
                    writeDemand.reset();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   486
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   487
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   488
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   489
            void requestMore() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   490
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   491
                    if (completed || cancelled) return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   492
                    boolean requestMore;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   493
                    long d;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   494
                    // don't fiddle with demand after cancel.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   495
                    // see dropSubscription.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   496
                    synchronized (InternalWriteSubscriber.this) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   497
                        if (cancelled) return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   498
                        d = writeDemand.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   499
                        requestMore = writeDemand.increaseIfFulfilled();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   500
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   501
                    if (requestMore) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   502
                        if (debug.on()) debug.log("write: requesting more...");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   503
                        upstreamSubscription.request(1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   504
                    } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   505
                        if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   506
                            debug.log("write: no need to request more: %d", d);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   507
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   508
                } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   509
                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   510
                        debug.log("write: error while requesting more: " + t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   511
                    signalError(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   512
                } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   513
                    debugState("leaving requestMore: ");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   514
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   515
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   516
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   517
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   518
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   519
    // ===================================================================== //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   520
    //                              Reading                                  //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   521
    // ===================================================================== //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   522
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   523
    // The InternalReadPublisher uses a SequentialScheduler to ensure that
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   524
    // onNext/onError/onComplete are called sequentially on the caller's
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   525
    // subscriber.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   526
    // However, it relies on the fact that the only time where
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   527
    // runOrSchedule() is called from a user/executor thread is in signalError,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   528
    // right after the errorRef has been set.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   529
    // Because the sequential scheduler's task always checks for errors first,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   530
    // and always terminate the scheduler on error, then it is safe to assume
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   531
    // that if it reaches the point where it reads from the channel, then
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   532
    // it is running in the SelectorManager thread. This is because all
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   533
    // other invocation of runOrSchedule() are triggered from within a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   534
    // ReadEvent.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   535
    //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   536
    // When pausing/resuming the event, some shortcuts can then be taken
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   537
    // when we know we're running in the selector manager thread
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   538
    // (in that case there's no need to call client.eventUpdated(readEvent);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   539
    //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   540
    private final class InternalReadPublisher
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   541
            implements Flow.Publisher<List<ByteBuffer>> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   542
        private final InternalReadSubscription subscriptionImpl
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   543
                = new InternalReadSubscription();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   544
        AtomicReference<ReadSubscription> pendingSubscription = new AtomicReference<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   545
        private volatile ReadSubscription subscription;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   546
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   547
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   548
        public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   549
            Objects.requireNonNull(s);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   550
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   551
            TubeSubscriber sub = FlowTube.asTubeSubscriber(s);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   552
            ReadSubscription target = new ReadSubscription(subscriptionImpl, sub);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   553
            ReadSubscription previous = pendingSubscription.getAndSet(target);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   554
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   555
            if (previous != null && previous != target) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   556
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   557
                    debug.log("read publisher: dropping pending subscriber: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   558
                              + previous.subscriber);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   559
                previous.errorRef.compareAndSet(null, errorRef.get());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   560
                previous.signalOnSubscribe();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   561
                if (subscriptionImpl.completed) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   562
                    previous.signalCompletion();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   563
                } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   564
                    previous.subscriber.dropSubscription();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   565
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   566
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   567
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   568
            if (debug.on()) debug.log("read publisher got subscriber");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   569
            subscriptionImpl.signalSubscribe();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   570
            debugState("leaving read.subscribe: ");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   571
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   572
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   573
        void signalError(Throwable error) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   574
            if (debug.on()) debug.log("error signalled " + error);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   575
            if (!errorRef.compareAndSet(null, error)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   576
                return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   577
            }
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   578
            if (Log.channel()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   579
                Log.logChannel("Error signalled on channel {0}: {1}",
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   580
                        channelDescr(), error);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   581
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   582
            subscriptionImpl.handleError();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   583
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   584
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   585
        final class ReadSubscription implements Flow.Subscription {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   586
            final InternalReadSubscription impl;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   587
            final TubeSubscriber  subscriber;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   588
            final AtomicReference<Throwable> errorRef = new AtomicReference<>();
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   589
            final BufferSource bufferSource;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   590
            volatile boolean subscribed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   591
            volatile boolean cancelled;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   592
            volatile boolean completed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   593
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   594
            public ReadSubscription(InternalReadSubscription impl,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   595
                                    TubeSubscriber subscriber) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   596
                this.impl = impl;
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   597
                this.bufferSource = subscriber.supportsRecycling()
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   598
                        ? new SSLDirectBufferSource(client)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   599
                        : SocketTube.this.sliceBuffersSource;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   600
                this.subscriber = subscriber;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   601
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   602
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   603
            @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   604
            public void cancel() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   605
                cancelled = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   606
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   607
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   608
            @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   609
            public void request(long n) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   610
                if (!cancelled) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   611
                    impl.request(n);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   612
                } else {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   613
                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   614
                        debug.log("subscription cancelled, ignoring request %d", n);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   615
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   616
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   617
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   618
            void signalCompletion() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   619
                assert subscribed || cancelled;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   620
                if (completed || cancelled) return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   621
                synchronized (this) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   622
                    if (completed) return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   623
                    completed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   624
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   625
                Throwable error = errorRef.get();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   626
                if (error != null) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   627
                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   628
                        debug.log("forwarding error to subscriber: " + error);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   629
                    subscriber.onError(error);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   630
                } else {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   631
                    if (debug.on()) debug.log("completing subscriber");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   632
                    subscriber.onComplete();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   633
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   634
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   635
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   636
            void signalOnSubscribe() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   637
                if (subscribed || cancelled) return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   638
                synchronized (this) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   639
                    if (subscribed || cancelled) return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   640
                    subscribed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   641
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   642
                subscriber.onSubscribe(this);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   643
                if (debug.on()) debug.log("onSubscribe called");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   644
                if (errorRef.get() != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   645
                    signalCompletion();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   646
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   647
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   648
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   649
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   650
        final class InternalReadSubscription implements Flow.Subscription {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   651
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   652
            private final Demand demand = new Demand();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   653
            final SequentialScheduler readScheduler;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   654
            private volatile boolean completed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   655
            private final ReadEvent readEvent;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   656
            private final AsyncEvent subscribeEvent;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   657
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   658
            InternalReadSubscription() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   659
                readScheduler = new SequentialScheduler(new SocketFlowTask(this::read));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   660
                subscribeEvent = new AsyncTriggerEvent(this::signalError,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   661
                                                       this::handleSubscribeEvent);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   662
                readEvent = new ReadEvent(channel, this);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   663
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   664
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   665
            /*
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   666
             * This method must be invoked before any other method of this class.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   667
             */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   668
            final void signalSubscribe() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   669
                if (readScheduler.isStopped() || completed) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   670
                    // if already completed or stopped we can handle any
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   671
                    // pending connection directly from here.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   672
                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   673
                        debug.log("handling pending subscription while completed");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   674
                    handlePending();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   675
                } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   676
                    try {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   677
                        if (debug.on()) debug.log("registering subscribe event");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   678
                        client.registerEvent(subscribeEvent);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   679
                    } catch (Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   680
                        signalError(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   681
                        handlePending();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   682
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   683
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   684
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   685
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   686
            final void handleSubscribeEvent() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   687
                assert client.isSelectorThread();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   688
                debug.log("subscribe event raised");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   689
                if (Log.channel()) Log.logChannel("Start reading from {0}", channelDescr());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   690
                readScheduler.runOrSchedule();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   691
                if (readScheduler.isStopped() || completed) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   692
                    // if already completed or stopped we can handle any
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   693
                    // pending connection directly from here.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   694
                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   695
                        debug.log("handling pending subscription when completed");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   696
                    handlePending();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   697
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   698
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   699
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   700
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   701
            /*
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   702
             * Although this method is thread-safe, the Reactive-Streams spec seems
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   703
             * to not require it to be as such. It's a responsibility of the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   704
             * subscriber to signal demand in a thread-safe manner.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   705
             *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   706
             * See Reactive Streams specification, rules 2.7 and 3.4.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   707
             */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   708
            @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   709
            public final void request(long n) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   710
                if (n > 0L) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   711
                    boolean wasFulfilled = demand.increase(n);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   712
                    if (wasFulfilled) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   713
                        if (debug.on()) debug.log("got some demand for reading");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   714
                        resumeReadEvent();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   715
                        // if demand has been changed from fulfilled
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   716
                        // to unfulfilled register read event;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   717
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   718
                } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   719
                    signalError(new IllegalArgumentException("non-positive request"));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   720
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   721
                debugState("leaving request("+n+"): ");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   722
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   723
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   724
            @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   725
            public final void cancel() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   726
                pauseReadEvent();
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   727
                if (Log.channel()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   728
                    Log.logChannel("Read subscription cancelled for channel {0}",
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   729
                            channelDescr());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   730
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   731
                readScheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   732
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   733
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   734
            private void resumeReadEvent() {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   735
                if (debug.on()) debug.log("resuming read event");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   736
                resumeEvent(readEvent, this::signalError);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   737
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   738
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   739
            private void pauseReadEvent() {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   740
                if (debug.on()) debug.log("pausing read event");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   741
                pauseEvent(readEvent, this::signalError);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   742
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   743
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   744
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   745
            final void handleError() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   746
                assert errorRef.get() != null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   747
                readScheduler.runOrSchedule();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   748
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   749
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   750
            final void signalError(Throwable error) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   751
                if (!errorRef.compareAndSet(null, error)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   752
                    return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   753
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   754
                if (debug.on()) debug.log("got read error: " + error);
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   755
                if (Log.channel()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   756
                    Log.logChannel("Read error signalled on channel {0}: {1}",
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   757
                            channelDescr(), error);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   758
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   759
                readScheduler.runOrSchedule();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   760
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   761
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   762
            final void signalReadable() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   763
                readScheduler.runOrSchedule();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   764
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   765
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   766
            /** The body of the task that runs in SequentialScheduler. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   767
            final void read() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   768
                // It is important to only call pauseReadEvent() when stopping
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   769
                // the scheduler. The event is automatically paused before
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   770
                // firing, and trying to pause it again could cause a race
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   771
                // condition between this loop, which calls tryDecrementDemand(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   772
                // and the thread that calls request(n), which will try to resume
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   773
                // reading.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   774
                try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   775
                    while(!readScheduler.isStopped()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   776
                        if (completed) return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   777
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   778
                        // make sure we have a subscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   779
                        if (handlePending()) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   780
                            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   781
                                debug.log("pending subscriber subscribed");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   782
                            return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   783
                        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   784
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   785
                        // If an error was signaled, we might not be in the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   786
                        // the selector thread, and that is OK, because we
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   787
                        // will just call onError and return.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   788
                        ReadSubscription current = subscription;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   789
                        Throwable error = errorRef.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   790
                        if (current == null)  {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   791
                            assert error != null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   792
                            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   793
                                debug.log("error raised before subscriber subscribed: %s",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   794
                                          (Object)error);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   795
                            return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   796
                        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   797
                        TubeSubscriber subscriber = current.subscriber;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   798
                        if (error != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   799
                            completed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   800
                            // safe to pause here because we're finished anyway.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   801
                            pauseReadEvent();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   802
                            if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   803
                                debug.log("Sending error " + error
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   804
                                          + " to subscriber " + subscriber);
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   805
                            if (Log.channel()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   806
                                Log.logChannel("Raising error with subscriber for {0}: {1}",
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   807
                                        channelDescr(), error);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   808
                            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   809
                            current.errorRef.compareAndSet(null, error);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   810
                            current.signalCompletion();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   811
                            readScheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   812
                            debugState("leaving read() loop with error: ");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   813
                            return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   814
                        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   815
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   816
                        // If we reach here then we must be in the selector thread.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   817
                        assert client.isSelectorThread();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   818
                        if (demand.tryDecrement()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   819
                            // we have demand.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   820
                            try {
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   821
                                List<ByteBuffer> bytes = readAvailable(current.bufferSource);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   822
                                if (bytes == EOF) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   823
                                    if (!completed) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   824
                                        if (debug.on()) debug.log("got read EOF");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   825
                                        if (Log.channel()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   826
                                            Log.logChannel("EOF read from channel: {0}",
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   827
                                                        channelDescr());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   828
                                        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   829
                                        completed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   830
                                        // safe to pause here because we're finished
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   831
                                        // anyway.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   832
                                        pauseReadEvent();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   833
                                        current.signalCompletion();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   834
                                        readScheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   835
                                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   836
                                    debugState("leaving read() loop after EOF: ");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   837
                                    return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   838
                                } else if (Utils.remaining(bytes) > 0) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   839
                                    // the subscriber is responsible for offloading
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   840
                                    // to another thread if needed.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   841
                                    if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   842
                                        debug.log("read bytes: " + Utils.remaining(bytes));
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   843
                                    assert !current.completed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   844
                                    subscriber.onNext(bytes);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   845
                                    // we could continue looping until the demand
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   846
                                    // reaches 0. However, that would risk starving
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   847
                                    // other connections (bound to other socket
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   848
                                    // channels) - as other selected keys activated
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   849
                                    // by the selector manager thread might be
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   850
                                    // waiting for this event to terminate.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   851
                                    // So resume the read event and return now...
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   852
                                    resumeReadEvent();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   853
                                    debugState("leaving read() loop after onNext: ");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   854
                                    return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   855
                                } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   856
                                    // nothing available!
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   857
                                    if (debug.on()) debug.log("no more bytes available");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   858
                                    // re-increment the demand and resume the read
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   859
                                    // event. This ensures that this loop is
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   860
                                    // executed again when the socket becomes
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   861
                                    // readable again.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   862
                                    demand.increase(1);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   863
                                    resumeReadEvent();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   864
                                    debugState("leaving read() loop with no bytes");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   865
                                    return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   866
                                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   867
                            } catch (Throwable x) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   868
                                signalError(x);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   869
                                continue;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   870
                            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   871
                        } else {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   872
                            if (debug.on()) debug.log("no more demand for reading");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   873
                            // the event is paused just after firing, so it should
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   874
                            // still be paused here, unless the demand was just
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   875
                            // incremented from 0 to n, in which case, the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   876
                            // event will be resumed, causing this loop to be
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   877
                            // invoked again when the socket becomes readable:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   878
                            // This is what we want.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   879
                            // Trying to pause the event here would actually
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   880
                            // introduce a race condition between this loop and
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   881
                            // request(n).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   882
                            debugState("leaving read() loop with no demand");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   883
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   884
                        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   885
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   886
                } catch (Throwable t) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   887
                    if (debug.on()) debug.log("Unexpected exception in read loop", t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   888
                    signalError(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   889
                } finally {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   890
                    if (readScheduler.isStopped()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   891
                        if (debug.on()) debug.log("Read scheduler stopped");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   892
                        if (Log.channel()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   893
                            Log.logChannel("Stopped reading from channel {0}", channelDescr());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   894
                        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   895
                    }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   896
                    handlePending();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   897
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   898
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   899
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   900
            boolean handlePending() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   901
                ReadSubscription pending = pendingSubscription.getAndSet(null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   902
                if (pending == null) return false;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   903
                if (debug.on())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   904
                    debug.log("handling pending subscription for %s",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   905
                            pending.subscriber);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   906
                ReadSubscription current = subscription;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   907
                if (current != null && current != pending && !completed) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   908
                    current.subscriber.dropSubscription();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   909
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   910
                if (debug.on()) debug.log("read demand reset to 0");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   911
                subscriptionImpl.demand.reset(); // subscriber will increase demand if it needs to.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   912
                pending.errorRef.compareAndSet(null, errorRef.get());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   913
                if (!readScheduler.isStopped()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   914
                    subscription = pending;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   915
                } else {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   916
                    if (debug.on()) debug.log("socket tube is already stopped");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   917
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   918
                if (debug.on()) debug.log("calling onSubscribe");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   919
                pending.signalOnSubscribe();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   920
                if (completed) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   921
                    pending.errorRef.compareAndSet(null, errorRef.get());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   922
                    pending.signalCompletion();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   923
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   924
                return true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   925
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   926
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   927
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   928
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   929
        // A repeatable ReadEvent which is paused after firing and can
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   930
        // be resumed if required - see SocketFlowEvent;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   931
        final class ReadEvent extends SocketFlowEvent {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   932
            final InternalReadSubscription sub;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   933
            ReadEvent(SocketChannel channel, InternalReadSubscription sub) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   934
                super(SelectionKey.OP_READ, channel);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   935
                this.sub = sub;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   936
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   937
            @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   938
            protected final void signalEvent() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   939
                try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   940
                    client.eventUpdated(this);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   941
                    sub.signalReadable();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   942
                } catch(Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   943
                    sub.signalError(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   944
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   945
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   946
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   947
            @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   948
            protected final void signalError(Throwable error) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   949
                sub.signalError(error);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   950
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   951
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   952
            @Override
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   953
            Logger debug() { return debug; }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   954
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   955
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   956
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   957
    // ===================================================================== //
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   958
    //                       Buffer Management                               //
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   959
    // ===================================================================== //
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   960
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   961
    // This interface is used by readAvailable(BufferSource);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   962
    public interface BufferSource {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   963
        /**
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   964
         * Returns a buffer to read data from the socket.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   965
         *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   966
         * @implNote
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   967
         * Different implementation can have different strategies, as to
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   968
         * which kind of buffer to return, or whether to return the same
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   969
         * buffer. The only constraints are that:
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   970
         *   a. the buffer returned must not be null
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   971
         *   b. the buffer position indicates where to start reading
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   972
         *   c. the buffer limit indicates where to stop reading.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   973
         *   d. the buffer is 'free' - that is - it is not used
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   974
         *      or retained by anybody else
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   975
         *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   976
         * @return A buffer to read data from the socket.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   977
         */
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   978
        ByteBuffer getBuffer();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   979
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   980
        /**
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   981
         * Appends the read-data in {@code buffer} to the list of buffer to
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   982
         * be sent downstream to the subscriber. May return a new
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   983
         * list, or append to the given list.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   984
         *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   985
         * @implNote
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   986
         * Different implementation can have different strategies, but
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   987
         * must obviously be consistent with the implementation of the
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   988
         * getBuffer() method. For instance, an implementation could
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   989
         * decide to add the buffer to the list and return a new buffer
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   990
         * next time getBuffer() is called, or could decide to add a buffer
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   991
         * slice to the list and return the same buffer (if remaining
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   992
         * space is available) next time getBuffer() is called.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   993
         *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   994
         * @param list    The list before adding the data. Can be null.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   995
         * @param buffer  The buffer containing the data to add to the list.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   996
         * @param start   The start position at which data were read.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   997
         *                The current buffer position indicates the end.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   998
         * @return A possibly new list where a buffer containing the
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   999
         *         data read from the socket has been added.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1000
         */
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1001
        List<ByteBuffer> append(List<ByteBuffer> list, ByteBuffer buffer, int start);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1002
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1003
        /**
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1004
         * Returns the given unused {@code buffer}, previously obtained from
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1005
         * {@code getBuffer}.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1006
         *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1007
         * @implNote This method can be used, if necessary, to return
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1008
         *  the unused buffer to the pull.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1009
         *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1010
         * @param buffer The unused buffer.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1011
         */
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1012
        default void returnUnused(ByteBuffer buffer) { }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1013
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1014
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1015
    // An implementation of BufferSource used for unencrypted data.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1016
    // This buffer source uses heap buffers and avoids wasting memory
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1017
    // by forwarding read-only buffer slices downstream.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1018
    // Buffers allocated through this source are simply GC'ed when
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1019
    // they are no longer referenced.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1020
    private static final class SliceBufferSource implements BufferSource {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1021
        private final Supplier<ByteBuffer> factory;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1022
        private volatile ByteBuffer current;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1023
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1024
        public SliceBufferSource() {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1025
            this(Utils::getBuffer);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1026
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1027
        public SliceBufferSource(Supplier<ByteBuffer> factory) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1028
            this.factory = Objects.requireNonNull(factory);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1029
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1030
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1031
        // Reuses the same buffer if some space remains available.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1032
        // Otherwise, returns a new heap buffer.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1033
        @Override
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1034
        public final ByteBuffer getBuffer() {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1035
            ByteBuffer buf = current;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1036
            buf = (buf == null || !buf.hasRemaining())
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1037
                    ? (current = factory.get()) : buf;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1038
            assert buf.hasRemaining();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1039
            return buf;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1040
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1041
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1042
        // Adds a read-only slice to the list, potentially returning a
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1043
        // new list with that slice at the end.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1044
        @Override
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1045
        public final List<ByteBuffer> append(List <ByteBuffer> list, ByteBuffer buf, int start) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1046
            // creates a slice to add to the list
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1047
            int limit = buf.limit();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1048
            buf.limit(buf.position());
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1049
            buf.position(start);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1050
            ByteBuffer slice = buf.slice();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1051
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1052
            // restore buffer state to what it was before creating the slice
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1053
            buf.position(buf.limit());
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1054
            buf.limit(limit);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1055
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1056
            // add the buffer to the list
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1057
            return SocketTube.listOf(list, slice.asReadOnlyBuffer());
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1058
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1059
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1060
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1061
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1062
    // An implementation of BufferSource used for encrypted data.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1063
    // This buffer source uses direct byte buffers that will be
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1064
    // recycled by the SocketTube subscriber.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1065
    //
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1066
    private static final class SSLDirectBufferSource implements BufferSource {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1067
        private final BufferSupplier factory;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1068
        private final HttpClientImpl client;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1069
        private ByteBuffer current;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1070
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1071
        public SSLDirectBufferSource(HttpClientImpl client) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1072
            this.client = Objects.requireNonNull(client);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1073
            this.factory = Objects.requireNonNull(client.getSSLBufferSupplier());
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1074
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1075
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1076
        // Obtains a 'free' byte buffer from the pool, or returns
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1077
        // the same buffer if nothing was read at the previous cycle.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1078
        // The subscriber will be responsible for recycling this
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1079
        // buffer into the pool (see SSLFlowDelegate.Reader)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1080
        @Override
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1081
        public final ByteBuffer getBuffer() {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1082
            assert client.isSelectorThread();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1083
            ByteBuffer buf = current;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1084
            if (buf == null) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1085
                buf = current = factory.get();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1086
            }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1087
            assert buf.hasRemaining();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1088
            assert buf.position() == 0;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1089
            return buf;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1090
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1091
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1092
        // Adds the buffer to the list. The buffer will be later returned to the
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1093
        // pool by the subscriber (see SSLFlowDelegate.Reader).
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1094
        // The next buffer returned by getBuffer() will be obtained from the
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1095
        // pool. It might be the same buffer or another one.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1096
        // Because socket tube can read up to MAX_BUFFERS = 3 buffers, and because
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1097
        // recycling will happen in the flow before onNext returns, then the
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1098
        // pool can not grow larger than MAX_BUFFERS = 3 buffers, even though
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1099
        // it's shared by all SSL connections opened on that client.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1100
        @Override
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1101
        public final List<ByteBuffer> append(List <ByteBuffer> list, ByteBuffer buf, int start) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1102
            assert client.isSelectorThread();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1103
            assert buf.isDirect();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1104
            assert start == 0;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1105
            assert current == buf;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1106
            current = null;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1107
            buf.limit(buf.position());
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1108
            buf.position(start);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1109
            // add the buffer to the list
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1110
            return SocketTube.listOf(list, buf);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1111
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1112
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1113
        @Override
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1114
        public void returnUnused(ByteBuffer buffer) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1115
            // if current is null, then the buffer will have been added to the
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1116
            // list, through append. Otherwise, current is not null, and needs
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1117
            // to be returned to prevent the buffer supplier pool from growing
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1118
            // to more than MAX_BUFFERS.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1119
            assert buffer == current;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1120
            ByteBuffer buf = current;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1121
            if (buf != null) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1122
                assert buf.position() == 0;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1123
                current = null;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1124
                // the supplier assert if buf has remaining
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1125
                buf.limit(buf.position());
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1126
                factory.recycle(buf);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1127
            }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1128
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1129
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1130
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1131
    // ===================================================================== //
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1132
    //                   Socket Channel Read/Write                           //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1133
    // ===================================================================== //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1134
    static final int MAX_BUFFERS = 3;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1135
    static final List<ByteBuffer> EOF = List.of();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1136
    static final List<ByteBuffer> NOTHING = List.of(Utils.EMPTY_BYTEBUFFER);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1137
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1138
    // readAvailable() will read bytes into the 'current' ByteBuffer until
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1139
    // the ByteBuffer is full, or 0 or -1 (EOF) is returned by read().
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1140
    // When that happens, a slice of the data that has been read so far
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1141
    // is inserted into the returned buffer list, and if the current buffer
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1142
    // has remaining space, that space will be used to read more data when
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1143
    // the channel becomes readable again.
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1144
    private List<ByteBuffer> readAvailable(BufferSource buffersSource) throws IOException {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1145
        ByteBuffer buf = buffersSource.getBuffer();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1146
        assert buf.hasRemaining();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1147
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1148
        int read;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1149
        int pos = buf.position();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1150
        List<ByteBuffer> list = null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1151
        while (buf.hasRemaining()) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1152
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1153
                while ((read = channel.read(buf)) > 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1154
                    if (!buf.hasRemaining())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1155
                        break;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1156
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1157
            } catch (IOException x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1158
                if (buf.position() == pos && list == null) {
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1159
                    // make sure that the buffer source will recycle
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1160
                    // 'buf' if needed
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1161
                    buffersSource.returnUnused(buf);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1162
                    // no bytes have been read, just throw...
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1163
                    throw x;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1164
                } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1165
                    // some bytes have been read, return them and fail next time
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1166
                    errorRef.compareAndSet(null, x);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1167
                    read = 0; // ensures outer loop will exit
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1168
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1169
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1170
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1171
            // nothing read;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1172
            if (buf.position() == pos) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1173
                // An empty list signals the end of data, and should only be
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1174
                // returned if read == -1. If some data has already been read,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1175
                // then it must be returned. -1 will be returned next time
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1176
                // the caller attempts to read something.
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1177
                buffersSource.returnUnused(buf);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1178
                if (list == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1179
                    // nothing read - list was null - return EOF or NOTHING
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1180
                    list = read == -1 ? EOF : NOTHING;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1181
                }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1182
                break;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1183
            }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1184
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1185
            // check whether this buffer has still some free space available.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1186
            // if so, we will keep it for the next round.
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1187
            list = buffersSource.append(list, buf, pos);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1188
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1189
            if (read <= 0 || list.size() == MAX_BUFFERS) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1190
                break;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1191
            }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1192
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1193
            buf = buffersSource.getBuffer();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1194
            pos = buf.position();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1195
            assert buf.hasRemaining();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1196
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1197
        return list;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1198
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1199
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1200
    private static <T> List<T> listOf(List<T> list, T item) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1201
        int size = list == null ? 0 : list.size();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1202
        switch (size) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1203
            case 0: return List.of(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1204
            case 1: return List.of(list.get(0), item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1205
            case 2: return List.of(list.get(0), list.get(1), item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1206
            default: // slow path if MAX_BUFFERS > 3
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
  1207
                List<T> res = list instanceof ArrayList ? list : new ArrayList<>(list);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1208
                res.add(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1209
                return res;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1210
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1211
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1212
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1213
    private long writeAvailable(List<ByteBuffer> bytes) throws IOException {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1214
        ByteBuffer[] srcs = bytes.toArray(Utils.EMPTY_BB_ARRAY);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1215
        final long remaining = Utils.remaining(srcs);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1216
        long written = 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1217
        while (remaining > written) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1218
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1219
                long w = channel.write(srcs);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1220
                assert w >= 0 : "negative number of bytes written:" + w;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1221
                if (w == 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1222
                    break;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1223
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1224
                written += w;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1225
            } catch (IOException x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1226
                if (written == 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1227
                    // no bytes were written just throw
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1228
                    throw x;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1229
                } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1230
                    // return how many bytes were written, will fail next time
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1231
                    break;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1232
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1233
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1234
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1235
        return written;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1236
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1237
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1238
    private void resumeEvent(SocketFlowEvent event,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1239
                             Consumer<Throwable> errorSignaler) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1240
        boolean registrationRequired;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1241
        synchronized(lock) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1242
            registrationRequired = !event.registered();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1243
            event.resume();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1244
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1245
        try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1246
            if (registrationRequired) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1247
                client.registerEvent(event);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1248
             } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1249
                client.eventUpdated(event);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1250
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1251
        } catch(Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1252
            errorSignaler.accept(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1253
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1254
   }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1255
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1256
    private void pauseEvent(SocketFlowEvent event,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1257
                            Consumer<Throwable> errorSignaler) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1258
        synchronized(lock) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1259
            event.pause();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1260
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1261
        try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1262
            client.eventUpdated(event);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1263
        } catch(Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1264
            errorSignaler.accept(t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1265
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1266
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1267
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1268
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1269
    public void connectFlows(TubePublisher writePublisher,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1270
                             TubeSubscriber readSubscriber) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
  1271
        if (debug.on()) debug.log("connecting flows");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1272
        this.subscribe(readSubscriber);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1273
        writePublisher.subscribe(this);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1274
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1275
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1276
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1277
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1278
    public String toString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1279
        return dbgString();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1280
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1281
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1282
    final String dbgString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1283
        return "SocketTube("+id+")";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1284
    }
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
  1285
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
  1286
    final String channelDescr() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
  1287
        return String.valueOf(channel);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
  1288
    }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
  1289
}