test/jdk/java/nio/channels/TestServers.java
author mli
Fri, 16 Mar 2018 18:15:31 +0800
changeset 49255 acdb8531cc8b
parent 47216 71c04702a3d5
permissions -rw-r--r--
8199215: Re-examine getFreePort method in test infrastructure library Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
     2
 * Copyright (c) 2012, 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: 1152
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1152
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    24
/* Test utility classes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    30
import java.util.ArrayList;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    31
import java.util.Date;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    32
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    34
import jdk.test.lib.Utils;
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    35
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    37
public class TestServers {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    38
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    39
    private TestServers() { }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    40
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    41
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    42
     * An abstract server identifies a server which listens on a port on on a
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    43
     * given machine.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    44
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    45
    static abstract class AbstractServer {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    46
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    47
        private AbstractServer() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    48
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    49
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    50
        public abstract int getPort();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    51
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    52
        public abstract InetAddress getAddress();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    53
    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    54
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    55
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    56
     * A downgraded type of AbstractServer which will refuse connections. Note:
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    57
     * use it once and throw it away - this implementation opens an anonymous
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    58
     * socket and closes it, returning the address of the closed socket. If
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    59
     * other servers are started afterwards, the address/port might get reused
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    60
     * and become connectable again - so it's not a good idea to assume that
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    61
     * connections using this address/port will always be refused. Connections
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    62
     * will be refused as long as the address/port of the refusing server has
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    63
     * not been reused.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    64
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    65
    static class RefusingServer extends AbstractServer {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    66
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    67
        final InetAddress address;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    68
        final int port;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    69
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    70
        private RefusingServer(InetAddress address, int port) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    71
            this.address = address;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    72
            this.port = port;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    73
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    74
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    75
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    76
        public int getPort() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    77
            return port;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    78
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    79
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    80
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    81
        public InetAddress getAddress() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    82
            return address;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    83
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    84
36963
1558f7600497 8037360: java/nio/channels/SocketChannel/Connect.java fails intermittently
alanb
parents: 14415
diff changeset
    85
        public static RefusingServer newRefusingServer() throws IOException {
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    86
            return new RefusingServer(InetAddress.getLocalHost(),
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    87
                       Utils.refusingEndpoint().getPort());
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    88
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    89
    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    90
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    91
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    92
     * An abstract class for implementing small TCP servers for the nio tests
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    93
     * purposes. Disclaimer: This is a naive implementation that uses the old
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    94
     * networking APIs (not those from {@code java.nio.*}) and shamelessly
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    95
     * extends/creates Threads instead of using an executor service.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    96
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    97
    static abstract class AbstractTcpServer extends AbstractServer
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    98
            implements Runnable, Closeable {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    99
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   100
        protected final long linger; // #of ms to wait before responding
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   101
        private Thread acceptThread; // thread waiting for accept
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   102
        // list of opened connections that should be closed on close.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   103
        private List<TcpConnectionThread> connections = new ArrayList<>();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   104
        private ServerSocket serverSocket; // the server socket
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   105
        private boolean started = false; // whether the server is started
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   106
        Throwable error = null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   107
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   108
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   109
         * Creates a new abstract TCP server.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   110
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   111
         * @param linger the amount of time the server should wait before
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   112
         * responding to requests.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   113
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   114
        protected AbstractTcpServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   115
            this.linger = linger;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   116
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   118
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   119
         * The local port to which the server is bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   120
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   121
         * @return The local port to which the server is bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   122
         * @exception IllegalStateException is thrown if the server is not
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   123
         * started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   124
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   125
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   126
        public final synchronized int getPort() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   127
            if (!started) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   128
                throw new IllegalStateException("Not started");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   129
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   130
            return serverSocket.getLocalPort();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   131
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   132
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   133
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   134
         * The local address to which the server is bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   135
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   136
         * @return The local address to which the server is bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   137
         * @exception IllegalStateException is thrown if the server is not
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   138
         * started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   139
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   140
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   141
        public final synchronized InetAddress getAddress() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   142
            if (!started) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   143
                throw new IllegalStateException("Not started");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   144
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   145
            return serverSocket.getInetAddress();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   146
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   147
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   148
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   149
         * Tells whether the server is started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   150
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   151
         * @return true if the server is started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   152
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   153
        public final synchronized boolean isStarted() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   154
            return started;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   155
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   157
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   158
         * Creates a new server socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   159
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   160
         * @param port local port to bind to.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   161
         * @param backlog requested maximum length of the queue of incoming
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   162
         * connections.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   163
         * @param address local address to bind to.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   164
         * @return a new bound server socket ready to accept connections.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   165
         * @throws IOException if the socket cannot be created or bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   166
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   167
        protected ServerSocket newServerSocket(int port, int backlog,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   168
                InetAddress address)
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   169
                throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   170
            return new ServerSocket(port, backlog, address);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   171
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   172
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   173
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   174
         * Starts listening for connections.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   175
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   176
         * @throws IOException if the server socket cannot be created or bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   177
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   178
        public final synchronized void start() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   179
            if (started) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   180
                return;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   181
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   182
            final ServerSocket socket =
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   183
                    newServerSocket(0, 100, InetAddress.getLocalHost());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   184
            serverSocket = socket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   185
            acceptThread = new Thread(this);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   186
            acceptThread.setDaemon(true);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   187
            acceptThread.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   188
            started = true;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   189
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   190
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   191
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   192
         * Calls {@code Thread.sleep(linger);}
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   193
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   194
        protected final void lingerIfRequired() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   195
            if (linger > 0) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   196
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   197
                    Thread.sleep(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   198
                } catch (InterruptedException x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   199
                    Thread.interrupted();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   200
                    final ServerSocket socket = serverSocket();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   201
                    if (socket != null && !socket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   202
                        System.err.println("Thread interrupted...");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   203
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   204
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   205
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   206
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   207
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   208
        final synchronized ServerSocket serverSocket() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   209
            return this.serverSocket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   210
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   212
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   213
         * The main accept loop.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   214
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   215
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   216
        public final void run() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   217
            final ServerSocket sSocket = serverSocket();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            try {
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   219
                Socket s;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   220
                while (isStarted() && !Thread.interrupted()
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   221
                        && (s = sSocket.accept()) != null) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   222
                    lingerIfRequired();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   223
                    listen(s);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   224
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   225
            } catch (Exception x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   226
                error = x;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   227
            } finally {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   228
                synchronized (this) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   229
                    if (!sSocket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   230
                        try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   231
                            sSocket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   232
                        } catch (IOException x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   233
                            System.err.println("Failed to close server socket");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   234
                        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   235
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   236
                    if (started && this.serverSocket == sSocket) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   237
                        started = false;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   238
                        this.serverSocket = null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   239
                        this.acceptThread = null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   240
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   241
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   242
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   243
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   244
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   245
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   246
         * Represents a connection accepted by the server.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   247
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   248
        protected abstract class TcpConnectionThread extends Thread {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   249
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   250
            protected final Socket socket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   251
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   252
            protected TcpConnectionThread(Socket socket) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   253
                this.socket = socket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   254
                this.setDaemon(true);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   255
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   256
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   257
            public void close() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   258
                socket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   259
                interrupt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   261
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   262
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   263
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   264
         * Creates a new TcpConnnectionThread to handle the connection through
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   265
         * an accepted socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   266
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   267
         * @param s the socket returned by {@code serverSocket.accept()}.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   268
         * @return a new TcpConnnectionThread to handle the connection through
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   269
         * an accepted socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   270
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   271
        protected abstract TcpConnectionThread createConnection(Socket s);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   272
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   273
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   274
         * Creates and starts a new TcpConnectionThread to handle the accepted
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   275
         * socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   276
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   277
         * @param s the socket returned by {@code serverSocket.accept()}.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   278
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   279
        private synchronized void listen(Socket s) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   280
            TcpConnectionThread c = createConnection(s);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   281
            c.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   282
            addConnection(c);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   283
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   284
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   285
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   286
         * Add the connection to the list of accepted connections.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   287
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   288
         * @param connection an accepted connection.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   289
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   290
        protected synchronized void addConnection(
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   291
                TcpConnectionThread connection) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   292
            connections.add(connection);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   293
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   294
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   295
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   296
         * Remove the connection from the list of accepted connections.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   297
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   298
         * @param connection an accepted connection.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   299
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   300
        protected synchronized void removeConnection(
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   301
                TcpConnectionThread connection) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   302
            connections.remove(connection);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   303
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   304
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   305
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   306
         * Close the server socket and all the connections present in the list
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   307
         * of accepted connections.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   308
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   309
         * @throws IOException
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   310
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   311
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   312
        public synchronized void close() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   313
            if (serverSocket != null && !serverSocket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   314
                serverSocket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   315
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   316
            if (acceptThread != null) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   317
                acceptThread.interrupt();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   318
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   319
            int failed = 0;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   320
            for (TcpConnectionThread c : connections) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   321
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   322
                    c.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   323
                } catch (IOException x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   324
                    // no matter - we're closing.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   325
                    failed++;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   326
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   327
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   328
            connections.clear();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   329
            if (failed > 0) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   330
                throw new IOException("Failed to close some connections");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   331
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   332
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   333
    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   334
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   335
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   336
     * A small TCP Server that emulates the echo service for tests purposes. See
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   337
     * http://en.wikipedia.org/wiki/Echo_Protocol This server uses an anonymous
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   338
     * port - NOT the standard port 7. We don't guarantee that its behavior
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   339
     * exactly matches the RFC - the only purpose of this server is to have
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   340
     * something that responds to nio tests...
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   341
     */
37876
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   342
    static class EchoServer extends AbstractTcpServer {
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   343
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   344
        public EchoServer() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   345
            this(0L);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   346
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   347
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   348
        public EchoServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   349
            super(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   350
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   351
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   352
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   353
        protected TcpConnectionThread createConnection(Socket s) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   354
            return new EchoConnection(s);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   355
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   356
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   357
        private final class EchoConnection extends TcpConnectionThread {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   358
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   359
            public EchoConnection(Socket socket) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   360
                super(socket);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   361
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   362
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   363
            @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   364
            public void run() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   365
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   366
                    final InputStream is = socket.getInputStream();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   367
                    final OutputStream out = socket.getOutputStream();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   368
                    byte[] b = new byte[255];
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   369
                    int n;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   370
                    while ((n = is.read(b)) > 0) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   371
                        lingerIfRequired();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   372
                        out.write(b, 0, n);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   373
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   374
                } catch (IOException io) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   375
                    // fall through to finally
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   376
                } finally {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   377
                    if (!socket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   378
                        try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   379
                            socket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   380
                        } catch (IOException x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   381
                            System.err.println(
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   382
                                    "Failed to close echo connection socket");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   383
                        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   384
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   385
                    removeConnection(this);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   386
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   387
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   388
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   389
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   390
        public static EchoServer startNewServer() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   391
            return startNewServer(0);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   392
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   393
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   394
        public static EchoServer startNewServer(long linger) throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   395
            final EchoServer echoServer = new EchoServer(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   396
            echoServer.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   397
            return echoServer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   401
    /**
37876
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   402
     * A small TCP Server that accept connections but does not response to any input.
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   403
     */
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   404
    static final class NoResponseServer extends EchoServer {
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   405
        public NoResponseServer() {
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   406
            super(Long.MAX_VALUE);
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   407
        }
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   408
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   409
        public static NoResponseServer startNewServer() throws IOException {
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   410
            final NoResponseServer noResponseServer = new NoResponseServer();
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   411
            noResponseServer.start();
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   412
            return noResponseServer;
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   413
        }
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   414
    }
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   415
8c1ab93d657a 8156002: java/nio/channels/SocketChannel/AdaptSocket.java Fails in Mesos on OSX
mli
parents: 36963
diff changeset
   416
    /**
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   417
     * A small TCP server that emulates the Day & Time service for tests
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   418
     * purposes. See http://en.wikipedia.org/wiki/Daytime_Protocol This server
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   419
     * uses an anonymous port - NOT the standard port 13. We don't guarantee
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   420
     * that its behavior exactly matches the RFC - the only purpose of this
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   421
     * server is to have something that responds to nio tests...
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   422
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   423
    static final class DayTimeServer extends AbstractTcpServer {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   424
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   425
        public DayTimeServer() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   426
            this(0L);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   427
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   428
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   429
        public DayTimeServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   430
            super(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   431
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   432
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   433
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   434
        protected TcpConnectionThread createConnection(Socket s) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   435
            return new DayTimeServerConnection(s);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   436
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   437
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   438
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   439
        protected void addConnection(TcpConnectionThread connection) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   440
            // do nothing - the connection just write the date and terminates.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   441
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   442
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   443
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   444
        protected void removeConnection(TcpConnectionThread connection) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   445
            // do nothing - we're not adding connections to the list...
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   446
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   447
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   448
        private final class DayTimeServerConnection extends TcpConnectionThread {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   449
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   450
            public DayTimeServerConnection(Socket socket) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   451
                super(socket);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   452
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   453
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   454
            @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   455
            public void run() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   456
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   457
                    final OutputStream out = socket.getOutputStream();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   458
                    lingerIfRequired();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   459
                    out.write(new Date(System.currentTimeMillis())
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   460
                            .toString().getBytes("US-ASCII"));
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   461
                    out.flush();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   462
                } catch (IOException io) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   463
                    // fall through to finally
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   464
                } finally {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   465
                    if (!socket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   466
                        try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   467
                            socket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   468
                        } catch (IOException x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   469
                            System.err.println(
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   470
                                    "Failed to close echo connection socket");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   471
                        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   472
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   473
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   474
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   475
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   476
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   477
        public static DayTimeServer startNewServer()
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   478
                throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   479
            return startNewServer(0);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   480
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   481
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   482
        public static DayTimeServer startNewServer(long linger)
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   483
                throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   484
            final DayTimeServer daytimeServer = new DayTimeServer(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   485
            daytimeServer.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   486
            return daytimeServer;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   487
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   490
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   491
     * An abstract class for implementing small UDP Servers for the nio tests
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   492
     * purposes. Disclaimer: This is a naive implementation that uses the old
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   493
     * networking APIs (not those from {@code java.nio.*}) and shamelessly
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   494
     * extends/creates Threads instead of using an executor service.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   495
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   496
    static abstract class AbstractUdpServer extends AbstractServer
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   497
            implements Runnable, Closeable {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   498
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   499
        protected final long linger; // #of ms to wait before responding
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   500
        private Thread acceptThread; // thread waiting for packets
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   501
        private DatagramSocket serverSocket; // the server socket
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   502
        private boolean started = false; // whether the server is started
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   503
        Throwable error = null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   504
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   505
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   506
         * Creates a new abstract UDP server.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   507
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   508
         * @param linger the amount of time the server should wait before
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   509
         * responding to requests.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   510
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   511
        protected AbstractUdpServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   512
            this.linger = linger;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   513
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   514
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   515
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   516
         * The local port to which the server is bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   517
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   518
         * @return The local port to which the server is bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   519
         * @exception IllegalStateException is thrown if the server is not
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   520
         * started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   521
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   522
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   523
        public final synchronized int getPort() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   524
            if (!started) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   525
                throw new IllegalStateException("Not started");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   526
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   527
            return serverSocket.getLocalPort();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   528
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   529
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   530
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   531
         * The local address to which the server is bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   532
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   533
         * @return The local address to which the server is bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   534
         * @exception IllegalStateException is thrown if the server is not
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   535
         * started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   536
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   537
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   538
        public final synchronized InetAddress getAddress() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   539
            if (!started) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   540
                throw new IllegalStateException("Not started");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   541
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   542
            return serverSocket.getLocalAddress();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   543
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   544
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   545
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   546
         * Tells whether the server is started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   547
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   548
         * @return true if the server is started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   549
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   550
        public final synchronized boolean isStarted() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   551
            return started;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   552
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   553
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   554
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   555
         * Creates a new datagram socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   556
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   557
         * @param port local port to bind to.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   558
         * @param address local address to bind to.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   559
         * @return a new bound server socket ready to listen for packets.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   560
         * @throws IOException if the socket cannot be created or bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   561
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   562
        protected DatagramSocket newDatagramSocket(int port,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   563
                InetAddress address)
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   564
                throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   565
            return new DatagramSocket(port, address);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   566
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   567
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   568
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   569
         * Starts listening for connections.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   570
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   571
         * @throws IOException if the server socket cannot be created or bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   572
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   573
        public final synchronized void start() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   574
            if (started) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   575
                return;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   576
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   577
            final DatagramSocket socket =
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   578
                    newDatagramSocket(0, InetAddress.getLocalHost());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   579
            serverSocket = socket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   580
            acceptThread = new Thread(this);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   581
            acceptThread.setDaemon(true);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   582
            acceptThread.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   583
            started = true;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   584
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   586
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   587
         * Calls {@code Thread.sleep(linger);}
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   588
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   589
        protected final void lingerIfRequired() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   590
            if (linger > 0) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   591
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   592
                    Thread.sleep(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   593
                } catch (InterruptedException x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   594
                    Thread.interrupted();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   595
                    final DatagramSocket socket = serverSocket();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   596
                    if (socket != null && !socket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   597
                        System.err.println("Thread interrupted...");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   598
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   599
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   600
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   601
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   602
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   603
        final synchronized DatagramSocket serverSocket() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   604
            return this.serverSocket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   605
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   606
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   607
        final synchronized boolean send(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   608
                DatagramPacket response) throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   609
            if (!socket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   610
                socket.send(response);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   611
                return true;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   612
            } else {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   613
                return false;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   614
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   615
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   616
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   617
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   618
         * The main receive loop.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   619
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   620
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   621
        public final void run() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   622
            final DatagramSocket sSocket = serverSocket();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   623
            try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   624
                final int size = Math.max(1024, sSocket.getReceiveBufferSize());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   625
                if (size > sSocket.getReceiveBufferSize()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   626
                    sSocket.setReceiveBufferSize(size);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   627
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   628
                while (isStarted() && !Thread.interrupted() && !sSocket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   629
                    final byte[] buf = new byte[size];
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   630
                    final DatagramPacket packet =
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   631
                            new DatagramPacket(buf, buf.length);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   632
                    lingerIfRequired();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   633
                    sSocket.receive(packet);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   634
                    //System.out.println("Received packet from: "
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   635
                    //        + packet.getAddress()+":"+packet.getPort());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   636
                    handle(sSocket, packet);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   637
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   638
            } catch (Exception x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   639
                error = x;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   640
            } finally {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   641
                synchronized (this) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   642
                    if (!sSocket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   643
                        sSocket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   644
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   645
                    if (started && this.serverSocket == sSocket) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   646
                        started = false;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   647
                        this.serverSocket = null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   648
                        this.acceptThread = null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   649
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   650
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   651
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   652
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   653
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   654
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   655
         * Represents an UDP request received by the server.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   656
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   657
        protected abstract class UdpRequestThread extends Thread {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   658
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   659
            protected final DatagramPacket request;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   660
            protected final DatagramSocket socket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   661
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   662
            protected UdpRequestThread(DatagramSocket socket, DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   663
                this.socket = socket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   664
                this.request = request;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   665
                this.setDaemon(true);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   666
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   667
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   668
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   669
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   670
         * Creates a new UdpRequestThread to handle a DatagramPacket received
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   671
         * through a DatagramSocket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   672
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   673
         * @param socket the socket through which the request was received.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   674
         * @param request the datagram packet received through the socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   675
         * @return a new UdpRequestThread to handle the request received through
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   676
         * a DatagramSocket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   677
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   678
        protected abstract UdpRequestThread createConnection(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   679
                DatagramPacket request);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   680
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   681
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   682
         * Creates and starts a new UdpRequestThread to handle the received
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   683
         * datagram packet.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   684
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   685
         * @param socket the socket through which the request was received.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   686
         * @param request the datagram packet received through the socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   687
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   688
        private synchronized void handle(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   689
                DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   690
            UdpRequestThread c = createConnection(socket, request);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   691
            // c can be null if the request requires no response.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   692
            if (c != null) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   693
                c.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   694
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   695
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   696
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   697
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   698
         * Close the server socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   699
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   700
         * @throws IOException
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   701
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   702
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   703
        public synchronized void close() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   704
            if (serverSocket != null && !serverSocket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   705
                serverSocket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   706
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   707
            if (acceptThread != null) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   708
                acceptThread.interrupt();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   709
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   710
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   713
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   714
     * A small UDP Server that emulates the discard service for tests purposes.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   715
     * See http://en.wikipedia.org/wiki/Discard_Protocol This server uses an
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   716
     * anonymous port - NOT the standard port 9. We don't guarantee that its
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   717
     * behavior exactly matches the RFC - the only purpose of this server is to
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   718
     * have something that responds to nio tests...
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   719
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   720
    static final class UdpDiscardServer extends AbstractUdpServer {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   721
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   722
        public UdpDiscardServer() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   723
            this(0L);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   724
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   725
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   726
        public UdpDiscardServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   727
            super(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   728
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   729
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   730
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   731
        protected UdpRequestThread createConnection(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   732
                DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   733
            // no response required
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   734
            return null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   735
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   736
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   737
        public static UdpDiscardServer startNewServer() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   738
            return startNewServer(0);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   739
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   740
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   741
        public static UdpDiscardServer startNewServer(long linger) throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   742
            final UdpDiscardServer discardServer = new UdpDiscardServer(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   743
            discardServer.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   744
            return discardServer;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   745
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   746
    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   747
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   748
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   749
     * A small UDP Server that emulates the echo service for tests purposes. See
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   750
     * http://en.wikipedia.org/wiki/Echo_Protocol This server uses an anonymous
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   751
     * port - NOT the standard port 7. We don't guarantee that its behavior
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   752
     * exactly matches the RFC - the only purpose of this server is to have
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   753
     * something that responds to nio tests...
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   754
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   755
    static final class UdpEchoServer extends AbstractUdpServer {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   756
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   757
        public UdpEchoServer() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   758
            this(0L);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   759
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   760
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   761
        public UdpEchoServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   762
            super(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   763
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   764
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   765
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   766
        protected UdpEchoRequest createConnection(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   767
                DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   768
            return new UdpEchoRequest(socket, request);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   769
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   770
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   771
        private final class UdpEchoRequest extends UdpRequestThread {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   772
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   773
            public UdpEchoRequest(DatagramSocket socket, DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   774
                super(socket, request);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   775
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   776
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   777
            @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   778
            public void run() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   779
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   780
                    lingerIfRequired();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   781
                    final DatagramPacket response =
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   782
                            new DatagramPacket(request.getData(),
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   783
                                    request.getOffset(), request.getLength(),
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   784
                                    request.getAddress(), request.getPort());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   785
                    send(socket, response);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   786
                } catch (IOException io) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   787
                    System.err.println("Failed to send response: " + io);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   788
                    io.printStackTrace(System.err);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   789
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   790
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   791
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   792
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   793
        public static UdpEchoServer startNewServer() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   794
            return startNewServer(0);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   795
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   796
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   797
        public static UdpEchoServer startNewServer(long linger) throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   798
            final UdpEchoServer echoServer = new UdpEchoServer(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   799
            echoServer.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   800
            return echoServer;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   801
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   802
    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   803
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   804
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   805
     * A small UDP server that emulates the Day & Time service for tests
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   806
     * purposes. See http://en.wikipedia.org/wiki/Daytime_Protocol This server
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   807
     * uses an anonymous port - NOT the standard port 13. We don't guarantee
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   808
     * that its behavior exactly matches the RFC - the only purpose of this
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   809
     * server is to have something that responds to nio tests...
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   810
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   811
    static final class UdpDayTimeServer extends AbstractUdpServer {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   812
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   813
        public UdpDayTimeServer() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   814
            this(0L);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   815
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   816
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   817
        public UdpDayTimeServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   818
            super(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   819
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   820
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   821
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   822
        protected UdpDayTimeRequestThread createConnection(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   823
                DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   824
            return new UdpDayTimeRequestThread(socket, request);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   825
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   826
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   827
        private final class UdpDayTimeRequestThread extends UdpRequestThread {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   828
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   829
            public UdpDayTimeRequestThread(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   830
                    DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   831
                super(socket, request);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   832
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   833
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   834
            @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   835
            public void run() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   836
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   837
                    lingerIfRequired();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   838
                    final byte[] data = new Date(System.currentTimeMillis())
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   839
                            .toString().getBytes("US-ASCII");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   840
                    final DatagramPacket response =
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   841
                            new DatagramPacket(data, 0, data.length,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   842
                                    request.getAddress(), request.getPort());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   843
                    send(socket, response);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   844
                } catch (IOException io) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   845
                    System.err.println("Failed to send response: " + io);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   846
                    io.printStackTrace(System.err);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   847
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   848
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   849
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   850
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   851
        public static UdpDayTimeServer startNewServer() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   852
            return startNewServer(0);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   853
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   854
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   855
        public static UdpDayTimeServer startNewServer(long linger)
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   856
                throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   857
            final UdpDayTimeServer echoServer = new UdpDayTimeServer(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   858
            echoServer.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   859
            return echoServer;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   860
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
}