jdk/src/java.httpclient/share/classes/java/net/http/WSReceiver.java
author prappo
Mon, 09 May 2016 23:33:09 +0100
changeset 37874 02589df0999a
child 38856 cc3a0d1e96e0
permissions -rw-r--r--
8087113: Websocket API and implementation Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     1
/*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
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
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    25
package java.net.http;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    26
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    27
import java.io.IOException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    28
import java.io.UncheckedIOException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import java.net.ProtocolException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    30
import java.net.http.WebSocket.Listener;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    31
import java.nio.ByteBuffer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
import java.nio.channels.SelectionKey;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    33
import java.util.Optional;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
import java.util.concurrent.CompletionStage;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
import java.util.concurrent.Executor;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
import java.util.concurrent.atomic.AtomicBoolean;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
import java.util.concurrent.atomic.AtomicLong;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
import java.util.function.Supplier;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    39
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
import static java.lang.System.Logger.Level.ERROR;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
import static java.net.http.WSUtils.EMPTY_BYTE_BUFFER;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
import static java.net.http.WSUtils.logger;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
/*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
 * Receives incoming data from the channel and converts it into a sequence of
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
 * messages, which are then passed to the listener.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
final class WSReceiver {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
    private final Listener listener;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
    private final WebSocket webSocket;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
    private final Supplier<WSShared<ByteBuffer>> buffersSupplier =
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
            new WSSharedPool<>(() -> ByteBuffer.allocateDirect(32768), 2);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
    private final RawChannel channel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
    private final RawChannel.NonBlockingEvent channelEvent;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
    private final WSSignalHandler handler;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    57
    private final AtomicLong demand = new AtomicLong();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
    private final AtomicBoolean readable = new AtomicBoolean();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
    private boolean started;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
    private volatile boolean closed;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
    private final WSFrame.Reader reader = new WSFrame.Reader();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
    private final WSFrameConsumer frameConsumer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
    private WSShared<ByteBuffer> buf = WSShared.wrap(EMPTY_BYTE_BUFFER);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
    private WSShared<ByteBuffer> data; // TODO: initialize with leftovers from the RawChannel
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
    WSReceiver(Listener listener, WebSocket webSocket, Executor executor,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
               RawChannel channel) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
        this.listener = listener;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
        this.webSocket = webSocket;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
        this.channel = channel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
        handler = new WSSignalHandler(executor, this::react);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    72
        channelEvent = createChannelEvent();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    73
        this.frameConsumer = new WSFrameConsumer(new MessageConsumer());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    74
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    76
    private void react() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    77
        synchronized (this) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    78
            while (demand.get() > 0 && !closed) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    79
                try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    80
                    if (data == null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    81
                        if (!getData()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    82
                            break;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    83
                        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    84
                    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    85
                    reader.readFrame(data, frameConsumer);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    86
                    if (!data.hasRemaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    87
                        data.dispose();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    88
                        data = null;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    89
                    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    90
                    // In case of exception we don't need to clean any state,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    91
                    // since it's the terminal condition anyway. Nothing will be
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    92
                    // retried.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    93
                } catch (WSProtocolException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    94
                    // Translate into ProtocolException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
                    closeExceptionally(new ProtocolException().initCause(e));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    96
                } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    97
                    closeExceptionally(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    98
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    99
            }
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
    long request(long n) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   104
        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
   105
        handler.signal();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   106
        assert newDemand >= 0 : newDemand;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   107
        return newDemand;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   108
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   109
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   110
    private boolean getData() throws IOException {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   111
        if (!readable.get()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   112
            return false;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   113
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   114
        if (!buf.hasRemaining()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   115
            buf.dispose();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   116
            buf = buffersSupplier.get();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   117
            assert buf.hasRemaining() : buf;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   118
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   119
        int oldPosition = buf.position();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   120
        int oldLimit = buf.limit();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   121
        int numRead = channel.read(buf.buffer());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   122
        if (numRead > 0) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   123
            data = buf.share(oldPosition, oldPosition + numRead);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   124
            buf.select(buf.limit(), oldLimit); // Move window to the free region
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   125
            return true;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   126
        } else if (numRead == 0) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   127
            readable.set(false);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   128
            channel.registerEvent(channelEvent);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   129
            return false;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   130
        } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   131
            assert numRead < 0 : numRead;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   132
            throw new WSProtocolException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   133
                    ("7.2.1.", "Stream ended before a Close frame has been received");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   134
        }
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
    void start() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   138
        synchronized (this) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   139
            if (started) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   140
                throw new IllegalStateException("Already started");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   141
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   142
            started = true;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   143
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   144
                channel.registerEvent(channelEvent);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   145
            } catch (IOException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   146
                throw new UncheckedIOException(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   147
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   148
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   149
                listener.onOpen(webSocket);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   150
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   151
                closeExceptionally(new RuntimeException("onOpen threw an exception", e));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   152
            }
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
    private void close() { // TODO: move to WS.java
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   157
        closed = true;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   158
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   159
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   160
    private void closeExceptionally(Throwable error) {  // TODO: move to WS.java
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   161
        close();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   162
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   163
            listener.onError(webSocket, error);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   164
        } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   165
            logger.log(ERROR, "onError threw an exception", e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   166
        }
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
    private final class MessageConsumer implements WSMessageConsumer {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   170
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   171
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   172
        public void onText(WebSocket.MessagePart part, WSDisposableText data) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   173
            decrementDemand();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   174
            CompletionStage<?> cs;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   175
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   176
                cs = listener.onText(webSocket, data, part);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   177
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   178
                closeExceptionally(new RuntimeException("onText threw an exception", e));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   179
                return;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   180
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   181
            follow(cs, data, "onText");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   182
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   183
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   184
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   185
        public void onBinary(WebSocket.MessagePart part, WSShared<ByteBuffer> data) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   186
            decrementDemand();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   187
            CompletionStage<?> cs;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   188
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   189
                cs = listener.onBinary(webSocket, data.buffer(), part);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   190
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   191
                closeExceptionally(new RuntimeException("onBinary threw an exception", e));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   192
                return;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   193
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   194
            follow(cs, data, "onBinary");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   195
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   196
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   197
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   198
        public void onPing(WSShared<ByteBuffer> data) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   199
            decrementDemand();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   200
            CompletionStage<?> cs;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   201
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   202
                cs = listener.onPing(webSocket, data.buffer());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   203
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   204
                closeExceptionally(new RuntimeException("onPing threw an exception", e));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   205
                return;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   206
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   207
            follow(cs, data, "onPing");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   208
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   209
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   210
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   211
        public void onPong(WSShared<ByteBuffer> data) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   212
            decrementDemand();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   213
            CompletionStage<?> cs;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   214
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   215
                cs = listener.onPong(webSocket, data.buffer());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   216
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   217
                closeExceptionally(new RuntimeException("onPong threw an exception", e));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   218
                return;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   219
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   220
            follow(cs, data, "onPong");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   221
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   222
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   223
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   224
        public void onClose(WebSocket.CloseCode code, CharSequence reason) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   225
            decrementDemand();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   226
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   227
                close();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   228
                listener.onClose(webSocket, Optional.ofNullable(code), reason.toString());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   229
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   230
                logger.log(ERROR, "onClose threw an exception", e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   231
            }
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
    private void follow(CompletionStage<?> cs, WSDisposable d, String source) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   236
        if (cs == null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   237
            d.dispose();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   238
        } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   239
            cs.whenComplete((whatever, error) -> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   240
                if (error != null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   241
                    String m = "CompletionStage returned by " + source + " completed exceptionally";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   242
                    closeExceptionally(new RuntimeException(m, error));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   243
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   244
                d.dispose();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   245
            });
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
    private void decrementDemand() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   250
        long newDemand = demand.decrementAndGet();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   251
        assert newDemand >= 0 : newDemand;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   252
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   253
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   254
    private RawChannel.NonBlockingEvent createChannelEvent() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   255
        return new RawChannel.NonBlockingEvent() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   256
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   257
            @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   258
            public int interestOps() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   259
                return SelectionKey.OP_READ;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   260
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   261
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   262
            @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   263
            public void handle() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   264
                boolean wasNotReadable = readable.compareAndSet(false, true);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   265
                assert wasNotReadable;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   266
                handler.signal();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   267
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   268
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   269
            @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   270
            public String toString() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   271
                return "Read readiness event [" + channel + "]";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   272
            }
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
}