1 |
//---PACKAGE DECLARATION--- |
2 |
|
3 |
//---IMPORTS--- |
4 |
import uk.ac.ukc.iscream.core.*; |
5 |
import java.net.Socket; |
6 |
import java.io.InputStream; |
7 |
import java.io.OutputStream; |
8 |
import java.io.IOException; |
9 |
import java.io.*; |
10 |
|
11 |
/** |
12 |
* <ONE LINE DESCRIPTION> |
13 |
* <DETAILED DESCRIPTION> |
14 |
* |
15 |
* @author $Author: tdb1 $ |
16 |
* @version $Id: HostInit.java,v 1.1 2000/11/13 20:40:40 tdb1 Exp $ |
17 |
*/ |
18 |
class HostInit extends Thread { |
19 |
|
20 |
//---FINAL ATTRIBUTES--- |
21 |
|
22 |
/** |
23 |
* The current CVS revision of this class |
24 |
*/ |
25 |
public final String REVISION = "$Revision: 1.1 $"; |
26 |
|
27 |
//---STATIC METHODS--- |
28 |
|
29 |
//---CONSTRUCTORS--- |
30 |
|
31 |
public HostInit(Socket socket, Configurator configurator, Logger logger) throws IOException { |
32 |
_configurator = configurator; |
33 |
_logger = logger; |
34 |
_socket = socket; |
35 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
36 |
_socketOut = new PrintWriter(_socket.getOutputStream()); |
37 |
_logger.write(toString(), "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"); |
49 |
} |
50 |
|
51 |
Configuration myConfig = _configurator.getConfiguration(_socket.getInetAddress().getHostName().toLowerCase()); |
52 |
if (myConfig == null) { |
53 |
_socketOut.println("ERROR"); |
54 |
throw new IOException("No Configuration File For Host"); |
55 |
} else { |
56 |
_socketOut.println("OK"); |
57 |
_socketOut.flush(); |
58 |
|
59 |
// get lastmodified |
60 |
inBound = _socketIn.readLine(); |
61 |
while(!inBound.equals("LASTMODIFIED")) { |
62 |
_socketOut.println("ERROR"); |
63 |
_socketOut.flush(); |
64 |
} |
65 |
_socketOut.println(myConfig.getLastModified()); |
66 |
_socketOut.flush(); |
67 |
|
68 |
// get properties |
69 |
inBound = _socketIn.readLine(); |
70 |
while(!inBound.equals("ENDCONFIG")) { |
71 |
|
72 |
// get the property |
73 |
try { |
74 |
String returnedProperty = myConfig.getProperty(inBound); |
75 |
|
76 |
_socketOut.println(returnedProperty); |
77 |
_socketOut.flush(); |
78 |
|
79 |
} catch (org.omg.CORBA.MARSHAL e) { |
80 |
_socketOut.println("ERROR"); |
81 |
_socketOut.flush(); |
82 |
} |
83 |
inBound = _socketIn.readLine(); |
84 |
} |
85 |
_logger.write(toString(), "configured host"); |
86 |
_socketOut.println("OK"); |
87 |
_socketOut.flush(); |
88 |
} |
89 |
|
90 |
} catch (Exception e) { |
91 |
_logger.write(toString(), e.toString()); |
92 |
} |
93 |
|
94 |
_socketOut.flush(); |
95 |
// Disconnect streams & socket |
96 |
try { |
97 |
_socketIn.close(); |
98 |
_socketOut.close(); |
99 |
_socket.close(); |
100 |
} catch (IOException e) { |
101 |
_logger.write(toString(), "exception on socket close"); |
102 |
} |
103 |
_logger.write(toString(), "finished"); |
104 |
} |
105 |
|
106 |
/** |
107 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
108 |
* method to provide clean logging (every class should have this). |
109 |
* |
110 |
* @return the name of this class and its CVS revision |
111 |
*/ |
112 |
public String toString() { |
113 |
return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName() |
114 |
+ "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
115 |
|
116 |
} |
117 |
|
118 |
//---PRIVATE METHODS--- |
119 |
|
120 |
//---ACCESSOR/MUTATOR METHODS--- |
121 |
|
122 |
//---ATTRIBUTES--- |
123 |
|
124 |
Configurator _configurator; |
125 |
Logger _logger; |
126 |
Socket _socket; |
127 |
BufferedReader _socketIn; |
128 |
PrintWriter _socketOut; |
129 |
|
130 |
//---STATIC ATTRIBUTES--- |
131 |
|
132 |
} |