blob: 19b9ed21d7f062a2b698d33d4c0c59da61812c86 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Sun Microsystems nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32import java.io.*;
33import java.net.*;
34import java.lang.Byte;
35
36/**
37 * Simple Java "server" using a single thread to handle each connection.
38 */
39
40public class SimpleServer
41{
42 private final static int BYTESPEROP= PollingServer.BYTESPEROP;
43 private final static int PORTNUM = PollingServer.PORTNUM;
44 private final static int MAXCONN = PollingServer.MAXCONN;
45
46 /*
47 * This synchronization object protects access to certain
48 * data (bytesRead,eventsToProcess) by concurrent Consumer threads.
49 */
50 private final static Object eventSync = new Object();
51
52 private static InputStream[] instr = new InputStream[MAXCONN];
53 private static int bytesRead;
54 private static int bytesToRead;
55
56 public SimpleServer() {
57 Socket[] sockArr = new Socket[MAXCONN];
58 long timestart, timestop;
59 int bytes;
60 int totalConn=0;
61
62
63 System.out.println ("Serv: Initializing port " + PORTNUM);
64 try {
65
66 ServerSocket skMain = new ServerSocket (PORTNUM);
67
68 bytesRead = 0;
69 Socket ctrlSock = skMain.accept();
70
71 BufferedReader ctrlReader =
72 new BufferedReader(new InputStreamReader(ctrlSock.getInputStream()));
73 String ctrlString = ctrlReader.readLine();
74 bytesToRead = Integer.valueOf(ctrlString).intValue();
75 ctrlString = ctrlReader.readLine();
76 totalConn = Integer.valueOf(ctrlString).intValue();
77
78 System.out.println("Receiving " + bytesToRead + " bytes from " +
79 totalConn + " client connections");
80
81 timestart = System.currentTimeMillis();
82
83 /*
84 * Take connections, spawn off connection handling threads
85 */
86 ConnHandler[] connHA = new ConnHandler[MAXCONN];
87 int conn = 0;
88 while ( conn < totalConn ) {
89 Socket sock = skMain.accept();
90 connHA[conn] = new ConnHandler(sock.getInputStream());
91 connHA[conn].start();
92 conn++;
93 }
94
95 while ( bytesRead < bytesToRead ) {
96 java.lang.Thread.sleep(500);
97 }
98 timestop = System.currentTimeMillis();
99 System.out.println("Time for all reads (" + totalConn +
100 " sockets) : " + (timestop-timestart));
101 // Tell the client it can now go away
102 byte[] buff = new byte[BYTESPEROP];
103 ctrlSock.getOutputStream().write(buff,0,BYTESPEROP);
104 } catch (Exception exc) { exc.printStackTrace(); }
105 }
106
107 /*
108 * main ... just create invoke the SimpleServer constructor.
109 */
110 public static void main (String args[])
111 {
112 SimpleServer server = new SimpleServer();
113 }
114
115 /*
116 * Connection Handler inner class...one of these per client connection.
117 */
118 class ConnHandler extends Thread {
119 private InputStream instr;
120 public ConnHandler(InputStream inputStr) { instr = inputStr; }
121
122 public void run() {
123 try {
124 int bytes;
125 byte[] buff = new byte[BYTESPEROP];
126
127 while ( bytesRead < bytesToRead ) {
128 bytes = instr.read (buff, 0, BYTESPEROP);
129 if (bytes > 0 ) {
130 synchronized(eventSync) {
131 bytesRead += bytes;
132 }
133 /*
134 * Any real server would do some synchronized and some
135 * unsynchronized work on behalf of the client, and
136 * most likely send some data back...but this is a
137 * gross oversimplification.
138 */
139 }
140 else {
141 if (bytesRead < bytesToRead)
142 System.out.println("instr.read returned : " + bytes);
143 }
144 }
145 }
146 catch (Exception e) {e.printStackTrace();}
147 }
148 }
149}