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