* tests/regression/jasmin/Makefile.am (build): Added cup.jar to
[cacao.git] / tests / network / udp / QuoteServerThread.java
1 /*
2  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Permission to use, copy, modify, and distribute this software
5  * and its documentation for NON-COMMERCIAL purposes and without
6  * fee is hereby granted provided that this copyright notice
7  * appears in all copies. Please refer to the file "copyright.html"
8  * for further important copyright and licensing information.
9  *
10  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
11  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
12  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
13  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
14  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
15  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
16  */
17 import java.io.*;
18 import java.net.*;
19 import java.util.*;
20
21 public class QuoteServerThread extends Thread {
22
23     protected DatagramSocket socket = null;
24     protected BufferedReader in = null;
25     protected boolean moreQuotes = true;
26
27     public QuoteServerThread() throws IOException {
28         this("QuoteServerThread");
29     }
30
31     public QuoteServerThread(String name) throws IOException {
32         super(name);
33
34         try {
35             socket = new DatagramSocket(4445);
36         } catch (Exception exc) {
37             System.err.println("Could not create socket: " + exc);
38         }
39
40         try {
41             in = new BufferedReader(new FileReader("one-liners.txt"));
42         } catch (FileNotFoundException e) {
43             System.err.println("Could not open quote file. Serving time instead.");
44         }
45     }
46
47     public void run() {
48
49         while (moreQuotes) {
50             try {
51                 byte[] buf = new byte[256];
52
53                     // receive request
54                 DatagramPacket packet = new DatagramPacket(buf, buf.length);
55                 socket.receive(packet);
56
57                     // figure out response
58                 String dString = null;
59                 if (in == null)
60                     dString = new Date().toString();
61                 else
62                     dString = getNextQuote();
63                 buf = dString.getBytes();
64
65                     // send the response to the client at "address" and "port"
66                 InetAddress address = packet.getAddress();
67                 int port = packet.getPort();
68                 packet = new DatagramPacket(buf, buf.length, address, port);
69                 socket.send(packet);
70             } catch (IOException e) {
71                 e.printStackTrace();
72                 moreQuotes = false;
73             }
74         }
75         socket.close();
76     }
77
78     protected String getNextQuote() {
79         String returnValue = null;
80         try {
81             if ((returnValue = in.readLine()) == null) {
82                 in.close();
83                 moreQuotes = false;
84                 returnValue = "No more quotes. Goodbye.";
85             }
86         } catch (IOException e) {
87             returnValue = "IOException occurred in server.";
88         }
89         return returnValue;
90     }
91 }