src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/common/SSLTube.java
author dfuchs
Mon, 06 Nov 2017 18:17:09 +0000
branchhttp-client-branch
changeset 55768 8674257c75ce
parent 55763 634d8e14c172
child 55792 0936888d5a4a
permissions -rw-r--r--
http-client-branch: SSLTube should call onSubscribe in the flow
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     1
/*
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     4
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    10
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    15
 * accompanied this code).
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    16
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    20
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    23
 * questions.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    24
 */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    25
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    26
package jdk.incubator.http.internal.common;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    27
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    28
import java.lang.System.Logger.Level;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    29
import java.nio.ByteBuffer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    30
import java.util.List;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    31
import java.util.Objects;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    32
import java.util.concurrent.CompletableFuture;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    33
import java.util.concurrent.Executor;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    34
import java.util.concurrent.Flow;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    35
import java.util.concurrent.atomic.AtomicReference;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    36
import java.util.function.Consumer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    37
import javax.net.ssl.SSLEngine;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    38
import javax.net.ssl.SSLHandshakeException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    39
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    40
import jdk.incubator.http.internal.common.SubscriberWrapper.SchedulingAction;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    41
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    42
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.FINISHED;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    43
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    44
public class SSLTube implements FlowTube {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    45
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    46
    static final boolean DEBUG = Utils.DEBUG; // revisit: temporary developer's flag.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    47
    final System.Logger debug =
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    48
            Utils.getDebugLogger(this::dbgString, DEBUG);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    49
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    50
    private final FlowTube tube;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    51
    private final SSLSubscriberWrapper readSubscriber;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    52
    private final SSLSubscriptionWrapper writeSubscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    53
    private final SSLFlowDelegate sslDelegate;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    54
    private final SSLEngine engine;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    55
    private volatile boolean finished;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    56
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    57
    public SSLTube(SSLEngine engine, Executor executor, FlowTube tube) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    58
        Objects.requireNonNull(engine);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    59
        Objects.requireNonNull(executor);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    60
        this.tube = Objects.requireNonNull(tube);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    61
        writeSubscription = new SSLSubscriptionWrapper();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    62
        readSubscriber = new SSLSubscriberWrapper();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    63
        this.engine = engine;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    64
        sslDelegate = new SSLTubeFlowDelegate(engine,
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    65
                                          executor,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    66
                                          readSubscriber,
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    67
                                          tube);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    68
        tube.subscribe(sslDelegate.upstreamReader());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    69
        sslDelegate.upstreamWriter().onSubscribe(writeSubscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    70
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    71
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    72
    // the other possibility would be to pass a lambda to the
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    73
    // constructor of SSLFlowDelegate (instead of subclassing it).
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    74
    final class SSLTubeFlowDelegate extends SSLFlowDelegate {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    75
        SSLTubeFlowDelegate(SSLEngine engine, Executor executor,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    76
                            SSLSubscriberWrapper readSubscriber,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    77
                            FlowTube tube) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    78
            super(engine, executor, readSubscriber, tube);
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    79
        }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    80
        protected SchedulingAction enterReadScheduling() {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    81
            readSubscriber.processPendingSubscriber();
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    82
            return SchedulingAction.CONTINUE;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    83
        }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    84
    }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    85
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    86
    public CompletableFuture<String> getALPN() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    87
        return sslDelegate.alpn();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    88
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    89
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    90
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    91
    public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    92
        readSubscriber.dropSubscription();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    93
        readSubscriber.setDelegate(s);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    94
        s.onSubscribe(readSubscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    95
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    96
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    97
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    98
     * Tells whether, or not, this FlowTube has finished receiving data.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    99
     *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   100
     * @return true when one of this FlowTube Subscriber's OnError or onComplete
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   101
     * methods have been invoked
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   102
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   103
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   104
    public boolean isFinished() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   105
        return finished;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   106
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   107
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   108
    private volatile Flow.Subscription readSubscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   109
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   110
    // The DelegateWrapper wraps a subscribed {@code Flow.Subscriber} and
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   111
    // tracks the subscriber's state. In particular it makes sure that
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   112
    // onComplete/onError are not called before onSubscribed.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   113
    final static class DelegateWrapper implements FlowTube.TubeSubscriber {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   114
        private final FlowTube.TubeSubscriber delegate;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   115
        private final System.Logger debug;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   116
        volatile boolean subscribedCalled;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   117
        volatile boolean subscribedDone;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   118
        volatile boolean completed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   119
        volatile Throwable error;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   120
        DelegateWrapper(Flow.Subscriber<? super List<ByteBuffer>> delegate,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   121
                        System.Logger debug) {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   122
            this.delegate = FlowTube.asTubeSubscriber(delegate);
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   123
            this.debug = debug;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   124
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   125
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   126
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   127
        public void dropSubscription() {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   128
            if (subscribedCalled && !completed) {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   129
                delegate.dropSubscription();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   130
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   131
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   132
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   133
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   134
        public void onNext(List<ByteBuffer> item) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   135
            assert subscribedCalled;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   136
            delegate.onNext(item);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   137
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   138
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   139
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   140
        public void onSubscribe(Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   141
            onSubscribe(delegate::onSubscribe, subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   142
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   143
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   144
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   145
        public void onConnection(Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   146
            onSubscribe(delegate::onConnection, subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   147
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   148
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   149
        private void onSubscribe(Consumer<Flow.Subscription> method,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   150
                                 Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   151
            subscribedCalled = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   152
            method.accept(subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   153
            Throwable x;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   154
            boolean finished;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   155
            synchronized (this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   156
                subscribedDone = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   157
                x = error;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   158
                finished = completed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   159
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   160
            if (x != null) {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   161
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   162
                          "Subscriber completed before subscribe: forwarding %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   163
                          (Object)x);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   164
                delegate.onError(x);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   165
            } else if (finished) {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   166
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   167
                          "Subscriber completed before subscribe: calling onComplete()");
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   168
                delegate.onComplete();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   169
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   170
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   171
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   172
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   173
        public void onError(Throwable t) {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   174
            if (completed) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   175
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   176
                          "Subscriber already completed: ignoring %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   177
                          (Object)t);
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   178
                return;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   179
            }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   180
            boolean subscribed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   181
            synchronized (this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   182
                if (completed) return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   183
                error = t;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   184
                completed = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   185
                subscribed = subscribedDone;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   186
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   187
            if (subscribed) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   188
                delegate.onError(t);
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   189
            } else {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   190
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   191
                          "Subscriber not yet subscribed: stored %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   192
                          (Object)t);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   193
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   194
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   195
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   196
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   197
        public void onComplete() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   198
            if (completed) return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   199
            boolean subscribed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   200
            synchronized (this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   201
                if (completed) return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   202
                completed = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   203
                subscribed = subscribedDone;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   204
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   205
            if (subscribed) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   206
                delegate.onComplete();
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   207
            } else {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   208
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   209
                          "Subscriber not yet subscribed: stored completed=true");
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   210
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   211
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   212
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   213
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   214
        public String toString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   215
            return "DelegateWrapper:" + delegate.toString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   216
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   217
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   218
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   219
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   220
    // Used to read data from the SSLTube.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   221
    final class SSLSubscriberWrapper implements FlowTube.TubeSubscriber {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   222
        private AtomicReference<DelegateWrapper> pendingDelegate =
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   223
                new AtomicReference<>();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   224
        private volatile DelegateWrapper subscribed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   225
        private volatile boolean onCompleteReceived;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   226
        private final AtomicReference<Throwable> errorRef
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   227
                = new AtomicReference<>();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   228
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   229
        // setDelegate can be called asynchronously when the SSLTube flow
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   230
        // is connected. At this time the permanent subscriber (this class)
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   231
        // may already be subscribed (readSubscription != null) or not.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   232
        // 1. If it's already subscribed (readSubscription != null), we
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   233
        //    are going to signal the SSLFlowDelegate reader, and make sure
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   234
        //    onSubscribed is called within the reader flow
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   235
        // 2. If it's not yet subscribed (readSubscription == null), then
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   236
        //    we're going to wait for onSubscribe/onConnection to be called.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   237
        //
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   238
        void setDelegate(Flow.Subscriber<? super List<ByteBuffer>> delegate) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   239
            debug.log(Level.DEBUG, "SSLSubscriberWrapper (reader) got delegate: %s",
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   240
                      delegate);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   241
            assert delegate != null;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   242
            DelegateWrapper delegateWrapper = new DelegateWrapper(delegate, debug);
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   243
            DelegateWrapper previous;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   244
            Flow.Subscription subscription;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   245
            boolean handleNow;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   246
            synchronized (this) {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   247
                previous = pendingDelegate.getAndSet(delegateWrapper);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   248
                subscription = readSubscription;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   249
                handleNow = this.errorRef.get() != null || finished;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   250
            }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   251
            if (previous != null) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   252
                previous.dropSubscription();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   253
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   254
            if (subscription == null) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   255
                debug.log(Level.DEBUG, "SSLSubscriberWrapper (reader) no subscription yet");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   256
                return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   257
            }
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   258
            if (handleNow || !sslDelegate.resumeReader()) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   259
                processPendingSubscriber();
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   260
            }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   261
        }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   262
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   263
        // Can be called outside of the flow if an error has already been
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   264
        // raise. Otherwise, must be called within the SSLFlowDelegate
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   265
        // downstream reader flow.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   266
        // If there is a subscription, and if there is a pending delegate,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   267
        // calls dropSubscription() on the previous delegate (if any),
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   268
        // then subscribe the pending delegate.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   269
        void processPendingSubscriber() {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   270
            Flow.Subscription subscription;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   271
            DelegateWrapper delegateWrapper, previous;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   272
            synchronized (this) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   273
                delegateWrapper = pendingDelegate.get();
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   274
                if (delegateWrapper == null) return;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   275
                subscription = readSubscription;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   276
                previous = subscribed;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   277
            }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   278
            if (subscription == null) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   279
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   280
                         "SSLSubscriberWrapper (reader) %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   281
                         "processPendingSubscriber: no subscription yet");
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   282
                return;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   283
            }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   284
            delegateWrapper = pendingDelegate.getAndSet(null);
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   285
            if (delegateWrapper == null) return;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   286
            if (previous != null) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   287
                previous.dropSubscription();
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   288
            }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   289
            onNewSubscription(delegateWrapper,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   290
                              delegateWrapper::onSubscribe,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   291
                              subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   292
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   293
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   294
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   295
        public void dropSubscription() {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   296
            DelegateWrapper subscriberImpl = subscribed;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   297
            if (subscriberImpl != null) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   298
                subscriberImpl.dropSubscription();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   299
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   300
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   301
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   302
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   303
        public void onConnection(Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   304
            debug.log(Level.DEBUG,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   305
                      "SSLSubscriberWrapper (reader) onConnection(%s)",
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   306
                      subscription);
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   307
            onSubscribeImpl(subscription);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   308
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   309
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   310
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   311
        public void onSubscribe(Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   312
            debug.log(Level.DEBUG,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   313
                      "SSLSubscriberWrapper (reader) onSubscribe(%s)",
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   314
                      subscription);
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   315
            onSubscribeImpl(subscription);
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   316
        }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   317
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   318
        // called in the reader flow, from either onSubscribe or onConnection.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   319
        private void onSubscribeImpl(Flow.Subscription subscription) {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   320
            assert subscription != null;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   321
            DelegateWrapper subscriberImpl, pending;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   322
            synchronized (this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   323
                readSubscription = subscription;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   324
                subscriberImpl = subscribed;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   325
                pending = pendingDelegate.get();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   326
            }
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   327
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   328
            if (subscriberImpl == null && pending == null) {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   329
                debug.log(Level.DEBUG,
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   330
                      "SSLSubscriberWrapper (reader) onSubscribeImpl: %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   331
                      "no delegate yet");
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   332
                return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   333
            }
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   334
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   335
            if (pending == null) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   336
                // There is no pending delegate, but we have a previously
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   337
                // subscribed delegate. This is obviously a re-subscribe.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   338
                // We are in the downstream reader flow, so we should call
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   339
                // onConnection directly.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   340
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   341
                      "SSLSubscriberWrapper (reader) onSubscribeImpl: %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   342
                      "resusbcribing");
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   343
                onNewSubscription(subscriberImpl,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   344
                                  subscriberImpl::onConnection,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   345
                                  subscription);
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   346
            } else {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   347
                // We have some pending subscriber: subscribe it now that we have
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   348
                // a subscription. If we already had a previous delegate then
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   349
                // it will get a dropSubscription().
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   350
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   351
                      "SSLSubscriberWrapper (reader) onSubscribeImpl: %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   352
                      "subscribing pending");
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   353
                processPendingSubscriber();
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   354
            }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   355
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   356
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   357
        private void onNewSubscription(DelegateWrapper subscriberImpl,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   358
                                       Consumer<Flow.Subscription> method,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   359
                                       Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   360
            assert subscriberImpl != null;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   361
            assert method != null;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   362
            assert subscription != null;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   363
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   364
            Throwable failed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   365
            boolean completed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   366
            // reset any demand that may have been made by the previous
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   367
            // subscriber
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   368
            sslDelegate.resetReaderDemand();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   369
            // send the subscription to the subscriber.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   370
            method.accept(subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   371
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   372
            // The following twisted logic is just here that we don't invoke
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   373
            // onError before onSubscribe. It also prevents race conditions
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   374
            // if onError is invoked concurrently with setDelegate.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   375
            synchronized (this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   376
                failed = this.errorRef.get();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   377
                completed = finished;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   378
                subscribed = subscriberImpl;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   379
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   380
            if (failed != null) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   381
                subscriberImpl.onError(failed);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   382
            } else if (completed) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   383
                subscriberImpl.onComplete();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   384
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   385
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   386
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   387
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   388
        public void onNext(List<ByteBuffer> item) {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   389
            subscribed.onNext(item);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   390
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   391
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   392
        public void onErrorImpl(Throwable throwable) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   393
            // The following twisted logic is just here that we don't invoke
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   394
            // onError before onSubscribe. It also prevents race conditions
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   395
            // if onError is invoked concurrently with setDelegate.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   396
            // See setDelegate.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   397
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   398
            errorRef.compareAndSet(null, throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   399
            Throwable failed = errorRef.get();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   400
            finished = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   401
            debug.log(Level.DEBUG, "%s: onErrorImpl: %s", this, throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   402
            DelegateWrapper subscriberImpl;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   403
            synchronized (this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   404
                subscriberImpl = subscribed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   405
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   406
            if (subscriberImpl != null) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   407
                subscriberImpl.onError(failed);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   408
            } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   409
                debug.log(Level.DEBUG, "%s: delegate null, stored %s", this, failed);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   410
            }
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   411
            // now if we have any pending subscriber, we should forward
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   412
            // the error to them immediately as the read scheduler will
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   413
            // already be stopped.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   414
            processPendingSubscriber();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   415
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   416
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   417
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   418
        public void onError(Throwable throwable) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   419
            assert !finished && !onCompleteReceived;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   420
            onErrorImpl(throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   421
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   422
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   423
        private boolean handshaking() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   424
            HandshakeStatus hs = engine.getHandshakeStatus();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   425
            return !(hs == NOT_HANDSHAKING || hs == FINISHED);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   426
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   427
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   428
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   429
        public void onComplete() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   430
            assert !finished && !onCompleteReceived;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   431
            onCompleteReceived = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   432
            DelegateWrapper subscriberImpl;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   433
            synchronized(this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   434
                subscriberImpl = subscribed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   435
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   436
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   437
            if (handshaking()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   438
                onErrorImpl(new SSLHandshakeException(
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   439
                        "Remote host terminated the handshake"));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   440
            } else if (subscriberImpl != null) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   441
                finished = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   442
                subscriberImpl.onComplete();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   443
            }
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   444
            // now if we have any pending subscriber, we should complete
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   445
            // them immediately as the read scheduler will already be stopped.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   446
            processPendingSubscriber();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   447
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   448
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   449
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   450
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   451
    public void connectFlows(TubePublisher writePub,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   452
                             TubeSubscriber readSub) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   453
        debug.log(Level.DEBUG, "connecting flows");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   454
        readSubscriber.setDelegate(readSub);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   455
        writePub.subscribe(this);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   456
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   457
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   458
    /** Outstanding write demand from the SSL Flow Delegate. */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   459
    private final Demand writeDemand = new Demand();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   460
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   461
    final class SSLSubscriptionWrapper implements Flow.Subscription {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   462
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   463
        volatile Flow.Subscription delegate;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   464
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   465
        void setSubscription(Flow.Subscription sub) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   466
            long demand = writeDemand.get(); // FIXME: isn't it a racy way of passing the demand?
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   467
            delegate = sub;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   468
            debug.log(Level.DEBUG, "setSubscription: demand=%d", demand);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   469
            if (demand > 0)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   470
                sub.request(demand);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   471
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   472
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   473
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   474
        public void request(long n) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   475
            writeDemand.increase(n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   476
            debug.log(Level.DEBUG, "request: n=%d", n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   477
            Flow.Subscription sub = delegate;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   478
            if (sub != null && n > 0) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   479
                sub.request(n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   480
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   481
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   482
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   483
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   484
        public void cancel() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   485
            // TODO:  no-op or error?
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   486
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   487
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   488
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   489
    /* Subscriber - writing side */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   490
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   491
    public void onSubscribe(Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   492
        Objects.requireNonNull(subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   493
        Flow.Subscription x = writeSubscription.delegate;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   494
        if (x != null)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   495
            x.cancel();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   496
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   497
        writeSubscription.setSubscription(subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   498
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   499
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   500
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   501
    public void onNext(List<ByteBuffer> item) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   502
        Objects.requireNonNull(item);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   503
        boolean decremented = writeDemand.tryDecrement();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   504
        assert decremented : "Unexpected writeDemand: ";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   505
        debug.log(Level.DEBUG,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   506
                "sending %d  buffers to SSL flow delegate", item.size());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   507
        sslDelegate.upstreamWriter().onNext(item);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   508
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   509
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   510
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   511
    public void onError(Throwable throwable) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   512
        Objects.requireNonNull(throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   513
        sslDelegate.upstreamWriter().onError(throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   514
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   515
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   516
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   517
    public void onComplete() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   518
        sslDelegate.upstreamWriter().onComplete();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   519
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   520
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   521
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   522
    public String toString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   523
        return dbgString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   524
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   525
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   526
    final String dbgString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   527
        return "SSLTube(" + tube + ")";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   528
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   529
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   530
}