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.30 by tdb, Sat May 18 18:16:02 2002 UTC

# Line 1 | Line 1
1 + /*
2 + * i-scream central monitoring system
3 + * Copyright (C) 2000-2002 i-scream
4 + *
5 + * This program is free software; you can redistribute it and/or
6 + * modify it under the terms of the GNU General Public License
7 + * as published by the Free Software Foundation; either version 2
8 + * of the License, or (at your option) any later version.
9 + *
10 + * This program is distributed in the hope that it will be useful,
11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 + * GNU General Public License for more details.
14 + *
15 + * You should have received a copy of the GNU General Public License
16 + * along with this program; if not, write to the Free Software
17 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 + */
19 +
20   //---PACKAGE DECLARATION---
21 + package uk.org.iscream.cms.server.filtermanager;
22  
23   //---IMPORTS---
24 < import uk.ac.ukc.iscream.core.*;
25 < import uk.ac.ukc.iscream.filter.*;
26 < import java.net.Socket;
27 < import java.io.InputStream;
28 < import java.io.OutputStream;
9 < import java.io.IOException;
24 > import uk.org.iscream.cms.server.core.*;
25 > import uk.org.iscream.cms.server.filter.*;
26 > import uk.org.iscream.cms.server.util.*;
27 > import uk.org.iscream.cms.server.componentmanager.*;
28 > import java.net.*;
29   import java.io.*;
30 < import org.omg.CORBA.*;
12 < import org.omg.CosNaming.*;
30 > import java.util.*;
31  
32   /**
33 < * <ONE LINE DESCRIPTION>
34 < * <DETAILED DESCRIPTION>
33 > * Handles setting up a host.
34 > * This class provides a host with appropriate configuration
35 > * and a reference to a Filter to which it should pass data.
36   *
37   * @author  $Author$
38   * @version $Id$
# Line 31 | Line 50 | class HostInit extends Thread {
50  
51   //---CONSTRUCTORS---
52  
53 <    public HostInit(Socket socket, ConfigurationManager configManager, Logger logRef, NamingContextExt ncRef) throws IOException {
54 <        _configManager = configManager;
55 <        _logRef = logRef;
53 >    /**
54 >     * Construct a new HostInit.
55 >     *
56 >     * @param socket The socket to which the host is connected
57 >     */
58 >    public HostInit(Socket socket) throws IOException {
59 >        // set the Thread name
60 >        setName("filtermanager.HostInit");
61 >        
62          _socket = socket;
63 <        _ncRef = ncRef;
63 >        // setup reader & writer
64          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
65 <        _socketOut = new PrintWriter(_socket.getOutputStream());
66 <        _logRef.write(toString(), Logger.SYSINIT, "created");
65 >        _socketOut = new PrintWriter(_socket.getOutputStream(), true);
66 >        _logger.write(toString(), Logger.SYSINIT, "created");
67      }
68      
69   //---PUBLIC METHODS---
70  
71 +    /**
72 +     * Main method in this class, which handles communicating with
73 +     * the host to determine it's setup.
74 +     */
75      public void run() {
76 +        // get a hook on the config proxy
77 +        ConfigurationProxy cp = ConfigurationProxy.getInstance();
78 +        // this is our config name
79 +        String configName = "Host." + _socket.getInetAddress().getHostName().toLowerCase();
80          try {
81 <            String inBound = _socketIn.readLine();
82 <            if (!inBound.equals("STARTCONFIG")) {
83 <                _socketOut.println("ERROR");
84 <                _socketOut.flush();
85 <                throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound);
86 <            }
87 <
88 <            Configuration myConfig = _configManager.getConfiguration(_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 <                _socketOut.flush();
95 <                
96 <                // get lastmodified
97 <                inBound = _socketIn.readLine();
98 <                if(!inBound.equals("LASTMODIFIED")) {
99 <                        // protocol error
81 >            // try for STARTCONFIG
82 >            getInBound("STARTCONFIG");
83 >            _socketOut.println("OK");
84 >            
85 >            // try for LASTMODIFIED
86 >            getInBound("LASTMODIFIED");
87 >            _socketOut.println(cp.getLastModified(configName));
88 >            
89 >            // try for FILELIST
90 >            getInBound("FILELIST");
91 >            _socketOut.println(cp.getFileList(configName));
92 >            
93 >            // try for FQDN
94 >            getInBound("FQDN");
95 >            _socketOut.println(_socket.getInetAddress().getHostName().toLowerCase());
96 >            
97 >            // get properties
98 >            String reqProperty = getInBound();
99 >            while(!reqProperty.equals("ENDCONFIG")) {
100 >                // get the property
101 >                try {
102 >                    String returnedProperty = cp.getProperty(configName, "Host."+reqProperty);
103 >                    _socketOut.println(returnedProperty);
104 >                } catch (PropertyNotFoundException e) {
105                      _socketOut.println("ERROR");
68                    _socketOut.flush();
69                    throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound);
106                  }
107 <                else {
108 <                        // send info
109 <                        _socketOut.println(myConfig.getLastModified());
110 <                        _socketOut.flush();
107 >                // get the next request
108 >                reqProperty = _socketIn.readLine();
109 >            }
110 >            _logger.write(toString(), Logger.SYSMSG, "configured host");
111 >            _socketOut.println("OK");
112 >            
113 >            // get filter reference
114 >            getInBound("FILTER");
115 >                // send info
116 >                String filterList = cp.getProperty(configName, "Host.filter");
117 >                FilterInfo filterInfoRef = null;
118 >                String filter = null;
119 >                StringTokenizer st = new StringTokenizer(filterList, ";");
120 >            while (filterInfoRef==null && st.hasMoreTokens()) {
121 >                filter = st.nextToken();
122 >                _logger.write(toString(), Logger.DEBUG, " looking for filter- " + filter);
123 >                try {
124 >                    filterInfoRef = FilterInfoHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.FilterInfo." + filter));
125 >                } catch (ComponentCORBAException e) {
126 >                    _logger.write(toString(), Logger.DEBUG, " unable to find filter- " + filter);
127                  }
76                
77                // get config fileList
78                inBound = _socketIn.readLine();
79                if(!inBound.equals("FILELIST")) {
80                        // protocol error
81                    _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
96                    try {
97                        String returnedProperty = myConfig.getProperty(inBound);    
98                        
99                        _socketOut.println(returnedProperty);
100                        _socketOut.flush();
101    
102                    } catch (org.omg.CORBA.MARSHAL e) {
103                        _socketOut.println("ERROR");
104                        _socketOut.flush();
105                    }
106                    inBound = _socketIn.readLine();
107                }
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                }
120                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
136                    _socketOut.println("ERROR");
137                    _socketOut.flush();
138                    throw new IOException("protocol error - expected:END got:" + inBound);
139                }
140                else {
141                        // send ok
142                        _socketOut.println("OK");
143                        _socketOut.flush();
144                }
145
128              }
129 +                
130 +                // hopefully we found a filter
131 +            if(filterInfoRef != null) {
132 +                _logger.write(toString(), Logger.DEBUG, " found filter- " + filter);
133 +                // tell the host about it...
134 +                    _socketOut.println(filterInfoRef.getHostName() + ";"
135 +                                     + filterInfoRef.getUDPPort() + ";"
136 +                                     + filterInfoRef.getTCPPort());
137 +                }
138 +                else {
139 +                    // ...or throw a wobbly (and tell the host!)
140 +                    _socketOut.println("ERROR");
141 +                    throw new IOException("unable to find filter for host");
142 +                }
143              
144 +            // confirm ok with END
145 +            getInBound("END");
146 +            _socketOut.println("OK");
147 +            
148          } catch (Exception e) {
149 <            _logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage());
149 >            _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
150          }
151          
152        _socketOut.flush();
152          // Disconnect streams & socket
153          try {
154              _socketIn.close();
155              _socketOut.close();
156              _socket.close();
157          } catch (IOException e) {
158 <            _logRef.write(toString(), Logger.ERROR, "exception on socket close");
158 >            _logger.write(toString(), Logger.ERROR, "exception on socket close");
159          }
160 <        _logRef.write(toString(), Logger.SYSMSG, "finished");
160 >        _logger.write(toString(), Logger.SYSMSG, "finished");
161      }
162      
163      /**
164       * Overrides the {@link java.lang.Object#toString() Object.toString()}
165       * method to provide clean logging (every class should have this).
166       *
167 +     * This uses the uk.org.iscream.cms.server.util.NameFormat class
168 +     * to format the toString()
169 +     *
170       * @return the name of this class and its CVS revision
171       */
172      public String toString() {
173 <        return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName()
174 <         + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")";
175 <        
173 >        return FormatName.getName(
174 >            _name,
175 >            getClass().getName(),
176 >            REVISION);
177      }
178  
179   //---PRIVATE METHODS---
180  
181 +    private String getInBound(String expected) throws IOException {
182 +        // grab the input
183 +        String inBound = getInBound();
184 +        // check if it's what we're expecting
185 +        if(!inBound.equals(expected)) {
186 +            throw new IOException("protocol error - expected:"+expected+" got:" + inBound);
187 +        }
188 +        // it should be ok then
189 +        return inBound;
190 +    }
191 +    
192 +    private String getInBound() throws IOException {
193 +        // grab the input
194 +        String inBound = _socketIn.readLine();
195 +        // check for null's, likely disconnection
196 +        if(inBound == null) {
197 +            throw new IOException("got null from host, maybe it died");
198 +        }
199 +        // it's a valid message it seems
200 +        return inBound;
201 +    }
202 +
203   //---ACCESSOR/MUTATOR METHODS---
204  
205   //---ATTRIBUTES---
206  
207 <    ConfigurationManager _configManager;
208 <    Logger _logRef;
209 <    Socket _socket;
210 <    BufferedReader _socketIn;
211 <    PrintWriter _socketOut;
212 <    NamingContextExt _ncRef;
207 >    /**
208 >     * This holds a reference to the
209 >     * system logger that is being used.
210 >     */
211 >    private Logger _logger = ReferenceManager.getInstance().getLogger();
212 >    
213 >    /**
214 >     * A reference to the Configuration Manager the system is using
215 >     */
216 >    private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
217 >    
218 >    /**
219 >     * This is the friendly identifier of the
220 >     * component this class is running in.
221 >     * eg, a Filter may be called "filter1",
222 >     * If this class does not have an owning
223 >     * component,  a name from the configuration
224 >     * can be placed here.  This name could also
225 >     * be changed to null for utility classes.
226 >     */
227 >    private String _name = FilterManager.NAME;
228 >    
229 >    /**
230 >     * The socket this class uses
231 >     */
232 >    private Socket _socket;
233 >
234 >    /**
235 >     * Used for the input stream of this socket
236 >     */
237 >    private BufferedReader _socketIn;
238 >    
239 >    /**
240 >     * Used for the output stream of this socket
241 >     */
242 >    private PrintWriter _socketOut;
243 >
244   //---STATIC ATTRIBUTES---
245  
246   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines