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$ |
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 |
> |
// set the Thread name |
41 |
> |
setName("filtermanager.HostInit"); |
42 |
> |
|
43 |
|
_socket = socket; |
44 |
< |
_ncRef = ncRef; |
44 |
> |
// setup reader & writer |
45 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
46 |
< |
_socketOut = new PrintWriter(_socket.getOutputStream()); |
47 |
< |
_logRef.write(toString(), Logger.SYSINIT, "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"); |
52 |
– |
_socketOut.flush(); |
61 |
|
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
62 |
|
} |
63 |
|
|
67 |
|
throw new IOException("error in host configuration"); |
68 |
|
} else { |
69 |
|
_socketOut.println("OK"); |
62 |
– |
_socketOut.flush(); |
70 |
|
|
71 |
|
// get lastmodified |
72 |
|
inBound = _socketIn.readLine(); |
73 |
|
if(!inBound.equals("LASTMODIFIED")) { |
74 |
|
// protocol error |
75 |
|
_socketOut.println("ERROR"); |
69 |
– |
_socketOut.flush(); |
76 |
|
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
77 |
|
} |
78 |
|
else { |
79 |
|
// send info |
80 |
|
_socketOut.println(myConfig.getLastModified()); |
75 |
– |
_socketOut.flush(); |
81 |
|
} |
82 |
|
|
83 |
|
// get config fileList |
85 |
|
if(!inBound.equals("FILELIST")) { |
86 |
|
// protocol error |
87 |
|
_socketOut.println("ERROR"); |
83 |
– |
_socketOut.flush(); |
88 |
|
throw new IOException("protocol error - expected:FILELIST got:" + inBound); |
89 |
|
} |
90 |
|
else { |
91 |
|
// send info |
92 |
|
_socketOut.println(myConfig.getFileList()); |
89 |
– |
_socketOut.flush(); |
93 |
|
} |
94 |
< |
|
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")) { |
113 |
|
String returnedProperty = myConfig.getProperty("Host."+inBound); |
114 |
|
|
115 |
|
_socketOut.println(returnedProperty); |
101 |
– |
_socketOut.flush(); |
116 |
|
|
117 |
|
} catch (org.omg.CORBA.MARSHAL e) { |
118 |
|
_socketOut.println("ERROR"); |
105 |
– |
_socketOut.flush(); |
119 |
|
} |
120 |
|
inBound = _socketIn.readLine(); |
121 |
|
} |
122 |
< |
_logRef.write(toString(), Logger.SYSMSG, "configured host"); |
122 |
> |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
123 |
|
_socketOut.println("OK"); |
111 |
– |
_socketOut.flush(); |
124 |
|
|
125 |
|
// get filter reference |
126 |
|
inBound = _socketIn.readLine(); |
127 |
|
if(!inBound.equals("FILTER")) { |
128 |
|
// protocol error |
129 |
|
_socketOut.println("ERROR"); |
118 |
– |
_socketOut.flush(); |
130 |
|
throw new IOException("protocol error - expected:FILTER got:" + inBound); |
131 |
|
} |
132 |
|
else { |
133 |
|
// send info |
134 |
< |
String parentFilter = myConfig.getProperty("Host.filter"); |
135 |
< |
_logRef.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter); |
136 |
< |
org.omg.CORBA.Object objRef = _ncRef.resolve(_ncRef.to_name("iscream.Filter." + parentFilter)); |
137 |
< |
Filter filter = FilterHelper.narrow(objRef); |
138 |
< |
_socketOut.println(filter.getHostName() + ";" |
139 |
< |
+ filter.getUDPPort() + ";" |
140 |
< |
+ filter.getTCPPort()); |
141 |
< |
_socketOut.flush(); |
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 |
165 |
|
if(!inBound.equals("END")) { |
166 |
|
// protocol error |
167 |
|
_socketOut.println("ERROR"); |
138 |
– |
_socketOut.flush(); |
168 |
|
throw new IOException("protocol error - expected:END got:" + inBound); |
169 |
|
} |
170 |
|
else { |
171 |
|
// send ok |
172 |
|
_socketOut.println("OK"); |
144 |
– |
_socketOut.flush(); |
173 |
|
} |
174 |
|
|
175 |
|
} |
176 |
|
|
177 |
|
} catch (Exception e) { |
178 |
< |
_logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
178 |
> |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e); |
179 |
|
} |
180 |
|
|
181 |
|
_socketOut.flush(); |
185 |
|
_socketOut.close(); |
186 |
|
_socket.close(); |
187 |
|
} catch (IOException e) { |
188 |
< |
_logRef.write(toString(), Logger.ERROR, "exception on socket close"); |
188 |
> |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
189 |
|
} |
190 |
< |
_logRef.write(toString(), Logger.SYSMSG, "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--- |
212 |
|
|
213 |
|
//---ATTRIBUTES--- |
214 |
|
|
215 |
< |
ConfigurationManager _configManager; |
216 |
< |
Logger _logRef; |
217 |
< |
Socket _socket; |
218 |
< |
BufferedReader _socketIn; |
219 |
< |
PrintWriter _socketOut; |
220 |
< |
NamingContextExt _ncRef; |
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 |
|
|
254 |
|
} |