src/java.net.http/share/classes/jdk/internal/net/http/websocket/TransportImpl.java
author chegar
Wed, 02 May 2018 02:36:17 -0700
changeset 49944 4690a2871b44
parent 49765 ee6f7a61f3a5
child 56451 9585061fdb04
permissions -rw-r--r--
8202423: Small HTTP Client refresh Reviewed-by: chegar, dfuchs, michaelm, prappo Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
package jdk.internal.net.http.websocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
import jdk.internal.net.http.common.Demand;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
import jdk.internal.net.http.common.Logger;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
import jdk.internal.net.http.common.MinimalFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
import jdk.internal.net.http.common.SequentialScheduler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
import jdk.internal.net.http.common.SequentialScheduler.CompleteRestartableTask;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
import jdk.internal.net.http.common.Utils;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
import java.lang.System.Logger.Level;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
import java.nio.ByteBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
import java.nio.CharBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
import java.nio.channels.SelectionKey;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import java.util.concurrent.CompletableFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import java.util.concurrent.atomic.AtomicLong;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import java.util.concurrent.atomic.AtomicReference;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import java.util.function.BiConsumer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import java.util.function.Supplier;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import static jdk.internal.net.http.websocket.TransportImpl.ChannelState.AVAILABLE;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
import static jdk.internal.net.http.websocket.TransportImpl.ChannelState.CLOSED;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import static jdk.internal.net.http.websocket.TransportImpl.ChannelState.UNREGISTERED;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
import static jdk.internal.net.http.websocket.TransportImpl.ChannelState.WAITING;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
public class TransportImpl implements Transport {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
    // -- Debugging infrastructure --
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
    private static final Logger debug =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
            Utils.getWebSocketLogger("[Transport]"::toString, Utils.DEBUG_WS);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
    /* Used for correlating enters to and exists from a method */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
    private final AtomicLong counter = new AtomicLong();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
    private final SequentialScheduler sendScheduler = new SequentialScheduler(new SendTask());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
    private final MessageQueue queue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
    private final MessageEncoder encoder = new MessageEncoder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
    /* A reusable buffer for writing, initially with no remaining bytes */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
    private final ByteBuffer dst = createWriteBuffer().position(0).limit(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
    /* This array is created once for gathering writes accepted by RawChannel */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
    private final ByteBuffer[] dstArray = new ByteBuffer[]{dst};
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
    private final MessageStreamConsumer messageConsumer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
    private final MessageDecoder decoder;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
    private final Frame.Reader reader = new Frame.Reader();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
    private final Demand demand = new Demand();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
    private final SequentialScheduler receiveScheduler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
    private final RawChannel channel;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
    private final Object closeLock = new Object();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
    private final RawChannel.RawEvent writeEvent = new WriteEvent();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
    private final RawChannel.RawEvent readEvent = new ReadEvent();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
    private final AtomicReference<ChannelState> writeState
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
            = new AtomicReference<>(UNREGISTERED);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
    private ByteBuffer data;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
    private volatile ChannelState readState = UNREGISTERED;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
    private boolean inputClosed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
    private boolean outputClosed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
    public TransportImpl(MessageQueue queue, MessageStreamConsumer consumer,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
                         RawChannel channel) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
        this.queue = queue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
        this.messageConsumer = consumer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
        this.channel = channel;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
        this.decoder = new MessageDecoder(this.messageConsumer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
        this.data = channel.initialByteBuffer();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
        // To ensure the initial non-final `data` will be visible
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
        // (happens-before) when `readEvent.handle()` invokes `receiveScheduler`
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
        // the following assignment is done last:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
        receiveScheduler = new SequentialScheduler(new ReceiveTask());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
    private ByteBuffer createWriteBuffer() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
        String name = "jdk.httpclient.websocket.writeBufferSize";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
        int capacity = Utils.getIntegerNetProperty(name, 16384);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
            debug.log("write buffer capacity %s", capacity);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
        // TODO (optimization?): allocateDirect if SSL?
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
        return ByteBuffer.allocate(capacity);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
    private boolean write() throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
            debug.log("writing to the channel");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
        long count = channel.write(dstArray, 0, dstArray.length);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
            debug.log("%s bytes written", count);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
        for (ByteBuffer b : dstArray) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
            if (b.hasRemaining()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
                return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
        return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
    public <T> CompletableFuture<T> sendText(CharSequence message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
                                             boolean isLast,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
                                             T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
                                             BiConsumer<? super T, ? super Throwable> action) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
        long id = 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
            id = counter.incrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
            debug.log("enter send text %s message.length=%s last=%s",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
                              id, message.length(), isLast);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
        // TODO (optimization?):
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
        // These sendXXX methods might be a good place to decide whether or not
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
        // we can write straight ahead, possibly returning null instead of
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
        // creating a CompletableFuture
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
        // Even if the text is already CharBuffer, the client will not be happy
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
        // if they discover the position is changing. So, no instanceof
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
        // cheating, wrap always.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
        CharBuffer text = CharBuffer.wrap(message);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
        MinimalFuture<T> f = new MinimalFuture<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
            queue.addText(text, isLast, attachment, action, f);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
            sendScheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
        } catch (IOException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
            action.accept(null, e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
            f.completeExceptionally(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
            debug.log("exit send text %s returned %s", id, f);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
        return f;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
    public <T> CompletableFuture<T> sendBinary(ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
                                               boolean isLast,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
                                               T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
                                               BiConsumer<? super T, ? super Throwable> action) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
        long id = 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
            id = counter.incrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
            debug.log("enter send binary %s message.remaining=%s last=%s",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
                              id, message.remaining(), isLast);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
        MinimalFuture<T> f = new MinimalFuture<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
            queue.addBinary(message, isLast, attachment, action, f);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
            sendScheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
        } catch (IOException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
            action.accept(null, e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
            f.completeExceptionally(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
            debug.log("exit send binary %s returned %s", id, f);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
        return f;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
    public <T> CompletableFuture<T> sendPing(ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
                                             T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
                                             BiConsumer<? super T, ? super Throwable> action) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
        long id = 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
            id = counter.incrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
            debug.log("enter send ping %s message.remaining=%s",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
                              id, message.remaining());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
        MinimalFuture<T> f = new MinimalFuture<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
            queue.addPing(message, attachment, action, f);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
            sendScheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
        } catch (IOException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
            action.accept(null, e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
            f.completeExceptionally(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
            debug.log("exit send ping %s returned %s", id, f);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
        return f;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
    public <T> CompletableFuture<T> sendPong(ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
                                             T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
                                             BiConsumer<? super T, ? super Throwable> action) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
        long id = 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
            id = counter.incrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
            debug.log("enter send pong %s message.remaining=%s",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
                              id, message.remaining());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
        MinimalFuture<T> f = new MinimalFuture<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
            queue.addPong(message, attachment, action, f);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
            sendScheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
        } catch (IOException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
            action.accept(null, e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
            f.completeExceptionally(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
            debug.log("exit send pong %s returned %s", id, f);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
        return f;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
    public <T> CompletableFuture<T> sendPong(Supplier<? extends ByteBuffer> message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
                                             T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
                                             BiConsumer<? super T, ? super Throwable> action) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
        long id = 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
            id = counter.incrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
            debug.log("enter send pong %s supplier=%s",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
                      id, message);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
        MinimalFuture<T> f = new MinimalFuture<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
            queue.addPong(message, attachment, action, f);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
            sendScheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
        } catch (IOException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
            action.accept(null, e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
            f.completeExceptionally(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
            debug.log("exit send pong %s returned %s", id, f);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
        return f;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
    public <T> CompletableFuture<T> sendClose(int statusCode,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
                                              String reason,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
                                              T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
                                              BiConsumer<? super T, ? super Throwable> action) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
        long id = 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
            id = counter.incrementAndGet();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
            debug.log("enter send close %s statusCode=%s reason.length=%s",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
                              id, statusCode, reason.length());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
        MinimalFuture<T> f = new MinimalFuture<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
            queue.addClose(statusCode, CharBuffer.wrap(reason), attachment, action, f);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
            sendScheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
        } catch (IOException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
            action.accept(null, e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
            f.completeExceptionally(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
            debug.log("exit send close %s returned %s", id, f);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
        return f;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
    public void request(long n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
            debug.log("request %s", n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
        if (demand.increase(n)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
            receiveScheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
    public void acknowledgeReception() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
        boolean decremented = demand.tryDecrement();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
        if (!decremented) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
            throw new InternalError();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   300
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   301
    public void closeOutput() throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   302
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
            debug.log("closeOutput");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
        synchronized (closeLock) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
            if (!outputClosed) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
                outputClosed = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
                    channel.shutdownOutput();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
                } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
                    if (inputClosed) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   312
                        channel.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   313
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   314
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   315
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   316
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   317
        writeState.set(CLOSED);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   318
        sendScheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   319
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   320
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   321
    /*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   322
     * Permanently stops reading from the channel and delivering messages
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   323
     * regardless of the current demand and data availability.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   324
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   325
    @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   326
    public void closeInput() throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   327
        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   328
            debug.log("closeInput");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   329
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   330
        synchronized (closeLock) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   331
            if (!inputClosed) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   332
                inputClosed = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   333
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   334
                    receiveScheduler.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   335
                    channel.shutdownInput();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   336
                } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   337
                    if (outputClosed) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   338
                        channel.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   339
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   340
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   341
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   342
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   343
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   344
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   345
    /* Common states for send and receive tasks */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   346
    enum ChannelState {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   347
        UNREGISTERED,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   348
        AVAILABLE,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   349
        WAITING,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   350
        CLOSED,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   351
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   352
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   353
    @SuppressWarnings({"rawtypes"})
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   354
    private class SendTask extends CompleteRestartableTask {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   355
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   356
        private final MessageQueue.QueueCallback<Boolean, IOException>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   357
                encodingCallback = new MessageQueue.QueueCallback<>() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   358
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   359
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   360
            public <T> Boolean onText(CharBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   361
                                      boolean isLast,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   362
                                      T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   363
                                      BiConsumer<? super T, ? super Throwable> action,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   364
                                      CompletableFuture<? super T> future) throws IOException
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   365
            {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   366
                return encoder.encodeText(message, isLast, dst);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   367
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   368
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   369
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   370
            public <T> Boolean onBinary(ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   371
                                        boolean isLast,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   372
                                        T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   373
                                        BiConsumer<? super T, ? super Throwable> action,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   374
                                        CompletableFuture<? super T> future) throws IOException
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   375
            {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   376
                return encoder.encodeBinary(message, isLast, dst);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   377
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   378
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   379
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   380
            public <T> Boolean onPing(ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   381
                                      T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   382
                                      BiConsumer<? super T, ? super Throwable> action,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   383
                                      CompletableFuture<? super T> future) throws IOException
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   384
            {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   385
                return encoder.encodePing(message, dst);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   386
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   387
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   388
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   389
            public <T> Boolean onPong(ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   390
                                      T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   391
                                      BiConsumer<? super T, ? super Throwable> action,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   392
                                      CompletableFuture<? super T> future) throws IOException
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   393
            {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   394
                return encoder.encodePong(message, dst);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   395
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   396
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   397
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   398
            public <T> Boolean onPong(Supplier<? extends ByteBuffer> message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   399
                                      T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   400
                                      BiConsumer<? super T, ? super Throwable> action,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   401
                                      CompletableFuture<? super T> future) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   402
                return encoder.encodePong(message.get(), dst);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   403
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   404
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   405
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   406
            public <T> Boolean onClose(int statusCode,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   407
                                       CharBuffer reason,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   408
                                       T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   409
                                       BiConsumer<? super T, ? super Throwable> action,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   410
                                       CompletableFuture<? super T> future) throws IOException
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   411
            {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   412
                return encoder.encodeClose(statusCode, reason, dst);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   413
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   414
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   415
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   416
            public Boolean onEmpty() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   417
                return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   418
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   419
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   420
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   421
        /* Whether the task sees the current head message for first time */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   422
        private boolean firstPass = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   423
        /* Whether the message has been fully encoded */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   424
        private boolean encoded;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   425
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   426
        // -- Current message completion communication fields --
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   427
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   428
        private Object attachment;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   429
        private BiConsumer action;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   430
        private CompletableFuture future;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   431
        private final MessageQueue.QueueCallback<Boolean, RuntimeException>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   432
                /* If there is a message, loads its completion communication fields */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   433
                loadCallback = new MessageQueue.QueueCallback<Boolean, RuntimeException>() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   434
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   435
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   436
            public <T> Boolean onText(CharBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   437
                                      boolean isLast,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   438
                                      T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   439
                                      BiConsumer<? super T, ? super Throwable> action,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   440
                                      CompletableFuture<? super T> future)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   441
            {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   442
                SendTask.this.attachment = attachment;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   443
                SendTask.this.action = action;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   444
                SendTask.this.future = future;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   445
                return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   446
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   447
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   448
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   449
            public <T> Boolean onBinary(ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   450
                                        boolean isLast,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   451
                                        T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   452
                                        BiConsumer<? super T, ? super Throwable> action,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   453
                                        CompletableFuture<? super T> future)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   454
            {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   455
                SendTask.this.attachment = attachment;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   456
                SendTask.this.action = action;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   457
                SendTask.this.future = future;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   458
                return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   459
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   460
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   461
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   462
            public <T> Boolean onPing(ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   463
                                      T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   464
                                      BiConsumer<? super T, ? super Throwable> action,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   465
                                      CompletableFuture<? super T> future)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   466
            {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   467
                SendTask.this.attachment = attachment;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   468
                SendTask.this.action = action;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   469
                SendTask.this.future = future;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   470
                return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   471
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   472
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   473
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   474
            public <T> Boolean onPong(ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   475
                                      T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   476
                                      BiConsumer<? super T, ? super Throwable> action,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   477
                                      CompletableFuture<? super T> future)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   478
            {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   479
                SendTask.this.attachment = attachment;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   480
                SendTask.this.action = action;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   481
                SendTask.this.future = future;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   482
                return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   483
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   484
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   485
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   486
            public <T> Boolean onPong(Supplier<? extends ByteBuffer> message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   487
                                      T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   488
                                      BiConsumer<? super T, ? super Throwable> action,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   489
                                      CompletableFuture<? super T> future)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   490
            {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   491
                SendTask.this.attachment = attachment;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   492
                SendTask.this.action = action;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   493
                SendTask.this.future = future;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   494
                return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   495
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   496
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   497
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   498
            public <T> Boolean onClose(int statusCode,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   499
                                       CharBuffer reason,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   500
                                       T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   501
                                       BiConsumer<? super T, ? super Throwable> action,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   502
                                       CompletableFuture<? super T> future)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   503
            {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   504
                SendTask.this.attachment = attachment;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   505
                SendTask.this.action = action;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   506
                SendTask.this.future = future;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   507
                return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   508
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   509
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   510
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   511
            public Boolean onEmpty() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   512
                return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   513
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   514
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   515
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   516
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   517
        public void run() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   518
            // Could have been only called in one of the following cases:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   519
            //   (a) A message has been added to the queue
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   520
            //   (b) The channel is ready for writing
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   521
            if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   522
                debug.log("enter send task");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   523
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   524
            while (!queue.isEmpty()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   525
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   526
                    if (dst.hasRemaining()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   527
                        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   528
                            debug.log("%s bytes remaining in buffer %s",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   529
                                      dst.remaining(), dst);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   530
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   531
                        // The previous part of the binary representation of the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   532
                        // message hasn't been fully written
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   533
                        if (!tryCompleteWrite()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   534
                            break;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   535
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   536
                    } else if (!encoded) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   537
                        if (firstPass) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   538
                            firstPass = false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   539
                            queue.peek(loadCallback);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   540
                            if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   541
                                debug.log("load message");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   542
                            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   543
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   544
                        dst.clear();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   545
                        encoded = queue.peek(encodingCallback);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   546
                        dst.flip();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   547
                        if (!tryCompleteWrite()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   548
                            break;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   549
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   550
                    } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   551
                        // All done, remove and complete
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   552
                        encoder.reset();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   553
                        removeAndComplete(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   554
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   555
                } catch (Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   556
                    if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   557
                        debug.log("send task exception %s", (Object) t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   558
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   559
                    // buffer cleanup: if there is an exception, the buffer
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   560
                    // should appear empty for the next write as there is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   561
                    // nothing to write
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   562
                    dst.position(dst.limit());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   563
                    encoder.reset();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   564
                    removeAndComplete(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   565
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   566
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   567
            if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   568
                debug.log("exit send task");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   569
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   570
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   571
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   572
        private boolean tryCompleteWrite() throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   573
            if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   574
                debug.log("enter writing");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   575
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   576
            boolean finished = false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   577
            loop:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   578
            while (true) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   579
                final ChannelState ws = writeState.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   580
                if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   581
                    debug.log("write state: %s", ws);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   582
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   583
                switch (ws) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   584
                    case WAITING:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   585
                        break loop;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   586
                    case UNREGISTERED:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   587
                        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   588
                            debug.log("registering write event");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   589
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   590
                        channel.registerEvent(writeEvent);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   591
                        writeState.compareAndSet(UNREGISTERED, WAITING);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   592
                        if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   593
                            debug.log("registered write event");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   594
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   595
                        break loop;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   596
                    case AVAILABLE:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   597
                        boolean written = write();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   598
                        if (written) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   599
                            if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   600
                                debug.log("finished writing to the channel");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   601
                            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   602
                            finished = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   603
                            break loop;   // All done
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   604
                        } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   605
                            writeState.compareAndSet(AVAILABLE, UNREGISTERED);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   606
                            continue loop; //  Effectively "goto UNREGISTERED"
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   607
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   608
                    case CLOSED:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   609
                        throw new IOException("Output closed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   610
                    default:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   611
                        throw new InternalError(String.valueOf(ws));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   612
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   613
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   614
            if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   615
                debug.log("exit writing");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   616
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   617
            return finished;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   618
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   619
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   620
        @SuppressWarnings("unchecked")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   621
        private void removeAndComplete(Throwable error) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   622
            if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   623
                debug.log("removeAndComplete error=%s", (Object) error);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   624
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   625
            queue.remove();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   626
            if (error != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   627
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   628
                    action.accept(null, error);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   629
                } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   630
                    future.completeExceptionally(error);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   631
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   632
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   633
                try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   634
                    action.accept(attachment, null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   635
                } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   636
                    future.complete(attachment);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   637
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   638
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   639
            encoded = false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   640
            firstPass = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   641
            attachment = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   642
            action = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   643
            future = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   644
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   645
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   646
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   647
    private class ReceiveTask extends CompleteRestartableTask {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   648
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   649
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   650
        public void run() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   651
            if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   652
                debug.log("enter receive task");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   653
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   654
            loop:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   655
            while (!receiveScheduler.isStopped()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   656
                ChannelState rs = readState;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   657
                if (data.hasRemaining()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   658
                    if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   659
                        debug.log("remaining bytes received %s",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   660
                                  data.remaining());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   661
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   662
                    if (!demand.isFulfilled()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   663
                        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   664
                            int oldPos = data.position();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   665
                            reader.readFrame(data, decoder);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   666
                            int newPos = data.position();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   667
                            // Reader always consumes bytes:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   668
                            assert oldPos != newPos : data;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   669
                        } catch (Throwable e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   670
                            receiveScheduler.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   671
                            messageConsumer.onError(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   672
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   673
                        if (!data.hasRemaining()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   674
                            rs = readState = UNREGISTERED;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   675
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   676
                        continue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   677
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   678
                    break loop;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   679
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   680
                if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   681
                    debug.log("receive state: %s", rs);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   682
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   683
                switch (rs) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   684
                    case WAITING:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   685
                        break loop;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   686
                    case UNREGISTERED:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   687
                        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   688
                            rs = readState = WAITING;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   689
                            channel.registerEvent(readEvent);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   690
                        } catch (Throwable e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   691
                            receiveScheduler.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   692
                            messageConsumer.onError(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   693
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   694
                        break loop;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   695
                    case AVAILABLE:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   696
                        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   697
                            data = channel.read();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   698
                        } catch (Throwable e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   699
                            receiveScheduler.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   700
                            messageConsumer.onError(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   701
                            break loop;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   702
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   703
                        if (data == null) { // EOF
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   704
                            receiveScheduler.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   705
                            messageConsumer.onComplete();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   706
                            break loop;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   707
                        } else if (!data.hasRemaining()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   708
                            // No data at the moment. Pretty much a "goto",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   709
                            // reusing the existing code path for registration
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   710
                            rs = readState = UNREGISTERED;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   711
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   712
                        continue loop;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   713
                    default:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   714
                        throw new InternalError(String.valueOf(rs));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   715
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   716
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   717
            if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   718
                debug.log("exit receive task");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   719
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   720
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   721
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   722
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   723
    private class WriteEvent implements RawChannel.RawEvent {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   724
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   725
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   726
        public int interestOps() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   727
            return SelectionKey.OP_WRITE;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   728
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   729
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   730
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   731
        public void handle() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   732
            if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   733
                debug.log("write event");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   734
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   735
            ChannelState s;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   736
            do {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   737
                s = writeState.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   738
                if (s == CLOSED) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   739
                    if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   740
                        debug.log("write state %s", s);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   741
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   742
                    break;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   743
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   744
            } while (!writeState.compareAndSet(s, AVAILABLE));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   745
            sendScheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   746
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   747
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   748
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   749
    private class ReadEvent implements RawChannel.RawEvent {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   750
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   751
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   752
        public int interestOps() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   753
            return SelectionKey.OP_READ;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   754
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   755
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   756
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   757
        public void handle() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   758
            if (debug.on()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   759
                debug.log("read event");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   760
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   761
            readState = AVAILABLE;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   762
            receiveScheduler.runOrSchedule();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   763
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   764
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   765
}