1 |
pjm2 |
1.1 |
import java.io.*; |
2 |
|
|
import java.net.*; |
3 |
|
|
import java.util.*; |
4 |
tdb |
1.2 |
import uk.ac.ukc.iscream.core.*; |
5 |
|
|
import uk.ac.ukc.iscream.filter.*; |
6 |
pjm2 |
1.1 |
|
7 |
|
|
// This class contains the main method to be run by |
8 |
|
|
// the filter children. It harvests UDP traffic. |
9 |
|
|
// |
10 |
|
|
// |
11 |
|
|
public class UDPReader extends Thread{ |
12 |
|
|
|
13 |
|
|
// It is normal to use this constructor in preference |
14 |
|
|
// to any other in this class. |
15 |
tdb |
1.2 |
public UDPReader(int port, Filter parent, Logger logger){ |
16 |
pjm2 |
1.1 |
this.logger = logger; |
17 |
|
|
this.port = port; |
18 |
tdb |
1.2 |
this.parent = parent; |
19 |
pjm2 |
1.1 |
} |
20 |
|
|
|
21 |
tdb |
1.2 |
public UDPReader(Filter parent, Logger logger){ |
22 |
|
|
this(4589, parent, logger); |
23 |
pjm2 |
1.1 |
} |
24 |
|
|
|
25 |
|
|
public void run() { |
26 |
|
|
|
27 |
|
|
DatagramSocket socket = null; |
28 |
|
|
try { |
29 |
|
|
socket = new DatagramSocket(port); |
30 |
|
|
} |
31 |
|
|
catch (BindException e){ |
32 |
|
|
logger.write(this.toString(), Logger.SYSMSG, "Could not start the UDPReader thread on port "+port+" as this port was already in use."); |
33 |
|
|
return; |
34 |
|
|
} |
35 |
|
|
catch (Exception e){ |
36 |
|
|
logger.write(this.toString(), Logger.SYSMSG, "Could not start the UDPReader thread on port "+port+"."); |
37 |
|
|
return; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+port); |
41 |
|
|
|
42 |
|
|
byte[] buf; |
43 |
|
|
|
44 |
|
|
boolean running = true; |
45 |
|
|
while (running){ |
46 |
|
|
try { |
47 |
|
|
buf = new byte[packetSizeLimit]; |
48 |
|
|
// receive request |
49 |
|
|
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
50 |
|
|
socket.receive(packet); |
51 |
tdb |
1.2 |
FilterThread t = new FilterThread(parent); |
52 |
pjm2 |
1.1 |
t.run(packet); |
53 |
|
|
} |
54 |
|
|
catch (IOException e) { |
55 |
|
|
logger.write(this.toString(), Logger.SYSMSG, "The UDPReader thread has been shut down as an exception occured: "+e); |
56 |
|
|
return; |
57 |
|
|
} |
58 |
|
|
} |
59 |
|
|
socket.close(); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
|
63 |
|
|
Logger logger; |
64 |
|
|
int port; |
65 |
tdb |
1.2 |
Filter parent; |
66 |
|
|
|
67 |
pjm2 |
1.1 |
final int packetSizeLimit = 8192; |
68 |
|
|
|
69 |
|
|
} |