jdk/src/java.httpclient/share/classes/java/net/http/WSSignalHandler.java
author prappo
Mon, 09 May 2016 23:33:09 +0100
changeset 37874 02589df0999a
child 39730 196f4e25d9f5
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  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  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  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.util.concurrent.Executor;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    28
import java.util.concurrent.RejectedExecutionException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import java.util.concurrent.atomic.AtomicInteger;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    30
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    31
import static java.util.Objects.requireNonNull;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    33
//
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
// The problem:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
// ------------
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
//   1. For every invocation of 'signal()' there must be at least
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
//      1 invocation of 'handler.run()' that goes after
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
//   2. There must be no more than 1 thread running the 'handler.run()'
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    39
//      at any given time
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
//
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
// For example, imagine each signal increments (+1) some number. Then the
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
// handler responds (eventually) the way that makes the number 0.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
//
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
// For each signal there's a response. Several signals may be handled by a
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
// single response.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
//
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
final class WSSignalHandler {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
    // In this state the task is neither submitted nor running.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
    // No one is handling signals. If a new signal has been received, the task
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
    // has to be submitted to the executor in order to handle this signal.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
    private static final int DONE    = 0;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
    // In this state the task is running.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
    // * If the signaller has found the task in this state it will try to change
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
    //   the state to RERUN in order to make the already running task to handle
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    57
    //   the new signal before exiting.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
    // * If the task has found itself in this state it will exit.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
    private static final int RUNNING = 1;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
    // A signal to the task, that it must rerun on the spot (without being
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
    // resubmitted to the executor).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
    // If the task has found itself in this state it resets the state to
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
    // RUNNING and repeats the pass.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
    private static final int RERUN   = 2;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
    private final AtomicInteger state = new AtomicInteger(DONE);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
    private final Executor executor;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
    private final Runnable task;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    72
    WSSignalHandler(Executor executor, Runnable handler) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    73
        this.executor = requireNonNull(executor);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    74
        requireNonNull(handler);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    76
        task = () -> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    77
            while (!Thread.currentThread().isInterrupted()) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    78
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    79
                try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    80
                    handler.run();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    81
                } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    82
                    // Sorry, the task won't be automatically retried;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    83
                    // hope next signals (if any) will kick off the handling
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    84
                    state.set(DONE);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    85
                    throw e;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    86
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    87
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    88
                int prev = state.getAndUpdate(s -> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    89
                    if (s == RUNNING) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    90
                        return DONE;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    91
                    } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    92
                        return RUNNING;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    93
                    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    94
                });
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    96
                // Can't be DONE, since only the task itself may transit state
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    97
                // into DONE (with one exception: RejectedExecution in signal();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    98
                // but in that case we couldn't be here at all)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    99
                assert prev == RUNNING || prev == RERUN;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   100
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   101
                if (prev == RUNNING) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   102
                    break;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   103
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   104
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   105
        };
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   106
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   107
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   108
    // Invoked by outer code to signal
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   109
    void signal() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   110
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   111
        int prev = state.getAndUpdate(s -> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   112
            switch (s) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   113
                case RUNNING:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   114
                    return RERUN;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   115
                case DONE:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   116
                    return RUNNING;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   117
                case RERUN:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   118
                    return RERUN;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   119
                default:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   120
                    throw new InternalError(String.valueOf(s));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   121
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   122
        });
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   123
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   124
        if (prev != DONE) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   125
            // Nothing to do! piggybacking on previous signal
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   126
            return;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   127
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   128
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   129
            executor.execute(task);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   130
        } catch (RejectedExecutionException e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   131
            // Sorry some signal() invocations may have been accepted, but won't
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   132
            // be done, since the 'task' couldn't be submitted
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   133
            state.set(DONE);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   134
            throw e;
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
}