Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/demo/jni/Poller/SimpleServer.java
32287 views
/*1* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940import java.io.*;41import java.net.*;42import java.lang.Byte;4344/**45* Simple Java "server" using a single thread to handle each connection.46*/4748public class SimpleServer49{50private final static int BYTESPEROP= PollingServer.BYTESPEROP;51private final static int PORTNUM = PollingServer.PORTNUM;52private final static int MAXCONN = PollingServer.MAXCONN;5354/*55* This synchronization object protects access to certain56* data (bytesRead,eventsToProcess) by concurrent Consumer threads.57*/58private final static Object eventSync = new Object();5960private static InputStream[] instr = new InputStream[MAXCONN];61private static int bytesRead;62private static int bytesToRead;6364public SimpleServer() {65Socket[] sockArr = new Socket[MAXCONN];66long timestart, timestop;67int bytes;68int totalConn=0;697071System.out.println ("Serv: Initializing port " + PORTNUM);72try {7374ServerSocket skMain = new ServerSocket (PORTNUM);7576bytesRead = 0;77Socket ctrlSock = skMain.accept();7879BufferedReader ctrlReader =80new BufferedReader(new InputStreamReader(ctrlSock.getInputStream()));81String ctrlString = ctrlReader.readLine();82bytesToRead = Integer.valueOf(ctrlString).intValue();83ctrlString = ctrlReader.readLine();84totalConn = Integer.valueOf(ctrlString).intValue();8586System.out.println("Receiving " + bytesToRead + " bytes from " +87totalConn + " client connections");8889timestart = System.currentTimeMillis();9091/*92* Take connections, spawn off connection handling threads93*/94ConnHandler[] connHA = new ConnHandler[MAXCONN];95int conn = 0;96while ( conn < totalConn ) {97Socket sock = skMain.accept();98connHA[conn] = new ConnHandler(sock.getInputStream());99connHA[conn].start();100conn++;101}102103while ( bytesRead < bytesToRead ) {104java.lang.Thread.sleep(500);105}106timestop = System.currentTimeMillis();107System.out.println("Time for all reads (" + totalConn +108" sockets) : " + (timestop-timestart));109// Tell the client it can now go away110byte[] buff = new byte[BYTESPEROP];111ctrlSock.getOutputStream().write(buff,0,BYTESPEROP);112} catch (Exception exc) { exc.printStackTrace(); }113}114115/*116* main ... just create invoke the SimpleServer constructor.117*/118public static void main (String args[])119{120SimpleServer server = new SimpleServer();121}122123/*124* Connection Handler inner class...one of these per client connection.125*/126class ConnHandler extends Thread {127private InputStream instr;128public ConnHandler(InputStream inputStr) { instr = inputStr; }129130public void run() {131try {132int bytes;133byte[] buff = new byte[BYTESPEROP];134135while ( bytesRead < bytesToRead ) {136bytes = instr.read (buff, 0, BYTESPEROP);137if (bytes > 0 ) {138synchronized(eventSync) {139bytesRead += bytes;140}141/*142* Any real server would do some synchronized and some143* unsynchronized work on behalf of the client, and144* most likely send some data back...but this is a145* gross oversimplification.146*/147}148else {149if (bytesRead < bytesToRead)150System.out.println("instr.read returned : " + bytes);151}152}153}154catch (Exception e) {e.printStackTrace();}155}156}157}158159160