ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/experimental/server/XMLReader/UDPReader.java
Revision: 1.6
Committed: Tue Nov 21 09:15:06 2000 UTC (23 years, 11 months ago) by pjm2
Branch: MAIN
Changes since 1.5: +25 -18 lines
Log Message:
Modified the UDPReader class to accept a Logger and port number as a
constructor argument (as opposed to command line parameters).

The main method has been replaced with a run() method so that the gubbins
of this program can be run as a thread from a Filter class.

File Contents

# User Rev Content
1 pjm2 1.1 import java.io.*;
2     import java.net.*;
3     import java.util.*;
4    
5     // This class contains the main method to be run by
6 pjm2 1.6 // the filter children. It harvests UDP traffic.
7 pjm2 1.1 //
8     //
9 pjm2 1.6 public class UDPReader extends Thread{
10 pjm2 1.1
11 pjm2 1.6 // It is normal to use this constructor in preference
12     // to any other in this class.
13     public UDPReader(int port, Logger logger){
14     this.logger = logger;
15     this.port = port;
16     }
17    
18     public UDPReader(Logger logger){
19     this(4589, logger);
20     }
21 pjm2 1.1
22 pjm2 1.6 public void run() {
23 pjm2 1.2
24 pjm2 1.3 DatagramSocket socket = null;
25     try {
26     socket = new DatagramSocket(port);
27     }
28     catch (BindException e){
29     System.out.println("Some other process is already listening on port "+port+".");
30     System.out.println("Please specify another port number on the command line.");
31 pjm2 1.6 return;
32     }
33     catch (Exception e){
34     System.out.println("An exception occured while creating the DatagramSocket.");
35     return;
36 pjm2 1.3 }
37    
38 pjm2 1.2 System.out.println("UDPReader ready and listening for UDP packets on port "+port);
39    
40 pjm2 1.4 byte[] buf;
41     UDPReaderThread t = new UDPReaderThread();
42    
43 pjm2 1.2 boolean running = true;
44     while (running){
45     try {
46 pjm2 1.4 buf = new byte[packetSizeLimit];
47 pjm2 1.1 // receive request
48 pjm2 1.2 DatagramPacket packet = new DatagramPacket(buf, buf.length);
49     socket.receive(packet);
50 pjm2 1.4 t.run(packet);
51 pjm2 1.2 }
52     catch (IOException e) {
53     System.out.println("An exception occured in the UDPReader!");
54     e.printStackTrace();
55     }
56 pjm2 1.1 }
57     socket.close();
58     }
59 pjm2 1.6
60    
61     Logger logger;
62     int port;
63    
64     final int packetSizeLimit = 8192;
65    
66 pjm2 1.1 }