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.4 by pjm2, Thu Nov 23 09:14:29 2000 UTC vs.
Revision 1.27 by tdb, Tue Mar 13 02:19:46 2001 UTC

# Line 1 | Line 1
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 >        // set the Thread name
45 >        setName("filter.FilterThread");
46 >        
47 >        _parent = parent;
48 >        _queue = queue;
49 >        _logger.write(toString(), Logger.SYSINIT, "created");
50      }
51 <    
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 <        String xml = new String(rawPacket);
62 <        if (xml.indexOf(0) != -1) {
63 <            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          }
111 <        else {
29 <            xml = xml.substring(0, xml.length());
30 <        }
31 <        System.out.println(xml);
111 >    }
112  
113 <        // Use my XMLPacketMaker to make an XMLPacket object.
114 <        XMLPacketMaker xmlPacketMaker = new XMLPacketMaker(xml);
115 <        XMLPacket packet = xmlPacketMaker.createXMLPacket();
116 <
117 <        if (packet == null){
118 <            System.out.println("UDPReaderThread - A null XMLPacket was returned, I think I'll ignore it!");
119 <            return;
120 <        }
121 <        System.out.println("UDPReaderThread - An XML Packet was read sucessfully: -");
122 <        packet.printAll();
123 <            // Now do something with this XMLPacket!!!
124 <            // .... let's try this...
125 <        parent.receiveXML(xml);
126 <    
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 FormatName.getName(
124 >            _name,
125 >            getClass().getName(),
126 >            REVISION);
127      }
128 +
129 + //---PRIVATE METHODS---
130 +
131 + //---ACCESSOR/MUTATOR METHODS---
132 +
133 + //---ATTRIBUTES---
134 +
135 +    /**
136 +     * Our parent filter
137 +     */
138 +    Filter _parent;
139      
140 <    Filter parent;
141 <    byte[] rawPacket;
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 >
164   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines