jdk/test/java/nio/channels/TestServers.java
author katleman
Thu, 17 Apr 2014 10:13:36 -0700
changeset 23942 7b722b4cdd2d
parent 14415 7a31b0e0cfaf
child 36963 1558f7600497
permissions -rw-r--r--
Added tag jdk9-b09 for changeset 667dccd79b65
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
     2
 * Copyright (c) 2012, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    35
public class TestServers {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    36
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    37
    private 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
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    40
     * 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
    41
     * given machine.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    42
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    43
    static abstract class AbstractServer {
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
        private 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
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    48
        public abstract int getPort();
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 InetAddress getAddress();
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
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
     * 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
    55
     * 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
    56
     * 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
    57
     * 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
    58
     * 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
    59
     * 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
    60
     * 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
    61
     * not been reused.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    62
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    63
    static class RefusingServer extends AbstractServer {
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
        final InetAddress address;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    66
        final int port;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    67
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    68
        private RefusingServer(InetAddress address, int port) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    69
            this.address = address;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    70
            this.port = port;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    71
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    72
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    73
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    74
        public int getPort() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    75
            return port;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    76
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    77
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    78
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    79
        public InetAddress getAddress() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    80
            return address;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    81
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    82
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    83
        public static RefusingServer startNewServer() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    84
            ServerSocket socket = new ServerSocket(0, 100,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    85
                    InetAddress.getLocalHost());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    86
            RefusingServer server = new RefusingServer(socket.getInetAddress(),
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    87
                    socket.getLocalPort());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    88
            socket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    89
            return server;
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
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    93
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    94
     * 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
    95
     * 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
    96
     * 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
    97
     * extends/creates Threads instead of using an executor service.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    98
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
    99
    static abstract class AbstractTcpServer extends AbstractServer
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   100
            implements Runnable, Closeable {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   101
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   102
        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
   103
        private Thread acceptThread; // thread waiting for accept
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   104
        // 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
   105
        private List<TcpConnectionThread> connections = new ArrayList<>();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   106
        private ServerSocket serverSocket; // the server socket
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   107
        private boolean started = false; // whether the server is started
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   108
        Throwable error = null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   109
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
         * Creates a new abstract TCP server.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   112
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   113
         * @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
   114
         * responding to requests.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   115
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   116
        protected AbstractTcpServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   117
            this.linger = linger;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   118
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
14415
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
         * 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
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   123
         * @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
   124
         * @exception IllegalStateException is thrown if the server is not
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   125
         * started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   126
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   127
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   128
        public final synchronized int getPort() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   129
            if (!started) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   130
                throw new IllegalStateException("Not started");
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
            return serverSocket.getLocalPort();
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
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
         * 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
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   138
         * @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
   139
         * @exception IllegalStateException is thrown if the server is not
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   140
         * started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   141
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   142
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   143
        public final synchronized InetAddress getAddress() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   144
            if (!started) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   145
                throw new IllegalStateException("Not started");
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
            return serverSocket.getInetAddress();
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
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
         * Tells whether 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
         * @return true if the server is started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   154
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   155
        public final synchronized boolean isStarted() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   156
            return started;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   157
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
14415
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
         * Creates a new server socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   161
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   162
         * @param port local port to bind to.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   163
         * @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
   164
         * connections.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   165
         * @param address local address to bind to.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   166
         * @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
   167
         * @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
   168
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   169
        protected ServerSocket newServerSocket(int port, int backlog,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   170
                InetAddress address)
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   171
                throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   172
            return new ServerSocket(port, backlog, address);
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
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
         * Starts listening for connections.
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
         * @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
   179
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   180
        public final synchronized void start() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   181
            if (started) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   182
                return;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   183
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   184
            final ServerSocket socket =
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   185
                    newServerSocket(0, 100, InetAddress.getLocalHost());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   186
            serverSocket = socket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   187
            acceptThread = new Thread(this);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   188
            acceptThread.setDaemon(true);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   189
            acceptThread.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   190
            started = true;
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
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
         * Calls {@code Thread.sleep(linger);}
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   195
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   196
        protected final void lingerIfRequired() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   197
            if (linger > 0) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   198
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   199
                    Thread.sleep(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   200
                } catch (InterruptedException x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   201
                    Thread.interrupted();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   202
                    final ServerSocket socket = serverSocket();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   203
                    if (socket != null && !socket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   204
                        System.err.println("Thread interrupted...");
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
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   209
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   210
        final synchronized ServerSocket serverSocket() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   211
            return this.serverSocket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   212
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
14415
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
         * The main accept loop.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   216
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   217
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   218
        public final void run() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   219
            final ServerSocket sSocket = serverSocket();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            try {
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   221
                Socket s;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   222
                while (isStarted() && !Thread.interrupted()
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   223
                        && (s = sSocket.accept()) != null) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   224
                    lingerIfRequired();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   225
                    listen(s);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   226
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   227
            } catch (Exception x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   228
                error = x;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   229
            } finally {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   230
                synchronized (this) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   231
                    if (!sSocket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   232
                        try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   233
                            sSocket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   234
                        } catch (IOException x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   235
                            System.err.println("Failed to close server socket");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   236
                        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   237
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   238
                    if (started && this.serverSocket == sSocket) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   239
                        started = false;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   240
                        this.serverSocket = null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   241
                        this.acceptThread = null;
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
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
         * Represents a connection accepted by the server.
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 abstract class TcpConnectionThread extends Thread {
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 final Socket socket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   253
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   254
            protected TcpConnectionThread(Socket socket) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   255
                this.socket = socket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   256
                this.setDaemon(true);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   257
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   258
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   259
            public void close() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   260
                socket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   261
                interrupt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
14415
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
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   265
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   266
         * Creates a new TcpConnnectionThread to handle the connection through
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   267
         * an accepted socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   268
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   269
         * @param s the socket returned by {@code serverSocket.accept()}.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   270
         * @return a new TcpConnnectionThread to handle the connection through
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   271
         * an accepted socket.
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
        protected abstract TcpConnectionThread createConnection(Socket s);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   274
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   275
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   276
         * 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
   277
         * socket.
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
         * @param s the socket returned by {@code serverSocket.accept()}.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   280
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   281
        private synchronized void listen(Socket s) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   282
            TcpConnectionThread c = createConnection(s);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   283
            c.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   284
            addConnection(c);
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
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
         * Add the connection to the list of accepted connections.
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
         * @param connection an accepted connection.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   291
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   292
        protected synchronized void addConnection(
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   293
                TcpConnectionThread connection) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   294
            connections.add(connection);
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
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
         * Remove the connection from the list of accepted connections.
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
         * @param connection an accepted connection.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   301
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   302
        protected synchronized void removeConnection(
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   303
                TcpConnectionThread connection) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   304
            connections.remove(connection);
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
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   307
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   308
         * 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
   309
         * of accepted connections.
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
         * @throws IOException
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   312
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   313
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   314
        public synchronized void close() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   315
            if (serverSocket != null && !serverSocket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   316
                serverSocket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   317
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   318
            if (acceptThread != null) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   319
                acceptThread.interrupt();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   320
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   321
            int failed = 0;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   322
            for (TcpConnectionThread c : connections) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   323
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   324
                    c.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   325
                } catch (IOException x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   326
                    // no matter - we're closing.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   327
                    failed++;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   328
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   329
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   330
            connections.clear();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   331
            if (failed > 0) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   332
                throw new IOException("Failed to close some connections");
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
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   337
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   338
     * 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
   339
     * 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
   340
     * 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
   341
     * 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
   342
     * something that responds to nio tests...
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
    static final class EchoServer extends AbstractTcpServer {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   345
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   346
        public EchoServer() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   347
            this(0L);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   348
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   349
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   350
        public EchoServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   351
            super(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   352
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   353
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   354
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   355
        protected TcpConnectionThread createConnection(Socket s) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   356
            return new EchoConnection(s);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   357
        }
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
        private final class EchoConnection extends TcpConnectionThread {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   360
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   361
            public EchoConnection(Socket socket) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   362
                super(socket);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   363
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   364
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   365
            @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   366
            public void run() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   367
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   368
                    final InputStream is = socket.getInputStream();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   369
                    final OutputStream out = socket.getOutputStream();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   370
                    byte[] b = new byte[255];
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   371
                    int n;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   372
                    while ((n = is.read(b)) > 0) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   373
                        lingerIfRequired();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   374
                        out.write(b, 0, n);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   375
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   376
                } catch (IOException io) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   377
                    // fall through to finally
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   378
                } finally {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   379
                    if (!socket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   380
                        try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   381
                            socket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   382
                        } catch (IOException x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   383
                            System.err.println(
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   384
                                    "Failed to close echo connection socket");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   385
                        }
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
                    removeConnection(this);
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
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   391
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   392
        public static EchoServer startNewServer() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   393
            return startNewServer(0);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   394
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   395
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   396
        public static EchoServer startNewServer(long linger) throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   397
            final EchoServer echoServer = new EchoServer(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   398
            echoServer.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   399
            return echoServer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   403
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   404
     * 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
   405
     * 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
   406
     * 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
   407
     * 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
   408
     * 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
   409
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   410
    static final class DayTimeServer extends AbstractTcpServer {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   411
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   412
        public DayTimeServer() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   413
            this(0L);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   414
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   416
        public DayTimeServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   417
            super(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   418
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   419
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   420
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   421
        protected TcpConnectionThread createConnection(Socket s) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   422
            return new DayTimeServerConnection(s);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   423
        }
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
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   426
        protected void addConnection(TcpConnectionThread connection) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   427
            // 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
   428
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   429
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   430
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   431
        protected void removeConnection(TcpConnectionThread connection) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   432
            // 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
   433
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   434
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   435
        private final class DayTimeServerConnection extends TcpConnectionThread {
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
            public DayTimeServerConnection(Socket socket) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   438
                super(socket);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   439
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   440
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   441
            @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   442
            public void run() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   443
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   444
                    final OutputStream out = socket.getOutputStream();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   445
                    lingerIfRequired();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   446
                    out.write(new Date(System.currentTimeMillis())
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   447
                            .toString().getBytes("US-ASCII"));
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   448
                    out.flush();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   449
                } catch (IOException io) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   450
                    // fall through to finally
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   451
                } finally {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   452
                    if (!socket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   453
                        try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   454
                            socket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   455
                        } catch (IOException x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   456
                            System.err.println(
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   457
                                    "Failed to close echo connection socket");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   458
                        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   459
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   460
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   461
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   462
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   463
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   464
        public static DayTimeServer startNewServer()
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   465
                throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   466
            return startNewServer(0);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   467
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   468
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   469
        public static DayTimeServer startNewServer(long linger)
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   470
                throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   471
            final DayTimeServer daytimeServer = new DayTimeServer(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   472
            daytimeServer.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   473
            return daytimeServer;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   474
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   477
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   478
     * 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
   479
     * 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
   480
     * 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
   481
     * extends/creates Threads instead of using an executor service.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   482
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   483
    static abstract class AbstractUdpServer extends AbstractServer
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   484
            implements Runnable, Closeable {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   485
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   486
        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
   487
        private Thread acceptThread; // thread waiting for packets
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   488
        private DatagramSocket serverSocket; // the server socket
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   489
        private boolean started = false; // whether the server is started
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   490
        Throwable error = null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   491
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   492
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   493
         * Creates a new abstract UDP server.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   494
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   495
         * @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
   496
         * responding to requests.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   497
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   498
        protected AbstractUdpServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   499
            this.linger = linger;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   500
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   501
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   502
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   503
         * The local port to which the server is bound.
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
         * @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
   506
         * @exception IllegalStateException is thrown if the server is not
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   507
         * started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   508
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   509
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   510
        public final synchronized int getPort() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   511
            if (!started) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   512
                throw new IllegalStateException("Not started");
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
            return serverSocket.getLocalPort();
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
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
         * The local address to which the server is bound.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   519
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   520
         * @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
   521
         * @exception IllegalStateException is thrown if the server is not
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   522
         * started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   523
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   524
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   525
        public final synchronized InetAddress getAddress() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   526
            if (!started) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   527
                throw new IllegalStateException("Not started");
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
            return serverSocket.getLocalAddress();
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
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
         * Tells whether the server is started.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   534
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   535
         * @return true if the server is 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
        public final synchronized boolean isStarted() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   538
            return started;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   539
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   540
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
         * Creates a new datagram socket.
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
         * @param port local port to bind to.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   545
         * @param address local address to bind to.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   546
         * @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
   547
         * @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
   548
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   549
        protected DatagramSocket newDatagramSocket(int port,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   550
                InetAddress address)
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   551
                throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   552
            return new DatagramSocket(port, address);
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
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   556
         * Starts listening for connections.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   557
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   558
         * @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
   559
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   560
        public final synchronized void start() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   561
            if (started) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   562
                return;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   563
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   564
            final DatagramSocket socket =
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   565
                    newDatagramSocket(0, InetAddress.getLocalHost());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   566
            serverSocket = socket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   567
            acceptThread = new Thread(this);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   568
            acceptThread.setDaemon(true);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   569
            acceptThread.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   570
            started = true;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   571
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   573
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   574
         * Calls {@code Thread.sleep(linger);}
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   575
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   576
        protected final void lingerIfRequired() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   577
            if (linger > 0) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   578
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   579
                    Thread.sleep(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   580
                } catch (InterruptedException x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   581
                    Thread.interrupted();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   582
                    final DatagramSocket socket = serverSocket();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   583
                    if (socket != null && !socket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   584
                        System.err.println("Thread interrupted...");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   585
                    }
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
            }
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
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   590
        final synchronized DatagramSocket serverSocket() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   591
            return this.serverSocket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   592
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   593
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   594
        final synchronized boolean send(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   595
                DatagramPacket response) throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   596
            if (!socket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   597
                socket.send(response);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   598
                return true;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   599
            } else {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   600
                return false;
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
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   604
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   605
         * The main receive loop.
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
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   608
        public final void run() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   609
            final DatagramSocket sSocket = serverSocket();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   610
            try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   611
                final int size = Math.max(1024, sSocket.getReceiveBufferSize());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   612
                if (size > sSocket.getReceiveBufferSize()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   613
                    sSocket.setReceiveBufferSize(size);
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
                while (isStarted() && !Thread.interrupted() && !sSocket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   616
                    final byte[] buf = new byte[size];
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   617
                    final DatagramPacket packet =
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   618
                            new DatagramPacket(buf, buf.length);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   619
                    lingerIfRequired();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   620
                    sSocket.receive(packet);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   621
                    //System.out.println("Received packet from: "
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   622
                    //        + packet.getAddress()+":"+packet.getPort());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   623
                    handle(sSocket, packet);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   624
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   625
            } catch (Exception x) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   626
                error = x;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   627
            } finally {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   628
                synchronized (this) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   629
                    if (!sSocket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   630
                        sSocket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   631
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   632
                    if (started && this.serverSocket == sSocket) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   633
                        started = false;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   634
                        this.serverSocket = null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   635
                        this.acceptThread = null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   636
                    }
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
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   639
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   640
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   641
        /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   642
         * Represents an UDP request received by the server.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   643
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   644
        protected abstract class UdpRequestThread extends Thread {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   645
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   646
            protected final DatagramPacket request;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   647
            protected final DatagramSocket socket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   648
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   649
            protected UdpRequestThread(DatagramSocket socket, DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   650
                this.socket = socket;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   651
                this.request = request;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   652
                this.setDaemon(true);
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
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
         * Creates a new UdpRequestThread to handle a DatagramPacket received
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   658
         * through a DatagramSocket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   659
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   660
         * @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
   661
         * @param request the datagram packet received through the socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   662
         * @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
   663
         * a DatagramSocket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   664
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   665
        protected abstract UdpRequestThread createConnection(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   666
                DatagramPacket request);
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
         * 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
   670
         * datagram packet.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   671
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   672
         * @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
   673
         * @param request the datagram packet received through the socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   674
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   675
        private synchronized void handle(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   676
                DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   677
            UdpRequestThread c = createConnection(socket, request);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   678
            // 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
   679
            if (c != null) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   680
                c.start();
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
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   683
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
         * Close the server socket.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   686
         *
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   687
         * @throws IOException
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   688
         */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   689
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   690
        public synchronized void close() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   691
            if (serverSocket != null && !serverSocket.isClosed()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   692
                serverSocket.close();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   693
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   694
            if (acceptThread != null) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   695
                acceptThread.interrupt();
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
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   700
    /**
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   701
     * 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
   702
     * 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
   703
     * 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
   704
     * 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
   705
     * have something that responds to nio tests...
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
    static final class UdpDiscardServer extends AbstractUdpServer {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   708
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   709
        public UdpDiscardServer() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   710
            this(0L);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   711
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   712
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   713
        public UdpDiscardServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   714
            super(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   715
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   716
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   717
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   718
        protected UdpRequestThread createConnection(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   719
                DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   720
            // no response required
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   721
            return null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   722
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   723
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   724
        public static UdpDiscardServer startNewServer() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   725
            return startNewServer(0);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   726
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   727
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   728
        public static UdpDiscardServer startNewServer(long linger) throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   729
            final UdpDiscardServer discardServer = new UdpDiscardServer(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   730
            discardServer.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   731
            return discardServer;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   732
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   733
    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   734
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
     * 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
   737
     * 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
   738
     * 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
   739
     * 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
   740
     * something that responds to nio tests...
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   741
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   742
    static final class UdpEchoServer extends AbstractUdpServer {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   743
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   744
        public UdpEchoServer() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   745
            this(0L);
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
        public UdpEchoServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   749
            super(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   750
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   751
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   752
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   753
        protected UdpEchoRequest createConnection(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   754
                DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   755
            return new UdpEchoRequest(socket, request);
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
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   758
        private final class UdpEchoRequest extends UdpRequestThread {
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
            public UdpEchoRequest(DatagramSocket socket, DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   761
                super(socket, request);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   762
            }
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
            @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   765
            public void run() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   766
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   767
                    lingerIfRequired();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   768
                    final DatagramPacket response =
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   769
                            new DatagramPacket(request.getData(),
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   770
                                    request.getOffset(), request.getLength(),
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   771
                                    request.getAddress(), request.getPort());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   772
                    send(socket, response);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   773
                } catch (IOException io) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   774
                    System.err.println("Failed to send response: " + io);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   775
                    io.printStackTrace(System.err);
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
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   778
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   779
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   780
        public static UdpEchoServer startNewServer() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   781
            return startNewServer(0);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   782
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   783
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   784
        public static UdpEchoServer startNewServer(long linger) throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   785
            final UdpEchoServer echoServer = new UdpEchoServer(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   786
            echoServer.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   787
            return echoServer;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   788
        }
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
     * 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
   793
     * 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
   794
     * 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
   795
     * 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
   796
     * 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
   797
     */
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   798
    static final class UdpDayTimeServer extends AbstractUdpServer {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   799
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   800
        public UdpDayTimeServer() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   801
            this(0L);
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
        public UdpDayTimeServer(long linger) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   805
            super(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   806
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   807
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   808
        @Override
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   809
        protected UdpDayTimeRequestThread createConnection(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   810
                DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   811
            return new UdpDayTimeRequestThread(socket, request);
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
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   814
        private final class UdpDayTimeRequestThread extends UdpRequestThread {
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
            public UdpDayTimeRequestThread(DatagramSocket socket,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   817
                    DatagramPacket request) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   818
                super(socket, request);
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
            public void run() {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   823
                try {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   824
                    lingerIfRequired();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   825
                    final byte[] data = new Date(System.currentTimeMillis())
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   826
                            .toString().getBytes("US-ASCII");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   827
                    final DatagramPacket response =
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   828
                            new DatagramPacket(data, 0, data.length,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   829
                                    request.getAddress(), request.getPort());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   830
                    send(socket, response);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   831
                } catch (IOException io) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   832
                    System.err.println("Failed to send response: " + io);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   833
                    io.printStackTrace(System.err);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   834
                }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   835
            }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   836
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   837
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   838
        public static UdpDayTimeServer startNewServer() throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   839
            return startNewServer(0);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   840
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   841
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   842
        public static UdpDayTimeServer startNewServer(long linger)
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   843
                throws IOException {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   844
            final UdpDayTimeServer echoServer = new UdpDayTimeServer(linger);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   845
            echoServer.start();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   846
            return echoServer;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 14342
diff changeset
   847
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
}