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
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/filtermanager/HostInit.java (file contents):
Revision 1.7 by tdb, Thu Nov 23 01:46:17 2000 UTC vs.
Revision 1.40 by tdb, Sun Aug 1 10:41:05 2004 UTC

# Line 1 | Line 1
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.filtermanager;
23  
24   //---IMPORTS---
25 < import uk.ac.ukc.iscream.core.*;
26 < import uk.ac.ukc.iscream.filter.*;
27 < import java.net.Socket;
28 < import java.io.InputStream;
29 < import java.io.OutputStream;
9 < import java.io.IOException;
25 > import uk.org.iscream.cms.server.core.*;
26 > import uk.org.iscream.cms.server.filter.*;
27 > import uk.org.iscream.cms.util.*;
28 > import uk.org.iscream.cms.server.componentmanager.*;
29 > import java.net.*;
30   import java.io.*;
31 < import org.omg.CORBA.*;
12 < import org.omg.CosNaming.*;
31 > import java.util.*;
32  
33   /**
34 < * <ONE LINE DESCRIPTION>
35 < * <DETAILED DESCRIPTION>
34 > * Handles setting up a host.
35 > * This class provides a host with appropriate configuration
36 > * and a reference to a Filter to which it should pass data.
37   *
38   * @author  $Author$
39   * @version $Id$
# Line 31 | Line 51 | class HostInit extends Thread {
51  
52   //---CONSTRUCTORS---
53  
54 <    public HostInit(Socket socket, ConfigurationManager configManager, Logger logRef, NamingContextExt ncRef) throws IOException {
55 <        _configManager = configManager;
56 <        _logRef = logRef;
54 >    /**
55 >     * Construct a new HostInit.
56 >     *
57 >     * @param socket The socket to which the host is connected
58 >     */
59 >    public HostInit(Socket socket) throws IOException {
60 >        // set the Thread name
61 >        setName("filtermanager.HostInit");
62 >        
63          _socket = socket;
64 <        _ncRef = ncRef;
64 >        // setup reader & writer
65          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
66 <        _socketOut = new PrintWriter(_socket.getOutputStream());
67 <        _logRef.write(toString(), Logger.SYSINIT, "created");
66 >        _socketOut = new PrintWriter(_socket.getOutputStream(), true);
67 >        _logger.write(toString(), Logger.SYSINIT, "created");
68      }
69      
70   //---PUBLIC METHODS---
71  
72 +    /**
73 +     * Main method in this class, which handles communicating with
74 +     * the host to determine it's setup.
75 +     */
76      public void run() {
77 +        // 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          try {
82 <            String inBound = _socketIn.readLine();
83 <            if (!inBound.equals("STARTCONFIG")) {
84 <                _socketOut.println("ERROR");
85 <                _socketOut.flush();
86 <                throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound);
87 <            }
88 <
89 <            Configuration myConfig = _configManager.getConfiguration(_socket.getInetAddress().getHostName().toLowerCase());
90 <            if (myConfig == null) {
91 <                _socketOut.println("ERROR");
92 <                throw new IOException("error in host configuration");
93 <            } else {
94 <                _socketOut.println("OK");
95 <                _socketOut.flush();
96 <                
97 <                // get lastmodified
98 <                inBound = _socketIn.readLine();
99 <                if(!inBound.equals("LASTMODIFIED")) {
100 <                        // protocol error
101 <                    _socketOut.println("ERROR");
102 <                    _socketOut.flush();
103 <                    throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound);
82 >            // 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 >                    _socketOut.println("OK");
91 >                    
92 >                    // try for LASTMODIFIED
93 >                    getInBound("LASTMODIFIED");
94 >                    _socketOut.println(cp.getLastModified(configName));
95 >                    
96 >                    // try for FILELIST
97 >                    getInBound("FILELIST");
98 >                    _socketOut.println(cp.getFileList(configName));
99 >                    
100 >                    // try for FQDN
101 >                    getInBound("FQDN");
102 >                    _socketOut.println(_socket.getInetAddress().getHostName().toLowerCase());
103 >                    
104 >                    // try for IP
105 >                    getInBound("IP");
106 >                    _socketOut.println(_socket.getInetAddress().getHostAddress());
107 >                    
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 >                            _socketOut.println(returnedProperty);
115 >                        } catch (PropertyNotFoundException e) {
116 >                            _socketOut.println("ERROR");
117 >                        }
118 >                        // get the next request
119 >                        reqProperty = _socketIn.readLine();
120 >                    }
121 >                    _logger.write(toString(), Logger.SYSMSG, "configured host");
122 >                    _socketOut.println("OK");
123 >                    
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 >                            _socketOut.println(filterInfoRef.getHostName() + ";"
147 >                            + filterInfoRef.getUDPPort());
148 >                        }
149 >                        catch(org.omg.CORBA.COMM_FAILURE e) {
150 >                            // failed to talk to filter, lets signal an error
151 >                            _socketOut.println("ERROR");
152 >                            throw new IOException("error communicating with filter - " + e);
153 >                        }
154 >                    }
155 >                    else {
156 >                        // ...or throw a wobbly (and tell the host!)
157 >                        _socketOut.println("ERROR");
158 >                        throw new IOException("unable to find filter for host");
159 >                    }
160 >                    
161                  }
162 <                else {
163 <                        // send info
164 <                        _socketOut.println(myConfig.getLastModified());
165 <                        _socketOut.flush();
166 <                }
167 <                
168 <                // get config fileList
169 <                inBound = _socketIn.readLine();
170 <                if(!inBound.equals("FILELIST")) {
171 <                        // protocol error
172 <                    _socketOut.println("ERROR");
82 <                    _socketOut.flush();
83 <                    throw new IOException("protocol error - expected:FILELIST got:" + inBound);
84 <                }
85 <                else {
86 <                        // send info
87 <                        _socketOut.println(myConfig.getFileList());
88 <                        _socketOut.flush();
89 <                }
90 <
91 <                // get properties
92 <                inBound = _socketIn.readLine();
93 <                while(!inBound.equals("ENDCONFIG")) {
94 <                          
95 <                    // get the property
162 >                else if(cmd.equals("CHECKCONFIG")) {
163 >                    // respond to CHECKCONFIG
164 >                    _socketOut.println("OK");
165 >                    
166 >                    // try for {filelist}
167 >                    String filelist = getInBound();
168 >                    _socketOut.println("OK");
169 >                    
170 >                    // try for {lastModified}
171 >                    String lastModified = getInBound();
172 >                    long lastmod;
173                      try {
174 <                        String returnedProperty = myConfig.getProperty(inBound);    
175 <                        
176 <                        _socketOut.println(returnedProperty);
100 <                        _socketOut.flush();
101 <    
102 <                    } catch (org.omg.CORBA.MARSHAL e) {
174 >                        lastmod = Long.parseLong(lastModified);
175 >                    }
176 >                    catch(NumberFormatException e) {
177                          _socketOut.println("ERROR");
178 <                        _socketOut.flush();
178 >                        throw new IOException("Last Modified invalid: " + e);
179                      }
180 <                    inBound = _socketIn.readLine();
180 >                    
181 >                    // check to see if a config update has happen
182 >                    boolean newConfig = _configManager.isModified(filelist, lastmod);
183 >                    if(newConfig) {
184 >                        // new config !
185 >                        _socketOut.println("EXPIRED");
186 >                    }
187 >                    else {
188 >                        // nothing has changed
189 >                        _socketOut.println("OK");
190 >                    }
191                  }
108                _logRef.write(toString(), Logger.SYSMSG, "configured host");
109                _socketOut.println("OK");
110                _socketOut.flush();
111                
112                // get filter reference
113                inBound = _socketIn.readLine();
114                if(!inBound.equals("FILTER")) {
115                        // protocol error
116                    _socketOut.println("ERROR");
117                    _socketOut.flush();
118                    throw new IOException("protocol error - expected:FILTER got:" + inBound);
119                }
192                  else {
121                        // send info
122                        String parentFilter =  myConfig.getProperty("Host.filter");
123                        _logRef.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter);
124                        org.omg.CORBA.Object objRef = _ncRef.resolve(_ncRef.to_name("iscream.Filter." + parentFilter));
125                    Filter filter = FilterHelper.narrow(objRef);
126                        _socketOut.println(filter.getHostName() + ";"
127                                         + filter.getUDPPort() + ";"
128                                         + filter.getTCPPort());
129                        _socketOut.flush();
130                }
131                
132                // confirm that all is ok
133                inBound = _socketIn.readLine();
134                if(!inBound.equals("END")) {
135                        // protocol error
193                      _socketOut.println("ERROR");
137                    _socketOut.flush();
138                    throw new IOException("protocol error - expected:END got:" + inBound);
194                  }
195 <                else {
196 <                        // send ok
142 <                        _socketOut.println("OK");
143 <                        _socketOut.flush();
144 <                }
145 <
195 >                // get the next command
196 >                cmd = getInBound();
197              }
198 +            // respond to END
199 +            _socketOut.println("OK");
200              
201          } catch (Exception e) {
202 <            _logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage());
202 >            _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
203          }
204          
152        _socketOut.flush();
205          // Disconnect streams & socket
206          try {
207              _socketIn.close();
208              _socketOut.close();
209              _socket.close();
210          } catch (IOException e) {
211 <            _logRef.write(toString(), Logger.ERROR, "exception on socket close");
211 >            _logger.write(toString(), Logger.ERROR, "exception on socket close");
212          }
213 <        _logRef.write(toString(), Logger.SYSMSG, "finished");
213 >        _logger.write(toString(), Logger.SYSMSG, "finished");
214      }
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 +     * This uses the uk.org.iscream.cms.util.NameFormat class
221 +     * to format the toString()
222 +     *
223       * @return the name of this class and its CVS revision
224       */
225      public String toString() {
226 <        return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName()
227 <         + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")";
228 <        
226 >        return FormatName.getName(
227 >            _name,
228 >            getClass().getName(),
229 >            REVISION);
230      }
231  
232   //---PRIVATE METHODS---
233  
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 +            _socketOut.println("ERROR");
240 +            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 +
257   //---ACCESSOR/MUTATOR METHODS---
258  
259   //---ATTRIBUTES---
260  
261 <    ConfigurationManager _configManager;
262 <    Logger _logRef;
263 <    Socket _socket;
264 <    BufferedReader _socketIn;
265 <    PrintWriter _socketOut;
266 <    NamingContextExt _ncRef;
261 >    /**
262 >     * This holds a reference to the
263 >     * system logger that is being used.
264 >     */
265 >    private Logger _logger = ReferenceManager.getInstance().getLogger();
266 >    
267 >    /**
268 >     * A reference to the Configuration Manager the system is using
269 >     */
270 >    private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
271 >    
272 >    /**
273 >     * This is the friendly identifier of the
274 >     * component this class is running in.
275 >     * eg, a Filter may be called "filter1",
276 >     * If this class does not have an owning
277 >     * component,  a name from the configuration
278 >     * can be placed here.  This name could also
279 >     * be changed to null for utility classes.
280 >     */
281 >    private String _name = FilterManager.NAME;
282 >    
283 >    /**
284 >     * The socket this class uses
285 >     */
286 >    private Socket _socket;
287 >
288 >    /**
289 >     * Used for the input stream of this socket
290 >     */
291 >    private BufferedReader _socketIn;
292 >    
293 >    /**
294 >     * Used for the output stream of this socket
295 >     */
296 >    private PrintWriter _socketOut;
297 >
298   //---STATIC ATTRIBUTES---
299  
300   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines