10 |
|
import uk.ac.ukc.iscream.util.*; |
11 |
|
|
12 |
|
/** |
13 |
< |
* Handle an incoming UDP packet as a separate thread. |
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 |
|
*/ |
32 |
|
//---STATIC METHODS--- |
33 |
|
|
34 |
|
//---CONSTRUCTORS--- |
35 |
< |
|
36 |
< |
// Class constructor. Obtains the byte[] from a DatagramPacket. |
37 |
< |
public FilterThread(DatagramPacket packet, Filter parent){ |
35 |
> |
|
36 |
> |
/** |
37 |
> |
* Class constructor. |
38 |
> |
*/ |
39 |
> |
public FilterThread(Queue queue, Filter parent){ |
40 |
|
_parent = parent; |
41 |
< |
_rawPacket = packet.getData(); |
42 |
< |
_logger.write(this.toString(), Logger.DEBUG, "created"); |
36 |
< |
|
41 |
> |
_queue = queue; |
42 |
> |
_logger.write(toString(), Logger.DEBUG, "created"); |
43 |
|
} |
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 |
– |
} |
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 |
< |
// -- maybe String.trim() would be better here ? |
54 |
< |
String xml = new String(_rawPacket); |
55 |
< |
if (xml.indexOf(0) != -1) { |
56 |
< |
xml = xml.substring(0, xml.indexOf(0)); |
51 |
> |
// get a queue for ourselves |
52 |
> |
int n = _queue.getQueue(); |
53 |
> |
// keep these 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 |
> |
|
74 |
> |
// Bundle the XML all on one line (saves space and simplifies |
75 |
> |
// the protocol between clientinterface and client. |
76 |
> |
StringTokenizer tokenizer = new StringTokenizer(new String(xml), "\n"); |
77 |
> |
xml = ""; |
78 |
> |
while (tokenizer.hasMoreTokens()) { |
79 |
> |
xml += tokenizer.nextToken(); |
80 |
> |
} |
81 |
> |
|
82 |
> |
|
83 |
> |
// Use XMLPacketMaker to make an XMLPacket object. |
84 |
> |
XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml); |
85 |
> |
XMLPacket packet = xmlPacketMaker.createXMLPacket(); |
86 |
> |
|
87 |
> |
if(packet != null && PluginFilterManager.getInstance().runFilters(packet)) { |
88 |
> |
// packet is not null |
89 |
> |
// packet was not dropped by a plugin |
90 |
> |
// ... best pass it on ! |
91 |
> |
_parent.receiveXML(xml); |
92 |
> |
} |
93 |
> |
else { |
94 |
> |
// either we had a null, or a plugin dropped it |
95 |
> |
_logger.write(toString(), Logger.DEBUG, "An XML packet was sucessfully filtered from the system."); |
96 |
> |
} |
97 |
|
} |
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 |
– |
if (packet == null){ |
65 |
– |
// A null XML packet was returned - don't pass it on. |
66 |
– |
_logger.write(this.toString(), Logger.SYSMSG, "An XML UDP packet was sucessfully filtered from the system."); |
67 |
– |
return; |
68 |
– |
} |
69 |
– |
|
70 |
– |
// Now do something with this XMLPacket!!! |
71 |
– |
// .... let's try this... |
72 |
– |
_parent.receiveXML(xml); |
73 |
– |
|
98 |
|
} |
99 |
|
|
100 |
|
/** |
101 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
102 |
|
* method to provide clean logging (every class should have this). |
103 |
|
* |
104 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
105 |
+ |
* to format the toString() |
106 |
+ |
* |
107 |
|
* @return the name of this class and its CVS revision |
108 |
|
*/ |
109 |
|
public String toString() { |
110 |
< |
return this.getClass().getName() + "{" + _name + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
110 |
> |
return FormatName.getName( |
111 |
> |
_name, |
112 |
> |
getClass().getName(), |
113 |
> |
REVISION); |
114 |
|
} |
115 |
|
|
116 |
|
//---PRIVATE METHODS--- |
119 |
|
|
120 |
|
//---ATTRIBUTES--- |
121 |
|
|
122 |
+ |
/** |
123 |
+ |
* Our parent filter |
124 |
+ |
*/ |
125 |
|
Filter _parent; |
126 |
< |
byte[] _rawPacket; |
127 |
< |
Logger _logger = ReferenceManager.getInstance().getLogger(); |
128 |
< |
String _name = ReferenceManager.getInstance().getName(); |
126 |
> |
|
127 |
> |
/** |
128 |
> |
* The Queue object |
129 |
> |
*/ |
130 |
> |
Queue _queue; |
131 |
> |
|
132 |
> |
/** |
133 |
> |
* This is the friendly identifier of the |
134 |
> |
* component this class is running in. |
135 |
> |
* eg, a Filter may be called "filter1", |
136 |
> |
* If this class does not have an owning |
137 |
> |
* component, a name from the configuration |
138 |
> |
* can be placed here. This name could also |
139 |
> |
* be changed to null for utility classes. |
140 |
> |
*/ |
141 |
> |
private String _name = FilterMain.NAME; |
142 |
> |
|
143 |
> |
/** |
144 |
> |
* This holds a reference to the |
145 |
> |
* system logger that is being used. |
146 |
> |
*/ |
147 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
148 |
|
|
149 |
|
//---STATIC ATTRIBUTES--- |
150 |
|
|