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.filter.*; |
10 |
+ |
import uk.ac.ukc.iscream.util.*; |
11 |
|
|
12 |
< |
// Handle an incoming UDP packet as a separate thread. |
12 |
> |
/** |
13 |
> |
* Handle an incoming packet as a separate thread. |
14 |
> |
* Passes the data through various plugins, then |
15 |
> |
* passes it on to the parent filter. |
16 |
> |
* |
17 |
> |
* Now grabs data from a single queue, rather than |
18 |
> |
* waiting to be contacted. |
19 |
> |
* |
20 |
> |
* @author $Author$ |
21 |
> |
* @version $Id$ |
22 |
> |
*/ |
23 |
|
public class FilterThread extends Thread{ |
24 |
|
|
25 |
< |
// Class constructor. Obtains the byte[] from a DatagramPacket. |
26 |
< |
public FilterThread(Filter parent, DatagramPacket packet){ |
27 |
< |
this.parent = parent; |
28 |
< |
this.rawPacket = packet.getData(); |
29 |
< |
} |
25 |
> |
//---FINAL ATTRIBUTES--- |
26 |
> |
|
27 |
> |
/** |
28 |
> |
* The current CVS revision of this class |
29 |
> |
*/ |
30 |
> |
public final String REVISION = "$Revision$"; |
31 |
|
|
32 |
< |
// Class constructor for passing XML Strings. |
33 |
< |
public FilterThread(Filter parent, String xml){ |
34 |
< |
this.parent = parent; |
35 |
< |
this.rawPacket = xml.getBytes(); |
32 |
> |
//---STATIC METHODS--- |
33 |
> |
|
34 |
> |
//---CONSTRUCTORS--- |
35 |
> |
|
36 |
> |
/** |
37 |
> |
* Class constructor. |
38 |
> |
*/ |
39 |
> |
public FilterThread(Queue queue, Filter parent){ |
40 |
> |
_parent = parent; |
41 |
> |
_queue = queue; |
42 |
> |
_logger.write(toString(), Logger.DEBUG, "created"); |
43 |
|
} |
44 |
< |
|
44 |
> |
|
45 |
> |
//---PUBLIC METHODS--- |
46 |
> |
|
47 |
> |
/** |
48 |
> |
* Runs the thread |
49 |
> |
*/ |
50 |
|
public void run(){ |
51 |
< |
|
52 |
< |
// Get a string without any null characters in it. |
53 |
< |
String xml = new String(rawPacket); |
54 |
< |
if (xml.indexOf(0) != -1) { |
55 |
< |
xml = xml.substring(0, xml.indexOf(0)); |
51 |
> |
// get a queue for ourselves |
52 |
> |
int n = _queue.getQueue(); |
53 |
> |
// keep this out here, saves recreating the object |
54 |
> |
String xml = null; |
55 |
> |
while(true) { |
56 |
> |
// get a String of xml |
57 |
> |
try { |
58 |
> |
xml = (String) _queue.get(n); |
59 |
> |
} |
60 |
> |
catch (InvalidQueueException e) { |
61 |
> |
_logger.write(toString(), Logger.ERROR, "Queue error: "+e); |
62 |
> |
} |
63 |
> |
|
64 |
> |
// Get a string without any null characters in it. |
65 |
> |
// -- maybe String.trim() would be better here ? |
66 |
> |
if (xml.indexOf(0) != -1) { |
67 |
> |
xml = xml.substring(0, xml.indexOf(0)); |
68 |
> |
} |
69 |
> |
else { |
70 |
> |
xml = xml.substring(0, xml.length()); |
71 |
> |
} |
72 |
> |
|
73 |
> |
// Use XMLPacketMaker to make an XMLPacket object. |
74 |
> |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
75 |
> |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
76 |
> |
|
77 |
> |
if(packet != null && PluginFilterManager.getInstance().runFilters(packet)) { |
78 |
> |
// packet is not null |
79 |
> |
// packet was not dropped by a plugin |
80 |
> |
// ... best pass it on ! |
81 |
> |
_parent.receiveXML(xml); |
82 |
> |
} |
83 |
> |
else { |
84 |
> |
// either we had a null, or a plugin dropped it |
85 |
> |
_logger.write(toString(), Logger.DEBUG, "An XML packet was sucessfully filtered from the system."); |
86 |
> |
} |
87 |
|
} |
88 |
< |
else { |
29 |
< |
xml = xml.substring(0, xml.length()); |
30 |
< |
} |
31 |
< |
System.out.println(xml); |
88 |
> |
} |
89 |
|
|
90 |
< |
// Use my XMLPacketMaker to make an XMLPacket object. |
91 |
< |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
92 |
< |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
93 |
< |
|
94 |
< |
if (packet == null){ |
95 |
< |
System.out.println("UDPReaderThread - A null XMLPacket was returned, I think I'll ignore it!"); |
96 |
< |
return; |
97 |
< |
} |
98 |
< |
System.out.println("UDPReaderThread - An XML Packet was read sucessfully: -"); |
99 |
< |
packet.printAll(); |
100 |
< |
// Now do something with this XMLPacket!!! |
101 |
< |
// .... let's try this... |
102 |
< |
parent.receiveXML(xml); |
103 |
< |
|
90 |
> |
/** |
91 |
> |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
92 |
> |
* method to provide clean logging (every class should have this). |
93 |
> |
* |
94 |
> |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
95 |
> |
* to format the toString() |
96 |
> |
* |
97 |
> |
* @return the name of this class and its CVS revision |
98 |
> |
*/ |
99 |
> |
public String toString() { |
100 |
> |
return FormatName.getName( |
101 |
> |
_name, |
102 |
> |
getClass().getName(), |
103 |
> |
REVISION); |
104 |
|
} |
105 |
+ |
|
106 |
+ |
//---PRIVATE METHODS--- |
107 |
+ |
|
108 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
109 |
+ |
|
110 |
+ |
//---ATTRIBUTES--- |
111 |
+ |
|
112 |
+ |
/** |
113 |
+ |
* Our parent filter |
114 |
+ |
*/ |
115 |
+ |
Filter _parent; |
116 |
|
|
117 |
< |
Filter parent; |
118 |
< |
byte[] rawPacket; |
117 |
> |
/** |
118 |
> |
* The Queue object |
119 |
> |
*/ |
120 |
> |
Queue _queue; |
121 |
> |
|
122 |
> |
/** |
123 |
> |
* This is the friendly identifier of the |
124 |
> |
* component this class is running in. |
125 |
> |
* eg, a Filter may be called "filter1", |
126 |
> |
* If this class does not have an owning |
127 |
> |
* component, a name from the configuration |
128 |
> |
* can be placed here. This name could also |
129 |
> |
* be changed to null for utility classes. |
130 |
> |
*/ |
131 |
> |
private String _name = FilterMain.NAME; |
132 |
> |
|
133 |
> |
/** |
134 |
> |
* This holds a reference to the |
135 |
> |
* system logger that is being used. |
136 |
> |
*/ |
137 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
138 |
> |
|
139 |
> |
//---STATIC ATTRIBUTES--- |
140 |
> |
|
141 |
|
} |