Clean merge -> gc7-branch
[cacao.git] / tests / network / GenericServer.java
1 //      File: GenericServer.java
2 //      Author: Thomas Lea  (leat@goodnet.com,  http://www.goodnet.com/~leat)
3 //      Date: 12/7/95
4 //      Version: 1.0  (for Beta JDK API)
5 //
6 // Copyright (c) 1995 Thomas Lea. All Rights Reserved.
7 //
8
9
10 // Please feel free to take this program and modify it to your hearts content.
11 // The only thing I ask is that my name and the above version/copyright 
12 // information stay at the top of this module.
13
14 import java.net.*;
15 import java.io.*;
16 import java.util.Vector;
17
18 class GenericServer
19 {
20     private static final int DEFAULT_PORT=8887;
21     private ConnectionManager cm = null;
22
23     public GenericServer(int port)
24     {
25         System.out.println("Server is initializing to port " + port);
26
27         cm = new ConnectionManager(port);
28         cm.start();
29     }
30
31     public static void main(String args[])
32     {
33         int server_port;
34
35         try
36         {
37             server_port = Integer.parseInt(args[0],10);
38         }
39         catch(Exception e)
40         {
41             System.out.println("Defaulting to port " + DEFAULT_PORT);
42                     server_port = DEFAULT_PORT;
43         }
44         new GenericServer(server_port);
45     }
46 }
47
48 // Waits for a connection then spawns a ServerConnection to deal with it
49 class ConnectionManager extends Thread
50 {
51     private static int _port;
52     private static Vector _my_threads = new Vector(5,2);
53                                            //size of 5 initially, grow by 2
54     private ServerSocket _main_socket = null;
55
56     public ConnectionManager(int port)
57     {
58         _port = port;
59     }
60
61     public void run()
62     {
63         serveRequests();
64     }
65
66     private void serveRequests()
67     {
68         try {_main_socket = new ServerSocket(_port);}
69         catch(Exception e) { System.err.println(e); System.exit(1);}
70
71         ServerConnection temp_sc = null;
72
73         while (true)
74         {
75             try
76             {
77                 Socket this_connection = _main_socket.accept();
78         
79                 temp_sc = new ServerConnection(this_connection);
80                 temp_sc.start();
81                 _my_threads.addElement(temp_sc);
82
83                 //clean up the vector if needed
84                 for(int i=0;i<ConnectionManager._my_threads.size();i++)
85                 if(!((ServerConnection)(_my_threads.elementAt(i))).isAlive())
86                         _my_threads.removeElementAt(i);
87             }
88             catch(Exception e)
89             {
90                 System.err.println("Exception:\n" + e);
91             }
92         }
93     }
94 }
95
96 class ServerConnection extends Thread
97 {
98     private Socket _mysocket;
99     private PrintStream _output;
100     private InputStream _input;
101     FileInputStream _dis;
102
103     public ServerConnection(Socket s)
104     {
105         _mysocket = s;
106     }
107
108     private void doServerWork()
109     {
110         //This is where the server actually does its work.
111         //when this method finishes, the socket will be closed
112         //and this thread will exit.
113
114         //This is just some junk... put your real work here.
115         try
116         {
117             byte b[] = new byte[1024];
118             int nbytes = _input.read(b,0,1024);
119             String str = new String(b,0,0,nbytes);
120
121             _dis = new FileInputStream("FILE.HTM");
122             StringBuffer buf = new StringBuffer(4096);
123                         
124             System.out.println("Received for server: " + str);
125
126             nbytes = _dis.available();
127             _output.println("HTTP/1.0 200 OK");
128             _output.println("MIME-Version: 1.0");
129             _output.println("Date: 22Dec95");
130             _output.println("Server: Java_Server_0.01");
131             _output.println("Content-type: text/html");
132 //          _output.println("Content-Length: 76");
133             _output.println("");
134 //      _output.println("<HEAD><TITLE>Clock 2.0 Title</TITLE></HEAD><BODY>");
135 //          _output.println("<H1>Clock 2.0</H1>");
136             b = new byte[nbytes];
137             _dis.read(b, 0, nbytes);
138             str = new String(b, 0, 0, nbytes);
139             _output.println(str);
140 //          _output.println("</BODY>");
141             _output.println("");
142 //          for(int i=0;i<10;i++)
143 //          {
144 //              _output.println("This is a message from the server");
145 //              sleep((int)(Math.random() * 4000));
146 //          }
147         }
148         catch(Exception e)
149         {
150         }
151     }
152
153     public void run()
154     {
155         System.out.println("Connected to: " +
156                    _mysocket.getInetAddress() +":"+ _mysocket.getPort());
157         try
158         {
159             _output = new PrintStream(_mysocket.getOutputStream());
160             _input = _mysocket.getInputStream();
161
162             //Lets get busy!
163             doServerWork();
164
165             //We are outta here....
166             _mysocket.close();
167         }
168         catch ( Exception e )
169         {
170             System.err.println( "Exception:\n" + e );
171         }
172
173         System.out.println("Disconnecting: " + _mysocket.toString());
174         stop();
175     }
176 }