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.22 |
* @version $Id: HostInit.java,v 1.21 2001/02/28 11:05:20 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.22 |
public final String REVISION = "$Revision: 1.21 $"; |
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.1 |
_socket = socket; |
41 |
tdb |
1.15 |
// setup reader & writer |
42 |
tdb |
1.1 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
43 |
tdb |
1.16 |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
44 |
tdb |
1.11 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
45 |
tdb |
1.1 |
} |
46 |
|
|
|
47 |
|
|
//---PUBLIC METHODS--- |
48 |
|
|
|
49 |
tdb |
1.15 |
/** |
50 |
|
|
* Main method in this class, which handles communicating with |
51 |
|
|
* the host to determine it's setup. |
52 |
|
|
*/ |
53 |
tdb |
1.1 |
public void run() { |
54 |
|
|
try { |
55 |
|
|
String inBound = _socketIn.readLine(); |
56 |
|
|
if (!inBound.equals("STARTCONFIG")) { |
57 |
|
|
_socketOut.println("ERROR"); |
58 |
tdb |
1.7 |
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
59 |
tdb |
1.1 |
} |
60 |
|
|
|
61 |
ajm |
1.8 |
Configuration myConfig = _configManager.getConfiguration("Host." + _socket.getInetAddress().getHostName().toLowerCase()); |
62 |
tdb |
1.1 |
if (myConfig == null) { |
63 |
|
|
_socketOut.println("ERROR"); |
64 |
tdb |
1.7 |
throw new IOException("error in host configuration"); |
65 |
tdb |
1.1 |
} else { |
66 |
|
|
_socketOut.println("OK"); |
67 |
|
|
|
68 |
tdb |
1.2 |
// get lastmodified |
69 |
|
|
inBound = _socketIn.readLine(); |
70 |
tdb |
1.4 |
if(!inBound.equals("LASTMODIFIED")) { |
71 |
|
|
// protocol error |
72 |
tdb |
1.2 |
_socketOut.println("ERROR"); |
73 |
tdb |
1.7 |
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
74 |
tdb |
1.4 |
} |
75 |
|
|
else { |
76 |
|
|
// send info |
77 |
|
|
_socketOut.println(myConfig.getLastModified()); |
78 |
tdb |
1.2 |
} |
79 |
|
|
|
80 |
tdb |
1.7 |
// 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 |
tdb |
1.18 |
|
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 |
tdb |
1.20 |
// send the fqdn (of the host) to the host |
101 |
tdb |
1.21 |
_socketOut.println(_socket.getInetAddress().getHostName().toLowerCase()); |
102 |
tdb |
1.18 |
} |
103 |
|
|
|
104 |
tdb |
1.2 |
// get properties |
105 |
tdb |
1.1 |
inBound = _socketIn.readLine(); |
106 |
|
|
while(!inBound.equals("ENDCONFIG")) { |
107 |
|
|
|
108 |
|
|
// get the property |
109 |
|
|
try { |
110 |
tdb |
1.10 |
String returnedProperty = myConfig.getProperty("Host."+inBound); |
111 |
tdb |
1.1 |
|
112 |
|
|
_socketOut.println(returnedProperty); |
113 |
|
|
|
114 |
|
|
} catch (org.omg.CORBA.MARSHAL e) { |
115 |
|
|
_socketOut.println("ERROR"); |
116 |
|
|
} |
117 |
|
|
inBound = _socketIn.readLine(); |
118 |
|
|
} |
119 |
tdb |
1.11 |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
120 |
tdb |
1.1 |
_socketOut.println("OK"); |
121 |
tdb |
1.7 |
|
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 |
tdb |
1.22 |
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 (Exception e) { // THIS SHOULD BE A MORE SPECIFIC EXCEPTION |
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 |
tdb |
1.7 |
} |
158 |
|
|
|
159 |
|
|
// confirm that all is ok |
160 |
|
|
inBound = _socketIn.readLine(); |
161 |
|
|
if(!inBound.equals("END")) { |
162 |
|
|
// protocol error |
163 |
|
|
_socketOut.println("ERROR"); |
164 |
|
|
throw new IOException("protocol error - expected:END got:" + inBound); |
165 |
|
|
} |
166 |
|
|
else { |
167 |
|
|
// send ok |
168 |
|
|
_socketOut.println("OK"); |
169 |
|
|
} |
170 |
|
|
|
171 |
tdb |
1.1 |
} |
172 |
|
|
|
173 |
|
|
} catch (Exception e) { |
174 |
tdb |
1.17 |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e); |
175 |
tdb |
1.1 |
} |
176 |
|
|
|
177 |
|
|
_socketOut.flush(); |
178 |
|
|
// Disconnect streams & socket |
179 |
|
|
try { |
180 |
|
|
_socketIn.close(); |
181 |
|
|
_socketOut.close(); |
182 |
|
|
_socket.close(); |
183 |
|
|
} catch (IOException e) { |
184 |
tdb |
1.11 |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
185 |
tdb |
1.1 |
} |
186 |
tdb |
1.11 |
_logger.write(toString(), Logger.SYSMSG, "finished"); |
187 |
tdb |
1.1 |
} |
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 |
ajm |
1.12 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
194 |
|
|
* to format the toString() |
195 |
|
|
* |
196 |
tdb |
1.1 |
* @return the name of this class and its CVS revision |
197 |
|
|
*/ |
198 |
|
|
public String toString() { |
199 |
ajm |
1.12 |
return FormatName.getName( |
200 |
|
|
_name, |
201 |
ajm |
1.13 |
getClass().getName(), |
202 |
ajm |
1.12 |
REVISION); |
203 |
tdb |
1.1 |
} |
204 |
|
|
|
205 |
|
|
//---PRIVATE METHODS--- |
206 |
|
|
|
207 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
208 |
|
|
|
209 |
|
|
//---ATTRIBUTES--- |
210 |
|
|
|
211 |
tdb |
1.11 |
/** |
212 |
ajm |
1.12 |
* This holds a reference to the |
213 |
|
|
* system logger that is being used. |
214 |
tdb |
1.11 |
*/ |
215 |
ajm |
1.12 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
216 |
tdb |
1.11 |
|
217 |
|
|
/** |
218 |
|
|
* A reference to the Configuration Manager the system is using |
219 |
|
|
*/ |
220 |
ajm |
1.12 |
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 |
tdb |
1.11 |
|
243 |
ajm |
1.12 |
/** |
244 |
|
|
* Used for the output stream of this socket |
245 |
|
|
*/ |
246 |
|
|
private PrintWriter _socketOut; |
247 |
tdb |
1.11 |
|
248 |
tdb |
1.1 |
//---STATIC ATTRIBUTES--- |
249 |
|
|
|
250 |
|
|
} |