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.1 |
import java.net.Socket; |
9 |
|
|
import java.io.*; |
10 |
|
|
|
11 |
|
|
/** |
12 |
tdb |
1.11 |
* Handles setting up a host. |
13 |
ajm |
1.12 |
* This class provides a host with appropriate configuration |
14 |
|
|
* and a reference to a Filter to which it should pass data. |
15 |
tdb |
1.1 |
* |
16 |
tdb |
1.10 |
* @author $Author: tdb1 $ |
17 |
ajm |
1.12 |
* @version $Id: HostInit.java,v 1.11 2000/12/07 00:02:17 tdb1 Exp $ |
18 |
tdb |
1.1 |
*/ |
19 |
|
|
class HostInit extends Thread { |
20 |
|
|
|
21 |
|
|
//---FINAL ATTRIBUTES--- |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* The current CVS revision of this class |
25 |
|
|
*/ |
26 |
ajm |
1.12 |
public final String REVISION = "$Revision: 1.11 $"; |
27 |
tdb |
1.1 |
|
28 |
|
|
//---STATIC METHODS--- |
29 |
|
|
|
30 |
|
|
//---CONSTRUCTORS--- |
31 |
|
|
|
32 |
tdb |
1.11 |
public HostInit(Socket socket) throws IOException { |
33 |
tdb |
1.1 |
_socket = socket; |
34 |
|
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
35 |
|
|
_socketOut = new PrintWriter(_socket.getOutputStream()); |
36 |
tdb |
1.11 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
37 |
tdb |
1.1 |
} |
38 |
|
|
|
39 |
|
|
//---PUBLIC METHODS--- |
40 |
|
|
|
41 |
|
|
public void run() { |
42 |
|
|
try { |
43 |
|
|
String inBound = _socketIn.readLine(); |
44 |
|
|
if (!inBound.equals("STARTCONFIG")) { |
45 |
|
|
_socketOut.println("ERROR"); |
46 |
|
|
_socketOut.flush(); |
47 |
tdb |
1.7 |
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
48 |
tdb |
1.1 |
} |
49 |
|
|
|
50 |
ajm |
1.8 |
Configuration myConfig = _configManager.getConfiguration("Host." + _socket.getInetAddress().getHostName().toLowerCase()); |
51 |
tdb |
1.1 |
if (myConfig == null) { |
52 |
|
|
_socketOut.println("ERROR"); |
53 |
tdb |
1.7 |
throw new IOException("error in host configuration"); |
54 |
tdb |
1.1 |
} else { |
55 |
|
|
_socketOut.println("OK"); |
56 |
|
|
_socketOut.flush(); |
57 |
|
|
|
58 |
tdb |
1.2 |
// get lastmodified |
59 |
|
|
inBound = _socketIn.readLine(); |
60 |
tdb |
1.4 |
if(!inBound.equals("LASTMODIFIED")) { |
61 |
|
|
// protocol error |
62 |
tdb |
1.2 |
_socketOut.println("ERROR"); |
63 |
|
|
_socketOut.flush(); |
64 |
tdb |
1.7 |
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
65 |
tdb |
1.4 |
} |
66 |
|
|
else { |
67 |
|
|
// send info |
68 |
|
|
_socketOut.println(myConfig.getLastModified()); |
69 |
|
|
_socketOut.flush(); |
70 |
tdb |
1.2 |
} |
71 |
|
|
|
72 |
tdb |
1.7 |
// get config fileList |
73 |
|
|
inBound = _socketIn.readLine(); |
74 |
|
|
if(!inBound.equals("FILELIST")) { |
75 |
|
|
// protocol error |
76 |
|
|
_socketOut.println("ERROR"); |
77 |
|
|
_socketOut.flush(); |
78 |
|
|
throw new IOException("protocol error - expected:FILELIST got:" + inBound); |
79 |
|
|
} |
80 |
|
|
else { |
81 |
|
|
// send info |
82 |
|
|
_socketOut.println(myConfig.getFileList()); |
83 |
|
|
_socketOut.flush(); |
84 |
|
|
} |
85 |
|
|
|
86 |
tdb |
1.2 |
// get properties |
87 |
tdb |
1.1 |
inBound = _socketIn.readLine(); |
88 |
|
|
while(!inBound.equals("ENDCONFIG")) { |
89 |
|
|
|
90 |
|
|
// get the property |
91 |
|
|
try { |
92 |
tdb |
1.10 |
String returnedProperty = myConfig.getProperty("Host."+inBound); |
93 |
tdb |
1.1 |
|
94 |
|
|
_socketOut.println(returnedProperty); |
95 |
|
|
_socketOut.flush(); |
96 |
|
|
|
97 |
|
|
} catch (org.omg.CORBA.MARSHAL e) { |
98 |
|
|
_socketOut.println("ERROR"); |
99 |
|
|
_socketOut.flush(); |
100 |
|
|
} |
101 |
|
|
inBound = _socketIn.readLine(); |
102 |
|
|
} |
103 |
tdb |
1.11 |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
104 |
tdb |
1.1 |
_socketOut.println("OK"); |
105 |
|
|
_socketOut.flush(); |
106 |
tdb |
1.7 |
|
107 |
|
|
// get filter reference |
108 |
|
|
inBound = _socketIn.readLine(); |
109 |
|
|
if(!inBound.equals("FILTER")) { |
110 |
|
|
// protocol error |
111 |
|
|
_socketOut.println("ERROR"); |
112 |
|
|
_socketOut.flush(); |
113 |
|
|
throw new IOException("protocol error - expected:FILTER got:" + inBound); |
114 |
|
|
} |
115 |
|
|
else { |
116 |
|
|
// send info |
117 |
|
|
String parentFilter = myConfig.getProperty("Host.filter"); |
118 |
tdb |
1.11 |
_logger.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter); |
119 |
|
|
Filter filter = FilterHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.Filter." + parentFilter)); |
120 |
tdb |
1.7 |
_socketOut.println(filter.getHostName() + ";" |
121 |
|
|
+ filter.getUDPPort() + ";" |
122 |
|
|
+ filter.getTCPPort()); |
123 |
|
|
_socketOut.flush(); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
// confirm that all is ok |
127 |
|
|
inBound = _socketIn.readLine(); |
128 |
|
|
if(!inBound.equals("END")) { |
129 |
|
|
// protocol error |
130 |
|
|
_socketOut.println("ERROR"); |
131 |
|
|
_socketOut.flush(); |
132 |
|
|
throw new IOException("protocol error - expected:END got:" + inBound); |
133 |
|
|
} |
134 |
|
|
else { |
135 |
|
|
// send ok |
136 |
|
|
_socketOut.println("OK"); |
137 |
|
|
_socketOut.flush(); |
138 |
|
|
} |
139 |
|
|
|
140 |
tdb |
1.1 |
} |
141 |
|
|
|
142 |
|
|
} catch (Exception e) { |
143 |
ajm |
1.12 |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e.getMessage()); |
144 |
tdb |
1.1 |
} |
145 |
|
|
|
146 |
|
|
_socketOut.flush(); |
147 |
|
|
// Disconnect streams & socket |
148 |
|
|
try { |
149 |
|
|
_socketIn.close(); |
150 |
|
|
_socketOut.close(); |
151 |
|
|
_socket.close(); |
152 |
|
|
} catch (IOException e) { |
153 |
tdb |
1.11 |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
154 |
tdb |
1.1 |
} |
155 |
tdb |
1.11 |
_logger.write(toString(), Logger.SYSMSG, "finished"); |
156 |
tdb |
1.1 |
} |
157 |
|
|
|
158 |
|
|
/** |
159 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
160 |
|
|
* method to provide clean logging (every class should have this). |
161 |
|
|
* |
162 |
ajm |
1.12 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
163 |
|
|
* to format the toString() |
164 |
|
|
* |
165 |
tdb |
1.1 |
* @return the name of this class and its CVS revision |
166 |
|
|
*/ |
167 |
|
|
public String toString() { |
168 |
ajm |
1.12 |
return FormatName.getName( |
169 |
|
|
_name, |
170 |
|
|
this.getClass().getName(), |
171 |
|
|
REVISION); |
172 |
tdb |
1.1 |
} |
173 |
|
|
|
174 |
|
|
//---PRIVATE METHODS--- |
175 |
|
|
|
176 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
177 |
|
|
|
178 |
|
|
//---ATTRIBUTES--- |
179 |
|
|
|
180 |
tdb |
1.11 |
/** |
181 |
ajm |
1.12 |
* This holds a reference to the |
182 |
|
|
* system logger that is being used. |
183 |
tdb |
1.11 |
*/ |
184 |
ajm |
1.12 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
185 |
tdb |
1.11 |
|
186 |
|
|
/** |
187 |
|
|
* A reference to the Configuration Manager the system is using |
188 |
|
|
*/ |
189 |
ajm |
1.12 |
private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
190 |
|
|
|
191 |
|
|
/** |
192 |
|
|
* This is the friendly identifier of the |
193 |
|
|
* component this class is running in. |
194 |
|
|
* eg, a Filter may be called "filter1", |
195 |
|
|
* If this class does not have an owning |
196 |
|
|
* component, a name from the configuration |
197 |
|
|
* can be placed here. This name could also |
198 |
|
|
* be changed to null for utility classes. |
199 |
|
|
*/ |
200 |
|
|
private String _name = FilterManager.NAME; |
201 |
|
|
|
202 |
|
|
/** |
203 |
|
|
* The socket this class uses |
204 |
|
|
*/ |
205 |
|
|
private Socket _socket; |
206 |
|
|
|
207 |
|
|
/** |
208 |
|
|
* Used for the input stream of this socket |
209 |
|
|
*/ |
210 |
|
|
private BufferedReader _socketIn; |
211 |
tdb |
1.11 |
|
212 |
ajm |
1.12 |
/** |
213 |
|
|
* Used for the output stream of this socket |
214 |
|
|
*/ |
215 |
|
|
private PrintWriter _socketOut; |
216 |
tdb |
1.11 |
|
217 |
tdb |
1.1 |
//---STATIC ATTRIBUTES--- |
218 |
|
|
|
219 |
|
|
} |