1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
|
|
|
3 |
|
|
//---IMPORTS--- |
4 |
|
|
import uk.ac.ukc.iscream.core.*; |
5 |
tdb |
1.7 |
import uk.ac.ukc.iscream.filter.*; |
6 |
tdb |
1.1 |
import java.net.Socket; |
7 |
|
|
import java.io.InputStream; |
8 |
|
|
import java.io.OutputStream; |
9 |
|
|
import java.io.IOException; |
10 |
|
|
import java.io.*; |
11 |
tdb |
1.7 |
import org.omg.CORBA.*; |
12 |
|
|
import org.omg.CosNaming.*; |
13 |
tdb |
1.1 |
|
14 |
|
|
/** |
15 |
|
|
* <ONE LINE DESCRIPTION> |
16 |
|
|
* <DETAILED DESCRIPTION> |
17 |
|
|
* |
18 |
|
|
* @author $Author: tdb1 $ |
19 |
ajm |
1.8 |
* @version $Id: HostInit.java,v 1.7 2000/11/23 01:46:17 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 |
ajm |
1.8 |
public final String REVISION = "$Revision: 1.7 $"; |
29 |
tdb |
1.1 |
|
30 |
|
|
//---STATIC METHODS--- |
31 |
|
|
|
32 |
|
|
//---CONSTRUCTORS--- |
33 |
|
|
|
34 |
tdb |
1.7 |
public HostInit(Socket socket, ConfigurationManager configManager, Logger logRef, NamingContextExt ncRef) throws IOException { |
35 |
tdb |
1.6 |
_configManager = configManager; |
36 |
tdb |
1.7 |
_logRef = logRef; |
37 |
tdb |
1.1 |
_socket = socket; |
38 |
tdb |
1.7 |
_ncRef = ncRef; |
39 |
tdb |
1.1 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
40 |
|
|
_socketOut = new PrintWriter(_socket.getOutputStream()); |
41 |
tdb |
1.7 |
_logRef.write(toString(), Logger.SYSINIT, "created"); |
42 |
tdb |
1.1 |
} |
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 |
tdb |
1.7 |
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
53 |
tdb |
1.1 |
} |
54 |
|
|
|
55 |
ajm |
1.8 |
Configuration myConfig = _configManager.getConfiguration("Host." + _socket.getInetAddress().getHostName().toLowerCase()); |
56 |
tdb |
1.1 |
if (myConfig == null) { |
57 |
|
|
_socketOut.println("ERROR"); |
58 |
tdb |
1.7 |
throw new IOException("error in host configuration"); |
59 |
tdb |
1.1 |
} else { |
60 |
|
|
_socketOut.println("OK"); |
61 |
|
|
_socketOut.flush(); |
62 |
|
|
|
63 |
tdb |
1.2 |
// get lastmodified |
64 |
|
|
inBound = _socketIn.readLine(); |
65 |
tdb |
1.4 |
if(!inBound.equals("LASTMODIFIED")) { |
66 |
|
|
// protocol error |
67 |
tdb |
1.2 |
_socketOut.println("ERROR"); |
68 |
|
|
_socketOut.flush(); |
69 |
tdb |
1.7 |
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
70 |
tdb |
1.4 |
} |
71 |
|
|
else { |
72 |
|
|
// send info |
73 |
|
|
_socketOut.println(myConfig.getLastModified()); |
74 |
|
|
_socketOut.flush(); |
75 |
tdb |
1.2 |
} |
76 |
|
|
|
77 |
tdb |
1.7 |
// 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 |
tdb |
1.2 |
// get properties |
92 |
tdb |
1.1 |
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 |
tdb |
1.7 |
_logRef.write(toString(), Logger.SYSMSG, "configured host"); |
109 |
tdb |
1.1 |
_socketOut.println("OK"); |
110 |
|
|
_socketOut.flush(); |
111 |
tdb |
1.7 |
|
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 |
tdb |
1.1 |
} |
147 |
|
|
|
148 |
|
|
} catch (Exception e) { |
149 |
tdb |
1.7 |
_logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
150 |
tdb |
1.1 |
} |
151 |
|
|
|
152 |
|
|
_socketOut.flush(); |
153 |
|
|
// Disconnect streams & socket |
154 |
|
|
try { |
155 |
|
|
_socketIn.close(); |
156 |
|
|
_socketOut.close(); |
157 |
|
|
_socket.close(); |
158 |
|
|
} catch (IOException e) { |
159 |
tdb |
1.7 |
_logRef.write(toString(), Logger.ERROR, "exception on socket close"); |
160 |
tdb |
1.1 |
} |
161 |
tdb |
1.7 |
_logRef.write(toString(), Logger.SYSMSG, "finished"); |
162 |
tdb |
1.1 |
} |
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 |
tdb |
1.6 |
ConfigurationManager _configManager; |
183 |
tdb |
1.7 |
Logger _logRef; |
184 |
tdb |
1.1 |
Socket _socket; |
185 |
|
|
BufferedReader _socketIn; |
186 |
|
|
PrintWriter _socketOut; |
187 |
tdb |
1.7 |
NamingContextExt _ncRef; |
188 |
tdb |
1.1 |
//---STATIC ATTRIBUTES--- |
189 |
|
|
|
190 |
|
|
} |