PollingServer.java revision 2362
0N/A * Copyright (c) 1999, 2001, Oracle and/or its affiliates. All rights reserved. 0N/A * Redistribution and use in source and binary forms, with or without 0N/A * modification, are permitted provided that the following conditions 0N/A * - Redistributions of source code must retain the above copyright 0N/A * notice, this list of conditions and the following disclaimer. 0N/A * - Redistributions in binary form must reproduce the above copyright 0N/A * notice, this list of conditions and the following disclaimer in the 0N/A * documentation and/or other materials provided with the distribution. 0N/A * - Neither the name of Oracle nor the names of its 0N/A * contributors may be used to endorse or promote products derived 0N/A * from this software without specific prior written permission. 0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0N/A * Simple Java "server" using the Poller class 0N/A * to multiplex on incoming connections. Note 0N/A * that handoff of events, via linked Q is not 0N/A * actually be a performance booster here, since 0N/A * the processing of events is cheaper than 0N/A * Although this demo does allow for concurrency 0N/A * in handling connections, it uses a rather 0N/A * primitive "gang scheduling" policy to keep 0N/A * This synchronization object protects access to certain 0N/A * data (bytesRead,eventsToProcess) by concurrent Consumer threads. 0N/A * Create the Poller object Mux, allow for up to MAXCONN * Start the consumer threads to read data. * Take connections, read Data * Process all the events we got from Mux.waitMultiple * New connection coming in on the ServerSocket * Add the socket to the Mux, keep track of mapping * the fdval returned by Mux.add to the connection. * We've got data from this client connection. * Put it on the queue for the consumer threads to process. // Tell the client it can now go away // Tell the cunsumer threads they can exit. * main ... just check if a concurrency was specified * This class is for handling the Client data. * The PollingServer spawns off a number of these based upon * the number of CPUs (or concurrency argument). * Each just loops grabbing events off the queue and if (
fd == -
1)
break;
/* got told we could exit */ * We have to map the fd value returned from waitMultiple * to the actual input stream associated with that fd. * Take a look at how the Mux.add() was done to see how * Any real server would do some synchronized and some * unsynchronized work on behalf of the client, and * most likely send some data back...but this is a * gross oversimplification.