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