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