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