src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/common/SSLTube.java
author dfuchs
Wed, 29 Nov 2017 11:15:19 +0000
branchhttp-client-branch
changeset 55909 583695a0ed6a
parent 55798 fa84be3c77e4
child 55950 5e1707e5a254
permissions -rw-r--r--
http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
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
55798
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    44
/**
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    45
 * An implementation of FlowTube that wraps another FlowTube in an
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    46
 * SSL flow.
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    47
 * <p>
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    48
 * The following diagram shows a typical usage of the SSLTube, where
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    49
 * the SSLTube wraps a SocketTube on the right hand side, and is connected
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    50
 * to an HttpConnection on the left hand side.
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    51
 *
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    52
 * <preformatted>{@code
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    53
 *                  +----------  SSLTube -------------------------+
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    54
 *                  |                                             |
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    55
 *                  |                    +---SSLFlowDelegate---+  |
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    56
 *  HttpConnection  |                    |                     |  |   SocketTube
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    57
 *    read sink  <- SSLSubscriberW.   <- Reader <- upstreamR.() <---- read source
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    58
 *  (a subscriber)  |                    |    \         /      |  |  (a publisher)
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    59
 *                  |                    |     SSLEngine       |  |
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    60
 *  HttpConnection  |                    |    /         \      |  |   SocketTube
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    61
 *  write source -> SSLSubscriptionW. -> upstreamW.() -> Writer ----> write sink
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    62
 *  (a publisher)   |                    |                     |  |  (a subscriber)
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    63
 *                  |                    +---------------------+  |
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    64
 *                  |                                             |
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    65
 *                  +---------------------------------------------+
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    66
 * }</preformatted>
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    67
 */
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    68
public class SSLTube implements FlowTube {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    69
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    70
    static final boolean DEBUG = Utils.DEBUG; // revisit: temporary developer's flag.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    71
    final System.Logger debug =
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    72
            Utils.getDebugLogger(this::dbgString, DEBUG);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    73
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    74
    private final FlowTube tube;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    75
    private final SSLSubscriberWrapper readSubscriber;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    76
    private final SSLSubscriptionWrapper writeSubscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    77
    private final SSLFlowDelegate sslDelegate;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    78
    private final SSLEngine engine;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    79
    private volatile boolean finished;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    80
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    81
    public SSLTube(SSLEngine engine, Executor executor, FlowTube tube) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    82
        Objects.requireNonNull(engine);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    83
        Objects.requireNonNull(executor);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    84
        this.tube = Objects.requireNonNull(tube);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    85
        writeSubscription = new SSLSubscriptionWrapper();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    86
        readSubscriber = new SSLSubscriberWrapper();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    87
        this.engine = engine;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    88
        sslDelegate = new SSLTubeFlowDelegate(engine,
55798
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    89
                                              executor,
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    90
                                              readSubscriber,
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
    91
                                              tube);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    92
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    93
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    94
    final class SSLTubeFlowDelegate extends SSLFlowDelegate {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    95
        SSLTubeFlowDelegate(SSLEngine engine, Executor executor,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    96
                            SSLSubscriberWrapper readSubscriber,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    97
                            FlowTube tube) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    98
            super(engine, executor, readSubscriber, tube);
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    99
        }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   100
        protected SchedulingAction enterReadScheduling() {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   101
            readSubscriber.processPendingSubscriber();
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   102
            return SchedulingAction.CONTINUE;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   103
        }
55798
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   104
        void connect(Flow.Subscriber<? super List<ByteBuffer>> downReader,
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   105
                     Flow.Subscriber<? super List<ByteBuffer>> downWriter) {
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   106
            assert downWriter == tube;
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   107
            assert downReader == readSubscriber;
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   108
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   109
            // Connect the read sink first. That's the left-hand side
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   110
            // downstream subscriber from the HttpConnection (or more
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   111
            // accurately, the SSLSubscriberWrapper that will wrap it
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   112
            // when SSLTube::connectFlows is called.
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   113
            reader.subscribe(downReader);
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   114
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   115
            // Connect the right hand side tube (the socket tube).
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   116
            //
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   117
            // The SSLFlowDelegate.writer publishes ByteBuffer to
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   118
            // the SocketTube for writing on the socket, and the
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   119
            // SSLFlowDelegate::upstreamReader subscribes to the
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   120
            // SocketTube to receive ByteBuffers read from the socket.
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   121
            //
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   122
            // Basically this method is equivalent to:
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   123
            //     // connect the read source:
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   124
            //     //   subscribe the SSLFlowDelegate upstream reader
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   125
            //     //   to the socket tube publisher.
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   126
            //     tube.subscribe(upstreamReader());
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   127
            //     // connect the write sink:
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   128
            //     //   subscribe the socket tube write subscriber
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   129
            //     //   with the SSLFlowDelegate downstream writer.
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   130
            //     writer.subscribe(tube);
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   131
            tube.connectFlows(FlowTube.asTubePublisher(writer),
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   132
                              FlowTube.asTubeSubscriber(upstreamReader()));
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   133
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   134
            // Finally connect the write source. That's the left
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   135
            // hand side publisher which will push ByteBuffer for
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   136
            // writing and encryption to the SSLFlowDelegate.
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   137
            // The writeSubscription is in fact the SSLSubscriptionWrapper
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   138
            // that will wrap the subscription provided by the
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   139
            // HttpConnection publisher when SSLTube::connectFlows
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   140
            // is called.
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   141
            upstreamWriter().onSubscribe(writeSubscription);
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   142
        }
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   143
    }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   144
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   145
    public CompletableFuture<String> getALPN() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   146
        return sslDelegate.alpn();
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
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   150
    public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   151
        readSubscriber.dropSubscription();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   152
        readSubscriber.setDelegate(s);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   153
        s.onSubscribe(readSubscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   154
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   155
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   156
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   157
     * Tells whether, or not, this FlowTube has finished receiving data.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   158
     *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   159
     * @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
   160
     * methods have been invoked
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   161
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   162
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   163
    public boolean isFinished() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   164
        return finished;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   165
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   166
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   167
    private volatile Flow.Subscription readSubscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   168
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   169
    // The DelegateWrapper wraps a subscribed {@code Flow.Subscriber} and
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   170
    // tracks the subscriber's state. In particular it makes sure that
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   171
    // onComplete/onError are not called before onSubscribed.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   172
    final static class DelegateWrapper implements FlowTube.TubeSubscriber {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   173
        private final FlowTube.TubeSubscriber delegate;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   174
        private final System.Logger debug;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   175
        volatile boolean subscribedCalled;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   176
        volatile boolean subscribedDone;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   177
        volatile boolean completed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   178
        volatile Throwable error;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   179
        DelegateWrapper(Flow.Subscriber<? super List<ByteBuffer>> delegate,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   180
                        System.Logger debug) {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   181
            this.delegate = FlowTube.asTubeSubscriber(delegate);
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   182
            this.debug = debug;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   183
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   184
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   185
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   186
        public void dropSubscription() {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   187
            if (subscribedCalled && !completed) {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   188
                delegate.dropSubscription();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   189
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   190
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   191
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   192
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   193
        public void onNext(List<ByteBuffer> item) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   194
            assert subscribedCalled;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   195
            delegate.onNext(item);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   196
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   197
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   198
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   199
        public void onSubscribe(Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   200
            onSubscribe(delegate::onSubscribe, subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   201
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   202
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   203
        private void onSubscribe(Consumer<Flow.Subscription> method,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   204
                                 Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   205
            subscribedCalled = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   206
            method.accept(subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   207
            Throwable x;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   208
            boolean finished;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   209
            synchronized (this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   210
                subscribedDone = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   211
                x = error;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   212
                finished = completed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   213
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   214
            if (x != null) {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   215
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   216
                          "Subscriber completed before subscribe: forwarding %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   217
                          (Object)x);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   218
                delegate.onError(x);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   219
            } else if (finished) {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   220
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   221
                          "Subscriber completed before subscribe: calling onComplete()");
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   222
                delegate.onComplete();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   223
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   224
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   225
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   226
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   227
        public void onError(Throwable t) {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   228
            if (completed) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   229
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   230
                          "Subscriber already completed: ignoring %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   231
                          (Object)t);
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   232
                return;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   233
            }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   234
            boolean subscribed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   235
            synchronized (this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   236
                if (completed) return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   237
                error = t;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   238
                completed = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   239
                subscribed = subscribedDone;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   240
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   241
            if (subscribed) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   242
                delegate.onError(t);
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   243
            } else {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   244
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   245
                          "Subscriber not yet subscribed: stored %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   246
                          (Object)t);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   247
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   248
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   249
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   250
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   251
        public void onComplete() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   252
            if (completed) return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   253
            boolean subscribed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   254
            synchronized (this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   255
                if (completed) return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   256
                completed = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   257
                subscribed = subscribedDone;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   258
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   259
            if (subscribed) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   260
                delegate.onComplete();
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   261
            } else {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   262
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   263
                          "Subscriber not yet subscribed: stored completed=true");
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   264
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   265
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   266
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   267
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   268
        public String toString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   269
            return "DelegateWrapper:" + delegate.toString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   270
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   271
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   272
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   273
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   274
    // Used to read data from the SSLTube.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   275
    final class SSLSubscriberWrapper implements FlowTube.TubeSubscriber {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   276
        private AtomicReference<DelegateWrapper> pendingDelegate =
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   277
                new AtomicReference<>();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   278
        private volatile DelegateWrapper subscribed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   279
        private volatile boolean onCompleteReceived;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   280
        private final AtomicReference<Throwable> errorRef
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   281
                = new AtomicReference<>();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   282
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   283
        // 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
   284
        // 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
   285
        // may already be subscribed (readSubscription != null) or not.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   286
        // 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
   287
        //    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
   288
        //    onSubscribed is called within the reader flow
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   289
        // 2. If it's not yet subscribed (readSubscription == null), then
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   290
        //    we're going to wait for onSubscribe to be called.
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   291
        //
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   292
        void setDelegate(Flow.Subscriber<? super List<ByteBuffer>> delegate) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   293
            debug.log(Level.DEBUG, "SSLSubscriberWrapper (reader) got delegate: %s",
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   294
                      delegate);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   295
            assert delegate != null;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   296
            DelegateWrapper delegateWrapper = new DelegateWrapper(delegate, debug);
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   297
            DelegateWrapper previous;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   298
            Flow.Subscription subscription;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   299
            boolean handleNow;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   300
            synchronized (this) {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   301
                previous = pendingDelegate.getAndSet(delegateWrapper);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   302
                subscription = readSubscription;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   303
                handleNow = this.errorRef.get() != null || finished;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   304
            }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   305
            if (previous != null) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   306
                previous.dropSubscription();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   307
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   308
            if (subscription == null) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   309
                debug.log(Level.DEBUG, "SSLSubscriberWrapper (reader) no subscription yet");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   310
                return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   311
            }
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   312
            if (handleNow || !sslDelegate.resumeReader()) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   313
                processPendingSubscriber();
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   314
            }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   315
        }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   316
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   317
        // 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
   318
        // raise. Otherwise, must be called within the SSLFlowDelegate
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   319
        // downstream reader flow.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   320
        // 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
   321
        // calls dropSubscription() on the previous delegate (if any),
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   322
        // then subscribe the pending delegate.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   323
        void processPendingSubscriber() {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   324
            Flow.Subscription subscription;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   325
            DelegateWrapper delegateWrapper, previous;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   326
            synchronized (this) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   327
                delegateWrapper = pendingDelegate.get();
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   328
                if (delegateWrapper == null) return;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   329
                subscription = readSubscription;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   330
                previous = subscribed;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   331
            }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   332
            if (subscription == null) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   333
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   334
                         "SSLSubscriberWrapper (reader) %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   335
                         "processPendingSubscriber: no subscription yet");
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   336
                return;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   337
            }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   338
            delegateWrapper = pendingDelegate.getAndSet(null);
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   339
            if (delegateWrapper == null) return;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   340
            if (previous != null) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   341
                previous.dropSubscription();
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   342
            }
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   343
            onNewSubscription(delegateWrapper, subscription);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   344
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   345
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   346
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   347
        public void dropSubscription() {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   348
            DelegateWrapper subscriberImpl = subscribed;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   349
            if (subscriberImpl != null) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   350
                subscriberImpl.dropSubscription();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   351
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   352
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   353
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   354
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   355
        public void onSubscribe(Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   356
            debug.log(Level.DEBUG,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   357
                      "SSLSubscriberWrapper (reader) onSubscribe(%s)",
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   358
                      subscription);
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   359
            onSubscribeImpl(subscription);
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   360
        }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   361
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   362
        // called in the reader flow, from onSubscribe.
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   363
        private void onSubscribeImpl(Flow.Subscription subscription) {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   364
            assert subscription != null;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   365
            DelegateWrapper subscriberImpl, pending;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   366
            synchronized (this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   367
                readSubscription = subscription;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   368
                subscriberImpl = subscribed;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   369
                pending = pendingDelegate.get();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   370
            }
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   371
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   372
            if (subscriberImpl == null && pending == null) {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   373
                debug.log(Level.DEBUG,
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   374
                      "SSLSubscriberWrapper (reader) onSubscribeImpl: %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   375
                      "no delegate yet");
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   376
                return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   377
            }
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   378
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   379
            if (pending == null) {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   380
                // 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
   381
                // subscribed delegate. This is obviously a re-subscribe.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   382
                // We are in the downstream reader flow, so we should call
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   383
                // onSubscribe directly.
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   384
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   385
                      "SSLSubscriberWrapper (reader) onSubscribeImpl: %s",
55792
0936888d5a4a http-client-branch: (cleanup) unused imports, unused methods, removed 1 class; typos;
prappo
parents: 55768
diff changeset
   386
                      "resubscribing");
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   387
                onNewSubscription(subscriberImpl, subscription);
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   388
            } else {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   389
                // 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
   390
                // 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
   391
                // it will get a dropSubscription().
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   392
                debug.log(Level.DEBUG,
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   393
                      "SSLSubscriberWrapper (reader) onSubscribeImpl: %s",
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   394
                      "subscribing pending");
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   395
                processPendingSubscriber();
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   396
            }
55763
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
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   399
        private void onNewSubscription(DelegateWrapper subscriberImpl,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   400
                                       Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   401
            assert subscriberImpl != null;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   402
            assert subscription != null;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   403
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   404
            Throwable failed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   405
            boolean completed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   406
            // reset any demand that may have been made by the previous
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   407
            // subscriber
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   408
            sslDelegate.resetReaderDemand();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   409
            // send the subscription to the subscriber.
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   410
            subscriberImpl.onSubscribe(subscription);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   411
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   412
            // 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
   413
            // onError before onSubscribe. It also prevents race conditions
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   414
            // if onError is invoked concurrently with setDelegate.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   415
            synchronized (this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   416
                failed = this.errorRef.get();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   417
                completed = finished;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   418
                subscribed = subscriberImpl;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   419
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   420
            if (failed != null) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   421
                subscriberImpl.onError(failed);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   422
            } else if (completed) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   423
                subscriberImpl.onComplete();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   424
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   425
        }
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
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   428
        public void onNext(List<ByteBuffer> item) {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   429
            subscribed.onNext(item);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   430
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   431
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   432
        public void onErrorImpl(Throwable throwable) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   433
            // 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
   434
            // onError before onSubscribe. It also prevents race conditions
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   435
            // if onError is invoked concurrently with setDelegate.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   436
            // See setDelegate.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   437
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   438
            errorRef.compareAndSet(null, throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   439
            Throwable failed = errorRef.get();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   440
            finished = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   441
            debug.log(Level.DEBUG, "%s: onErrorImpl: %s", this, throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   442
            DelegateWrapper subscriberImpl;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   443
            synchronized (this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   444
                subscriberImpl = subscribed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   445
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   446
            if (subscriberImpl != null) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   447
                subscriberImpl.onError(failed);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   448
            } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   449
                debug.log(Level.DEBUG, "%s: delegate null, stored %s", this, failed);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   450
            }
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   451
            // 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
   452
            // 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
   453
            // already be stopped.
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   454
            processPendingSubscriber();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   455
        }
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
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   458
        public void onError(Throwable throwable) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   459
            assert !finished && !onCompleteReceived;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   460
            onErrorImpl(throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   461
        }
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
        private boolean handshaking() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   464
            HandshakeStatus hs = engine.getHandshakeStatus();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   465
            return !(hs == NOT_HANDSHAKING || hs == FINISHED);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   466
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   467
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   468
        private boolean handshakeFailed() {
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   469
            // sslDelegate can be null if we reach here
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   470
            // during the initial handshake, as that happens
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   471
            // within the SSLFlowDelegate constructor.
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   472
            // In that case we will want to raise an exception.
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   473
            return handshaking()
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   474
                    && (sslDelegate == null
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   475
                    || !sslDelegate.closeNotifyReceived());
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   476
        }
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   477
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   478
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   479
        public void onComplete() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   480
            assert !finished && !onCompleteReceived;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   481
            onCompleteReceived = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   482
            DelegateWrapper subscriberImpl;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   483
            synchronized(this) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   484
                subscriberImpl = subscribed;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   485
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   486
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   487
            if (handshakeFailed()) {
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   488
                debug.log(Level.DEBUG,
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   489
                        "handshake: %s, inbound done: %s outbound done: %s",
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   490
                        engine.getHandshakeStatus(),
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   491
                        engine.isInboundDone(),
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   492
                        engine.isOutboundDone());
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   493
                onErrorImpl(new SSLHandshakeException(
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   494
                        "Remote host terminated the handshake"));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   495
            } else if (subscriberImpl != null) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   496
                finished = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   497
                subscriberImpl.onComplete();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   498
            }
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   499
            // 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
   500
            // 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
   501
            processPendingSubscriber();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   502
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   503
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   504
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   505
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   506
    public void connectFlows(TubePublisher writePub,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   507
                             TubeSubscriber readSub) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   508
        debug.log(Level.DEBUG, "connecting flows");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   509
        readSubscriber.setDelegate(readSub);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   510
        writePub.subscribe(this);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   511
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   512
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   513
    /** Outstanding write demand from the SSL Flow Delegate. */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   514
    private final Demand writeDemand = new Demand();
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
    final class SSLSubscriptionWrapper implements Flow.Subscription {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   517
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   518
        volatile Flow.Subscription delegate;
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
        void setSubscription(Flow.Subscription sub) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   521
            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
   522
            delegate = sub;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   523
            debug.log(Level.DEBUG, "setSubscription: demand=%d", demand);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   524
            if (demand > 0)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   525
                sub.request(demand);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   526
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   527
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   528
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   529
        public void request(long n) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   530
            writeDemand.increase(n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   531
            debug.log(Level.DEBUG, "request: n=%d", n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   532
            Flow.Subscription sub = delegate;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   533
            if (sub != null && n > 0) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   534
                sub.request(n);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   535
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   536
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   537
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   538
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   539
        public void cancel() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   540
            // TODO:  no-op or error?
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   541
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   542
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   543
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   544
    /* Subscriber - writing side */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   545
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   546
    public void onSubscribe(Flow.Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   547
        Objects.requireNonNull(subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   548
        Flow.Subscription x = writeSubscription.delegate;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   549
        if (x != null)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   550
            x.cancel();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   551
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   552
        writeSubscription.setSubscription(subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   553
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   554
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   555
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   556
    public void onNext(List<ByteBuffer> item) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   557
        Objects.requireNonNull(item);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   558
        boolean decremented = writeDemand.tryDecrement();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   559
        assert decremented : "Unexpected writeDemand: ";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   560
        debug.log(Level.DEBUG,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   561
                "sending %d  buffers to SSL flow delegate", item.size());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   562
        sslDelegate.upstreamWriter().onNext(item);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   563
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   564
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   565
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   566
    public void onError(Throwable throwable) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   567
        Objects.requireNonNull(throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   568
        sslDelegate.upstreamWriter().onError(throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   569
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   570
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   571
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   572
    public void onComplete() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   573
        sslDelegate.upstreamWriter().onComplete();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   574
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   575
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   576
    @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   577
    public String toString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   578
        return dbgString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   579
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   580
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   581
    final String dbgString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   582
        return "SSLTube(" + tube + ")";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   583
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   584
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   585
}