test/jdk/java/nio/channels/Selector/SelectorTest.java
author mli
Fri, 31 Aug 2018 10:00:22 +0800
changeset 51602 dbb0e798deeb
parent 47216 71c04702a3d5
permissions -rw-r--r--
8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set" Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5970
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @summary Test selectors and socketchannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @library ..
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 7668
diff changeset
    27
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.channels.spi.SelectorProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class SelectorTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static List clientList = new LinkedList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static int NUM_CLIENTS = 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static int TEST_PORT = 31452;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static PrintStream log = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static int FINISH_TIME = 30000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Usage note
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * java SelectorTest [server] [client <host>] [<port>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * No arguments runs both client and server in separate threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * using the default port of 31452.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * client runs the client on this machine and connects to server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * at the given IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * server runs the server on localhost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (args.length == 0) {
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    61
            Server server = new Server(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                Thread.sleep(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            } catch (InterruptedException e) { }
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    66
            InetSocketAddress isa
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    67
                = new InetSocketAddress(InetAddress.getLocalHost(), server.port());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            Client client = new Client(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            client.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if ((server.finish(FINISH_TIME) & client.finish(FINISH_TIME)) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                throw new Exception("Failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } else if (args[0].equals("server")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (args.length > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                TEST_PORT = Integer.parseInt(args[1]);
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    78
            Server server = new Server(TEST_PORT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (server.finish(FINISH_TIME) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                throw new Exception("Failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } else if (args[0].equals("client")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (args.length < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                log.println("No host specified: terminating.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            String ip = args[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (args.length > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                TEST_PORT = Integer.parseInt(args[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            InetAddress ia = InetAddress.getByName(ip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            InetSocketAddress isa = new InetSocketAddress(ia, TEST_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            Client client = new Client(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            client.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (client.finish(FINISH_TIME) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                throw new Exception("Failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.out.println("Usage note:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            System.out.println("java SelectorTest [server] [client <host>] [<port>]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            System.out.println("No arguments runs both client and server in separate threads using the default port of 31452.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            System.out.println("client runs the client on this machine and connects to the server specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            System.out.println("server runs the server on localhost.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    static class Client extends TestThread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        InetSocketAddress isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Client(InetSocketAddress isa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            super("Client", SelectorTest.log);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            this.isa = isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        public void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            log.println("starting client...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            for (int i=0; i<NUM_CLIENTS; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                clientList.add(new RemoteEntity(i, isa, log));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            Collections.shuffle(clientList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            log.println("created "+NUM_CLIENTS+" clients");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                for (Iterator i = clientList.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    RemoteEntity re = (RemoteEntity) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    if (re.cycle()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                Collections.shuffle(clientList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            } while (clientList.size() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    static class Server extends TestThread {
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   138
        private final ServerSocketChannel ssc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        private List socketList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        private ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        private int connectionsAccepted = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        private Selector pollSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        private Selector acceptSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        private Set pkeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        private Set pskeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   147
        Server(int port) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            super("Server", SelectorTest.log);
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   149
            this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(port));
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   150
        }
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   151
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   152
        int port() {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   153
            return ssc.socket().getLocalPort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        public void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            log.println("starting server...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            acceptSelector = SelectorProvider.provider().openSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            pollSelector = SelectorProvider.provider().openSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            pkeys = pollSelector.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            pskeys = pollSelector.selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            Set readyKeys = acceptSelector.selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            RequestHandler rh = new RequestHandler(pollSelector, log);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            Thread requestThread = new Thread(rh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            requestThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            ssc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            SelectionKey acceptKey = ssc.register(acceptSelector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                                  SelectionKey.OP_ACCEPT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            while(connectionsAccepted < SelectorTest.NUM_CLIENTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                int keysAdded = acceptSelector.select(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (keysAdded > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    Iterator i = readyKeys.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    while(i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        SelectionKey sk = (SelectionKey)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        ServerSocketChannel nextReady =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                            (ServerSocketChannel)sk.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        SocketChannel sc = nextReady.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        connectionsAccepted++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        if (sc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                            synchronized (pkeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                               sc.register(pollSelector, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                "Socket does not support Channels");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            acceptKey.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            requestThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            acceptSelector.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            pollSelector.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
class RemoteEntity {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private static Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    ByteBuffer data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    int dataWrittenIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    int totalDataLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    boolean initiated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    boolean connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    boolean written = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    boolean acked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private SocketChannel sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    ByteBuffer ackBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    PrintStream log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    InetSocketAddress server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    RemoteEntity(int id, InetSocketAddress server, PrintStream log)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        int connectFailures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this.log = log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        sc = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // Prepare the data buffer to write out from this entity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        // Let's use both slow and fast buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (rnd.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            data = ByteBuffer.allocateDirect(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            data = ByteBuffer.allocate(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        String number = Integer.toString(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (number.length() == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            number = "0"+number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        String source = "Testing from " + number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        data.put(source.getBytes("8859_1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        data.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        totalDataLength = source.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // Allocate an ack buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        ackBuffer = ByteBuffer.allocateDirect(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private void reset() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        sc = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    private void connect() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            connected = sc.connect(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            initiated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }  catch (ConnectException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            initiated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private void finishConnect() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            connected = sc.finishConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }  catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            initiated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    int id() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    boolean cycle() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (!initiated)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        else if (!connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            finishConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        else if (!written)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            writeCycle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        else if (!acked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            ackCycle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        else if (!closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private void ackCycle() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        //log.println("acking from "+id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int bytesRead = sc.read(ackBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (bytesRead > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            acked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private void close() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    private void writeCycle() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        log.println("writing from "+id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        int numBytesToWrite = rnd.nextInt(10)+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        int newWriteTarget = dataWrittenIndex + numBytesToWrite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (newWriteTarget > totalDataLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            newWriteTarget = totalDataLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        data.limit(newWriteTarget);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        int bytesWritten = sc.write(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (bytesWritten > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            dataWrittenIndex += bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (dataWrittenIndex == totalDataLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            written = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            sc.socket().shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
class RequestHandler implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private static Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private Selector selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private int connectionsHandled = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private HashMap dataBin = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    PrintStream log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public RequestHandler(Selector selector, PrintStream log) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        this.selector = selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        this.log = log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        log.println("starting request handler...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int connectionsAccepted = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        Set nKeys = selector.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        Set readyKeys = selector.selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            while(connectionsHandled < SelectorTest.NUM_CLIENTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                int numKeys = selector.select(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                // Process channels with data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                synchronized (nKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    if (readyKeys.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        Iterator i = readyKeys.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        while(i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                            SelectionKey sk = (SelectionKey)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                            i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                            SocketChannel sc = (SocketChannel)sk.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                            if (sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                read(sk, sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                // Give other threads a chance to run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                if (numKeys == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        Thread.sleep(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    } catch (Exception x) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            log.println("Unexpected error 1: "+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private void read(SelectionKey sk, SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        ByteBuffer bin = (ByteBuffer)dataBin.get(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (bin == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (rnd.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                bin = ByteBuffer.allocateDirect(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                bin = ByteBuffer.allocate(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            dataBin.put(sc, bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            bytesRead = sc.read(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        } while(bytesRead > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (bytesRead == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            sk.interestOps(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            bin.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            int size = bin.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            byte[] data = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            for(int j=0; j<size; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                data[j] = bin.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            String message = new String(data, "8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            connectionsHandled++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            acknowledge(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            log.println("Received >>>"+message + "<<<");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            log.println("Handled: "+connectionsHandled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private void acknowledge(SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            ByteBuffer ackBuffer = ByteBuffer.allocateDirect(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            String s = "ack";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            ackBuffer.put(s.getBytes("8859_1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            ackBuffer.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            int bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            while(bytesWritten == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                bytesWritten += sc.write(ackBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
}