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