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/UDPReader.java
Revision: 1.19
Committed: Tue Mar 19 17:22:12 2002 UTC (22 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.18: +19 -4 lines
Log Message:
Added ACL support to the TCP and UDP parts of the Filter. Not so sure about
doing the same to the CORBA input bit though :)

File Contents

# User Rev Content
1 tdb 1.6 //---PACKAGE DECLARATION---
2 tdb 1.18 package uk.org.iscream.cms.server.filter;
3 tdb 1.6
4     //---IMPORTS---
5 pjm2 1.1 import java.io.*;
6     import java.net.*;
7     import java.util.*;
8 tdb 1.18 import uk.org.iscream.cms.server.core.*;
9     import uk.org.iscream.cms.server.componentmanager.*;
10     import uk.org.iscream.cms.server.filter.*;
11     import uk.org.iscream.cms.server.util.*;
12 pjm2 1.1
13 tdb 1.6 /**
14     * This class contains the main method to be run by
15 tdb 1.14 * the filterd. It harvests UDP traffic, and queues it.
16 tdb 1.6 *
17 tdb 1.19 * @author $Author: tdb $
18     * @version $Id: UDPReader.java,v 1.18 2001/05/29 17:02:35 tdb Exp $
19 tdb 1.6 */
20 pjm2 1.1 public class UDPReader extends Thread{
21    
22 tdb 1.6 //---FINAL ATTRIBUTES---
23    
24     /**
25     * The current CVS revision of this class
26     */
27 tdb 1.19 public final String REVISION = "$Revision: 1.18 $";
28 tdb 1.15
29 ajm 1.11 /**
30     * The maximum size of a packet
31     */
32     private final int packetSizeLimit = 8192;
33 tdb 1.6
34     //---STATIC METHODS---
35    
36     //---CONSTRUCTORS---
37    
38 ajm 1.10 /**
39 tdb 1.14 * Constructs a new UDPReader.
40     *
41     * @param port The port on which we listen for UDP data
42     * @param queue The queue which we are using
43 ajm 1.10 */
44 tdb 1.12 public UDPReader(int port, Queue queue){
45 tdb 1.16 // set the Thread name
46     setName("filter.UDPReader");
47    
48 tdb 1.8 _port = port;
49 tdb 1.12 _queue = queue;
50 tdb 1.15 _logger.write(toString(), Logger.SYSINIT, "started");
51 pjm2 1.1 }
52    
53 tdb 1.6 //---PUBLIC METHODS---
54 tdb 1.14
55     /**
56     * The main method in the class. Reads and queues XML sent
57     * over UDP.
58     */
59 pjm2 1.1 public void run() {
60 tdb 1.19 // get our ACL from the configuration
61     ACL udpACL = null;
62     try {
63     String stringACL = ConfigurationProxy.getInstance().getProperty(FilterMain.NAME, "Filter.UDPACL");
64     udpACL = new ACL(stringACL);
65     }
66     catch(PropertyNotFoundException e) {
67     _logger.write(toString(), Logger.WARNING, "No ACL found for UDPReader: " + e);
68     }
69 tdb 1.14
70     // setup a Datagram socket
71 pjm2 1.1 DatagramSocket socket = null;
72     try {
73 tdb 1.19 // use an ACLServerSocket if we have an ACL
74     if(udpACL != null) {
75     socket = new ACLDatagramSocket(udpACL, _port);
76     }
77     else {
78     socket = new DatagramSocket(_port);
79     }
80 pjm2 1.1 }
81     catch (BindException e){
82 tdb 1.8 _logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+" as this port was already in use.");
83 pjm2 1.1 return;
84     }
85     catch (Exception e){
86 tdb 1.8 _logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+".");
87 pjm2 1.1 return;
88     }
89    
90 tdb 1.8 _logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port);
91 tdb 1.15
92 pjm2 1.1 byte[] buf;
93    
94 tdb 1.14 // read UDP packets and queue them
95 pjm2 1.1 boolean running = true;
96     while (running){
97     try {
98 tdb 1.15
99     // receive request and put it in the Queue
100 pjm2 1.1 buf = new byte[packetSizeLimit];
101     DatagramPacket packet = new DatagramPacket(buf, buf.length);
102     socket.receive(packet);
103 tdb 1.12 String xml = new String(packet.getData());
104     _queue.add(xml);
105 pjm2 1.1 }
106     catch (IOException e) {
107 tdb 1.8 _logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e);
108 tdb 1.14 running = false;
109 pjm2 1.1 }
110     }
111     socket.close();
112     }
113 tdb 1.15
114 tdb 1.6 /**
115     * Overrides the {@link java.lang.Object#toString() Object.toString()}
116     * method to provide clean logging (every class should have this).
117     *
118 tdb 1.18 * This uses the uk.org.iscream.cms.server.util.NameFormat class
119 ajm 1.11 * to format the toString()
120     *
121 tdb 1.6 * @return the name of this class and its CVS revision
122     */
123     public String toString() {
124 ajm 1.11 return FormatName.getName(
125     _name,
126     getClass().getName(),
127     REVISION);
128 tdb 1.6 }
129    
130     //---PRIVATE METHODS---
131    
132     //---ACCESSOR/MUTATOR METHODS---
133    
134     //---ATTRIBUTES---
135    
136 ajm 1.11 /**
137     * This is the friendly identifier of the
138     * component this class is running in.
139     * eg, a Filter may be called "filter1",
140     * If this class does not have an owning
141     * component, a name from the configuration
142     * can be placed here. This name could also
143     * be changed to null for utility classes.
144     */
145     private String _name = FilterMain.NAME;
146 tdb 1.15
147 ajm 1.11 /**
148     * This holds a reference to the
149     * system logger that is being used.
150     */
151     private Logger _logger = ReferenceManager.getInstance().getLogger();
152    
153     /**
154     * The port that this reader is using
155     */
156 tdb 1.8 int _port;
157 ajm 1.11
158     /**
159 tdb 1.12 * The Queue object
160 ajm 1.11 */
161 tdb 1.15 Queue _queue;
162 tdb 1.6
163     //---STATIC ATTRIBUTES---
164    
165 pjm2 1.1 }