3 |
|
import java.util.*; |
4 |
|
|
5 |
|
// This class contains the main method to be run by |
6 |
< |
// the filter children. It harvests UDP traffic |
6 |
> |
// the filter children. It harvests UDP traffic. |
7 |
|
// |
8 |
|
// |
9 |
< |
public class UDPReader { |
9 |
> |
public class UDPReader extends Thread{ |
10 |
|
|
11 |
< |
public static void main(String[] args) throws IOException { |
11 |
> |
// 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 |
|
|
22 |
< |
// default UDP listening port. |
23 |
< |
int port = 4589; |
24 |
< |
final int packetSizeLimit = 8192; |
22 |
> |
public void receiveXML(String xml){ |
23 |
> |
UDPReaderThread t = new UDPReaderThread(); |
24 |
> |
t.run(xml); |
25 |
> |
} |
26 |
|
|
27 |
< |
// Allow the user to choose their own port number. |
18 |
< |
if (args.length == 1){ |
19 |
< |
try { |
20 |
< |
port = new Integer(args[0]).intValue(); |
21 |
< |
} |
22 |
< |
catch (Exception e){ |
23 |
< |
// If something went wrong, use the default again. |
24 |
< |
port = 4589; |
25 |
< |
} |
26 |
< |
} |
27 |
> |
public void run() { |
28 |
|
|
29 |
|
DatagramSocket socket = null; |
30 |
|
try { |
31 |
|
socket = new DatagramSocket(port); |
32 |
|
} |
33 |
|
catch (BindException e){ |
34 |
< |
System.out.println("Some other process is already listening on port "+port+"."); |
35 |
< |
System.out.println("Please specify another port number on the command line."); |
35 |
< |
System.exit(0); |
34 |
> |
logger.write(this.toString(), Logger.SYSMSG, "Could not start the UDPReader thread on port "+port+" as this port was already in use."); |
35 |
> |
return; |
36 |
|
} |
37 |
+ |
catch (Exception e){ |
38 |
+ |
logger.write(this.toString(), Logger.SYSMSG, "Could not start the UDPReader thread on port "+port+"."); |
39 |
+ |
return; |
40 |
+ |
} |
41 |
|
|
42 |
< |
System.out.println("UDPReader ready and listening for UDP packets on port "+port); |
42 |
> |
logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+port); |
43 |
|
|
44 |
+ |
byte[] buf; |
45 |
+ |
|
46 |
|
boolean running = true; |
47 |
|
while (running){ |
48 |
|
try { |
49 |
< |
byte[] buf = new byte[packetSizeLimit]; |
49 |
> |
buf = new byte[packetSizeLimit]; |
50 |
|
// receive request |
51 |
|
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
52 |
|
socket.receive(packet); |
53 |
< |
UDPReaderThread thread = new UDPReaderThread(packet); |
54 |
< |
thread.run(); |
53 |
> |
UDPReaderThread t = new UDPReaderThread(); |
54 |
> |
t.run(packet); |
55 |
|
} |
56 |
|
catch (IOException e) { |
57 |
< |
System.out.println("An exception occured in the UDPReader!"); |
58 |
< |
e.printStackTrace(); |
57 |
> |
logger.write(this.toString(), Logger.SYSMSG, "The UDPReader thread has been shut down as an exception occured: "+e); |
58 |
> |
return; |
59 |
|
} |
60 |
|
} |
61 |
|
socket.close(); |
62 |
|
} |
63 |
+ |
|
64 |
+ |
|
65 |
+ |
Logger logger; |
66 |
+ |
int port; |
67 |
+ |
|
68 |
+ |
final int packetSizeLimit = 8192; |
69 |
+ |
|
70 |
|
} |