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.24 by tdb, Mon Mar 5 23:53:39 2001 UTC vs.
Revision 1.41 by tdb, Sun Sep 25 09:57:42 2005 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.ac.ukc.iscream.filtermanager;
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 uk.ac.ukc.iscream.util.*;
28 < import uk.ac.ukc.iscream.componentmanager.*;
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 java.util.*;
# Line 37 | Line 57 | class HostInit extends Thread {
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          // setup reader & writer
65          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
# Line 51 | Line 74 | class HostInit extends Thread {
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 <                throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound);
86 <            }
87 <
88 <            Configuration myConfig = _configManager.getConfiguration("Host." + _socket.getInetAddress().getHostName().toLowerCase());
89 <            if (myConfig == null) {
90 <                _socketOut.println("ERROR");
91 <                throw new IOException("error in host configuration");
92 <            } else {
93 <                _socketOut.println("OK");
94 <                
95 <                // get lastmodified
96 <                inBound = _socketIn.readLine();
97 <                if(!inBound.equals("LASTMODIFIED")) {
98 <                        // protocol error
99 <                    _socketOut.println("ERROR");
100 <                    throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound);
101 <                }
102 <                else {
103 <                        // send info
104 <                        _socketOut.println(myConfig.getLastModified());
105 <                }
106 <                
107 <                // get config fileList
108 <                inBound = _socketIn.readLine();
109 <                if(!inBound.equals("FILELIST")) {
110 <                        // protocol error
111 <                    _socketOut.println("ERROR");
112 <                    throw new IOException("protocol error - expected:FILELIST got:" + inBound);
113 <                }
114 <                else {
115 <                        // send info
116 <                        _socketOut.println(myConfig.getFileList());
117 <                }
118 <                
119 <                // send the FQDN to the host
93 <                inBound = _socketIn.readLine();
94 <                if(!inBound.equals("FQDN")) {
95 <                        // protocol error
96 <                    _socketOut.println("ERROR");
97 <                    throw new IOException("protocol error - expected:FQDN got:" + inBound);
98 <                }
99 <                else {
100 <                    // send the fqdn (of the host) to the host
101 <                    _socketOut.println(_socket.getInetAddress().getHostName().toLowerCase());
102 <                }
103 <                
104 <                // get properties
105 <                inBound = _socketIn.readLine();
106 <                while(!inBound.equals("ENDCONFIG")) {
107 <                          
108 <                    // get the property
109 <                    try {
110 <                        String returnedProperty = myConfig.getProperty("Host."+inBound);    
111 <                        
112 <                        _socketOut.println(returnedProperty);
113 <    
114 <                    } catch (org.omg.CORBA.MARSHAL e) {
115 <                        _socketOut.println("ERROR");
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 >                    send("OK");
91 >                    
92 >                    // try for LASTMODIFIED
93 >                    getInBound("LASTMODIFIED");
94 >                    send("" + cp.getLastModified(configName));
95 >                    
96 >                    // try for FILELIST
97 >                    getInBound("FILELIST");
98 >                    send(cp.getFileList(configName));
99 >                    
100 >                    // try for FQDN
101 >                    getInBound("FQDN");
102 >                    send(_socket.getInetAddress().getHostName().toLowerCase());
103 >                    
104 >                    // try for IP
105 >                    getInBound("IP");
106 >                    send(_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 >                            send(returnedProperty);
115 >                        } catch (PropertyNotFoundException e) {
116 >                            send("ERROR");
117 >                        }
118 >                        // get the next request
119 >                        reqProperty = _socketIn.readLine();
120                      }
121 <                    inBound = _socketIn.readLine();
122 <                }
123 <                _logger.write(toString(), Logger.SYSMSG, "configured host");
124 <                _socketOut.println("OK");
125 <                
126 <                // get filter reference
127 <                inBound = _socketIn.readLine();
128 <                if(!inBound.equals("FILTER")) {
129 <                        // protocol error
130 <                    _socketOut.println("ERROR");
131 <                    throw new IOException("protocol error - expected:FILTER got:" + inBound);
128 <                }
129 <                else {
130 <                        // send info
131 <                        String filterList = myConfig.getProperty("Host.filter");
132 <                        Filter filterRef = null;
133 <                        String filter = null;
134 <                        StringTokenizer st = new StringTokenizer(filterList, ";");
135 <                    while (filterRef==null && st.hasMoreTokens()) {
121 >                    _logger.write(toString(), Logger.SYSMSG, "configured host");
122 >                    send("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 <                            filterRef = FilterHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.Filter." + filter));
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(filterRef != null) {
140 >                    
141 >                    // hopefully we found a filter
142 >                    if(filterInfoRef != null) {
143                          _logger.write(toString(), Logger.DEBUG, " found filter- " + filter);
144 <                        // tell the host about it...
145 <                            _socketOut.println(filterRef.getHostName() + ";"
146 <                                             + filterRef.getUDPPort() + ";"
147 <                                             + filterRef.getTCPPort());
148 <                        }
149 <                        else {
150 <                            // ...or throw a wobbly (and tell the host!)
151 <                            _socketOut.println("ERROR");
152 <                            throw new IOException("unable to find filter for host");
153 <                        }
144 >                        try {
145 >                            // tell the host about it...
146 >                            send(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 >                            send("ERROR");
152 >                            throw new IOException("error communicating with filter - " + e);
153 >                        }
154 >                    }
155 >                    else {
156 >                        // ...or throw a wobbly (and tell the host!)
157 >                        send("ERROR");
158 >                        throw new IOException("unable to find filter for host");
159 >                    }
160 >                    
161                  }
162 <                
163 <                // confirm that all is ok
164 <                inBound = _socketIn.readLine();
165 <                if(!inBound.equals("END")) {
166 <                        // protocol error
167 <                    _socketOut.println("ERROR");
168 <                    throw new IOException("protocol error - expected:END got:" + inBound);
162 >                else if(cmd.equals("CHECKCONFIG")) {
163 >                    // respond to CHECKCONFIG
164 >                    send("OK");
165 >                    
166 >                    // try for {filelist}
167 >                    String filelist = getInBound();
168 >                    send("OK");
169 >                    
170 >                    // try for {lastModified}
171 >                    String lastModified = getInBound();
172 >                    long lastmod;
173 >                    try {
174 >                        lastmod = Long.parseLong(lastModified);
175 >                    }
176 >                    catch(NumberFormatException e) {
177 >                        send("ERROR");
178 >                        throw new IOException("Last Modified invalid: " + e);
179 >                    }
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 >                        send("EXPIRED");
186 >                    }
187 >                    else {
188 >                        // nothing has changed
189 >                        send("OK");
190 >                    }
191                  }
192                  else {
193 <                        // send ok
169 <                        _socketOut.println("OK");
193 >                    send("ERROR");
194                  }
195 <
195 >                // get the next command
196 >                cmd = getInBound();
197              }
198 +            // respond to END
199 +            send("OK");
200              
201          } catch (Exception e) {
202              _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
203          }
204          
178        _socketOut.flush();
205          // Disconnect streams & socket
206          try {
207              _socketIn.close();
# Line 191 | Line 217 | class HostInit extends Thread {
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.ac.ukc.iscream.util.NameFormat class
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
# Line 204 | Line 230 | class HostInit extends Thread {
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 +            send("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 +        /* 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  
265   //---ACCESSOR/MUTATOR METHODS---
266  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines