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. |
14 |
< |
int port = 4589; |
15 |
< |
final int packetSizeLimit = 8192; |
22 |
> |
public void run() { |
23 |
|
|
17 |
– |
// 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 |
– |
|
24 |
|
DatagramSocket socket = null; |
25 |
|
try { |
26 |
|
socket = new DatagramSocket(port); |
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 |
< |
System.exit(0); |
31 |
> |
return; |
32 |
|
} |
33 |
+ |
catch (Exception e){ |
34 |
+ |
System.out.println("An exception occured while creating the DatagramSocket."); |
35 |
+ |
return; |
36 |
+ |
} |
37 |
|
|
38 |
|
System.out.println("UDPReader ready and listening for UDP packets on port "+port); |
39 |
|
|
40 |
|
byte[] buf; |
41 |
– |
UDPReaderThread t = new UDPReaderThread(); |
41 |
|
|
42 |
|
boolean running = true; |
43 |
|
while (running){ |
45 |
– |
System.gc(); |
44 |
|
try { |
45 |
|
buf = new byte[packetSizeLimit]; |
46 |
|
// receive request |
47 |
|
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
48 |
|
socket.receive(packet); |
49 |
+ |
UDPReaderThread t = new UDPReaderThread(); |
50 |
|
t.run(packet); |
51 |
|
} |
52 |
|
catch (IOException e) { |
56 |
|
} |
57 |
|
socket.close(); |
58 |
|
} |
59 |
+ |
|
60 |
+ |
|
61 |
+ |
Logger logger; |
62 |
+ |
int port; |
63 |
+ |
|
64 |
+ |
final int packetSizeLimit = 8192; |
65 |
+ |
|
66 |
|
} |