jdk/src/java.httpclient/share/classes/java/net/http/WSReceiver.java
author chegar
Mon, 18 Jul 2016 15:34:22 +0100
changeset 39730 196f4e25d9f5
parent 39729 ef2b0635618f
permissions -rw-r--r--
8160993: Fix headers in the java/net/http package Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     1
/*
39730
196f4e25d9f5 8160993: Fix headers in the java/net/http package
chegar
parents: 39729
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     4
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    10
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    15
 * accompanied this code).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    16
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    20
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    23
 * questions.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    24
 */
39730
196f4e25d9f5 8160993: Fix headers in the java/net/http package
chegar
parents: 39729
diff changeset
    25
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    26
package java.net.http;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    27
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    28
import java.io.IOException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import java.io.UncheckedIOException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    30
import java.net.ProtocolException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    31
import java.net.http.WebSocket.Listener;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
import java.nio.ByteBuffer;
38856
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
    33
import java.nio.CharBuffer;
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
import java.nio.channels.SelectionKey;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
import java.util.Optional;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
import java.util.concurrent.CompletionStage;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
import java.util.concurrent.Executor;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
import java.util.concurrent.atomic.AtomicBoolean;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    39
import java.util.concurrent.atomic.AtomicLong;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
import java.util.function.Supplier;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
import static java.lang.System.Logger.Level.ERROR;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
import static java.net.http.WSUtils.EMPTY_BYTE_BUFFER;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
import static java.net.http.WSUtils.logger;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
/*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
 * Receives incoming data from the channel and converts it into a sequence of
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
 * messages, which are then passed to the listener.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
final class WSReceiver {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
    private final Listener listener;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
    private final WebSocket webSocket;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
    private final Supplier<WSShared<ByteBuffer>> buffersSupplier =
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
            new WSSharedPool<>(() -> ByteBuffer.allocateDirect(32768), 2);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
    private final RawChannel channel;
39729
ef2b0635618f 8161474: Extract interface from java.net.http.RawChannel
prappo
parents: 39133
diff changeset
    57
    private final RawChannel.RawEvent channelEvent;
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
    private final WSSignalHandler handler;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
    private final AtomicLong demand = new AtomicLong();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
    private final AtomicBoolean readable = new AtomicBoolean();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
    private boolean started;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
    private volatile boolean closed;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
    private final WSFrame.Reader reader = new WSFrame.Reader();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
    private final WSFrameConsumer frameConsumer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
    private WSShared<ByteBuffer> buf = WSShared.wrap(EMPTY_BYTE_BUFFER);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
    private WSShared<ByteBuffer> data; // TODO: initialize with leftovers from the RawChannel
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
    WSReceiver(Listener listener, WebSocket webSocket, Executor executor,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
               RawChannel channel) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
        this.listener = listener;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
        this.webSocket = webSocket;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    72
        this.channel = channel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    73
        handler = new WSSignalHandler(executor, this::react);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    74
        channelEvent = createChannelEvent();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
        this.frameConsumer = new WSFrameConsumer(new MessageConsumer());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    76
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    77
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    78
    private void react() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    79
        synchronized (this) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    80
            while (demand.get() > 0 && !closed) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    81
                try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    82
                    if (data == null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    83
                        if (!getData()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    84
                            break;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    85
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    86
                    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    87
                    reader.readFrame(data, frameConsumer);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    88
                    if (!data.hasRemaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    89
                        data.dispose();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    90
                        data = null;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    91
                    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    92
                    // In case of exception we don't need to clean any state,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    93
                    // since it's the terminal condition anyway. Nothing will be
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    94
                    // retried.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
                } catch (WSProtocolException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    96
                    // Translate into ProtocolException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    97
                    closeExceptionally(new ProtocolException().initCause(e));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    98
                } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    99
                    closeExceptionally(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   100
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   101
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   102
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   103
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   104
39133
b5641ce64cf7 8156742: Miscellaneous WebSocket API improvements
prappo
parents: 38856
diff changeset
   105
    void request(long n) {
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   106
        long newDemand = demand.accumulateAndGet(n, (p, i) -> p + i < 0 ? Long.MAX_VALUE : p + i);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   107
        handler.signal();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   108
        assert newDemand >= 0 : newDemand;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   109
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   110
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   111
    private boolean getData() throws IOException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   112
        if (!readable.get()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   113
            return false;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   114
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   115
        if (!buf.hasRemaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   116
            buf.dispose();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   117
            buf = buffersSupplier.get();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   118
            assert buf.hasRemaining() : buf;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   119
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   120
        int oldPosition = buf.position();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   121
        int oldLimit = buf.limit();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   122
        int numRead = channel.read(buf.buffer());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   123
        if (numRead > 0) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   124
            data = buf.share(oldPosition, oldPosition + numRead);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   125
            buf.select(buf.limit(), oldLimit); // Move window to the free region
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   126
            return true;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   127
        } else if (numRead == 0) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   128
            readable.set(false);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   129
            channel.registerEvent(channelEvent);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   130
            return false;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   131
        } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   132
            assert numRead < 0 : numRead;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   133
            throw new WSProtocolException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   134
                    ("7.2.1.", "Stream ended before a Close frame has been received");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   135
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   136
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   137
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   138
    void start() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   139
        synchronized (this) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   140
            if (started) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   141
                throw new IllegalStateException("Already started");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   142
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   143
            started = true;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   144
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   145
                channel.registerEvent(channelEvent);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   146
            } catch (IOException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   147
                throw new UncheckedIOException(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   148
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   149
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   150
                listener.onOpen(webSocket);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   151
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   152
                closeExceptionally(new RuntimeException("onOpen threw an exception", e));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   153
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   154
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   155
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   156
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   157
    private void close() { // TODO: move to WS.java
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   158
        closed = true;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   159
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   160
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   161
    private void closeExceptionally(Throwable error) {  // TODO: move to WS.java
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   162
        close();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   163
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   164
            listener.onError(webSocket, error);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   165
        } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   166
            logger.log(ERROR, "onError threw an exception", e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   167
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   168
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   169
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   170
    private final class MessageConsumer implements WSMessageConsumer {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   171
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   172
        @Override
38856
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   173
        public void onText(WebSocket.MessagePart part, WSShared<CharBuffer> data) {
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   174
            decrementDemand();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   175
            CompletionStage<?> cs;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   176
            try {
38856
cc3a0d1e96e0 8156650: Simplify Text message support in WebSocket API
prappo
parents: 37874
diff changeset
   177
                cs = listener.onText(webSocket, data.buffer(), part);
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   178
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   179
                closeExceptionally(new RuntimeException("onText threw an exception", e));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   180
                return;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   181
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   182
            follow(cs, data, "onText");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   183
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   184
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   185
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   186
        public void onBinary(WebSocket.MessagePart part, WSShared<ByteBuffer> data) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   187
            decrementDemand();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   188
            CompletionStage<?> cs;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   189
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   190
                cs = listener.onBinary(webSocket, data.buffer(), part);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   191
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   192
                closeExceptionally(new RuntimeException("onBinary threw an exception", e));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   193
                return;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   194
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   195
            follow(cs, data, "onBinary");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   196
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   197
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   198
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   199
        public void onPing(WSShared<ByteBuffer> data) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   200
            decrementDemand();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   201
            CompletionStage<?> cs;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   202
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   203
                cs = listener.onPing(webSocket, data.buffer());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   204
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   205
                closeExceptionally(new RuntimeException("onPing threw an exception", e));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   206
                return;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   207
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   208
            follow(cs, data, "onPing");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   209
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   210
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   211
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   212
        public void onPong(WSShared<ByteBuffer> data) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   213
            decrementDemand();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   214
            CompletionStage<?> cs;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   215
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   216
                cs = listener.onPong(webSocket, data.buffer());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   217
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   218
                closeExceptionally(new RuntimeException("onPong threw an exception", e));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   219
                return;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   220
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   221
            follow(cs, data, "onPong");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   222
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   223
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   224
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   225
        public void onClose(WebSocket.CloseCode code, CharSequence reason) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   226
            decrementDemand();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   227
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   228
                close();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   229
                listener.onClose(webSocket, Optional.ofNullable(code), reason.toString());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   230
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   231
                logger.log(ERROR, "onClose threw an exception", e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   232
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   233
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   234
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   235
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   236
    private void follow(CompletionStage<?> cs, WSDisposable d, String source) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   237
        if (cs == null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   238
            d.dispose();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   239
        } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   240
            cs.whenComplete((whatever, error) -> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   241
                if (error != null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   242
                    String m = "CompletionStage returned by " + source + " completed exceptionally";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   243
                    closeExceptionally(new RuntimeException(m, error));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   244
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   245
                d.dispose();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   246
            });
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   247
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   248
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   249
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   250
    private void decrementDemand() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   251
        long newDemand = demand.decrementAndGet();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   252
        assert newDemand >= 0 : newDemand;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   253
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   254
39729
ef2b0635618f 8161474: Extract interface from java.net.http.RawChannel
prappo
parents: 39133
diff changeset
   255
    private RawChannel.RawEvent createChannelEvent() {
ef2b0635618f 8161474: Extract interface from java.net.http.RawChannel
prappo
parents: 39133
diff changeset
   256
        return new RawChannel.RawEvent() {
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   257
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   258
            @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   259
            public int interestOps() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   260
                return SelectionKey.OP_READ;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   261
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   262
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   263
            @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   264
            public void handle() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   265
                boolean wasNotReadable = readable.compareAndSet(false, true);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   266
                assert wasNotReadable;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   267
                handler.signal();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   268
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   269
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   270
            @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   271
            public String toString() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   272
                return "Read readiness event [" + channel + "]";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   273
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   274
        };
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   275
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   276
}