ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/filter/FilterThread.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/filter/FilterThread.java (file contents):
Revision 1.12 by ajm, Thu Nov 30 03:16:17 2000 UTC vs.
Revision 1.26 by tdb, Sat Mar 10 04:03:07 2001 UTC

# Line 6 | Line 6 | 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   /**
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   */
# Line 27 | Line 33 | public class FilterThread extends Thread{
33   //---STATIC METHODS---
34  
35   //---CONSTRUCTORS---
36 <
37 <    // Class constructor. Obtains the byte[] from a DatagramPacket.
38 <    public FilterThread(DatagramPacket packet, Filter parent){
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 <        _rawPacket = packet.getData();
46 <        _logger.write(this.toString(), Logger.DEBUG, "created");
36 <        
45 >        _queue = queue;
46 >        _logger.write(toString(), Logger.SYSINIT, "created");
47      }
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    }
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 <        //  -- maybe String.trim() would be better here ?
59 <        String xml = new String(_rawPacket);
60 <        if (xml.indexOf(0) != -1) {
61 <            xml = xml.substring(0, xml.indexOf(0));
56 >        // setup the XMLPacketMaker
57 >        XMLPacketMaker xmlPacketMaker = new XMLPacketMaker();
58 >        // get a queue for ourselves
59 >        int n = _queue.getQueue();
60 >        // keep these out here, saves recreating the object
61 >        String xml = null;
62 >        while(true) {
63 >            // get a String of xml
64 >            try {
65 >                xml = (String) _queue.get(n);
66 >            }
67 >            catch (InvalidQueueException e) {
68 >                _logger.write(toString(), Logger.ERROR, "Queue error: "+e);
69 >            }
70 >            
71 >            // Get a string without any null characters in it.
72 >            //  -- maybe String.trim() would be better here ?
73 >            if (xml.indexOf(0) != -1) {
74 >                xml = xml.substring(0, xml.indexOf(0));
75 >            }
76 >            else {
77 >                xml = xml.substring(0, xml.length());
78 >            }
79 >            
80 >            // Bundle the XML all on one line (saves space and simplifies
81 >            // the protocol between clientinterface and client).
82 >            StringTokenizer tokenizer = new StringTokenizer(new String(xml), "\n");
83 >            xml = "";
84 >            while (tokenizer.hasMoreTokens()) {
85 >                xml += tokenizer.nextToken();
86 >            }
87 >            
88 >            // Use XMLPacketMaker to make an XMLPacket object.
89 >            XMLPacket packet = null;
90 >            try {
91 >                packet = xmlPacketMaker.createXMLPacket(xml);
92 >            } catch(InvalidXMLException e) {
93 >                _logger.write(toString(), Logger.ERROR, "Invalid XML: "+e);
94 >                // skip the rest of this loop iteration
95 >                continue;
96 >            }
97 >                              
98 >            // XMLPacket is ok, so run filters...
99 >            if(PluginFilterManager.getInstance().runFilters(packet)) {
100 >                // and pass it on...
101 >                _parent.receiveXML(xml);
102 >            }
103 >            else {
104 >                // ... or filtered it
105 >                _logger.write(toString(), Logger.DEBUG, "An XML packet was sucessfully filtered from the system.");
106 >            }
107          }
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    
108      }
109  
110      /**
111       * Overrides the {@link java.lang.Object#toString() Object.toString()}
112       * method to provide clean logging (every class should have this).
113       *
114 +     * This uses the uk.ac.ukc.iscream.util.NameFormat class
115 +     * to format the toString()
116 +     *
117       * @return the name of this class and its CVS revision
118       */
119      public String toString() {
120 <        return this.getClass().getName() + "{" + _name + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")";
120 >        return FormatName.getName(
121 >            _name,
122 >            getClass().getName(),
123 >            REVISION);
124      }
125  
126   //---PRIVATE METHODS---
# Line 89 | Line 129 | public class FilterThread extends Thread{
129  
130   //---ATTRIBUTES---
131  
132 +    /**
133 +     * Our parent filter
134 +     */
135      Filter _parent;
136 <    byte[] _rawPacket;
137 <    Logger _logger = ReferenceManager.getInstance().getLogger();
138 <    String _name = ReferenceManager.getInstance().getName();
136 >    
137 >    /**
138 >     * The Queue object
139 >     */
140 >    Queue _queue;
141 >    
142 >    /**
143 >     * This is the friendly identifier of the
144 >     * component this class is running in.
145 >     * eg, a Filter may be called "filter1",
146 >     * If this class does not have an owning
147 >     * component,  a name from the configuration
148 >     * can be placed here.  This name could also
149 >     * be changed to null for utility classes.
150 >     */
151 >    private String _name = FilterMain.NAME;
152 >
153 >    /**
154 >     * This holds a reference to the
155 >     * system logger that is being used.
156 >     */
157 >    private Logger _logger = ReferenceManager.getInstance().getLogger();
158  
159   //---STATIC ATTRIBUTES---
160  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines