jdk/src/demo/solaris/jni/Poller/PollingServer.java
author hannesw
Mon, 23 Nov 2015 17:52:04 +0100
changeset 34446 a0dd624a12a7
parent 25859 3317bb8137f4
permissions -rw-r--r--
8143821: Wrong test name in JDK-8143304 Reviewed-by: attila, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.lang.Byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Simple Java "server" using the Poller class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * to multiplex on incoming connections.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * that handoff of events, via linked Q is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * actually be a performance booster here, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * the processing of events is cheaper than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the overhead in scheduling/executing them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Although this demo does allow for concurrency
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * in handling connections, it uses a rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * primitive "gang scheduling" policy to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the code simpler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class PollingServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  public final static int MAXCONN    = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  public final static int PORTNUM    = 4444;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  public final static int BYTESPEROP = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
   * This synchronization object protects access to certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
   * data (bytesRead,eventsToProcess) by concurrent Consumer threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  private final static Object eventSync = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  private static InputStream[] instr = new InputStream[MAXCONN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  private static int[] mapping = new int[65535];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  private static LinkedQueue linkedQ = new LinkedQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
  private static int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
  private static int bytesToRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
  private static int eventsToProcess=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
  public PollingServer(int concurrency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    Socket[] sockArr = new Socket[MAXCONN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    long timestart, timestop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    short[] revents = new short[MAXCONN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int[] fds = new int[MAXCONN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    int bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    Poller Mux;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    int serverFd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    int totalConn=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    int connects=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    System.out.println ("Serv: Initializing port " + PORTNUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      ServerSocket skMain = new ServerSocket (PORTNUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
       * Create the Poller object Mux, allow for up to MAXCONN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
       * sockets/filedescriptors to be polled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      Mux = new Poller(MAXCONN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      serverFd = Mux.add(skMain, Poller.POLLIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
      Socket ctrlSock = skMain.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
      BufferedReader ctrlReader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        new BufferedReader(new InputStreamReader(ctrlSock.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
      String ctrlString = ctrlReader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
      bytesToRead = Integer.valueOf(ctrlString).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
      ctrlString = ctrlReader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
      totalConn = Integer.valueOf(ctrlString).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      System.out.println("Receiving " + bytesToRead + " bytes from " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                         totalConn + " client connections");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
      timestart = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
       * Start the consumer threads to read data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
      for (int consumerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
           consumerThread < concurrency; consumerThread++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        new Consumer(consumerThread).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
       * Take connections, read Data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
      int numEvents=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
      while ( bytesRead < bytesToRead ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int loopWaits=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        while (eventsToProcess > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
          synchronized (eventSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            loopWaits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (eventsToProcess <= 0) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            try { eventSync.wait(); } catch (Exception e) {e.printStackTrace();};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (loopWaits > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
          System.out.println("Done waiting...loops = " + loopWaits +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                             " events " + numEvents +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                             " bytes read : " + bytesRead );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (bytesRead >= bytesToRead) break; // may be done!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         * Wait for events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        numEvents = Mux.waitMultiple(100, fds, revents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        synchronized (eventSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
          eventsToProcess = numEvents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * Process all the events we got from Mux.waitMultiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        while ( (cnt < numEvents) && (bytesRead < bytesToRead) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
          int fd = fds[cnt];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
          if (revents[cnt] == Poller.POLLIN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (fd == serverFd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
              /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
               * New connection coming in on the ServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
               * Add the socket to the Mux, keep track of mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
               * the fdval returned by Mux.add to the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
               */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
              sockArr[connects] = skMain.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
              instr[connects] = sockArr[connects].getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
              int fdval = Mux.add(sockArr[connects], Poller.POLLIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
              mapping[fdval] = connects;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
              synchronized(eventSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                eventsToProcess--; // just processed this one!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
              connects++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
              /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
               * We've got data from this client connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
               * Put it on the queue for the consumer threads to process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
               */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
              linkedQ.put(new Integer(fd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            System.out.println("Got revents[" + cnt + "] == " + revents[cnt]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
          cnt++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
      timestop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
      System.out.println("Time for all reads (" + totalConn +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                         " sockets) : " + (timestop-timestart));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
      // Tell the client it can now go away
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
      byte[] buff = new byte[BYTESPEROP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
      ctrlSock.getOutputStream().write(buff,0,BYTESPEROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
      // Tell the cunsumer threads they can exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
      for (int cThread = 0; cThread < concurrency; cThread++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        linkedQ.put(new Integer(-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    } catch (Exception exc) { exc.printStackTrace(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
   * main ... just check if a concurrency was specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
  public static void main (String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    int concurrency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    if (args.length == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
      concurrency = java.lang.Integer.valueOf(args[0]).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
      concurrency = Poller.getNumCPUs() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    PollingServer server = new PollingServer(concurrency);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
   * This class is for handling the Client data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
   * The PollingServer spawns off a number of these based upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
   * the number of CPUs (or concurrency argument).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
   * Each just loops grabbing events off the queue and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
   * processing them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
  class Consumer extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private int threadNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public Consumer(int i) { threadNumber = i; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
      byte[] buff = new byte[BYTESPEROP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
      int bytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
      InputStream instream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
      while (bytesRead < bytesToRead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
          Integer Fd = (Integer) linkedQ.take();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
          int fd = Fd.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
          if (fd == -1) break; /* got told we could exit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
          /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
           * We have to map the fd value returned from waitMultiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
           * to the actual input stream associated with that fd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
           * Take a look at how the Mux.add() was done to see how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
           * we stored that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
          int map = mapping[fd];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
          instream = instr[map];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
          bytes = instream.read(buff,0,BYTESPEROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } catch (Exception e) { System.out.println(e.toString()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (bytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
          /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
           * Any real server would do some synchronized and some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
           * unsynchronized work on behalf of the client, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
           * most likely send some data back...but this is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
           * gross oversimplification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
          synchronized(eventSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            bytesRead += bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            eventsToProcess--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (eventsToProcess <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
              eventSync.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
}