jdk/src/solaris/demo/jni/Poller/PollingServer.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2001 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *   - Neither the name of Sun Microsystems nor the names of its
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.Byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Simple Java "server" using the Poller class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * to multiplex on incoming connections.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * that handoff of events, via linked Q is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * actually be a performance booster here, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the processing of events is cheaper than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * the overhead in scheduling/executing them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Although this demo does allow for concurrency
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * in handling connections, it uses a rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * primitive "gang scheduling" policy to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the code simpler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class PollingServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  public final static int MAXCONN    = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  public final static int PORTNUM    = 4444;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  public final static int BYTESPEROP = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
   * This synchronization object protects access to certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
   * data (bytesRead,eventsToProcess) by concurrent Consumer threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
  private final static Object eventSync = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  private static InputStream[] instr = new InputStream[MAXCONN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  private static int[] mapping = new int[65535];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  private static LinkedQueue linkedQ = new LinkedQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
  private static int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  private static int bytesToRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
  private static int eventsToProcess=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  public PollingServer(int concurrency) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    Socket[] sockArr = new Socket[MAXCONN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    long timestart, timestop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    short[] revents = new short[MAXCONN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    int[] fds = new int[MAXCONN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    int bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    Poller Mux;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    int serverFd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    int totalConn=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    int connects=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    System.out.println ("Serv: Initializing port " + PORTNUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
      ServerSocket skMain = new ServerSocket (PORTNUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
       * Create the Poller object Mux, allow for up to MAXCONN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
       * sockets/filedescriptors to be polled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
      Mux = new Poller(MAXCONN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
      serverFd = Mux.add(skMain, Poller.POLLIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
      Socket ctrlSock = skMain.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      BufferedReader ctrlReader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        new BufferedReader(new InputStreamReader(ctrlSock.getInputStream()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
      String ctrlString = ctrlReader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
      bytesToRead = Integer.valueOf(ctrlString).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      ctrlString = ctrlReader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      totalConn = Integer.valueOf(ctrlString).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
      System.out.println("Receiving " + bytesToRead + " bytes from " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                         totalConn + " client connections");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
      timestart = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
       * Start the consumer threads to read data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
      for (int consumerThread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
           consumerThread < concurrency; consumerThread++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        new Consumer(consumerThread).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
      /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
       * Take connections, read Data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
      int numEvents=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
      while ( bytesRead < bytesToRead ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int loopWaits=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        while (eventsToProcess > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
          synchronized (eventSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            loopWaits++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (eventsToProcess <= 0) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            try { eventSync.wait(); } catch (Exception e) {e.printStackTrace();};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (loopWaits > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
          System.out.println("Done waiting...loops = " + loopWaits +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                             " events " + numEvents +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                             " bytes read : " + bytesRead );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (bytesRead >= bytesToRead) break; // may be done!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * Wait for events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        numEvents = Mux.waitMultiple(100, fds, revents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        synchronized (eventSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
          eventsToProcess = numEvents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * Process all the events we got from Mux.waitMultiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        while ( (cnt < numEvents) && (bytesRead < bytesToRead) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
          int fd = fds[cnt];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
          if (revents[cnt] == Poller.POLLIN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (fd == serverFd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
              /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
               * New connection coming in on the ServerSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
               * Add the socket to the Mux, keep track of mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
               * the fdval returned by Mux.add to the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
               */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
              sockArr[connects] = skMain.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
              instr[connects] = sockArr[connects].getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
              int fdval = Mux.add(sockArr[connects], Poller.POLLIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
              mapping[fdval] = connects;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
              synchronized(eventSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                eventsToProcess--; // just processed this one!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
              connects++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
              /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
               * We've got data from this client connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
               * Put it on the queue for the consumer threads to process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
               */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
              linkedQ.put(new Integer(fd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
          } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            System.out.println("Got revents[" + cnt + "] == " + revents[cnt]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
          cnt++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
      timestop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
      System.out.println("Time for all reads (" + totalConn +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                         " sockets) : " + (timestop-timestart));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
      // Tell the client it can now go away
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
      byte[] buff = new byte[BYTESPEROP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
      ctrlSock.getOutputStream().write(buff,0,BYTESPEROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
      // Tell the cunsumer threads they can exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
      for (int cThread = 0; cThread < concurrency; cThread++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        linkedQ.put(new Integer(-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    } catch (Exception exc) { exc.printStackTrace(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
   * main ... just check if a concurrency was specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
  public static void main (String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    int concurrency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    if (args.length == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
      concurrency = java.lang.Integer.valueOf(args[0]).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
      concurrency = Poller.getNumCPUs() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    PollingServer server = new PollingServer(concurrency);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
  /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
   * This class is for handling the Client data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
   * The PollingServer spawns off a number of these based upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
   * the number of CPUs (or concurrency argument).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
   * Each just loops grabbing events off the queue and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
   * processing them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
  class Consumer extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private int threadNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public Consumer(int i) { threadNumber = i; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
      byte[] buff = new byte[BYTESPEROP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
      int bytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
      InputStream instream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
      while (bytesRead < bytesToRead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
          Integer Fd = (Integer) linkedQ.take();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
          int fd = Fd.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
          if (fd == -1) break; /* got told we could exit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
          /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
           * We have to map the fd value returned from waitMultiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
           * to the actual input stream associated with that fd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
           * Take a look at how the Mux.add() was done to see how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
           * we stored that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
          int map = mapping[fd];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
          instream = instr[map];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
          bytes = instream.read(buff,0,BYTESPEROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (Exception e) { System.out.println(e.toString()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (bytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
          /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
           * Any real server would do some synchronized and some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
           * unsynchronized work on behalf of the client, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
           * most likely send some data back...but this is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
           * gross oversimplification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
          synchronized(eventSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            bytesRead += bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            eventsToProcess--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (eventsToProcess <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
              eventSync.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
}