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