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.*; |
12 |
– |
import org.omg.CORBA.*; |
13 |
– |
import org.omg.CosNaming.*; |
11 |
|
|
12 |
|
/** |
13 |
< |
* <ONE LINE DESCRIPTION> |
14 |
< |
* <DETAILED DESCRIPTION> |
13 |
> |
* Handles setting up a host. |
14 |
> |
* This class provides a host with appropriate configuration |
15 |
> |
* and a reference to a Filter to which it should pass data. |
16 |
|
* |
17 |
|
* @author $Author$ |
18 |
|
* @version $Id$ |
30 |
|
|
31 |
|
//---CONSTRUCTORS--- |
32 |
|
|
33 |
< |
public HostInit(Socket socket, ConfigurationManager configManager, Logger logRef, NamingContextExt ncRef) throws IOException { |
34 |
< |
_configManager = configManager; |
35 |
< |
_logRef = logRef; |
33 |
> |
/** |
34 |
> |
* Construct a new HostInit. |
35 |
> |
* |
36 |
> |
* @param socket The socket to which the host is connected |
37 |
> |
*/ |
38 |
> |
public HostInit(Socket socket) throws IOException { |
39 |
|
_socket = socket; |
40 |
< |
_ncRef = ncRef; |
40 |
> |
// setup reader & writer |
41 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
42 |
< |
_socketOut = new PrintWriter(_socket.getOutputStream()); |
43 |
< |
_logRef.write(toString(), Logger.SYSINIT, "created"); |
42 |
> |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
43 |
> |
_logger.write(toString(), Logger.SYSINIT, "created"); |
44 |
|
} |
45 |
|
|
46 |
|
//---PUBLIC METHODS--- |
47 |
|
|
48 |
+ |
/** |
49 |
+ |
* Main method in this class, which handles communicating with |
50 |
+ |
* the host to determine it's setup. |
51 |
+ |
*/ |
52 |
|
public void run() { |
53 |
|
try { |
54 |
|
String inBound = _socketIn.readLine(); |
55 |
|
if (!inBound.equals("STARTCONFIG")) { |
56 |
|
_socketOut.println("ERROR"); |
52 |
– |
_socketOut.flush(); |
57 |
|
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
58 |
|
} |
59 |
|
|
63 |
|
throw new IOException("error in host configuration"); |
64 |
|
} else { |
65 |
|
_socketOut.println("OK"); |
62 |
– |
_socketOut.flush(); |
66 |
|
|
67 |
|
// get lastmodified |
68 |
|
inBound = _socketIn.readLine(); |
69 |
|
if(!inBound.equals("LASTMODIFIED")) { |
70 |
|
// protocol error |
71 |
|
_socketOut.println("ERROR"); |
69 |
– |
_socketOut.flush(); |
72 |
|
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
73 |
|
} |
74 |
|
else { |
75 |
|
// send info |
76 |
|
_socketOut.println(myConfig.getLastModified()); |
75 |
– |
_socketOut.flush(); |
77 |
|
} |
78 |
|
|
79 |
|
// get config fileList |
81 |
|
if(!inBound.equals("FILELIST")) { |
82 |
|
// protocol error |
83 |
|
_socketOut.println("ERROR"); |
83 |
– |
_socketOut.flush(); |
84 |
|
throw new IOException("protocol error - expected:FILELIST got:" + inBound); |
85 |
|
} |
86 |
|
else { |
87 |
|
// send info |
88 |
|
_socketOut.println(myConfig.getFileList()); |
89 |
– |
_socketOut.flush(); |
89 |
|
} |
90 |
< |
|
90 |
> |
|
91 |
> |
// send the FQDN to the host |
92 |
> |
inBound = _socketIn.readLine(); |
93 |
> |
if(!inBound.equals("FQDN")) { |
94 |
> |
// protocol error |
95 |
> |
_socketOut.println("ERROR"); |
96 |
> |
throw new IOException("protocol error - expected:FQDN got:" + inBound); |
97 |
> |
} |
98 |
> |
else { |
99 |
> |
try { |
100 |
> |
// try to send the fqdn to the host |
101 |
> |
_socketOut.println(InetAddress.getLocalHost().getHostName()); |
102 |
> |
} |
103 |
> |
catch(UnknownHostException e) { |
104 |
> |
// we failed for some reason |
105 |
> |
_socketOut.println("ERROR"); |
106 |
> |
} |
107 |
> |
} |
108 |
> |
|
109 |
|
// get properties |
110 |
|
inBound = _socketIn.readLine(); |
111 |
|
while(!inBound.equals("ENDCONFIG")) { |
112 |
|
|
113 |
|
// get the property |
114 |
|
try { |
115 |
< |
String returnedProperty = myConfig.getProperty(inBound); |
115 |
> |
String returnedProperty = myConfig.getProperty("Host."+inBound); |
116 |
|
|
117 |
|
_socketOut.println(returnedProperty); |
101 |
– |
_socketOut.flush(); |
118 |
|
|
119 |
|
} catch (org.omg.CORBA.MARSHAL e) { |
120 |
|
_socketOut.println("ERROR"); |
105 |
– |
_socketOut.flush(); |
121 |
|
} |
122 |
|
inBound = _socketIn.readLine(); |
123 |
|
} |
124 |
< |
_logRef.write(toString(), Logger.SYSMSG, "configured host"); |
124 |
> |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
125 |
|
_socketOut.println("OK"); |
111 |
– |
_socketOut.flush(); |
126 |
|
|
127 |
|
// get filter reference |
128 |
|
inBound = _socketIn.readLine(); |
129 |
|
if(!inBound.equals("FILTER")) { |
130 |
|
// protocol error |
131 |
|
_socketOut.println("ERROR"); |
118 |
– |
_socketOut.flush(); |
132 |
|
throw new IOException("protocol error - expected:FILTER got:" + inBound); |
133 |
|
} |
134 |
|
else { |
135 |
|
// send info |
136 |
|
String parentFilter = myConfig.getProperty("Host.filter"); |
137 |
< |
_logRef.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter); |
138 |
< |
org.omg.CORBA.Object objRef = _ncRef.resolve(_ncRef.to_name("iscream.Filter." + parentFilter)); |
126 |
< |
Filter filter = FilterHelper.narrow(objRef); |
137 |
> |
_logger.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter); |
138 |
> |
Filter filter = FilterHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.Filter." + parentFilter)); |
139 |
|
_socketOut.println(filter.getHostName() + ";" |
140 |
|
+ filter.getUDPPort() + ";" |
141 |
|
+ filter.getTCPPort()); |
130 |
– |
_socketOut.flush(); |
142 |
|
} |
143 |
|
|
144 |
|
// confirm that all is ok |
146 |
|
if(!inBound.equals("END")) { |
147 |
|
// protocol error |
148 |
|
_socketOut.println("ERROR"); |
138 |
– |
_socketOut.flush(); |
149 |
|
throw new IOException("protocol error - expected:END got:" + inBound); |
150 |
|
} |
151 |
|
else { |
152 |
|
// send ok |
153 |
|
_socketOut.println("OK"); |
144 |
– |
_socketOut.flush(); |
154 |
|
} |
155 |
|
|
156 |
|
} |
157 |
|
|
158 |
|
} catch (Exception e) { |
159 |
< |
_logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
159 |
> |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e); |
160 |
|
} |
161 |
|
|
162 |
|
_socketOut.flush(); |
166 |
|
_socketOut.close(); |
167 |
|
_socket.close(); |
168 |
|
} catch (IOException e) { |
169 |
< |
_logRef.write(toString(), Logger.ERROR, "exception on socket close"); |
169 |
> |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
170 |
|
} |
171 |
< |
_logRef.write(toString(), Logger.SYSMSG, "finished"); |
171 |
> |
_logger.write(toString(), Logger.SYSMSG, "finished"); |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
176 |
|
* method to provide clean logging (every class should have this). |
177 |
|
* |
178 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
179 |
+ |
* to format the toString() |
180 |
+ |
* |
181 |
|
* @return the name of this class and its CVS revision |
182 |
|
*/ |
183 |
|
public String toString() { |
184 |
< |
return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName() |
185 |
< |
+ "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
186 |
< |
|
184 |
> |
return FormatName.getName( |
185 |
> |
_name, |
186 |
> |
getClass().getName(), |
187 |
> |
REVISION); |
188 |
|
} |
189 |
|
|
190 |
|
//---PRIVATE METHODS--- |
193 |
|
|
194 |
|
//---ATTRIBUTES--- |
195 |
|
|
196 |
< |
ConfigurationManager _configManager; |
197 |
< |
Logger _logRef; |
198 |
< |
Socket _socket; |
199 |
< |
BufferedReader _socketIn; |
200 |
< |
PrintWriter _socketOut; |
201 |
< |
NamingContextExt _ncRef; |
196 |
> |
/** |
197 |
> |
* This holds a reference to the |
198 |
> |
* system logger that is being used. |
199 |
> |
*/ |
200 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
201 |
> |
|
202 |
> |
/** |
203 |
> |
* A reference to the Configuration Manager the system is using |
204 |
> |
*/ |
205 |
> |
private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
206 |
> |
|
207 |
> |
/** |
208 |
> |
* This is the friendly identifier of the |
209 |
> |
* component this class is running in. |
210 |
> |
* eg, a Filter may be called "filter1", |
211 |
> |
* If this class does not have an owning |
212 |
> |
* component, a name from the configuration |
213 |
> |
* can be placed here. This name could also |
214 |
> |
* be changed to null for utility classes. |
215 |
> |
*/ |
216 |
> |
private String _name = FilterManager.NAME; |
217 |
> |
|
218 |
> |
/** |
219 |
> |
* The socket this class uses |
220 |
> |
*/ |
221 |
> |
private Socket _socket; |
222 |
> |
|
223 |
> |
/** |
224 |
> |
* Used for the input stream of this socket |
225 |
> |
*/ |
226 |
> |
private BufferedReader _socketIn; |
227 |
> |
|
228 |
> |
/** |
229 |
> |
* Used for the output stream of this socket |
230 |
> |
*/ |
231 |
> |
private PrintWriter _socketOut; |
232 |
> |
|
233 |
|
//---STATIC ATTRIBUTES--- |
234 |
|
|
235 |
|
} |