1 |
+ |
//---PACKAGE DECLARATION--- |
2 |
+ |
package uk.ac.ukc.iscream.filter; |
3 |
+ |
|
4 |
+ |
//---IMPORTS--- |
5 |
|
import java.io.*; |
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 |
< |
public class UDPReaderThread extends Thread{ |
13 |
> |
/** |
14 |
> |
* Handle an incoming packet as a separate thread. |
15 |
> |
* Passes the data through various plugins, then |
16 |
> |
* passes it on to the parent filter. |
17 |
> |
* |
18 |
> |
* Now grabs data from a single queue, rather than |
19 |
> |
* waiting to be contacted. |
20 |
> |
* |
21 |
> |
* @author $Author$ |
22 |
> |
* @version $Id$ |
23 |
> |
*/ |
24 |
> |
public class FilterThread extends Thread{ |
25 |
|
|
26 |
< |
public FilterThread(){ |
27 |
< |
// no-args constructor. |
28 |
< |
} |
26 |
> |
//---FINAL ATTRIBUTES--- |
27 |
> |
|
28 |
> |
/** |
29 |
> |
* The current CVS revision of this class |
30 |
> |
*/ |
31 |
> |
public final String REVISION = "$Revision$"; |
32 |
|
|
33 |
< |
public void run(DatagramPacket packet){ |
34 |
< |
rawPacket = packet.getData(); |
35 |
< |
start(); |
33 |
> |
//---STATIC METHODS--- |
34 |
> |
|
35 |
> |
//---CONSTRUCTORS--- |
36 |
> |
|
37 |
> |
/** |
38 |
> |
* Constructs an instance of a FilterThread |
39 |
> |
* |
40 |
> |
* @param queue the Queue this filter is using |
41 |
> |
* @param parent a CORBA reference to our parent filter |
42 |
> |
*/ |
43 |
> |
public FilterThread(Queue queue, Filter parent){ |
44 |
> |
// set the Thread name |
45 |
> |
setName("filter.FilterThread"); |
46 |
> |
|
47 |
> |
_parent = parent; |
48 |
> |
_queue = queue; |
49 |
> |
_logger.write(toString(), Logger.SYSINIT, "created"); |
50 |
|
} |
15 |
– |
|
16 |
– |
public void run(String xml){ |
17 |
– |
rawPacket = xml.getBytes(); |
18 |
– |
start(); |
19 |
– |
} |
20 |
– |
|
21 |
– |
public void run(){ |
22 |
– |
|
23 |
– |
// Get a string without any null characters in it. |
24 |
– |
String xml = new String(rawPacket); |
25 |
– |
xml = xml.substring(0, xml.indexOf(0)); |
51 |
|
|
52 |
< |
// USe my XMLPacketMaker to make an XMLPacket object. |
53 |
< |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
54 |
< |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
55 |
< |
|
56 |
< |
if (packet == null){ |
57 |
< |
System.out.println("UDPReaderThread - A null XMLPacket was returned, I think I'll ignore it!"); |
58 |
< |
return; |
52 |
> |
//---PUBLIC METHODS--- |
53 |
> |
|
54 |
> |
/** |
55 |
> |
* Runs the thread, getting data from the Queue and |
56 |
> |
* sending it on to the parent filter. |
57 |
> |
*/ |
58 |
> |
public void run(){ |
59 |
> |
// setup the XMLPacketMaker |
60 |
> |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(); |
61 |
> |
// get a queue for ourselves |
62 |
> |
int n = _queue.getQueue(); |
63 |
> |
// keep these out here, saves recreating the object |
64 |
> |
String xml = null; |
65 |
> |
while(true) { |
66 |
> |
// get a String of xml |
67 |
> |
try { |
68 |
> |
xml = (String) _queue.get(n); |
69 |
> |
} |
70 |
> |
catch (InvalidQueueException e) { |
71 |
> |
_logger.write(toString(), Logger.ERROR, "Queue error: "+e); |
72 |
> |
} |
73 |
> |
|
74 |
> |
// Get a string without any null characters in it. |
75 |
> |
// -- maybe String.trim() would be better here ? |
76 |
> |
if (xml.indexOf(0) != -1) { |
77 |
> |
xml = xml.substring(0, xml.indexOf(0)); |
78 |
> |
} |
79 |
> |
else { |
80 |
> |
xml = xml.substring(0, xml.length()); |
81 |
> |
} |
82 |
> |
|
83 |
> |
// Bundle the XML all on one line (saves space and simplifies |
84 |
> |
// the protocol between clientinterface and client). |
85 |
> |
StringTokenizer tokenizer = new StringTokenizer(new String(xml), "\n"); |
86 |
> |
xml = ""; |
87 |
> |
while (tokenizer.hasMoreTokens()) { |
88 |
> |
xml += tokenizer.nextToken(); |
89 |
> |
} |
90 |
> |
|
91 |
> |
// Use XMLPacketMaker to make an XMLPacket object. |
92 |
> |
XMLPacket packet = null; |
93 |
> |
try { |
94 |
> |
packet = xmlPacketMaker.createXMLPacket(xml); |
95 |
> |
} catch(InvalidXMLException e) { |
96 |
> |
_logger.write(toString(), Logger.ERROR, "Invalid XML: "+e); |
97 |
> |
// skip the rest of this loop iteration |
98 |
> |
continue; |
99 |
> |
} |
100 |
> |
|
101 |
> |
// XMLPacket is ok, so run filters... |
102 |
> |
if(PluginFilterManager.getInstance().runFilters(packet)) { |
103 |
> |
// and pass it on... |
104 |
> |
_parent.receiveXML(xml); |
105 |
> |
} |
106 |
> |
else { |
107 |
> |
// ... or filtered it |
108 |
> |
_logger.write(toString(), Logger.DEBUG, "An XML packet was sucessfully filtered from the system."); |
109 |
> |
} |
110 |
|
} |
35 |
– |
System.out.println("UDPReaderThread - An XML Packet was read sucessfully: -"); |
36 |
– |
packet.printAll(); |
37 |
– |
// Now do something with this XMLPacket!!! |
38 |
– |
// .... but what? ;-) |
39 |
– |
|
111 |
|
} |
112 |
+ |
|
113 |
+ |
/** |
114 |
+ |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
115 |
+ |
* method to provide clean logging (every class should have this). |
116 |
+ |
* |
117 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
118 |
+ |
* to format the toString() |
119 |
+ |
* |
120 |
+ |
* @return the name of this class and its CVS revision |
121 |
+ |
*/ |
122 |
+ |
public String toString() { |
123 |
+ |
return FormatName.getName( |
124 |
+ |
_name, |
125 |
+ |
getClass().getName(), |
126 |
+ |
REVISION); |
127 |
+ |
} |
128 |
+ |
|
129 |
+ |
//---PRIVATE METHODS--- |
130 |
+ |
|
131 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
132 |
+ |
|
133 |
+ |
//---ATTRIBUTES--- |
134 |
+ |
|
135 |
+ |
/** |
136 |
+ |
* Our parent filter |
137 |
+ |
*/ |
138 |
+ |
Filter _parent; |
139 |
|
|
140 |
< |
byte[] rawPacket; |
140 |
> |
/** |
141 |
> |
* The Queue object |
142 |
> |
*/ |
143 |
> |
Queue _queue; |
144 |
> |
|
145 |
> |
/** |
146 |
> |
* This is the friendly identifier of the |
147 |
> |
* component this class is running in. |
148 |
> |
* eg, a Filter may be called "filter1", |
149 |
> |
* If this class does not have an owning |
150 |
> |
* component, a name from the configuration |
151 |
> |
* can be placed here. This name could also |
152 |
> |
* be changed to null for utility classes. |
153 |
> |
*/ |
154 |
> |
private String _name = FilterMain.NAME; |
155 |
> |
|
156 |
> |
/** |
157 |
> |
* This holds a reference to the |
158 |
> |
* system logger that is being used. |
159 |
> |
*/ |
160 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
161 |
> |
|
162 |
> |
//---STATIC ATTRIBUTES--- |
163 |
> |
|
164 |
|
} |