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 |
< |
* Handle an incoming UDP packet as a separate thread. |
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 |
|
*/ |
33 |
|
//---STATIC METHODS--- |
34 |
|
|
35 |
|
//---CONSTRUCTORS--- |
36 |
< |
|
37 |
< |
// Class constructor. Obtains the byte[] from a DatagramPacket. |
38 |
< |
public FilterThread(DatagramPacket packet, Filter parent){ |
39 |
< |
_parent = parent; |
40 |
< |
_rawPacket = packet.getData(); |
41 |
< |
_logger.write(this.toString(), Logger.DEBUG, "created"); |
36 |
> |
|
37 |
> |
/** |
38 |
> |
* Constructs an instance of a FilterThread |
39 |
> |
* |
40 |
> |
* @param queue the Queue this filter is using |
41 |
> |
*/ |
42 |
> |
public FilterThread(Queue queue){ |
43 |
> |
// set the Thread name |
44 |
> |
setName("filter.FilterThread"); |
45 |
|
|
46 |
+ |
_queue = queue; |
47 |
+ |
_logger.write(toString(), Logger.SYSINIT, "created"); |
48 |
|
} |
38 |
– |
|
39 |
– |
// Class constructor for passing XML Strings. |
40 |
– |
public FilterThread(String xml, Filter parent){ |
41 |
– |
_parent = parent; |
42 |
– |
_rawPacket = xml.getBytes(); |
43 |
– |
_logger.write(this.toString(), Logger.DEBUG, "created"); |
44 |
– |
} |
49 |
|
|
50 |
|
//---PUBLIC METHODS--- |
51 |
|
|
52 |
+ |
/** |
53 |
+ |
* Runs the thread, getting data from the Queue and |
54 |
+ |
* sending it on to the parent filter. |
55 |
+ |
*/ |
56 |
|
public void run(){ |
57 |
< |
|
58 |
< |
// Get a string without any null characters in it. |
59 |
< |
// -- maybe String.trim() would be better here ? |
60 |
< |
String xml = new String(_rawPacket); |
61 |
< |
if (xml.indexOf(0) != -1) { |
62 |
< |
xml = xml.substring(0, xml.indexOf(0)); |
57 |
> |
// setup the XMLPacketMaker |
58 |
> |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(); |
59 |
> |
// get a queue for ourselves |
60 |
> |
int n = _queue.getQueue(); |
61 |
> |
// keep these out here, saves recreating the object |
62 |
> |
String xml = null; |
63 |
> |
while(true) { |
64 |
> |
// get a String of xml |
65 |
> |
try { |
66 |
> |
xml = (String) _queue.get(n); |
67 |
> |
} |
68 |
> |
catch (InvalidQueueException e) { |
69 |
> |
_logger.write(toString(), Logger.ERROR, "Queue error: "+e); |
70 |
> |
} |
71 |
> |
|
72 |
> |
// Get a string without any null characters in it. |
73 |
> |
// -- maybe String.trim() would be better here ? |
74 |
> |
if (xml.indexOf(0) != -1) { |
75 |
> |
xml = xml.substring(0, xml.indexOf(0)); |
76 |
> |
} |
77 |
> |
else { |
78 |
> |
xml = xml.substring(0, xml.length()); |
79 |
> |
} |
80 |
> |
|
81 |
> |
// Bundle the XML all on one line (saves space and simplifies |
82 |
> |
// the protocol between clientinterface and client). |
83 |
> |
StringTokenizer tokenizer = new StringTokenizer(new String(xml), "\n"); |
84 |
> |
xml = ""; |
85 |
> |
while (tokenizer.hasMoreTokens()) { |
86 |
> |
xml += tokenizer.nextToken(); |
87 |
> |
} |
88 |
> |
|
89 |
> |
// Use XMLPacketMaker to make an XMLPacket object. |
90 |
> |
XMLPacket packet = null; |
91 |
> |
try { |
92 |
> |
packet = xmlPacketMaker.createXMLPacket(xml); |
93 |
> |
} catch(InvalidXMLException e) { |
94 |
> |
_logger.write(toString(), Logger.ERROR, "Invalid XML: "+e); |
95 |
> |
// skip the rest of this loop iteration |
96 |
> |
continue; |
97 |
> |
} |
98 |
> |
|
99 |
> |
// get parent |
100 |
> |
Filter parent; |
101 |
> |
try { |
102 |
> |
String parentFilterName = ConfigurationProxy.getInstance().getProperty(FilterMain.NAME, "Filter.parentFilter"); |
103 |
> |
parent = FilterHelper.narrow(_refman.getCORBARef("iscream.Filter." + parentFilterName)); |
104 |
> |
} catch (PropertyNotFoundException e) { |
105 |
> |
continue; |
106 |
> |
} |
107 |
> |
|
108 |
> |
// XMLPacket is ok, run filters... |
109 |
> |
if(PluginFilterManager.getInstance().runFilters(packet)) { |
110 |
> |
// and pass it on... |
111 |
> |
parent.receiveXML(xml); |
112 |
> |
} |
113 |
> |
else { |
114 |
> |
// ... or filtered it |
115 |
> |
_logger.write(toString(), Logger.DEBUG, "An XML packet was sucessfully filtered from the system."); |
116 |
> |
} |
117 |
|
} |
56 |
– |
else { |
57 |
– |
xml = xml.substring(0, xml.length()); |
58 |
– |
} |
59 |
– |
|
60 |
– |
// Use my XMLPacketMaker to make an XMLPacket object. |
61 |
– |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
62 |
– |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
63 |
– |
|
64 |
– |
_logger.write(this.toString(), Logger.DEBUG, "got data, filtering and passing to parent - " + packet.printAll()); |
65 |
– |
|
66 |
– |
if (packet == null){ |
67 |
– |
// A null XML packet was returned - don't pass it on. |
68 |
– |
_logger.write(this.toString(), Logger.SYSMSG, "An XML UDP packet was sucessfully filtered from the system."); |
69 |
– |
return; |
70 |
– |
} |
71 |
– |
|
72 |
– |
// Now do something with this XMLPacket!!! |
73 |
– |
// .... let's try this... |
74 |
– |
_parent.receiveXML(xml); |
75 |
– |
|
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
122 |
|
* method to provide clean logging (every class should have this). |
123 |
|
* |
124 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
125 |
+ |
* to format the toString() |
126 |
+ |
* |
127 |
|
* @return the name of this class and its CVS revision |
128 |
|
*/ |
129 |
|
public String toString() { |
130 |
< |
return this.getClass().getName() + "{" + _name + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
130 |
> |
return FormatName.getName( |
131 |
> |
_name, |
132 |
> |
getClass().getName(), |
133 |
> |
REVISION); |
134 |
|
} |
135 |
|
|
136 |
|
//---PRIVATE METHODS--- |
138 |
|
//---ACCESSOR/MUTATOR METHODS--- |
139 |
|
|
140 |
|
//---ATTRIBUTES--- |
141 |
+ |
|
142 |
+ |
/** |
143 |
+ |
* The Queue object |
144 |
+ |
*/ |
145 |
+ |
Queue _queue; |
146 |
+ |
|
147 |
+ |
/** |
148 |
+ |
* This is the friendly identifier of the |
149 |
+ |
* component this class is running in. |
150 |
+ |
* eg, a Filter may be called "filter1", |
151 |
+ |
* If this class does not have an owning |
152 |
+ |
* component, a name from the configuration |
153 |
+ |
* can be placed here. This name could also |
154 |
+ |
* be changed to null for utility classes. |
155 |
+ |
*/ |
156 |
+ |
private String _name = FilterMain.NAME; |
157 |
|
|
158 |
< |
Filter _parent; |
159 |
< |
byte[] _rawPacket; |
160 |
< |
Logger _logger = ReferenceManager.getInstance().getLogger(); |
161 |
< |
String _name = ReferenceManager.getInstance().getName(); |
158 |
> |
/** |
159 |
> |
* This holds a reference to the |
160 |
> |
* system logger that is being used. |
161 |
> |
*/ |
162 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
163 |
> |
|
164 |
> |
/** |
165 |
> |
* A reference to the reference manager in use |
166 |
> |
*/ |
167 |
> |
private ReferenceManager _refman = ReferenceManager.getInstance(); |
168 |
|
|
169 |
|
//---STATIC ATTRIBUTES--- |
170 |
|
|