1 |
tdb |
1.6 |
//---PACKAGE DECLARATION--- |
2 |
|
|
package uk.ac.ukc.iscream.filter; |
3 |
|
|
|
4 |
|
|
//---IMPORTS--- |
5 |
pjm2 |
1.1 |
import java.io.*; |
6 |
|
|
import java.net.*; |
7 |
|
|
import java.util.*; |
8 |
tdb |
1.2 |
import uk.ac.ukc.iscream.core.*; |
9 |
|
|
import uk.ac.ukc.iscream.filter.*; |
10 |
ajm |
1.9 |
import uk.ac.ukc.iscream.util.*; |
11 |
pjm2 |
1.1 |
|
12 |
tdb |
1.6 |
/** |
13 |
|
|
* This class contains the main method to be run by |
14 |
|
|
* the filter children. It harvests UDP traffic. |
15 |
|
|
* |
16 |
ajm |
1.10 |
* @author $Author: ajm4 $ |
17 |
ajm |
1.11 |
* @version $Id: UDPReader.java,v 1.10 2000/12/13 13:36:46 ajm4 Exp $ |
18 |
tdb |
1.6 |
*/ |
19 |
pjm2 |
1.1 |
public class UDPReader extends Thread{ |
20 |
|
|
|
21 |
tdb |
1.6 |
//---FINAL ATTRIBUTES--- |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* The current CVS revision of this class |
25 |
|
|
*/ |
26 |
ajm |
1.11 |
public final String REVISION = "$Revision: 1.10 $"; |
27 |
|
|
|
28 |
|
|
/** |
29 |
|
|
* The maximum size of a packet |
30 |
|
|
*/ |
31 |
|
|
private final int packetSizeLimit = 8192; |
32 |
tdb |
1.6 |
|
33 |
|
|
//---STATIC METHODS--- |
34 |
|
|
|
35 |
|
|
//---CONSTRUCTORS--- |
36 |
|
|
|
37 |
ajm |
1.10 |
/** |
38 |
|
|
* It is normal to use this constructor in preference |
39 |
|
|
* to any other in this class. |
40 |
|
|
*/ |
41 |
tdb |
1.8 |
public UDPReader(int port, Filter parent){ |
42 |
|
|
_port = port; |
43 |
|
|
_parent = parent; |
44 |
pjm2 |
1.1 |
} |
45 |
|
|
|
46 |
tdb |
1.6 |
//---PUBLIC METHODS--- |
47 |
|
|
|
48 |
pjm2 |
1.1 |
public void run() { |
49 |
|
|
|
50 |
|
|
DatagramSocket socket = null; |
51 |
|
|
try { |
52 |
tdb |
1.8 |
socket = new DatagramSocket(_port); |
53 |
pjm2 |
1.1 |
} |
54 |
|
|
catch (BindException e){ |
55 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+" as this port was already in use."); |
56 |
pjm2 |
1.1 |
return; |
57 |
|
|
} |
58 |
|
|
catch (Exception e){ |
59 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+"."); |
60 |
pjm2 |
1.1 |
return; |
61 |
|
|
} |
62 |
|
|
|
63 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port); |
64 |
pjm2 |
1.1 |
|
65 |
|
|
byte[] buf; |
66 |
|
|
|
67 |
|
|
boolean running = true; |
68 |
|
|
while (running){ |
69 |
|
|
try { |
70 |
pjm2 |
1.3 |
|
71 |
|
|
// receive request and pass on to the FilterThread. |
72 |
pjm2 |
1.1 |
buf = new byte[packetSizeLimit]; |
73 |
|
|
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
74 |
|
|
socket.receive(packet); |
75 |
tdb |
1.8 |
FilterThread t = new FilterThread(packet, _parent); |
76 |
pjm2 |
1.3 |
t.start(); |
77 |
|
|
|
78 |
pjm2 |
1.1 |
} |
79 |
|
|
catch (IOException e) { |
80 |
tdb |
1.8 |
_logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
81 |
pjm2 |
1.1 |
return; |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
socket.close(); |
85 |
|
|
} |
86 |
tdb |
1.6 |
|
87 |
|
|
/** |
88 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
89 |
|
|
* method to provide clean logging (every class should have this). |
90 |
|
|
* |
91 |
ajm |
1.11 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
92 |
|
|
* to format the toString() |
93 |
|
|
* |
94 |
tdb |
1.6 |
* @return the name of this class and its CVS revision |
95 |
|
|
*/ |
96 |
|
|
public String toString() { |
97 |
ajm |
1.11 |
return FormatName.getName( |
98 |
|
|
_name, |
99 |
|
|
getClass().getName(), |
100 |
|
|
REVISION); |
101 |
tdb |
1.6 |
} |
102 |
|
|
|
103 |
|
|
//---PRIVATE METHODS--- |
104 |
|
|
|
105 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
106 |
|
|
|
107 |
|
|
//---ATTRIBUTES--- |
108 |
|
|
|
109 |
ajm |
1.11 |
/** |
110 |
|
|
* This is the friendly identifier of the |
111 |
|
|
* component this class is running in. |
112 |
|
|
* eg, a Filter may be called "filter1", |
113 |
|
|
* If this class does not have an owning |
114 |
|
|
* component, a name from the configuration |
115 |
|
|
* can be placed here. This name could also |
116 |
|
|
* be changed to null for utility classes. |
117 |
|
|
*/ |
118 |
|
|
private String _name = FilterMain.NAME; |
119 |
|
|
|
120 |
|
|
/** |
121 |
|
|
* This holds a reference to the |
122 |
|
|
* system logger that is being used. |
123 |
|
|
*/ |
124 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
125 |
|
|
|
126 |
|
|
/** |
127 |
|
|
* The port that this reader is using |
128 |
|
|
*/ |
129 |
tdb |
1.8 |
int _port; |
130 |
ajm |
1.11 |
|
131 |
|
|
/** |
132 |
|
|
* The parent of this filter |
133 |
|
|
*/ |
134 |
tdb |
1.8 |
Filter _parent; |
135 |
tdb |
1.6 |
|
136 |
|
|
//---STATIC ATTRIBUTES--- |
137 |
|
|
|
138 |
pjm2 |
1.1 |
} |