2 |
|
|
3 |
|
//---IMPORTS--- |
4 |
|
import uk.ac.ukc.iscream.core.*; |
5 |
+ |
import uk.ac.ukc.iscream.filter.*; |
6 |
|
import java.net.Socket; |
7 |
|
import java.io.InputStream; |
8 |
|
import java.io.OutputStream; |
9 |
|
import java.io.IOException; |
10 |
|
import java.io.*; |
11 |
+ |
import org.omg.CORBA.*; |
12 |
+ |
import org.omg.CosNaming.*; |
13 |
|
|
14 |
|
/** |
15 |
|
* <ONE LINE DESCRIPTION> |
31 |
|
|
32 |
|
//---CONSTRUCTORS--- |
33 |
|
|
34 |
< |
public HostInit(Socket socket, ConfigurationManager configManager, Logger logger) throws IOException { |
34 |
> |
public HostInit(Socket socket, ConfigurationManager configManager, Logger logRef, NamingContextExt ncRef) throws IOException { |
35 |
|
_configManager = configManager; |
36 |
< |
_logger = logger; |
36 |
> |
_logRef = logRef; |
37 |
|
_socket = socket; |
38 |
+ |
_ncRef = ncRef; |
39 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
40 |
|
_socketOut = new PrintWriter(_socket.getOutputStream()); |
41 |
< |
_logger.write(toString(), Logger.SYSINIT, "created"); |
41 |
> |
_logRef.write(toString(), Logger.SYSINIT, "created"); |
42 |
|
} |
43 |
|
|
44 |
|
//---PUBLIC METHODS--- |
49 |
|
if (!inBound.equals("STARTCONFIG")) { |
50 |
|
_socketOut.println("ERROR"); |
51 |
|
_socketOut.flush(); |
52 |
< |
throw new IOException("Protocol Error"); |
52 |
> |
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
53 |
|
} |
54 |
|
|
55 |
|
Configuration myConfig = _configManager.getConfiguration(_socket.getInetAddress().getHostName().toLowerCase()); |
56 |
|
if (myConfig == null) { |
57 |
|
_socketOut.println("ERROR"); |
58 |
< |
throw new IOException("No Configuration File For Host"); |
58 |
> |
throw new IOException("error in host configuration"); |
59 |
|
} else { |
60 |
|
_socketOut.println("OK"); |
61 |
|
_socketOut.flush(); |
66 |
|
// protocol error |
67 |
|
_socketOut.println("ERROR"); |
68 |
|
_socketOut.flush(); |
69 |
< |
throw new IOException("Protocol Error"); |
69 |
> |
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
70 |
|
} |
71 |
|
else { |
72 |
|
// send info |
74 |
|
_socketOut.flush(); |
75 |
|
} |
76 |
|
|
77 |
+ |
// get config fileList |
78 |
+ |
inBound = _socketIn.readLine(); |
79 |
+ |
if(!inBound.equals("FILELIST")) { |
80 |
+ |
// protocol error |
81 |
+ |
_socketOut.println("ERROR"); |
82 |
+ |
_socketOut.flush(); |
83 |
+ |
throw new IOException("protocol error - expected:FILELIST got:" + inBound); |
84 |
+ |
} |
85 |
+ |
else { |
86 |
+ |
// send info |
87 |
+ |
_socketOut.println(myConfig.getFileList()); |
88 |
+ |
_socketOut.flush(); |
89 |
+ |
} |
90 |
+ |
|
91 |
|
// get properties |
92 |
|
inBound = _socketIn.readLine(); |
93 |
|
while(!inBound.equals("ENDCONFIG")) { |
105 |
|
} |
106 |
|
inBound = _socketIn.readLine(); |
107 |
|
} |
108 |
< |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
108 |
> |
_logRef.write(toString(), Logger.SYSMSG, "configured host"); |
109 |
|
_socketOut.println("OK"); |
110 |
|
_socketOut.flush(); |
111 |
+ |
|
112 |
+ |
// get filter reference |
113 |
+ |
inBound = _socketIn.readLine(); |
114 |
+ |
if(!inBound.equals("FILTER")) { |
115 |
+ |
// protocol error |
116 |
+ |
_socketOut.println("ERROR"); |
117 |
+ |
_socketOut.flush(); |
118 |
+ |
throw new IOException("protocol error - expected:FILTER got:" + inBound); |
119 |
+ |
} |
120 |
+ |
else { |
121 |
+ |
// send info |
122 |
+ |
String parentFilter = myConfig.getProperty("Host.filter"); |
123 |
+ |
_logRef.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter); |
124 |
+ |
org.omg.CORBA.Object objRef = _ncRef.resolve(_ncRef.to_name("iscream.Filter." + parentFilter)); |
125 |
+ |
Filter filter = FilterHelper.narrow(objRef); |
126 |
+ |
_socketOut.println(filter.getHostName() + ";" |
127 |
+ |
+ filter.getUDPPort() + ";" |
128 |
+ |
+ filter.getTCPPort()); |
129 |
+ |
_socketOut.flush(); |
130 |
+ |
} |
131 |
+ |
|
132 |
+ |
// confirm that all is ok |
133 |
+ |
inBound = _socketIn.readLine(); |
134 |
+ |
if(!inBound.equals("END")) { |
135 |
+ |
// protocol error |
136 |
+ |
_socketOut.println("ERROR"); |
137 |
+ |
_socketOut.flush(); |
138 |
+ |
throw new IOException("protocol error - expected:END got:" + inBound); |
139 |
+ |
} |
140 |
+ |
else { |
141 |
+ |
// send ok |
142 |
+ |
_socketOut.println("OK"); |
143 |
+ |
_socketOut.flush(); |
144 |
+ |
} |
145 |
+ |
|
146 |
|
} |
147 |
|
|
148 |
|
} catch (Exception e) { |
149 |
< |
_logger.write(toString(), Logger.ERROR, e.toString()); |
149 |
> |
_logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
150 |
|
} |
151 |
|
|
152 |
|
_socketOut.flush(); |
156 |
|
_socketOut.close(); |
157 |
|
_socket.close(); |
158 |
|
} catch (IOException e) { |
159 |
< |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
159 |
> |
_logRef.write(toString(), Logger.ERROR, "exception on socket close"); |
160 |
|
} |
161 |
< |
_logger.write(toString(), Logger.SYSMSG, "finished"); |
161 |
> |
_logRef.write(toString(), Logger.SYSMSG, "finished"); |
162 |
|
} |
163 |
|
|
164 |
|
/** |
180 |
|
//---ATTRIBUTES--- |
181 |
|
|
182 |
|
ConfigurationManager _configManager; |
183 |
< |
Logger _logger; |
183 |
> |
Logger _logRef; |
184 |
|
Socket _socket; |
185 |
|
BufferedReader _socketIn; |
186 |
|
PrintWriter _socketOut; |
187 |
< |
|
187 |
> |
NamingContextExt _ncRef; |
188 |
|
//---STATIC ATTRIBUTES--- |
189 |
|
|
190 |
|
} |