test/jdk/java/nio/channels/spi/SelectorProvider/inheritedChannel/EchoService.java
author mli
Thu, 19 Jul 2018 16:22:19 +0800
changeset 51117 c96c7d08ae49
parent 47216 71c04702a3d5
permissions -rw-r--r--
8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed Reviewed-by: alanb, simonis
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * An "echo" service designed to be used with inetd. It can be configured in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * inetd.conf to be used by any of the following types of services :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *      stream  tcp   nowait
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *      stream  tcp6  nowait
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *      stream  tcp   wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *      stream  tcp6  wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *      dgram   udp   wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *      dgram   udp6  wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * If configured as a "tcp nowait" service then inetd will launch a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * VM to run the EchoService each time that a client connects to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the TCP port. The EchoService simply echos any messages it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * receives from the client and shuts if the client closes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * If configured as a "tcp wait" service then inetd will launch a VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * to run the EchoService when a client connects to the port. When
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * launched the EchoService takes over the listener socket. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * terminates when all clients have disconnected and the service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * is idle for a few seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * If configured as a "udp wait" service then a VM will be launched for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * each UDP packet to the configured port. System.inheritedChannel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * will return a DatagramChannel. The echo service here will terminate after
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * echoing the UDP packet back to the client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * The service closes the inherited network channel when complete. To
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * facilate testing that the channel is closed the "tcp nowait" service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * can close the connection after a given number of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.io.IOException;
45578
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    59
import java.net.SocketAddress;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    60
import java.nio.ByteBuffer;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    61
import java.nio.channels.Channel;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    62
import java.nio.channels.DatagramChannel;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    63
import java.nio.channels.SelectionKey;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    64
import java.nio.channels.Selector;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    65
import java.nio.channels.ServerSocketChannel;
cd8f28842351 8182465: Refactor shell test java/nio/channels/spi/SelectorProvider/inheritedChannel/run_tests.sh to java
amlu
parents: 5506
diff changeset
    66
import java.nio.channels.SocketChannel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    68
import jdk.test.lib.Utils;
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
    69
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class EchoService {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static void doIt(SocketChannel sc, int closeAfter, int delay) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        ByteBuffer bb = ByteBuffer.allocate(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            int n = sc.read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            total += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            // echo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            sc.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            // close after X bytes?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (closeAfter > 0 && total >= closeAfter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (delay > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                Thread.currentThread().sleep(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            } catch (InterruptedException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static void doIt(DatagramChannel dc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        ByteBuffer bb = ByteBuffer.allocate(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        SocketAddress sa = dc.receive(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        dc.send(bb, sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        dc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // A worker thread to service a single connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // The class maintains a count of the number of worker threads so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // can the service can terminate then all clients disconnect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static class Worker implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        private static int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        private static Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        public static int count() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        private SocketChannel sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        Worker(SocketChannel sc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            this.sc = sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                doIt(sc, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public static void main(String args[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        Channel c = System.inheritedChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // tcp nowait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (c instanceof SocketChannel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            int closeAfter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            int delay = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                closeAfter = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (args.length > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                delay = Integer.parseInt(args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            doIt((SocketChannel)c, closeAfter, delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // tcp wait - in this case we take over the listener socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // In this test case we create a thread to service each connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // and terminate after all clients are gone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (c instanceof ServerSocketChannel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            ServerSocketChannel ssc = (ServerSocketChannel)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            ssc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            Selector sel = ssc.provider().openSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            SelectionKey sk = ssc.register(sel, SelectionKey.OP_ACCEPT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            SocketChannel sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            for (;;) {
51117
c96c7d08ae49 8207316: java/nio/channels/spi/SelectorProvider/inheritedChannel/InheritedChannelTest.java failed
mli
parents: 47216
diff changeset
   178
                 sel.select((int)Utils.adjustTimeout(5000));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                 if (sk.isAcceptable() && ((sc = ssc.accept()) != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    Worker w = new Worker(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    (new Thread(w)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                 } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                     // if all clients have disconnected then we die as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                     if (Worker.count() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // udp wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (c instanceof DatagramChannel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            doIt((DatagramChannel)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // linger?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            int delay = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                Thread.currentThread().sleep(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } catch (InterruptedException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
}