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; |
8 |
> |
import uk.ac.ukc.iscream.componentmanager.*; |
9 |
> |
import java.net.*; |
10 |
|
import java.io.*; |
11 |
|
|
12 |
|
/** |
30 |
|
|
31 |
|
//---CONSTRUCTORS--- |
32 |
|
|
33 |
+ |
/** |
34 |
+ |
* Construct a new HostInit. |
35 |
+ |
* |
36 |
+ |
* @param socket The socket to which the host is connected |
37 |
+ |
*/ |
38 |
|
public HostInit(Socket socket) throws IOException { |
39 |
|
_socket = socket; |
40 |
+ |
// setup reader & writer |
41 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
42 |
< |
_socketOut = new PrintWriter(_socket.getOutputStream()); |
42 |
> |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
43 |
|
_logger.write(toString(), Logger.SYSINIT, "created"); |
44 |
|
} |
45 |
|
|
46 |
|
//---PUBLIC METHODS--- |
47 |
|
|
48 |
+ |
/** |
49 |
+ |
* Main method in this class, which handles communicating with |
50 |
+ |
* the host to determine it's setup. |
51 |
+ |
*/ |
52 |
|
public void run() { |
53 |
|
try { |
54 |
|
String inBound = _socketIn.readLine(); |
55 |
|
if (!inBound.equals("STARTCONFIG")) { |
56 |
|
_socketOut.println("ERROR"); |
46 |
– |
_socketOut.flush(); |
57 |
|
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
58 |
|
} |
59 |
|
|
63 |
|
throw new IOException("error in host configuration"); |
64 |
|
} else { |
65 |
|
_socketOut.println("OK"); |
56 |
– |
_socketOut.flush(); |
66 |
|
|
67 |
|
// get lastmodified |
68 |
|
inBound = _socketIn.readLine(); |
69 |
|
if(!inBound.equals("LASTMODIFIED")) { |
70 |
|
// protocol error |
71 |
|
_socketOut.println("ERROR"); |
63 |
– |
_socketOut.flush(); |
72 |
|
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
73 |
|
} |
74 |
|
else { |
75 |
|
// send info |
76 |
|
_socketOut.println(myConfig.getLastModified()); |
69 |
– |
_socketOut.flush(); |
77 |
|
} |
78 |
|
|
79 |
|
// get config fileList |
81 |
|
if(!inBound.equals("FILELIST")) { |
82 |
|
// protocol error |
83 |
|
_socketOut.println("ERROR"); |
77 |
– |
_socketOut.flush(); |
84 |
|
throw new IOException("protocol error - expected:FILELIST got:" + inBound); |
85 |
|
} |
86 |
|
else { |
87 |
|
// send info |
88 |
|
_socketOut.println(myConfig.getFileList()); |
83 |
– |
_socketOut.flush(); |
89 |
|
} |
90 |
< |
|
90 |
> |
|
91 |
> |
// send the FQDN to the host |
92 |
> |
inBound = _socketIn.readLine(); |
93 |
> |
if(!inBound.equals("FQDN")) { |
94 |
> |
// protocol error |
95 |
> |
_socketOut.println("ERROR"); |
96 |
> |
throw new IOException("protocol error - expected:FQDN got:" + inBound); |
97 |
> |
} |
98 |
> |
else { |
99 |
> |
// send the fqdn (of the host) to the host |
100 |
> |
_socketOut.println(_socket.getInetAddress().getHostName()); |
101 |
> |
} |
102 |
> |
|
103 |
|
// get properties |
104 |
|
inBound = _socketIn.readLine(); |
105 |
|
while(!inBound.equals("ENDCONFIG")) { |
109 |
|
String returnedProperty = myConfig.getProperty("Host."+inBound); |
110 |
|
|
111 |
|
_socketOut.println(returnedProperty); |
95 |
– |
_socketOut.flush(); |
112 |
|
|
113 |
|
} catch (org.omg.CORBA.MARSHAL e) { |
114 |
|
_socketOut.println("ERROR"); |
99 |
– |
_socketOut.flush(); |
115 |
|
} |
116 |
|
inBound = _socketIn.readLine(); |
117 |
|
} |
118 |
|
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
119 |
|
_socketOut.println("OK"); |
105 |
– |
_socketOut.flush(); |
120 |
|
|
121 |
|
// get filter reference |
122 |
|
inBound = _socketIn.readLine(); |
123 |
|
if(!inBound.equals("FILTER")) { |
124 |
|
// protocol error |
125 |
|
_socketOut.println("ERROR"); |
112 |
– |
_socketOut.flush(); |
126 |
|
throw new IOException("protocol error - expected:FILTER got:" + inBound); |
127 |
|
} |
128 |
|
else { |
133 |
|
_socketOut.println(filter.getHostName() + ";" |
134 |
|
+ filter.getUDPPort() + ";" |
135 |
|
+ filter.getTCPPort()); |
123 |
– |
_socketOut.flush(); |
136 |
|
} |
137 |
|
|
138 |
|
// confirm that all is ok |
140 |
|
if(!inBound.equals("END")) { |
141 |
|
// protocol error |
142 |
|
_socketOut.println("ERROR"); |
131 |
– |
_socketOut.flush(); |
143 |
|
throw new IOException("protocol error - expected:END got:" + inBound); |
144 |
|
} |
145 |
|
else { |
146 |
|
// send ok |
147 |
|
_socketOut.println("OK"); |
137 |
– |
_socketOut.flush(); |
148 |
|
} |
149 |
|
|
150 |
|
} |
151 |
|
|
152 |
|
} catch (Exception e) { |
153 |
< |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e.getMessage()); |
153 |
> |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e); |
154 |
|
} |
155 |
|
|
156 |
|
_socketOut.flush(); |
177 |
|
public String toString() { |
178 |
|
return FormatName.getName( |
179 |
|
_name, |
180 |
< |
this.getClass().getName(), |
180 |
> |
getClass().getName(), |
181 |
|
REVISION); |
182 |
|
} |
183 |
|
|