6 |
|
import java.net.*; |
7 |
|
import java.util.*; |
8 |
|
import uk.ac.ukc.iscream.core.*; |
9 |
+ |
import uk.ac.ukc.iscream.componentmanager.*; |
10 |
|
import uk.ac.ukc.iscream.filter.*; |
11 |
|
import uk.ac.ukc.iscream.util.*; |
12 |
|
|
13 |
|
/** |
14 |
|
* This class contains the main method to be run by |
15 |
< |
* the filter children. It harvests UDP traffic. |
15 |
> |
* the filterd. It harvests UDP traffic, and queues it. |
16 |
|
* |
17 |
|
* @author $Author$ |
18 |
|
* @version $Id$ |
26 |
|
*/ |
27 |
|
public final String REVISION = "$Revision$"; |
28 |
|
|
29 |
+ |
/** |
30 |
+ |
* The maximum size of a packet |
31 |
+ |
*/ |
32 |
+ |
private final int packetSizeLimit = 8192; |
33 |
+ |
|
34 |
|
//---STATIC METHODS--- |
35 |
|
|
36 |
|
//---CONSTRUCTORS--- |
37 |
|
|
38 |
|
/** |
39 |
< |
* It is normal to use this constructor in preference |
40 |
< |
* to any other in this class. |
39 |
> |
* Constructs a new UDPReader. |
40 |
> |
* |
41 |
> |
* @param port The port on which we listen for UDP data |
42 |
> |
* @param queue The queue which we are using |
43 |
|
*/ |
44 |
< |
public UDPReader(int port, Filter parent){ |
44 |
> |
public UDPReader(int port, Queue queue){ |
45 |
|
_port = port; |
46 |
< |
_parent = parent; |
46 |
> |
_queue = queue; |
47 |
> |
_logger.write(toString(), Logger.SYSINIT, "started"); |
48 |
|
} |
49 |
|
|
50 |
|
//---PUBLIC METHODS--- |
51 |
< |
|
51 |
> |
|
52 |
> |
/** |
53 |
> |
* The main method in the class. Reads and queues XML sent |
54 |
> |
* over UDP. |
55 |
> |
*/ |
56 |
|
public void run() { |
57 |
< |
|
57 |
> |
|
58 |
> |
// setup a Datagram socket |
59 |
|
DatagramSocket socket = null; |
60 |
|
try { |
61 |
|
socket = new DatagramSocket(_port); |
70 |
|
} |
71 |
|
|
72 |
|
_logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port); |
73 |
< |
|
73 |
> |
|
74 |
|
byte[] buf; |
75 |
|
|
76 |
+ |
// read UDP packets and queue them |
77 |
|
boolean running = true; |
78 |
|
while (running){ |
79 |
|
try { |
80 |
< |
|
81 |
< |
// receive request and pass on to the FilterThread. |
80 |
> |
|
81 |
> |
// receive request and put it in the Queue |
82 |
|
buf = new byte[packetSizeLimit]; |
83 |
|
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
84 |
|
socket.receive(packet); |
85 |
< |
FilterThread t = new FilterThread(packet, _parent); |
86 |
< |
t.start(); |
72 |
< |
|
85 |
> |
String xml = new String(packet.getData()); |
86 |
> |
_queue.add(xml); |
87 |
|
} |
88 |
|
catch (IOException e) { |
89 |
|
_logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
90 |
< |
return; |
90 |
> |
running = false; |
91 |
|
} |
92 |
|
} |
93 |
|
socket.close(); |
94 |
|
} |
95 |
< |
|
95 |
> |
|
96 |
|
/** |
97 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
98 |
|
* method to provide clean logging (every class should have this). |
99 |
|
* |
100 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
101 |
+ |
* to format the toString() |
102 |
+ |
* |
103 |
|
* @return the name of this class and its CVS revision |
104 |
|
*/ |
105 |
|
public String toString() { |
106 |
< |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
106 |
> |
return FormatName.getName( |
107 |
> |
_name, |
108 |
> |
getClass().getName(), |
109 |
> |
REVISION); |
110 |
|
} |
111 |
|
|
112 |
|
//---PRIVATE METHODS--- |
115 |
|
|
116 |
|
//---ATTRIBUTES--- |
117 |
|
|
118 |
< |
Logger _logger = ReferenceManager.getInstance().getLogger(); |
118 |
> |
/** |
119 |
> |
* This is the friendly identifier of the |
120 |
> |
* component this class is running in. |
121 |
> |
* eg, a Filter may be called "filter1", |
122 |
> |
* If this class does not have an owning |
123 |
> |
* component, a name from the configuration |
124 |
> |
* can be placed here. This name could also |
125 |
> |
* be changed to null for utility classes. |
126 |
> |
*/ |
127 |
> |
private String _name = FilterMain.NAME; |
128 |
> |
|
129 |
> |
/** |
130 |
> |
* This holds a reference to the |
131 |
> |
* system logger that is being used. |
132 |
> |
*/ |
133 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
134 |
> |
|
135 |
> |
/** |
136 |
> |
* The port that this reader is using |
137 |
> |
*/ |
138 |
|
int _port; |
139 |
< |
Filter _parent; |
140 |
< |
|
141 |
< |
final int packetSizeLimit = 8192; |
139 |
> |
|
140 |
> |
/** |
141 |
> |
* The Queue object |
142 |
> |
*/ |
143 |
> |
Queue _queue; |
144 |
|
|
145 |
|
//---STATIC ATTRIBUTES--- |
146 |
|
|