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.9 |
public void receiveXML(String xml){ |
23 |
|
|
UDPReaderThread t = new UDPReaderThread(); |
24 |
|
|
t.run(xml); |
25 |
|
|
} |
26 |
|
|
|
27 |
pjm2 |
1.6 |
public void run() { |
28 |
pjm2 |
1.2 |
|
29 |
pjm2 |
1.3 |
DatagramSocket socket = null; |
30 |
|
|
try { |
31 |
|
|
socket = new DatagramSocket(port); |
32 |
|
|
} |
33 |
|
|
catch (BindException e){ |
34 |
pjm2 |
1.8 |
logger.write(this.toString(), Logger.SYSMSG, "Could not start the UDPReader thread on port "+port+" as this port was already in use."); |
35 |
pjm2 |
1.6 |
return; |
36 |
|
|
} |
37 |
|
|
catch (Exception e){ |
38 |
pjm2 |
1.8 |
logger.write(this.toString(), Logger.SYSMSG, "Could not start the UDPReader thread on port "+port+"."); |
39 |
pjm2 |
1.6 |
return; |
40 |
pjm2 |
1.3 |
} |
41 |
|
|
|
42 |
pjm2 |
1.8 |
logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+port); |
43 |
pjm2 |
1.2 |
|
44 |
pjm2 |
1.4 |
byte[] buf; |
45 |
|
|
|
46 |
pjm2 |
1.2 |
boolean running = true; |
47 |
|
|
while (running){ |
48 |
|
|
try { |
49 |
pjm2 |
1.4 |
buf = new byte[packetSizeLimit]; |
50 |
pjm2 |
1.1 |
// receive request |
51 |
pjm2 |
1.2 |
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
52 |
|
|
socket.receive(packet); |
53 |
pjm2 |
1.7 |
UDPReaderThread t = new UDPReaderThread(); |
54 |
pjm2 |
1.4 |
t.run(packet); |
55 |
pjm2 |
1.2 |
} |
56 |
|
|
catch (IOException e) { |
57 |
pjm2 |
1.8 |
logger.write(this.toString(), Logger.SYSMSG, "The UDPReader thread has been shut down as an exception occured: "+e); |
58 |
|
|
return; |
59 |
pjm2 |
1.2 |
} |
60 |
pjm2 |
1.1 |
} |
61 |
|
|
socket.close(); |
62 |
|
|
} |
63 |
pjm2 |
1.6 |
|
64 |
|
|
|
65 |
|
|
Logger logger; |
66 |
|
|
int port; |
67 |
|
|
|
68 |
|
|
final int packetSizeLimit = 8192; |
69 |
|
|
|
70 |
pjm2 |
1.1 |
} |