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.24
Committed: Mon Mar 5 23:53:39 2001 UTC (23 years, 3 months ago) by tdb
Branch: MAIN
Changes since 1.23: +4 -3 lines
Log Message:
The filtermanager returns something more useful to the Host if it can't find a
filter for it.

File Contents

# Content
1 //---PACKAGE DECLARATION---
2 package uk.ac.ukc.iscream.filtermanager;
3
4 //---IMPORTS---
5 import uk.ac.ukc.iscream.core.*;
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 * 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: tdb1 $
19 * @version $Id: HostInit.java,v 1.23 2001/03/05 09:50:58 tdb1 Exp $
20 */
21 class HostInit extends Thread {
22
23 //---FINAL ATTRIBUTES---
24
25 /**
26 * The current CVS revision of this class
27 */
28 public final String REVISION = "$Revision: 1.23 $";
29
30 //---STATIC METHODS---
31
32 //---CONSTRUCTORS---
33
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 // setup reader & writer
42 _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
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");
58 throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound);
59 }
60
61 Configuration myConfig = _configManager.getConfiguration("Host." + _socket.getInetAddress().getHostName().toLowerCase());
62 if (myConfig == null) {
63 _socketOut.println("ERROR");
64 throw new IOException("error in host configuration");
65 } else {
66 _socketOut.println("OK");
67
68 // get lastmodified
69 inBound = _socketIn.readLine();
70 if(!inBound.equals("LASTMODIFIED")) {
71 // protocol error
72 _socketOut.println("ERROR");
73 throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound);
74 }
75 else {
76 // send info
77 _socketOut.println(myConfig.getLastModified());
78 }
79
80 // get config fileList
81 inBound = _socketIn.readLine();
82 if(!inBound.equals("FILELIST")) {
83 // protocol error
84 _socketOut.println("ERROR");
85 throw new IOException("protocol error - expected:FILELIST got:" + inBound);
86 }
87 else {
88 // send info
89 _socketOut.println(myConfig.getFileList());
90 }
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("Host."+inBound);
111
112 _socketOut.println(returnedProperty);
113
114 } catch (org.omg.CORBA.MARSHAL e) {
115 _socketOut.println("ERROR");
116 }
117 inBound = _socketIn.readLine();
118 }
119 _logger.write(toString(), Logger.SYSMSG, "configured host");
120 _socketOut.println("OK");
121
122 // get filter reference
123 inBound = _socketIn.readLine();
124 if(!inBound.equals("FILTER")) {
125 // protocol error
126 _socketOut.println("ERROR");
127 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()) {
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 (and tell the host!)
155 _socketOut.println("ERROR");
156 throw new IOException("unable to find filter for host");
157 }
158 }
159
160 // confirm that all is ok
161 inBound = _socketIn.readLine();
162 if(!inBound.equals("END")) {
163 // protocol error
164 _socketOut.println("ERROR");
165 throw new IOException("protocol error - expected:END got:" + inBound);
166 }
167 else {
168 // send ok
169 _socketOut.println("OK");
170 }
171
172 }
173
174 } catch (Exception e) {
175 _logger.write(toString(), Logger.ERROR, "ERROR - " + e);
176 }
177
178 _socketOut.flush();
179 // Disconnect streams & socket
180 try {
181 _socketIn.close();
182 _socketOut.close();
183 _socket.close();
184 } catch (IOException e) {
185 _logger.write(toString(), Logger.ERROR, "exception on socket close");
186 }
187 _logger.write(toString(), Logger.SYSMSG, "finished");
188 }
189
190 /**
191 * Overrides the {@link java.lang.Object#toString() Object.toString()}
192 * method to provide clean logging (every class should have this).
193 *
194 * This uses the uk.ac.ukc.iscream.util.NameFormat class
195 * to format the toString()
196 *
197 * @return the name of this class and its CVS revision
198 */
199 public String toString() {
200 return FormatName.getName(
201 _name,
202 getClass().getName(),
203 REVISION);
204 }
205
206 //---PRIVATE METHODS---
207
208 //---ACCESSOR/MUTATOR METHODS---
209
210 //---ATTRIBUTES---
211
212 /**
213 * This holds a reference to the
214 * system logger that is being used.
215 */
216 private Logger _logger = ReferenceManager.getInstance().getLogger();
217
218 /**
219 * A reference to the Configuration Manager the system is using
220 */
221 private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM();
222
223 /**
224 * This is the friendly identifier of the
225 * component this class is running in.
226 * eg, a Filter may be called "filter1",
227 * If this class does not have an owning
228 * component, a name from the configuration
229 * can be placed here. This name could also
230 * be changed to null for utility classes.
231 */
232 private String _name = FilterManager.NAME;
233
234 /**
235 * The socket this class uses
236 */
237 private Socket _socket;
238
239 /**
240 * Used for the input stream of this socket
241 */
242 private BufferedReader _socketIn;
243
244 /**
245 * Used for the output stream of this socket
246 */
247 private PrintWriter _socketOut;
248
249 //---STATIC ATTRIBUTES---
250
251 }