1 |
//---PACKAGE DECLARATION--- |
2 |
|
3 |
//---IMPORTS--- |
4 |
import uk.ac.ukc.iscream.core.*; |
5 |
import uk.ac.ukc.iscream.filter.*; |
6 |
import java.net.Socket; |
7 |
import java.io.InputStream; |
8 |
import java.io.OutputStream; |
9 |
import java.io.IOException; |
10 |
import java.io.*; |
11 |
import org.omg.CORBA.*; |
12 |
import org.omg.CosNaming.*; |
13 |
|
14 |
/** |
15 |
* <ONE LINE DESCRIPTION> |
16 |
* <DETAILED DESCRIPTION> |
17 |
* |
18 |
* @author $Author: tdb1 $ |
19 |
* @version $Id: HostInit.java,v 1.6 2000/11/20 18:55:24 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.6 $"; |
29 |
|
30 |
//---STATIC METHODS--- |
31 |
|
32 |
//---CONSTRUCTORS--- |
33 |
|
34 |
public HostInit(Socket socket, ConfigurationManager configManager, Logger logRef, NamingContextExt ncRef) throws IOException { |
35 |
_configManager = configManager; |
36 |
_logRef = logRef; |
37 |
_socket = socket; |
38 |
_ncRef = ncRef; |
39 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
40 |
_socketOut = new PrintWriter(_socket.getOutputStream()); |
41 |
_logRef.write(toString(), Logger.SYSINIT, "created"); |
42 |
} |
43 |
|
44 |
//---PUBLIC METHODS--- |
45 |
|
46 |
public void run() { |
47 |
try { |
48 |
String inBound = _socketIn.readLine(); |
49 |
if (!inBound.equals("STARTCONFIG")) { |
50 |
_socketOut.println("ERROR"); |
51 |
_socketOut.flush(); |
52 |
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
53 |
} |
54 |
|
55 |
Configuration myConfig = _configManager.getConfiguration(_socket.getInetAddress().getHostName().toLowerCase()); |
56 |
if (myConfig == null) { |
57 |
_socketOut.println("ERROR"); |
58 |
throw new IOException("error in host configuration"); |
59 |
} else { |
60 |
_socketOut.println("OK"); |
61 |
_socketOut.flush(); |
62 |
|
63 |
// get lastmodified |
64 |
inBound = _socketIn.readLine(); |
65 |
if(!inBound.equals("LASTMODIFIED")) { |
66 |
// protocol error |
67 |
_socketOut.println("ERROR"); |
68 |
_socketOut.flush(); |
69 |
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
70 |
} |
71 |
else { |
72 |
// send info |
73 |
_socketOut.println(myConfig.getLastModified()); |
74 |
_socketOut.flush(); |
75 |
} |
76 |
|
77 |
// get config fileList |
78 |
inBound = _socketIn.readLine(); |
79 |
if(!inBound.equals("FILELIST")) { |
80 |
// protocol error |
81 |
_socketOut.println("ERROR"); |
82 |
_socketOut.flush(); |
83 |
throw new IOException("protocol error - expected:FILELIST got:" + inBound); |
84 |
} |
85 |
else { |
86 |
// send info |
87 |
_socketOut.println(myConfig.getFileList()); |
88 |
_socketOut.flush(); |
89 |
} |
90 |
|
91 |
// get properties |
92 |
inBound = _socketIn.readLine(); |
93 |
while(!inBound.equals("ENDCONFIG")) { |
94 |
|
95 |
// get the property |
96 |
try { |
97 |
String returnedProperty = myConfig.getProperty(inBound); |
98 |
|
99 |
_socketOut.println(returnedProperty); |
100 |
_socketOut.flush(); |
101 |
|
102 |
} catch (org.omg.CORBA.MARSHAL e) { |
103 |
_socketOut.println("ERROR"); |
104 |
_socketOut.flush(); |
105 |
} |
106 |
inBound = _socketIn.readLine(); |
107 |
} |
108 |
_logRef.write(toString(), Logger.SYSMSG, "configured host"); |
109 |
_socketOut.println("OK"); |
110 |
_socketOut.flush(); |
111 |
|
112 |
// get filter reference |
113 |
inBound = _socketIn.readLine(); |
114 |
if(!inBound.equals("FILTER")) { |
115 |
// protocol error |
116 |
_socketOut.println("ERROR"); |
117 |
_socketOut.flush(); |
118 |
throw new IOException("protocol error - expected:FILTER got:" + inBound); |
119 |
} |
120 |
else { |
121 |
// send info |
122 |
String parentFilter = myConfig.getProperty("Host.filter"); |
123 |
_logRef.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter); |
124 |
org.omg.CORBA.Object objRef = _ncRef.resolve(_ncRef.to_name("iscream.Filter." + parentFilter)); |
125 |
Filter filter = FilterHelper.narrow(objRef); |
126 |
_socketOut.println(filter.getHostName() + ";" |
127 |
+ filter.getUDPPort() + ";" |
128 |
+ filter.getTCPPort()); |
129 |
_socketOut.flush(); |
130 |
} |
131 |
|
132 |
// confirm that all is ok |
133 |
inBound = _socketIn.readLine(); |
134 |
if(!inBound.equals("END")) { |
135 |
// protocol error |
136 |
_socketOut.println("ERROR"); |
137 |
_socketOut.flush(); |
138 |
throw new IOException("protocol error - expected:END got:" + inBound); |
139 |
} |
140 |
else { |
141 |
// send ok |
142 |
_socketOut.println("OK"); |
143 |
_socketOut.flush(); |
144 |
} |
145 |
|
146 |
} |
147 |
|
148 |
} catch (Exception e) { |
149 |
_logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
150 |
} |
151 |
|
152 |
_socketOut.flush(); |
153 |
// Disconnect streams & socket |
154 |
try { |
155 |
_socketIn.close(); |
156 |
_socketOut.close(); |
157 |
_socket.close(); |
158 |
} catch (IOException e) { |
159 |
_logRef.write(toString(), Logger.ERROR, "exception on socket close"); |
160 |
} |
161 |
_logRef.write(toString(), Logger.SYSMSG, "finished"); |
162 |
} |
163 |
|
164 |
/** |
165 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
166 |
* method to provide clean logging (every class should have this). |
167 |
* |
168 |
* @return the name of this class and its CVS revision |
169 |
*/ |
170 |
public String toString() { |
171 |
return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName() |
172 |
+ "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
173 |
|
174 |
} |
175 |
|
176 |
//---PRIVATE METHODS--- |
177 |
|
178 |
//---ACCESSOR/MUTATOR METHODS--- |
179 |
|
180 |
//---ATTRIBUTES--- |
181 |
|
182 |
ConfigurationManager _configManager; |
183 |
Logger _logRef; |
184 |
Socket _socket; |
185 |
BufferedReader _socketIn; |
186 |
PrintWriter _socketOut; |
187 |
NamingContextExt _ncRef; |
188 |
//---STATIC ATTRIBUTES--- |
189 |
|
190 |
} |