jdk/test/java/nio/channels/Selector/SelectorTest.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5970 d4e98bbfb0be
child 30046 cf2c86e1819e
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
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 ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.spi.SelectorProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class SelectorTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static List clientList = new LinkedList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static int NUM_CLIENTS = 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static int TEST_PORT = 31452;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static PrintStream log = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static int FINISH_TIME = 30000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Usage note
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * java SelectorTest [server] [client <host>] [<port>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * No arguments runs both client and server in separate threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * using the default port of 31452.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * client runs the client on this machine and connects to server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * at the given IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * server runs the server on localhost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (args.length == 0) {
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    60
            Server server = new Server(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                Thread.sleep(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            } catch (InterruptedException e) { }
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    65
            InetSocketAddress isa
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    66
                = new InetSocketAddress(InetAddress.getLocalHost(), server.port());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            Client client = new Client(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            client.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if ((server.finish(FINISH_TIME) & client.finish(FINISH_TIME)) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                throw new Exception("Failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } else if (args[0].equals("server")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (args.length > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                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
    77
            Server server = new Server(TEST_PORT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (server.finish(FINISH_TIME) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                throw new Exception("Failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        } else if (args[0].equals("client")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (args.length < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                log.println("No host specified: terminating.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            String ip = args[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (args.length > 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                TEST_PORT = Integer.parseInt(args[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            InetAddress ia = InetAddress.getByName(ip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            InetSocketAddress isa = new InetSocketAddress(ia, TEST_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            Client client = new Client(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            client.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (client.finish(FINISH_TIME) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                throw new Exception("Failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            System.out.println("Usage note:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.out.println("java SelectorTest [server] [client <host>] [<port>]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            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
   104
            System.out.println("client runs the client on this machine and connects to the server specified.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            System.out.println("server runs the server on localhost.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static class Client extends TestThread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        InetSocketAddress isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Client(InetSocketAddress isa) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            super("Client", SelectorTest.log);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            this.isa = isa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        public void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            log.println("starting client...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            for (int i=0; i<NUM_CLIENTS; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                clientList.add(new RemoteEntity(i, isa, log));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            Collections.shuffle(clientList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            log.println("created "+NUM_CLIENTS+" clients");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                for (Iterator i = clientList.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    RemoteEntity re = (RemoteEntity) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    if (re.cycle()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                Collections.shuffle(clientList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            } while (clientList.size() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    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
   137
        private final ServerSocketChannel ssc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        private List socketList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        private ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        private int connectionsAccepted = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        private Selector pollSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        private Selector acceptSelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        private Set pkeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        private Set pskeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   146
        Server(int port) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            super("Server", SelectorTest.log);
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   148
            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
   149
        }
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
        int port() {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   152
            return ssc.socket().getLocalPort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        public void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            log.println("starting server...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            acceptSelector = SelectorProvider.provider().openSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            pollSelector = SelectorProvider.provider().openSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            pkeys = pollSelector.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            pskeys = pollSelector.selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            Set readyKeys = acceptSelector.selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            RequestHandler rh = new RequestHandler(pollSelector, log);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            Thread requestThread = new Thread(rh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            requestThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            ssc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            SelectionKey acceptKey = ssc.register(acceptSelector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                                  SelectionKey.OP_ACCEPT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            while(connectionsAccepted < SelectorTest.NUM_CLIENTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                int keysAdded = acceptSelector.select(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if (keysAdded > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    Iterator i = readyKeys.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    while(i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        SelectionKey sk = (SelectionKey)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        ServerSocketChannel nextReady =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                            (ServerSocketChannel)sk.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        SocketChannel sc = nextReady.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        connectionsAccepted++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        if (sc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                            sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            synchronized (pkeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                               sc.register(pollSelector, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            throw new RuntimeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                "Socket does not support Channels");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        }
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
            acceptKey.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            requestThread.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            acceptSelector.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            pollSelector.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
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
class RemoteEntity {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private static Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    ByteBuffer data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    int dataWrittenIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    int totalDataLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    boolean initiated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    boolean connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    boolean written = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    boolean acked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private SocketChannel sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    ByteBuffer ackBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    PrintStream log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    InetSocketAddress server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    RemoteEntity(int id, InetSocketAddress server, PrintStream log)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int connectFailures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        this.log = log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        sc = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // Prepare the data buffer to write out from this entity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // Let's use both slow and fast buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (rnd.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            data = ByteBuffer.allocateDirect(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            data = ByteBuffer.allocate(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        String number = Integer.toString(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (number.length() == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            number = "0"+number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        String source = "Testing from " + number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        data.put(source.getBytes("8859_1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        data.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        totalDataLength = source.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // Allocate an ack buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        ackBuffer = ByteBuffer.allocateDirect(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private void reset() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        sc = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private void connect() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            connected = sc.connect(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            initiated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }  catch (ConnectException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            initiated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private void finishConnect() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            connected = sc.finishConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }  catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            initiated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    int id() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    boolean cycle() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (!initiated)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        else if (!connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            finishConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        else if (!written)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            writeCycle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        else if (!acked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            ackCycle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        else if (!closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private void ackCycle() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        //log.println("acking from "+id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        int bytesRead = sc.read(ackBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (bytesRead > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            acked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private void close() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private void writeCycle() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        log.println("writing from "+id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        int numBytesToWrite = rnd.nextInt(10)+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        int newWriteTarget = dataWrittenIndex + numBytesToWrite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (newWriteTarget > totalDataLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            newWriteTarget = totalDataLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        data.limit(newWriteTarget);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        int bytesWritten = sc.write(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (bytesWritten > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            dataWrittenIndex += bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (dataWrittenIndex == totalDataLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            written = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            sc.socket().shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
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
class RequestHandler implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    private static Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private Selector selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private int connectionsHandled = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private HashMap dataBin = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    PrintStream log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public RequestHandler(Selector selector, PrintStream log) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        this.selector = selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        this.log = log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        log.println("starting request handler...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        int connectionsAccepted = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        Set nKeys = selector.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        Set readyKeys = selector.selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            while(connectionsHandled < SelectorTest.NUM_CLIENTS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                int numKeys = selector.select(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                // Process channels with data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                synchronized (nKeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    if (readyKeys.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        Iterator i = readyKeys.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        while(i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            SelectionKey sk = (SelectionKey)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                            i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                            SocketChannel sc = (SocketChannel)sk.channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                            if (sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                read(sk, sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        }
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
                // Give other threads a chance to run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                if (numKeys == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                        Thread.sleep(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    } catch (Exception x) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            log.println("Unexpected error 1: "+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private void read(SelectionKey sk, SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        ByteBuffer bin = (ByteBuffer)dataBin.get(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (bin == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (rnd.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                bin = ByteBuffer.allocateDirect(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                bin = ByteBuffer.allocate(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            dataBin.put(sc, bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            bytesRead = sc.read(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        } while(bytesRead > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (bytesRead == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            sk.interestOps(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            bin.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            int size = bin.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            byte[] data = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            for(int j=0; j<size; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                data[j] = bin.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            String message = new String(data, "8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            connectionsHandled++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            acknowledge(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            log.println("Received >>>"+message + "<<<");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            log.println("Handled: "+connectionsHandled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    private void acknowledge(SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            ByteBuffer ackBuffer = ByteBuffer.allocateDirect(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            String s = "ack";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            ackBuffer.put(s.getBytes("8859_1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            ackBuffer.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            int bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            while(bytesWritten == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                bytesWritten += sc.write(ackBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
}