ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/filtermanager/HostInit.java
Revision: 1.41
Committed: Sun Sep 25 09:57:42 2005 UTC (18 years, 7 months ago) by tdb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.40: +29 -21 lines
Log Message:
Fix compile problems on j2se 1.5 - our Queue class conflicted with one in
java.util. Also fix an API issue when running the server on Windows - the
println method sends '\r\n' on Windows instead of '\n' on Unix, which
confuses applications such as ihost.

Patch provided by: skel

File Contents

# User Rev Content
1 tdb 1.30 /*
2     * i-scream central monitoring system
3 tdb 1.40 * http://www.i-scream.org
4 tdb 1.30 * 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 tdb 1.1 //---PACKAGE DECLARATION---
22 tdb 1.28 package uk.org.iscream.cms.server.filtermanager;
23 tdb 1.1
24     //---IMPORTS---
25 tdb 1.28 import uk.org.iscream.cms.server.core.*;
26     import uk.org.iscream.cms.server.filter.*;
27 tdb 1.34 import uk.org.iscream.cms.util.*;
28 tdb 1.28 import uk.org.iscream.cms.server.componentmanager.*;
29 tdb 1.18 import java.net.*;
30 tdb 1.1 import java.io.*;
31 tdb 1.22 import java.util.*;
32 tdb 1.1
33     /**
34 tdb 1.11 * Handles setting up a host.
35 ajm 1.12 * This class provides a host with appropriate configuration
36     * and a reference to a Filter to which it should pass data.
37 tdb 1.1 *
38 tdb 1.29 * @author $Author: tdb $
39 tdb 1.41 * @version $Id: HostInit.java,v 1.40 2004/08/01 10:41:05 tdb Exp $
40 tdb 1.1 */
41     class HostInit extends Thread {
42    
43     //---FINAL ATTRIBUTES---
44    
45     /**
46     * The current CVS revision of this class
47     */
48 tdb 1.41 public final String REVISION = "$Revision: 1.40 $";
49 tdb 1.1
50     //---STATIC METHODS---
51    
52     //---CONSTRUCTORS---
53    
54 tdb 1.15 /**
55     * Construct a new HostInit.
56     *
57     * @param socket The socket to which the host is connected
58     */
59 tdb 1.11 public HostInit(Socket socket) throws IOException {
60 tdb 1.25 // set the Thread name
61     setName("filtermanager.HostInit");
62    
63 tdb 1.1 _socket = socket;
64 tdb 1.15 // setup reader & writer
65 tdb 1.1 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
66 tdb 1.16 _socketOut = new PrintWriter(_socket.getOutputStream(), true);
67 tdb 1.11 _logger.write(toString(), Logger.SYSINIT, "created");
68 tdb 1.1 }
69    
70     //---PUBLIC METHODS---
71    
72 tdb 1.15 /**
73     * Main method in this class, which handles communicating with
74     * the host to determine it's setup.
75     */
76 tdb 1.1 public void run() {
77 tdb 1.26 // get a hook on the config proxy
78     ConfigurationProxy cp = ConfigurationProxy.getInstance();
79     // this is our config name
80     String configName = "Host." + _socket.getInetAddress().getHostName().toLowerCase();
81 tdb 1.1 try {
82 tdb 1.35 // look for a command:
83     // STARTCONFIG - to get a configuration
84     // CHECKCONFIG - to check a configuration
85     // END - to finish
86     String cmd = getInBound();
87     while(!cmd.equals("END")) {
88     if(cmd.equals("STARTCONFIG")) {
89     // respond to STARTCONFIG
90 tdb 1.41 send("OK");
91 tdb 1.35
92     // try for LASTMODIFIED
93     getInBound("LASTMODIFIED");
94 tdb 1.41 send("" + cp.getLastModified(configName));
95 tdb 1.35
96     // try for FILELIST
97     getInBound("FILELIST");
98 tdb 1.41 send(cp.getFileList(configName));
99 tdb 1.35
100     // try for FQDN
101     getInBound("FQDN");
102 tdb 1.41 send(_socket.getInetAddress().getHostName().toLowerCase());
103 tdb 1.39
104     // try for IP
105     getInBound("IP");
106 tdb 1.41 send(_socket.getInetAddress().getHostAddress());
107 tdb 1.35
108     // get properties
109     String reqProperty = getInBound();
110     while(!reqProperty.equals("ENDCONFIG")) {
111     // get the property
112     try {
113     String returnedProperty = cp.getProperty(configName, "Host."+reqProperty);
114 tdb 1.41 send(returnedProperty);
115 tdb 1.35 } catch (PropertyNotFoundException e) {
116 tdb 1.41 send("ERROR");
117 tdb 1.35 }
118     // get the next request
119     reqProperty = _socketIn.readLine();
120     }
121     _logger.write(toString(), Logger.SYSMSG, "configured host");
122 tdb 1.41 send("OK");
123 tdb 1.35
124     // get filter reference
125     getInBound("FILTER");
126     // send info
127     String filterList = cp.getProperty(configName, "Host.filter");
128     FilterInfo filterInfoRef = null;
129     String filter = null;
130     StringTokenizer st = new StringTokenizer(filterList, ";");
131     while (filterInfoRef==null && st.hasMoreTokens()) {
132     filter = st.nextToken();
133     _logger.write(toString(), Logger.DEBUG, " looking for filter- " + filter);
134     try {
135     filterInfoRef = FilterInfoHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.FilterInfo\\." + filter));
136     } catch (ComponentCORBAException e) {
137     _logger.write(toString(), Logger.DEBUG, " unable to find filter- " + filter);
138     }
139     }
140    
141     // hopefully we found a filter
142     if(filterInfoRef != null) {
143     _logger.write(toString(), Logger.DEBUG, " found filter- " + filter);
144     try {
145     // tell the host about it...
146 tdb 1.41 send(filterInfoRef.getHostName() + ";"
147 tdb 1.35 + filterInfoRef.getUDPPort());
148     }
149     catch(org.omg.CORBA.COMM_FAILURE e) {
150     // failed to talk to filter, lets signal an error
151 tdb 1.41 send("ERROR");
152 tdb 1.35 throw new IOException("error communicating with filter - " + e);
153     }
154     }
155     else {
156     // ...or throw a wobbly (and tell the host!)
157 tdb 1.41 send("ERROR");
158 tdb 1.35 throw new IOException("unable to find filter for host");
159     }
160    
161     }
162     else if(cmd.equals("CHECKCONFIG")) {
163     // respond to CHECKCONFIG
164 tdb 1.41 send("OK");
165 tdb 1.35
166     // try for {filelist}
167     String filelist = getInBound();
168 tdb 1.41 send("OK");
169 tdb 1.35
170     // try for {lastModified}
171     String lastModified = getInBound();
172 tdb 1.37 long lastmod;
173     try {
174     lastmod = Long.parseLong(lastModified);
175     }
176     catch(NumberFormatException e) {
177 tdb 1.41 send("ERROR");
178 tdb 1.37 throw new IOException("Last Modified invalid: " + e);
179     }
180    
181 tdb 1.35 // check to see if a config update has happen
182 tdb 1.37 boolean newConfig = _configManager.isModified(filelist, lastmod);
183 tdb 1.35 if(newConfig) {
184     // new config !
185 tdb 1.41 send("EXPIRED");
186 tdb 1.35 }
187     else {
188     // nothing has changed
189 tdb 1.41 send("OK");
190 tdb 1.35 }
191     }
192     else {
193 tdb 1.41 send("ERROR");
194 tdb 1.4 }
195 tdb 1.35 // get the next command
196     cmd = getInBound();
197 tdb 1.1 }
198 tdb 1.35 // respond to END
199 tdb 1.41 send("OK");
200 tdb 1.1
201     } catch (Exception e) {
202 tdb 1.17 _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
203 tdb 1.1 }
204    
205     // Disconnect streams & socket
206     try {
207     _socketIn.close();
208     _socketOut.close();
209     _socket.close();
210     } catch (IOException e) {
211 tdb 1.11 _logger.write(toString(), Logger.ERROR, "exception on socket close");
212 tdb 1.1 }
213 tdb 1.11 _logger.write(toString(), Logger.SYSMSG, "finished");
214 tdb 1.1 }
215    
216     /**
217     * Overrides the {@link java.lang.Object#toString() Object.toString()}
218     * method to provide clean logging (every class should have this).
219     *
220 tdb 1.34 * This uses the uk.org.iscream.cms.util.NameFormat class
221 ajm 1.12 * to format the toString()
222     *
223 tdb 1.1 * @return the name of this class and its CVS revision
224     */
225     public String toString() {
226 ajm 1.12 return FormatName.getName(
227     _name,
228 ajm 1.13 getClass().getName(),
229 ajm 1.12 REVISION);
230 tdb 1.1 }
231    
232     //---PRIVATE METHODS---
233 tdb 1.26
234     private String getInBound(String expected) throws IOException {
235     // grab the input
236     String inBound = getInBound();
237     // check if it's what we're expecting
238     if(!inBound.equals(expected)) {
239 tdb 1.41 send("ERROR");
240 tdb 1.26 throw new IOException("protocol error - expected:"+expected+" got:" + inBound);
241     }
242     // it should be ok then
243     return inBound;
244     }
245    
246     private String getInBound() throws IOException {
247     // grab the input
248     String inBound = _socketIn.readLine();
249     // check for null's, likely disconnection
250     if(inBound == null) {
251     throw new IOException("got null from host, maybe it died");
252     }
253     // it's a valid message it seems
254     return inBound;
255     }
256 tdb 1.41
257     /* Use this instead of println. println under windows terminates the lines
258     * in \c\n which is not the expected result for the ihost client.
259     */
260     private void send(String data) {
261     _socketOut.print(data + "\n");
262     _socketOut.flush();
263     }
264 tdb 1.1
265     //---ACCESSOR/MUTATOR METHODS---
266    
267     //---ATTRIBUTES---
268    
269 tdb 1.11 /**
270 ajm 1.12 * This holds a reference to the
271     * system logger that is being used.
272 tdb 1.11 */
273 ajm 1.12 private Logger _logger = ReferenceManager.getInstance().getLogger();
274 tdb 1.11
275     /**
276     * A reference to the Configuration Manager the system is using
277     */
278 ajm 1.12 private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
279    
280     /**
281     * This is the friendly identifier of the
282     * component this class is running in.
283     * eg, a Filter may be called "filter1",
284     * If this class does not have an owning
285     * component, a name from the configuration
286     * can be placed here. This name could also
287     * be changed to null for utility classes.
288     */
289     private String _name = FilterManager.NAME;
290    
291     /**
292     * The socket this class uses
293     */
294     private Socket _socket;
295    
296     /**
297     * Used for the input stream of this socket
298     */
299     private BufferedReader _socketIn;
300 tdb 1.11
301 ajm 1.12 /**
302     * Used for the output stream of this socket
303     */
304     private PrintWriter _socketOut;
305 tdb 1.11
306 tdb 1.1 //---STATIC ATTRIBUTES---
307    
308     }