1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org |
4 |
+ |
* Copyright (C) 2000-2002 i-scream |
5 |
+ |
* |
6 |
+ |
* This program is free software; you can redistribute it and/or |
7 |
+ |
* modify it under the terms of the GNU General Public License |
8 |
+ |
* as published by the Free Software Foundation; either version 2 |
9 |
+ |
* of the License, or (at your option) any later version. |
10 |
+ |
* |
11 |
+ |
* This program is distributed in the hope that it will be useful, |
12 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
+ |
* GNU General Public License for more details. |
15 |
+ |
* |
16 |
+ |
* You should have received a copy of the GNU General Public License |
17 |
+ |
* along with this program; if not, write to the Free Software |
18 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
+ |
*/ |
20 |
+ |
|
21 |
+ |
//---PACKAGE DECLARATION--- |
22 |
+ |
package uk.org.iscream.cms.server.filter; |
23 |
+ |
|
24 |
+ |
//---IMPORTS--- |
25 |
|
import java.io.*; |
26 |
|
import java.net.*; |
27 |
< |
import java.util.*; |
28 |
< |
import uk.ac.ukc.iscream.core.*; |
29 |
< |
import uk.ac.ukc.iscream.filter.*; |
27 |
> |
import uk.org.iscream.cms.server.core.*; |
28 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
29 |
> |
import uk.org.iscream.cms.server.filter.*; |
30 |
> |
import uk.org.iscream.cms.util.*; |
31 |
|
|
32 |
< |
// This class contains the main method to be run by |
33 |
< |
// the filter children. It harvests UDP traffic. |
34 |
< |
// |
32 |
> |
/** |
33 |
> |
* This class contains the main method to be run by |
34 |
> |
* the filterd. It harvests UDP traffic, and queues it. |
35 |
> |
* |
36 |
> |
* @author $Author$ |
37 |
> |
* @version $Id$ |
38 |
> |
*/ |
39 |
|
public class UDPReader extends Thread{ |
40 |
|
|
41 |
< |
// It is normal to use this constructor in preference |
42 |
< |
// to any other in this class. |
43 |
< |
public UDPReader(int port, Filter parent, Logger logger){ |
44 |
< |
this.logger = logger; |
45 |
< |
this.port = port; |
46 |
< |
this.parent = parent; |
18 |
< |
} |
41 |
> |
//---FINAL ATTRIBUTES--- |
42 |
> |
|
43 |
> |
/** |
44 |
> |
* The current CVS revision of this class |
45 |
> |
*/ |
46 |
> |
public final String REVISION = "$Revision$"; |
47 |
|
|
48 |
< |
public UDPReader(Filter parent, Logger logger){ |
49 |
< |
this(4589, parent, logger); |
48 |
> |
/** |
49 |
> |
* The maximum size of a packet |
50 |
> |
*/ |
51 |
> |
private final int packetSizeLimit = 32768; |
52 |
> |
|
53 |
> |
//---STATIC METHODS--- |
54 |
> |
|
55 |
> |
//---CONSTRUCTORS--- |
56 |
> |
|
57 |
> |
/** |
58 |
> |
* Constructs a new UDPReader. |
59 |
> |
* |
60 |
> |
* @param port The port on which we listen for UDP data |
61 |
> |
* @param queue The queue which we are using |
62 |
> |
*/ |
63 |
> |
public UDPReader(int port, Queue queue){ |
64 |
> |
// set the Thread name |
65 |
> |
setName("filter.UDPReader"); |
66 |
> |
|
67 |
> |
_port = port; |
68 |
> |
_queue = queue; |
69 |
> |
_logger.write(toString(), Logger.SYSINIT, "started"); |
70 |
|
} |
71 |
|
|
72 |
+ |
//---PUBLIC METHODS--- |
73 |
+ |
|
74 |
+ |
/** |
75 |
+ |
* The main method in the class. Reads and queues XML sent |
76 |
+ |
* over UDP. |
77 |
+ |
*/ |
78 |
|
public void run() { |
79 |
< |
|
79 |
> |
// setup an empty ACL defaulting to ALLOW |
80 |
> |
ACL acl = new ACL(ACL.ALLOW); |
81 |
> |
|
82 |
> |
// setup a Datagram socket |
83 |
|
DatagramSocket socket = null; |
84 |
|
try { |
85 |
< |
socket = new DatagramSocket(port); |
85 |
> |
socket = new ACLDatagramSocket(acl, _port); |
86 |
|
} |
87 |
|
catch (BindException e){ |
88 |
< |
logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+port+" as this port was already in use."); |
88 |
> |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+" as this port was already in use."); |
89 |
|
return; |
90 |
|
} |
91 |
|
catch (Exception e){ |
92 |
< |
logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+port+"."); |
92 |
> |
_logger.write(this.toString(), Logger.FATAL, "Could not start the UDPReader thread on port "+_port+"."); |
93 |
|
return; |
94 |
|
} |
95 |
|
|
96 |
< |
logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+port); |
97 |
< |
|
96 |
> |
_logger.write(this.toString(), Logger.SYSMSG, "UDPReader thread ready and listening for UDP packets on port "+_port); |
97 |
> |
|
98 |
|
byte[] buf; |
99 |
|
|
100 |
+ |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
101 |
+ |
String stringACL = ""; |
102 |
+ |
String newStringACL = ""; |
103 |
+ |
|
104 |
+ |
// read UDP packets and queue them |
105 |
|
boolean running = true; |
106 |
|
while (running){ |
107 |
+ |
// get hold of the ACL in the configuration |
108 |
|
try { |
109 |
< |
|
110 |
< |
// receive request and pass on to the FilterThread. |
109 |
> |
newStringACL = cp.getProperty("Filter." + FilterMain.NAME, "Filter.UDPACL"); |
110 |
> |
} |
111 |
> |
catch(PropertyNotFoundException e) { |
112 |
> |
// if we can't find it, we'll just use a null ACL |
113 |
> |
newStringACL = ""; |
114 |
> |
_logger.write(toString(), Logger.WARNING, "No ACL found for UDPReader, using empty ACL instead: " + e); |
115 |
> |
} |
116 |
> |
// check to see if the ACL has changed |
117 |
> |
if(!newStringACL.equals(stringACL)) { |
118 |
> |
_logger.write(toString(), Logger.SYSMSG, "Reloading Access Control List"); |
119 |
> |
// clear the ACL |
120 |
> |
acl.clear(); |
121 |
> |
// set the default to something sane |
122 |
> |
acl.setDefaultMode(ACL.ALLOW); |
123 |
> |
// add the new ACL (this may change the default) |
124 |
> |
acl.add(newStringACL); |
125 |
> |
stringACL = newStringACL; |
126 |
> |
} |
127 |
> |
try { |
128 |
> |
|
129 |
> |
// receive request and put it in the Queue |
130 |
|
buf = new byte[packetSizeLimit]; |
131 |
|
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
132 |
|
socket.receive(packet); |
133 |
< |
FilterThread t = new FilterThread(parent, packet); |
134 |
< |
t.start(); |
53 |
< |
|
133 |
> |
String xml = new String(packet.getData()); |
134 |
> |
_queue.add(xml); |
135 |
|
} |
136 |
|
catch (IOException e) { |
137 |
< |
logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
138 |
< |
return; |
137 |
> |
_logger.write(this.toString(), Logger.WARNING, "This UDPReader thread has been shut down as an exception occured: "+e); |
138 |
> |
running = false; |
139 |
|
} |
140 |
|
} |
141 |
|
socket.close(); |
142 |
|
} |
143 |
|
|
144 |
< |
|
145 |
< |
Logger logger; |
146 |
< |
int port; |
147 |
< |
Filter parent; |
144 |
> |
/** |
145 |
> |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
146 |
> |
* method to provide clean logging (every class should have this). |
147 |
> |
* |
148 |
> |
* This uses the uk.org.iscream.cms.util.NameFormat class |
149 |
> |
* to format the toString() |
150 |
> |
* |
151 |
> |
* @return the name of this class and its CVS revision |
152 |
> |
*/ |
153 |
> |
public String toString() { |
154 |
> |
return FormatName.getName( |
155 |
> |
_name, |
156 |
> |
getClass().getName(), |
157 |
> |
REVISION); |
158 |
> |
} |
159 |
|
|
160 |
< |
final int packetSizeLimit = 8192; |
160 |
> |
//---PRIVATE METHODS--- |
161 |
> |
|
162 |
> |
//---ACCESSOR/MUTATOR METHODS--- |
163 |
> |
|
164 |
> |
//---ATTRIBUTES--- |
165 |
> |
|
166 |
> |
/** |
167 |
> |
* This is the friendly identifier of the |
168 |
> |
* component this class is running in. |
169 |
> |
* eg, a Filter may be called "filter1", |
170 |
> |
* If this class does not have an owning |
171 |
> |
* component, a name from the configuration |
172 |
> |
* can be placed here. This name could also |
173 |
> |
* be changed to null for utility classes. |
174 |
> |
*/ |
175 |
> |
private String _name = FilterMain.NAME; |
176 |
|
|
177 |
+ |
/** |
178 |
+ |
* This holds a reference to the |
179 |
+ |
* system logger that is being used. |
180 |
+ |
*/ |
181 |
+ |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
182 |
+ |
|
183 |
+ |
/** |
184 |
+ |
* The port that this reader is using |
185 |
+ |
*/ |
186 |
+ |
int _port; |
187 |
+ |
|
188 |
+ |
/** |
189 |
+ |
* The Queue object |
190 |
+ |
*/ |
191 |
+ |
Queue _queue; |
192 |
+ |
|
193 |
+ |
//---STATIC ATTRIBUTES--- |
194 |
+ |
|
195 |
|
} |