jdk/test/sun/net/www/http/HttpClient/MultiThreadTest.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6115 7c523cf2bc8a
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: 6115
diff changeset
     2
 * Copyright (c) 2002, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4636628
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary HttpURLConnection duplicates HTTP GET requests when used with multiple threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This tests keep-alive behavior using chunkedinputstreams
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * It checks that keep-alive connections are used and also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * that requests are not being repeated (due to errors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * It also checks that the keepalive connections are closed eventually
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * because the test will not terminate if the connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * are not closed by the keep-alive timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class MultiThreadTest extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Is debugging enabled - start with -d to enable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static Object threadlock = new Object ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static int threadCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static Object getLock() { return threadlock; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static void debug(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static int reqnum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    void doRequest(String uri) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        URL url = new URL(uri + "?foo="+reqnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        reqnum ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        HttpURLConnection http = (HttpURLConnection)url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        InputStream in = http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        byte b[] = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            n = in.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (n > 0) total += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        debug ("client: read " + total + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        http.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    String uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    byte[] b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    int requests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    MultiThreadTest(int port, int requests) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        uri = "http://localhost:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                     port + "/foo.html";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        b = new byte [256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.requests = requests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        synchronized (threadlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            threadCounter ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            for (int i=0; i<requests; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                doRequest (uri);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new RuntimeException (e.getMessage());
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   103
        } finally {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   104
            synchronized (threadlock) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   105
                threadCounter --;
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   106
                if (threadCounter == 0) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   107
                    threadlock.notifyAll();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   108
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static int threads=5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int x = 0, arg_len = args.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int requests = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (arg_len > 0 && args[0].equals("-d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            x = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            arg_len --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (arg_len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            threads = Integer.parseInt (args[x]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            requests = Integer.parseInt (args[x+1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        /* start the server */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        ServerSocket ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Server svr = new Server(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        svr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Object lock = MultiThreadTest.getLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            for (int i=0; i<threads; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                MultiThreadTest t = new MultiThreadTest(ss.getLocalPort(), requests);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                t.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                lock.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // shutdown server - we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        svr.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        int cnt = svr.connectionCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        MultiThreadTest.debug("Connections = " + cnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int reqs = Worker.getRequests ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        MultiThreadTest.debug("Requests = " + reqs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        System.out.println ("Connection count = " + cnt + " Request count = " + reqs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (cnt > threads) { // could be less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new RuntimeException ("Expected "+threads + " connections: used " +cnt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if  (reqs != threads*requests) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new RuntimeException ("Expected "+ threads*requests+ " requests: got " +reqs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Server thread to accept connection and create worker threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * to service each connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    class Server extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        int connectionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        boolean shutdown = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Server(ServerSocket ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            this.ss = ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        public synchronized int connectionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return connectionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        public synchronized void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            shutdown = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                ss.setSoTimeout(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        MultiThreadTest.debug("server: calling accept.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        MultiThreadTest.debug("server: return accept.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    } catch (SocketTimeoutException te) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        MultiThreadTest.debug("server: STE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                            if (shutdown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                MultiThreadTest.debug("server: Shuting down.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        id = connectionCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    Worker w = new Worker(s, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    w.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    MultiThreadTest.debug("server: Started worker " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                } catch (Exception e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Worker thread to service single connection - can service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * multiple http requests on same connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    class Worker extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Worker(Socket s, int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        static int requests = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        static Object rlock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        public static int getRequests () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            synchronized (rlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                return requests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        public static void incRequests () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            synchronized (rlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                requests++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        int readUntil (InputStream in, char[] seq) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            int i=0, count=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                int c = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                if (c == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if (c == seq[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    if (i == seq.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                int max = 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                byte b[] = new byte[1000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                InputStream in = new BufferedInputStream (s.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                // response to client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                    new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                                s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    // read entire request from client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    int n=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    n = readUntil (in, new char[] {'\r','\n', '\r','\n'});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        MultiThreadTest.debug("worker: " + id + ": Shutdown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    MultiThreadTest.debug("worker " + id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        ": Read request from client " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        "(" + n + " bytes).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    incRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    out.print("HTTP/1.1 200 OK\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    out.print("Transfer-Encoding: chunked\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    out.print("Content-Type: text/html\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    out.print("Connection: Keep-Alive\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    out.print ("Keep-Alive: timeout=15, max="+max+"\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    out.print ("6\r\nHello \r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    out.print ("5\r\nWorld\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    out.print ("0\r\n\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    if (--max == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                } catch (Exception e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }