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