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.3 by tdb, Wed Nov 15 00:39:07 2000 UTC vs.
Revision 1.25 by tdb, Tue Mar 13 02:19:47 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 + package uk.ac.ukc.iscream.filtermanager;
3  
4   //---IMPORTS---
5   import uk.ac.ukc.iscream.core.*;
6 < import java.net.Socket;
7 < import java.io.InputStream;
8 < import java.io.OutputStream;
9 < import java.io.IOException;
6 > import uk.ac.ukc.iscream.filter.*;
7 > import uk.ac.ukc.iscream.util.*;
8 > import uk.ac.ukc.iscream.componentmanager.*;
9 > import java.net.*;
10   import java.io.*;
11 + import java.util.*;
12  
13   /**
14 < * <ONE LINE DESCRIPTION>
15 < * <DETAILED DESCRIPTION>
14 > * Handles setting up a host.
15 > * This class provides a host with appropriate configuration
16 > * and a reference to a Filter to which it should pass data.
17   *
18   * @author  $Author$
19   * @version $Id$
# Line 28 | Line 31 | class HostInit extends Thread {
31  
32   //---CONSTRUCTORS---
33  
34 <    public HostInit(Socket socket, Configurator configurator, Logger logger) throws IOException {
35 <        _configurator = configurator;
36 <        _logger = logger;
34 >    /**
35 >     * Construct a new HostInit.
36 >     *
37 >     * @param socket The socket to which the host is connected
38 >     */
39 >    public HostInit(Socket socket) throws IOException {
40 >        // set the Thread name
41 >        setName("filtermanager.HostInit");
42 >        
43          _socket = socket;
44 +        // setup reader & writer
45          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
46 <        _socketOut = new PrintWriter(_socket.getOutputStream());
47 <        _logger.write(toString(), "created");
46 >        _socketOut = new PrintWriter(_socket.getOutputStream(), true);
47 >        _logger.write(toString(), Logger.SYSINIT, "created");
48      }
49      
50   //---PUBLIC METHODS---
51  
52 +    /**
53 +     * Main method in this class, which handles communicating with
54 +     * the host to determine it's setup.
55 +     */
56      public void run() {
57          try {
58              String inBound = _socketIn.readLine();
59              if (!inBound.equals("STARTCONFIG")) {
60                  _socketOut.println("ERROR");
61 <                _socketOut.flush();
48 <                throw new IOException("Protocol Error");
61 >                throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound);
62              }
63  
64 <            Configuration myConfig = _configurator.getConfiguration(_socket.getInetAddress().getHostName().toLowerCase());
64 >            Configuration myConfig = _configManager.getConfiguration("Host." + _socket.getInetAddress().getHostName().toLowerCase());
65              if (myConfig == null) {
66                  _socketOut.println("ERROR");
67 <                throw new IOException("No Configuration File For Host");
67 >                throw new IOException("error in host configuration");
68              } else {
69                  _socketOut.println("OK");
57                _socketOut.flush();
70                  
71                  // get lastmodified
72                  inBound = _socketIn.readLine();
73 <                while(!inBound.equals("LASTMODIFIED")) {
73 >                if(!inBound.equals("LASTMODIFIED")) {
74 >                        // protocol error
75                      _socketOut.println("ERROR");
76 <                    _socketOut.flush();
64 <                    inBound = _socketIn.readLine();
76 >                    throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound);
77                  }
78 <                _socketOut.println(myConfig.getLastModified());
79 <                _socketOut.flush();
78 >                else {
79 >                        // send info
80 >                        _socketOut.println(myConfig.getLastModified());
81 >                }
82                  
83 +                // get config fileList
84 +                inBound = _socketIn.readLine();
85 +                if(!inBound.equals("FILELIST")) {
86 +                        // protocol error
87 +                    _socketOut.println("ERROR");
88 +                    throw new IOException("protocol error - expected:FILELIST got:" + inBound);
89 +                }
90 +                else {
91 +                        // send info
92 +                        _socketOut.println(myConfig.getFileList());
93 +                }
94 +                
95 +                // send the FQDN to the host
96 +                inBound = _socketIn.readLine();
97 +                if(!inBound.equals("FQDN")) {
98 +                        // protocol error
99 +                    _socketOut.println("ERROR");
100 +                    throw new IOException("protocol error - expected:FQDN got:" + inBound);
101 +                }
102 +                else {
103 +                    // send the fqdn (of the host) to the host
104 +                    _socketOut.println(_socket.getInetAddress().getHostName().toLowerCase());
105 +                }
106 +                
107                  // get properties
108                  inBound = _socketIn.readLine();
109                  while(!inBound.equals("ENDCONFIG")) {
110                            
111                      // get the property
112                      try {
113 <                        String returnedProperty = myConfig.getProperty(inBound);    
113 >                        String returnedProperty = myConfig.getProperty("Host."+inBound);    
114                          
115                          _socketOut.println(returnedProperty);
78                        _socketOut.flush();
116      
117                      } catch (org.omg.CORBA.MARSHAL e) {
118                          _socketOut.println("ERROR");
82                        _socketOut.flush();
119                      }
120                      inBound = _socketIn.readLine();
121                  }
122 <                _logger.write(toString(), "configured host");
122 >                _logger.write(toString(), Logger.SYSMSG, "configured host");
123                  _socketOut.println("OK");
124 <                _socketOut.flush();
124 >                
125 >                // get filter reference
126 >                inBound = _socketIn.readLine();
127 >                if(!inBound.equals("FILTER")) {
128 >                        // protocol error
129 >                    _socketOut.println("ERROR");
130 >                    throw new IOException("protocol error - expected:FILTER got:" + inBound);
131 >                }
132 >                else {
133 >                        // send info
134 >                        String filterList = myConfig.getProperty("Host.filter");
135 >                        Filter filterRef = null;
136 >                        String filter = null;
137 >                        StringTokenizer st = new StringTokenizer(filterList, ";");
138 >                    while (filterRef==null && st.hasMoreTokens()) {
139 >                        filter = st.nextToken();
140 >                        _logger.write(toString(), Logger.DEBUG, " looking for filter- " + filter);
141 >                        try {
142 >                            filterRef = FilterHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.Filter." + filter));
143 >                        } catch (ComponentCORBAException e) {
144 >                            _logger.write(toString(), Logger.DEBUG, " unable to find filter- " + filter);
145 >                        }
146 >                    }
147 >                        
148 >                        // hopefully we found a filter
149 >                    if(filterRef != null) {
150 >                        _logger.write(toString(), Logger.DEBUG, " found filter- " + filter);
151 >                        // tell the host about it...
152 >                            _socketOut.println(filterRef.getHostName() + ";"
153 >                                             + filterRef.getUDPPort() + ";"
154 >                                             + filterRef.getTCPPort());
155 >                        }
156 >                        else {
157 >                            // ...or throw a wobbly (and tell the host!)
158 >                            _socketOut.println("ERROR");
159 >                            throw new IOException("unable to find filter for host");
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);
169 >                }
170 >                else {
171 >                        // send ok
172 >                        _socketOut.println("OK");
173 >                }
174 >
175              }
176              
177          } catch (Exception e) {
178 <            _logger.write(toString(), e.toString());
178 >            _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
179          }
180          
181          _socketOut.flush();
# Line 99 | Line 185 | class HostInit extends Thread {
185              _socketOut.close();
186              _socket.close();
187          } catch (IOException e) {
188 <            _logger.write(toString(), "exception on socket close");
188 >            _logger.write(toString(), Logger.ERROR, "exception on socket close");
189          }
190 <        _logger.write(toString(), "finished");
190 >        _logger.write(toString(), Logger.SYSMSG, "finished");
191      }
192      
193      /**
194       * Overrides the {@link java.lang.Object#toString() Object.toString()}
195       * method to provide clean logging (every class should have this).
196       *
197 +     * This uses the uk.ac.ukc.iscream.util.NameFormat class
198 +     * to format the toString()
199 +     *
200       * @return the name of this class and its CVS revision
201       */
202      public String toString() {
203 <        return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName()
204 <         + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")";
205 <        
203 >        return FormatName.getName(
204 >            _name,
205 >            getClass().getName(),
206 >            REVISION);
207      }
208  
209   //---PRIVATE METHODS---
# Line 122 | Line 212 | class HostInit extends Thread {
212  
213   //---ATTRIBUTES---
214  
215 <    Configurator _configurator;
216 <    Logger _logger;
217 <    Socket _socket;
218 <    BufferedReader _socketIn;
219 <    PrintWriter _socketOut;
215 >    /**
216 >     * This holds a reference to the
217 >     * system logger that is being used.
218 >     */
219 >    private Logger _logger = ReferenceManager.getInstance().getLogger();
220 >    
221 >    /**
222 >     * A reference to the Configuration Manager the system is using
223 >     */
224 >    private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
225 >    
226 >    /**
227 >     * This is the friendly identifier of the
228 >     * component this class is running in.
229 >     * eg, a Filter may be called "filter1",
230 >     * If this class does not have an owning
231 >     * component,  a name from the configuration
232 >     * can be placed here.  This name could also
233 >     * be changed to null for utility classes.
234 >     */
235 >    private String _name = FilterManager.NAME;
236 >    
237 >    /**
238 >     * The socket this class uses
239 >     */
240 >    private Socket _socket;
241 >
242 >    /**
243 >     * Used for the input stream of this socket
244 >     */
245 >    private BufferedReader _socketIn;
246 >    
247 >    /**
248 >     * Used for the output stream of this socket
249 >     */
250 >    private PrintWriter _socketOut;
251  
252   //---STATIC ATTRIBUTES---
253  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines