2362N/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. 2362N/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. 4378N/A * This source code is provided to illustrate the usage of a given feature 4378N/A * or technique and has been deliberately simplified. Additional steps 4378N/A * required for a production-quality application, such as security checks, 4378N/A * input validation and proper error handling, might not be present in 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 0N/A * Start the consumer threads to read data. 0N/A * Take connections, read Data 0N/A * Process all the events we got from Mux.waitMultiple 0N/A * New connection coming in on the ServerSocket 0N/A * Add the socket to the Mux, keep track of mapping 0N/A * the fdval returned by Mux.add to the connection. 0N/A * We've got data from this client connection. 0N/A * Put it on the queue for the consumer threads to process. 0N/A // Tell the client it can now go away 0N/A // Tell the cunsumer threads they can exit. 0N/A * main ... just check if a concurrency was specified 0N/A * This class is for handling the Client data. 0N/A * The PollingServer spawns off a number of these based upon 0N/A * the number of CPUs (or concurrency argument). 0N/A * Each just loops grabbing events off the queue and 0N/A if (
fd == -
1)
break;
/* got told we could exit */ 0N/A * We have to map the fd value returned from waitMultiple 0N/A * to the actual input stream associated with that fd. 0N/A * Take a look at how the Mux.add() was done to see how 0N/A * Any real server would do some synchronized and some 0N/A * unsynchronized work on behalf of the client, and 0N/A * most likely send some data back...but this is a 0N/A * gross oversimplification.