jdk/test/sun/net/www/httptest/HttpServer.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
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.net.www.MessageHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class implements a simple HTTP server. It uses multiple threads to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * handle connections in parallel, and also multiple connections/requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * can be handled per thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * It must be instantiated with a {@link HttpCallback} object to which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * requests are given and must be handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Simple synchronization between the client(s) and server can be done
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * using the {@link #waitForCondition(String)}, {@link #setCondition(String)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * {@link #rendezvous(String,int)} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * NOTE NOTE NOTE NOTE NOTE NOTE NOTE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * If changes are made here, please sure they are propagated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the HTTPS equivalent in the JSSE regression test suite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * NOTE NOTE NOTE NOTE NOTE NOTE NOTE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class HttpServer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    ServerSocketChannel schan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    int threads;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    int cperthread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    HttpCallback cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    Server[] servers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Create a <code>HttpServer<code> instance with the specified callback object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * for handling requests. One thread is created to handle requests,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * and up to ten TCP connections will be handled simultaneously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param cb the callback object which is invoked to handle each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *  incoming request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public HttpServer (HttpCallback cb) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this (cb, 1, 10, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Create a <code>HttpServer<code> instance with the specified number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * threads and maximum number of connections per thread. This functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * the same as the 4 arg constructor, where the port argument is set to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param cb the callback object which is invoked to handle each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *     incoming request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param threads the number of threads to create to handle requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *     in parallel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param cperthread the number of simultaneous TCP connections to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *     handle per thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public HttpServer (HttpCallback cb, int threads, int cperthread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this (cb, threads, cperthread, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Create a <code>HttpServer<code> instance with the specified number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * of threads and maximum number of connections per thread and running on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * the specified port. The specified number of threads are created to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * handle incoming requests, and each thread is allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * to handle a number of simultaneous TCP connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param cb the callback object which is invoked to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *  each incoming request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param threads the number of threads to create to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *  requests in parallel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param cperthread the number of simultaneous TCP connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *  to handle per thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param port the port number to bind the server to. <code>Zero</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *  means choose any free port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public HttpServer (HttpCallback cb, int threads, int cperthread, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        schan = ServerSocketChannel.open ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        InetSocketAddress addr = new InetSocketAddress (port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        schan.socket().bind (addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.threads = threads;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.cb = cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.cperthread = cperthread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        servers = new Server [threads];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        for (int i=0; i<threads; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            servers[i] = new Server (cb, schan, cperthread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            servers[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /** Tell all threads in the server to exit within 5 seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *  This is an abortive termination. Just prior to the thread exiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *  all channels in that thread waiting to be closed are forceably closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public void terminate () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        for (int i=0; i<threads; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            servers[i].terminate ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * return the local port number to which the server is bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return the local port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public int getLocalPort () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return schan.socket().getLocalPort ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static class Server extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        ServerSocketChannel schan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Selector selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        SelectionKey listenerKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        SelectionKey key; /* the current key being processed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        HttpCallback cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        ByteBuffer consumeBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int maxconn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int nconn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        ClosedChannelList clist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        boolean shutdown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        Server (HttpCallback cb, ServerSocketChannel schan, int maxconn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            this.schan = schan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            this.maxconn = maxconn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            this.cb = cb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            nconn = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            consumeBuffer = ByteBuffer.allocate (512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            clist = new ClosedChannelList ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                selector = Selector.open ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                schan.configureBlocking (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                listenerKey = schan.register (selector, SelectionKey.OP_ACCEPT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                System.err.println ("Server could not start: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        /* Stop the thread as soon as possible */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        public synchronized void terminate () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            shutdown = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        public void run ()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    selector.select (1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    Set selected = selector.selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    Iterator iter = selected.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        key = (SelectionKey)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        if (key.equals (listenerKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                            SocketChannel sock = schan.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            if (sock == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                /* false notification */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                            sock.configureBlocking (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                            sock.register (selector, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                            nconn ++;
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   191
                            System.out.println("SERVER: new connection. chan[" + sock + "]");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                            if (nconn == maxconn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                /* deregister */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                listenerKey.cancel ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                listenerKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                            if (key.isReadable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                SocketChannel chan = (SocketChannel) key.channel();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   201
                                System.out.println("SERVER: connection readable. chan[" + chan + "]");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                if (key.attachment() != null) {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   203
                                    System.out.println("Server: comsume");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                    closed = consume (chan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                    closed = read (chan, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                    chan.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                    key.cancel ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                    if (nconn == maxconn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                        listenerKey = schan.register (selector, SelectionKey.OP_ACCEPT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                    nconn --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    clist.check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    if (shutdown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        clist.terminate ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                System.out.println ("Server exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                // TODO finish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        /* read all the data off the channel without looking at it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
             * return true if connection closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        boolean consume (SocketChannel chan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                consumeBuffer.clear ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                int c = chan.read (consumeBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                if (c == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        /* return true if the connection is closed, false otherwise */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        private boolean read (SocketChannel chan, SelectionKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            HttpTransaction msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            boolean res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                InputStream is = new BufferedInputStream (new NioInputStream (chan));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                String requestline = readLine (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                MessageHeader mhead = new MessageHeader (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                String clen = mhead.findValue ("Content-Length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                String trferenc = mhead.findValue ("Transfer-Encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                String data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if (trferenc != null && trferenc.equals ("chunked"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    data = new String (readChunkedData (is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                else if (clen != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    data = new String (readNormalData (is, Integer.parseInt (clen)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                String[] req = requestline.split (" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                if (req.length < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    /* invalid request line */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                String cmd = req[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                URI uri = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    uri = new URI (req[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    msg = new HttpTransaction (this, cmd, uri, mhead, data, null, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    cb.request (msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                } catch (URISyntaxException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    System.err.println ("Invalid URI: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    msg = new HttpTransaction (this, cmd, null, null, null, null, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    msg.sendResponse (501, "Whatever");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                res = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                res = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        byte[] readNormalData (InputStream is, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            byte [] buf  = new byte [len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            int c, off=0, remain=len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            while (remain > 0 && ((c=is.read (buf, off, remain))>0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                remain -= c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                off += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        private void readCRLF(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            int cr = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            int lf = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (((cr & 0xff) != 0x0d) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                ((lf & 0xff) != 0x0a)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    "Expected <CR><LF>:  got '" + cr + "/" + lf + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        byte[] readChunkedData (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            LinkedList l = new LinkedList ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            for (int len=readChunkLen(is); len!=0; len=readChunkLen(is)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                l.add (readNormalData(is, len));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                total += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                readCRLF(is);  // CRLF at end of chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            readCRLF(is); // CRLF at end of Chunked Stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            byte[] buf = new byte [total];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            Iterator i = l.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                byte[] b = (byte[])i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                System.arraycopy (b, 0, buf, x, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                x += b.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        private int readChunkLen (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            int c, len=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            boolean done=false, readCR=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                c = is.read ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (c == '\n' && readCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    if (c == '\r' && !readCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        readCR = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        int x=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        if (c >= 'a' && c <= 'f') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                            x = c - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        } else if (c >= 'A' && c <= 'F') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                            x = c - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        } else if (c >= '0' && c <= '9') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                            x = c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        len = len * 16 + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        private String readLine (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            boolean done=false, readCR=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            byte[] b = new byte [512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            int c, l = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                c = is.read ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                if (c == '\n' && readCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    if (c == '\r' && !readCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        readCR = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        b[l++] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return new String (b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        /** close the channel associated with the current key by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         * 1. shutdownOutput (send a FIN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * 2. mark the key so that incoming data is to be consumed and discarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         * 3. After a period, close the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        synchronized void orderlyCloseChannel (SelectionKey key) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            SocketChannel ch = (SocketChannel)key.channel ();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   381
            System.out.println("SERVER: orderlyCloseChannel chan[" + ch + "]");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            ch.socket().shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            key.attach (this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            clist.add (key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        synchronized void abortiveCloseChannel (SelectionKey key) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            SocketChannel ch = (SocketChannel)key.channel ();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   389
            System.out.println("SERVER: abortiveCloseChannel chan[" + ch + "]");
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   390
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            Socket s = ch.socket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            s.setSoLinger (true, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            ch.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Implements blocking reading semantics on top of a non-blocking channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    static class NioInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        SocketChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        Selector selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        ByteBuffer chanbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        SelectionKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        int available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        byte[] one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        ByteBuffer markBuf; /* reads may be satisifed from this buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        boolean marked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        boolean reset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        int readlimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        public NioInputStream (SocketChannel chan) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            this.channel = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            selector = Selector.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            chanbuf = ByteBuffer.allocate (1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            key = chan.register (selector, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            available = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            one = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            closed = marked = reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        public synchronized int read (byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return read (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        public synchronized int read () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return read (one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        public synchronized int read (byte[] b, int off, int srclen) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            int canreturn, willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if (reset) { /* satisfy from markBuf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                canreturn = markBuf.remaining ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                willreturn = canreturn>srclen ? srclen : canreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                markBuf.get(b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                if (canreturn == willreturn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            } else { /* satisfy from channel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                canreturn = available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                if (canreturn == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    block ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    canreturn = available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                willreturn = canreturn>srclen ? srclen : canreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                chanbuf.get(b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                available -= willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (marked) { /* copy into markBuf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        markBuf.put (b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    } catch (BufferOverflowException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                        marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        public synchronized int available () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                throw new IOException ("Stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            if (reset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                return markBuf.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (available > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            chanbuf.clear ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            available = channel.read (chanbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if (available > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                chanbuf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            else if (available == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                throw new IOException ("Stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * block() only called when available==0 and buf is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        private synchronized void block () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            //assert available == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            int n = selector.select ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            //assert n == 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            selector.selectedKeys().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            available ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            channel.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        public synchronized void mark (int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            this.readlimit = readlimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            markBuf = ByteBuffer.allocate (readlimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            marked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        public synchronized void reset () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            if (closed )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            if (!marked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                throw new IOException ("Stream not marked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            reset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            markBuf.flip ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    static class NioOutputStream extends OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        SocketChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        ByteBuffer buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        SelectionKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        Selector selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        byte[] one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        public NioOutputStream (SocketChannel channel) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            this.channel = channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            selector = Selector.open ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            key = channel.register (selector, SelectionKey.OP_WRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            one = new byte [1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        public synchronized void write (int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            one[0] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            write (one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        public synchronized void write (byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            write (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        public synchronized void write (byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                throw new IOException ("stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            buf = ByteBuffer.allocate (len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            buf.put (b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            buf.flip ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            while ((n = channel.write (buf)) < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                len -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                if (len == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                selector.select ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                selector.selectedKeys().clear ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            channel.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Utilities for synchronization. A condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * identified by a string name, and is initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * upon first use (ie. setCondition() or waitForCondition()). Threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * are blocked until some thread calls (or has called) setCondition() for the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * condition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * A rendezvous built on a condition is also provided for synchronizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * N threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    private static HashMap conditions = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * Modifiable boolean object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    private static class BValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        boolean v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Modifiable int object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    private static class IValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        int v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        IValue (int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            v =i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    private static BValue getCond (String condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        synchronized (conditions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            BValue cond = (BValue) conditions.get (condition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            if (cond == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                cond = new BValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                conditions.put (condition, cond);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            return cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * Set the condition to true. Any threads that are currently blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * waiting on the condition, will be unblocked and allowed to continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * Threads that subsequently call waitForCondition() will not block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * If the named condition did not exist prior to the call, then it is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    public static void setCondition (String condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        BValue cond = getCond (condition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        synchronized (cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (cond.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            cond.v = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            cond.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * If the named condition does not exist, then it is created and initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * to false. If the condition exists or has just been created and its value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * is false, then the thread blocks until another thread sets the condition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * If the condition exists and is already set to true, then this call returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * immediately without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    public static void waitForCondition (String condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        BValue cond = getCond (condition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        synchronized (cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            if (!cond.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    cond.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /* conditions must be locked when accessing this */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    static HashMap rv = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Force N threads to rendezvous (ie. wait for each other) before proceeding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * The first thread(s) to call are blocked until the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * thread makes the call. Then all threads continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * All threads that call with the same condition name, must use the same value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * for N (or the results may be not be as expected).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * Obviously, if fewer than N threads make the rendezvous then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * will be a hang.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    public static void rendezvous (String condition, int N) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        BValue cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        IValue iv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        String name = "RV_"+condition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        /* get the condition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        synchronized (conditions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            cond = (BValue)conditions.get (name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            if (cond == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                /* we are first caller */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                if (N < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    throw new RuntimeException ("rendezvous must be called with N >= 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                cond = new BValue ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                conditions.put (name, cond);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                iv = new IValue (N-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                rv.put (name, iv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                /* already initialised, just decrement the counter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                iv = (IValue) rv.get (name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                iv.v --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        if (iv.v > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            waitForCondition (name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            setCondition (name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            synchronized (conditions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                clearCondition (name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                rv.remove (name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * If the named condition exists and is set then remove it, so it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * be re-initialized and used again. If the condition does not exist, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * exists but is not set, then the call returns without doing anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * Note, some higher level synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * may be needed between clear and the other operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    public static void clearCondition(String condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        BValue cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        synchronized (conditions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            cond = (BValue) conditions.get (condition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (cond == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            synchronized (cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                if (cond.v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    conditions.remove (condition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
}