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
Revision: 1.25
Committed: Tue Mar 13 02:19:47 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.24: +5 -2 lines
Log Message:
Given all the classes that extend Thread a name using Thread.setName(). It is
only representative as far as it will tell us which class the Thread is, but
this will go some way to aiding debugging. If time permitted, more effort could
be taken to name each thread according to what it was dealing with.

File Contents

# User Rev Content
1 tdb 1.1 //---PACKAGE DECLARATION---
2 tdb 1.9 package uk.ac.ukc.iscream.filtermanager;
3 tdb 1.1
4     //---IMPORTS---
5     import uk.ac.ukc.iscream.core.*;
6 tdb 1.7 import uk.ac.ukc.iscream.filter.*;
7 tdb 1.11 import uk.ac.ukc.iscream.util.*;
8 tdb 1.14 import uk.ac.ukc.iscream.componentmanager.*;
9 tdb 1.18 import java.net.*;
10 tdb 1.1 import java.io.*;
11 tdb 1.22 import java.util.*;
12 tdb 1.1
13     /**
14 tdb 1.11 * Handles setting up a host.
15 ajm 1.12 * This class provides a host with appropriate configuration
16     * and a reference to a Filter to which it should pass data.
17 tdb 1.1 *
18 tdb 1.15 * @author $Author: tdb1 $
19 tdb 1.25 * @version $Id: HostInit.java,v 1.24 2001/03/05 23:53:39 tdb1 Exp $
20 tdb 1.1 */
21     class HostInit extends Thread {
22    
23     //---FINAL ATTRIBUTES---
24    
25     /**
26     * The current CVS revision of this class
27     */
28 tdb 1.25 public final String REVISION = "$Revision: 1.24 $";
29 tdb 1.1
30     //---STATIC METHODS---
31    
32     //---CONSTRUCTORS---
33    
34 tdb 1.15 /**
35     * Construct a new HostInit.
36     *
37     * @param socket The socket to which the host is connected
38     */
39 tdb 1.11 public HostInit(Socket socket) throws IOException {
40 tdb 1.25 // set the Thread name
41     setName("filtermanager.HostInit");
42    
43 tdb 1.1 _socket = socket;
44 tdb 1.15 // setup reader & writer
45 tdb 1.1 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
46 tdb 1.16 _socketOut = new PrintWriter(_socket.getOutputStream(), true);
47 tdb 1.11 _logger.write(toString(), Logger.SYSINIT, "created");
48 tdb 1.1 }
49    
50     //---PUBLIC METHODS---
51    
52 tdb 1.15 /**
53     * Main method in this class, which handles communicating with
54     * the host to determine it's setup.
55     */
56 tdb 1.1 public void run() {
57     try {
58     String inBound = _socketIn.readLine();
59     if (!inBound.equals("STARTCONFIG")) {
60     _socketOut.println("ERROR");
61 tdb 1.7 throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound);
62 tdb 1.1 }
63    
64 ajm 1.8 Configuration myConfig = _configManager.getConfiguration("Host." + _socket.getInetAddress().getHostName().toLowerCase());
65 tdb 1.1 if (myConfig == null) {
66     _socketOut.println("ERROR");
67 tdb 1.7 throw new IOException("error in host configuration");
68 tdb 1.1 } else {
69     _socketOut.println("OK");
70    
71 tdb 1.2 // get lastmodified
72     inBound = _socketIn.readLine();
73 tdb 1.4 if(!inBound.equals("LASTMODIFIED")) {
74     // protocol error
75 tdb 1.2 _socketOut.println("ERROR");
76 tdb 1.7 throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound);
77 tdb 1.4 }
78     else {
79     // send info
80     _socketOut.println(myConfig.getLastModified());
81 tdb 1.2 }
82    
83 tdb 1.7 // 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 tdb 1.18
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 tdb 1.20 // send the fqdn (of the host) to the host
104 tdb 1.21 _socketOut.println(_socket.getInetAddress().getHostName().toLowerCase());
105 tdb 1.18 }
106    
107 tdb 1.2 // get properties
108 tdb 1.1 inBound = _socketIn.readLine();
109     while(!inBound.equals("ENDCONFIG")) {
110    
111     // get the property
112     try {
113 tdb 1.10 String returnedProperty = myConfig.getProperty("Host."+inBound);
114 tdb 1.1
115     _socketOut.println(returnedProperty);
116    
117     } catch (org.omg.CORBA.MARSHAL e) {
118     _socketOut.println("ERROR");
119     }
120     inBound = _socketIn.readLine();
121     }
122 tdb 1.11 _logger.write(toString(), Logger.SYSMSG, "configured host");
123 tdb 1.1 _socketOut.println("OK");
124 tdb 1.7
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 tdb 1.22 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 tdb 1.23 } catch (ComponentCORBAException e) {
144 tdb 1.22 _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 tdb 1.24 // ...or throw a wobbly (and tell the host!)
158     _socketOut.println("ERROR");
159 tdb 1.22 throw new IOException("unable to find filter for host");
160     }
161 tdb 1.7 }
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 tdb 1.1 }
176    
177     } catch (Exception e) {
178 tdb 1.17 _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
179 tdb 1.1 }
180    
181     _socketOut.flush();
182     // Disconnect streams & socket
183     try {
184     _socketIn.close();
185     _socketOut.close();
186     _socket.close();
187     } catch (IOException e) {
188 tdb 1.11 _logger.write(toString(), Logger.ERROR, "exception on socket close");
189 tdb 1.1 }
190 tdb 1.11 _logger.write(toString(), Logger.SYSMSG, "finished");
191 tdb 1.1 }
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 ajm 1.12 * This uses the uk.ac.ukc.iscream.util.NameFormat class
198     * to format the toString()
199     *
200 tdb 1.1 * @return the name of this class and its CVS revision
201     */
202     public String toString() {
203 ajm 1.12 return FormatName.getName(
204     _name,
205 ajm 1.13 getClass().getName(),
206 ajm 1.12 REVISION);
207 tdb 1.1 }
208    
209     //---PRIVATE METHODS---
210    
211     //---ACCESSOR/MUTATOR METHODS---
212    
213     //---ATTRIBUTES---
214    
215 tdb 1.11 /**
216 ajm 1.12 * This holds a reference to the
217     * system logger that is being used.
218 tdb 1.11 */
219 ajm 1.12 private Logger _logger = ReferenceManager.getInstance().getLogger();
220 tdb 1.11
221     /**
222     * A reference to the Configuration Manager the system is using
223     */
224 ajm 1.12 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 tdb 1.11
247 ajm 1.12 /**
248     * Used for the output stream of this socket
249     */
250     private PrintWriter _socketOut;
251 tdb 1.11
252 tdb 1.1 //---STATIC ATTRIBUTES---
253    
254     }