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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines