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.9 by tdb, Wed Nov 29 19:19:12 2000 UTC vs.
Revision 1.23 by tdb, Mon Mar 5 09:50:58 2001 UTC

# Line 4 | Line 4 | package uk.ac.ukc.iscream.filtermanager;
4   //---IMPORTS---
5   import uk.ac.ukc.iscream.core.*;
6   import uk.ac.ukc.iscream.filter.*;
7 < import java.net.Socket;
8 < import java.io.InputStream;
9 < import java.io.OutputStream;
10 < import java.io.IOException;
7 > import uk.ac.ukc.iscream.util.*;
8 > import uk.ac.ukc.iscream.componentmanager.*;
9 > import java.net.*;
10   import java.io.*;
11 < import org.omg.CORBA.*;
13 < import org.omg.CosNaming.*;
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 32 | Line 31 | class HostInit extends Thread {
31  
32   //---CONSTRUCTORS---
33  
34 <    public HostInit(Socket socket, ConfigurationManager configManager, Logger logRef, NamingContextExt ncRef) throws IOException {
35 <        _configManager = configManager;
36 <        _logRef = logRef;
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          _socket = socket;
41 <        _ncRef = ncRef;
41 >        // setup reader & writer
42          _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
43 <        _socketOut = new PrintWriter(_socket.getOutputStream());
44 <        _logRef.write(toString(), Logger.SYSINIT, "created");
43 >        _socketOut = new PrintWriter(_socket.getOutputStream(), true);
44 >        _logger.write(toString(), Logger.SYSINIT, "created");
45      }
46      
47   //---PUBLIC METHODS---
48  
49 +    /**
50 +     * Main method in this class, which handles communicating with
51 +     * the host to determine it's setup.
52 +     */
53      public void run() {
54          try {
55              String inBound = _socketIn.readLine();
56              if (!inBound.equals("STARTCONFIG")) {
57                  _socketOut.println("ERROR");
52                _socketOut.flush();
58                  throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound);
59              }
60  
# Line 59 | Line 64 | class HostInit extends Thread {
64                  throw new IOException("error in host configuration");
65              } else {
66                  _socketOut.println("OK");
62                _socketOut.flush();
67                  
68                  // get lastmodified
69                  inBound = _socketIn.readLine();
70                  if(!inBound.equals("LASTMODIFIED")) {
71                          // protocol error
72                      _socketOut.println("ERROR");
69                    _socketOut.flush();
73                      throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound);
74                  }
75                  else {
76                          // send info
77                          _socketOut.println(myConfig.getLastModified());
75                        _socketOut.flush();
78                  }
79                  
80                  // get config fileList
# Line 80 | Line 82 | class HostInit extends Thread {
82                  if(!inBound.equals("FILELIST")) {
83                          // protocol error
84                      _socketOut.println("ERROR");
83                    _socketOut.flush();
85                      throw new IOException("protocol error - expected:FILELIST got:" + inBound);
86                  }
87                  else {
88                          // send info
89                          _socketOut.println(myConfig.getFileList());
89                        _socketOut.flush();
90                  }
91 <
91 >                
92 >                // 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(inBound);    
110 >                        String returnedProperty = myConfig.getProperty("Host."+inBound);    
111                          
112                          _socketOut.println(returnedProperty);
101                        _socketOut.flush();
113      
114                      } catch (org.omg.CORBA.MARSHAL e) {
115                          _socketOut.println("ERROR");
105                        _socketOut.flush();
116                      }
117                      inBound = _socketIn.readLine();
118                  }
119 <                _logRef.write(toString(), Logger.SYSMSG, "configured host");
119 >                _logger.write(toString(), Logger.SYSMSG, "configured host");
120                  _socketOut.println("OK");
111                _socketOut.flush();
121                  
122                  // get filter reference
123                  inBound = _socketIn.readLine();
124                  if(!inBound.equals("FILTER")) {
125                          // protocol error
126                      _socketOut.println("ERROR");
118                    _socketOut.flush();
127                      throw new IOException("protocol error - expected:FILTER got:" + inBound);
128                  }
129                  else {
130                          // send info
131 <                        String parentFilter =  myConfig.getProperty("Host.filter");
132 <                        _logRef.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter);
133 <                        org.omg.CORBA.Object objRef = _ncRef.resolve(_ncRef.to_name("iscream.Filter." + parentFilter));
134 <                    Filter filter = FilterHelper.narrow(objRef);
135 <                        _socketOut.println(filter.getHostName() + ";"
136 <                                         + filter.getUDPPort() + ";"
137 <                                         + filter.getTCPPort());
138 <                        _socketOut.flush();
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()) {
136 >                        filter = st.nextToken();
137 >                        _logger.write(toString(), Logger.DEBUG, " looking for filter- " + filter);
138 >                        try {
139 >                            filterRef = FilterHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.Filter." + filter));
140 >                        } catch (ComponentCORBAException e) {
141 >                            _logger.write(toString(), Logger.DEBUG, " unable to find filter- " + filter);
142 >                        }
143 >                    }
144 >                        
145 >                        // hopefully we found a filter
146 >                    if(filterRef != null) {
147 >                        _logger.write(toString(), Logger.DEBUG, " found filter- " + filter);
148 >                        // tell the host about it...
149 >                            _socketOut.println(filterRef.getHostName() + ";"
150 >                                             + filterRef.getUDPPort() + ";"
151 >                                             + filterRef.getTCPPort());
152 >                        }
153 >                        else {
154 >                            // ...or throw a wobbly
155 >                            throw new IOException("unable to find filter for host");
156 >                        }
157                  }
158                  
159                  // confirm that all is ok
# Line 135 | Line 161 | class HostInit extends Thread {
161                  if(!inBound.equals("END")) {
162                          // protocol error
163                      _socketOut.println("ERROR");
138                    _socketOut.flush();
164                      throw new IOException("protocol error - expected:END got:" + inBound);
165                  }
166                  else {
167                          // send ok
168                          _socketOut.println("OK");
144                        _socketOut.flush();
169                  }
170  
171              }
172              
173          } catch (Exception e) {
174 <            _logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage());
174 >            _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
175          }
176          
177          _socketOut.flush();
# Line 157 | Line 181 | class HostInit extends Thread {
181              _socketOut.close();
182              _socket.close();
183          } catch (IOException e) {
184 <            _logRef.write(toString(), Logger.ERROR, "exception on socket close");
184 >            _logger.write(toString(), Logger.ERROR, "exception on socket close");
185          }
186 <        _logRef.write(toString(), Logger.SYSMSG, "finished");
186 >        _logger.write(toString(), Logger.SYSMSG, "finished");
187      }
188      
189      /**
190       * Overrides the {@link java.lang.Object#toString() Object.toString()}
191       * method to provide clean logging (every class should have this).
192       *
193 +     * This uses the uk.ac.ukc.iscream.util.NameFormat class
194 +     * to format the toString()
195 +     *
196       * @return the name of this class and its CVS revision
197       */
198      public String toString() {
199 <        return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName()
200 <         + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")";
201 <        
199 >        return FormatName.getName(
200 >            _name,
201 >            getClass().getName(),
202 >            REVISION);
203      }
204  
205   //---PRIVATE METHODS---
# Line 180 | Line 208 | class HostInit extends Thread {
208  
209   //---ATTRIBUTES---
210  
211 <    ConfigurationManager _configManager;
212 <    Logger _logRef;
213 <    Socket _socket;
214 <    BufferedReader _socketIn;
215 <    PrintWriter _socketOut;
216 <    NamingContextExt _ncRef;
211 >    /**
212 >     * This holds a reference to the
213 >     * system logger that is being used.
214 >     */
215 >    private Logger _logger = ReferenceManager.getInstance().getLogger();
216 >    
217 >    /**
218 >     * A reference to the Configuration Manager the system is using
219 >     */
220 >    private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
221 >    
222 >    /**
223 >     * This is the friendly identifier of the
224 >     * component this class is running in.
225 >     * eg, a Filter may be called "filter1",
226 >     * If this class does not have an owning
227 >     * component,  a name from the configuration
228 >     * can be placed here.  This name could also
229 >     * be changed to null for utility classes.
230 >     */
231 >    private String _name = FilterManager.NAME;
232 >    
233 >    /**
234 >     * The socket this class uses
235 >     */
236 >    private Socket _socket;
237 >
238 >    /**
239 >     * Used for the input stream of this socket
240 >     */
241 >    private BufferedReader _socketIn;
242 >    
243 >    /**
244 >     * Used for the output stream of this socket
245 >     */
246 >    private PrintWriter _socketOut;
247 >
248   //---STATIC ATTRIBUTES---
249  
250   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines