| 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 uk.ac.ukc.iscream.componentmanager.*; |
| 9 |
import java.net.*; |
| 10 |
import java.io.*; |
| 11 |
import java.util.*; |
| 12 |
|
| 13 |
/** |
| 14 |
* Handles setting up a host. |
| 15 |
* This class provides a host with appropriate configuration |
| 16 |
* and a reference to a Filter to which it should pass data. |
| 17 |
* |
| 18 |
* @author $Author: tdb1 $ |
| 19 |
* @version $Id: HostInit.java,v 1.24 2001/03/05 23:53:39 tdb1 Exp $ |
| 20 |
*/ |
| 21 |
class HostInit extends Thread { |
| 22 |
|
| 23 |
//---FINAL ATTRIBUTES--- |
| 24 |
|
| 25 |
/** |
| 26 |
* The current CVS revision of this class |
| 27 |
*/ |
| 28 |
public final String REVISION = "$Revision: 1.24 $"; |
| 29 |
|
| 30 |
//---STATIC METHODS--- |
| 31 |
|
| 32 |
//---CONSTRUCTORS--- |
| 33 |
|
| 34 |
/** |
| 35 |
* Construct a new HostInit. |
| 36 |
* |
| 37 |
* @param socket The socket to which the host is connected |
| 38 |
*/ |
| 39 |
public HostInit(Socket socket) throws IOException { |
| 40 |
// set the Thread name |
| 41 |
setName("filtermanager.HostInit"); |
| 42 |
|
| 43 |
_socket = socket; |
| 44 |
// setup reader & writer |
| 45 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
| 46 |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
| 47 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
| 48 |
} |
| 49 |
|
| 50 |
//---PUBLIC METHODS--- |
| 51 |
|
| 52 |
/** |
| 53 |
* Main method in this class, which handles communicating with |
| 54 |
* the host to determine it's setup. |
| 55 |
*/ |
| 56 |
public void run() { |
| 57 |
try { |
| 58 |
String inBound = _socketIn.readLine(); |
| 59 |
if (!inBound.equals("STARTCONFIG")) { |
| 60 |
_socketOut.println("ERROR"); |
| 61 |
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
| 62 |
} |
| 63 |
|
| 64 |
Configuration myConfig = _configManager.getConfiguration("Host." + _socket.getInetAddress().getHostName().toLowerCase()); |
| 65 |
if (myConfig == null) { |
| 66 |
_socketOut.println("ERROR"); |
| 67 |
throw new IOException("error in host configuration"); |
| 68 |
} else { |
| 69 |
_socketOut.println("OK"); |
| 70 |
|
| 71 |
// get lastmodified |
| 72 |
inBound = _socketIn.readLine(); |
| 73 |
if(!inBound.equals("LASTMODIFIED")) { |
| 74 |
// protocol error |
| 75 |
_socketOut.println("ERROR"); |
| 76 |
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
| 77 |
} |
| 78 |
else { |
| 79 |
// send info |
| 80 |
_socketOut.println(myConfig.getLastModified()); |
| 81 |
} |
| 82 |
|
| 83 |
// get config fileList |
| 84 |
inBound = _socketIn.readLine(); |
| 85 |
if(!inBound.equals("FILELIST")) { |
| 86 |
// protocol error |
| 87 |
_socketOut.println("ERROR"); |
| 88 |
throw new IOException("protocol error - expected:FILELIST got:" + inBound); |
| 89 |
} |
| 90 |
else { |
| 91 |
// send info |
| 92 |
_socketOut.println(myConfig.getFileList()); |
| 93 |
} |
| 94 |
|
| 95 |
// send the FQDN to the host |
| 96 |
inBound = _socketIn.readLine(); |
| 97 |
if(!inBound.equals("FQDN")) { |
| 98 |
// protocol error |
| 99 |
_socketOut.println("ERROR"); |
| 100 |
throw new IOException("protocol error - expected:FQDN got:" + inBound); |
| 101 |
} |
| 102 |
else { |
| 103 |
// send the fqdn (of the host) to the host |
| 104 |
_socketOut.println(_socket.getInetAddress().getHostName().toLowerCase()); |
| 105 |
} |
| 106 |
|
| 107 |
// get properties |
| 108 |
inBound = _socketIn.readLine(); |
| 109 |
while(!inBound.equals("ENDCONFIG")) { |
| 110 |
|
| 111 |
// get the property |
| 112 |
try { |
| 113 |
String returnedProperty = myConfig.getProperty("Host."+inBound); |
| 114 |
|
| 115 |
_socketOut.println(returnedProperty); |
| 116 |
|
| 117 |
} catch (org.omg.CORBA.MARSHAL e) { |
| 118 |
_socketOut.println("ERROR"); |
| 119 |
} |
| 120 |
inBound = _socketIn.readLine(); |
| 121 |
} |
| 122 |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
| 123 |
_socketOut.println("OK"); |
| 124 |
|
| 125 |
// get filter reference |
| 126 |
inBound = _socketIn.readLine(); |
| 127 |
if(!inBound.equals("FILTER")) { |
| 128 |
// protocol error |
| 129 |
_socketOut.println("ERROR"); |
| 130 |
throw new IOException("protocol error - expected:FILTER got:" + inBound); |
| 131 |
} |
| 132 |
else { |
| 133 |
// send info |
| 134 |
String filterList = myConfig.getProperty("Host.filter"); |
| 135 |
Filter filterRef = null; |
| 136 |
String filter = null; |
| 137 |
StringTokenizer st = new StringTokenizer(filterList, ";"); |
| 138 |
while (filterRef==null && st.hasMoreTokens()) { |
| 139 |
filter = st.nextToken(); |
| 140 |
_logger.write(toString(), Logger.DEBUG, " looking for filter- " + filter); |
| 141 |
try { |
| 142 |
filterRef = FilterHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.Filter." + filter)); |
| 143 |
} catch (ComponentCORBAException e) { |
| 144 |
_logger.write(toString(), Logger.DEBUG, " unable to find filter- " + filter); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
// hopefully we found a filter |
| 149 |
if(filterRef != null) { |
| 150 |
_logger.write(toString(), Logger.DEBUG, " found filter- " + filter); |
| 151 |
// tell the host about it... |
| 152 |
_socketOut.println(filterRef.getHostName() + ";" |
| 153 |
+ filterRef.getUDPPort() + ";" |
| 154 |
+ filterRef.getTCPPort()); |
| 155 |
} |
| 156 |
else { |
| 157 |
// ...or throw a wobbly (and tell the host!) |
| 158 |
_socketOut.println("ERROR"); |
| 159 |
throw new IOException("unable to find filter for host"); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// confirm that all is ok |
| 164 |
inBound = _socketIn.readLine(); |
| 165 |
if(!inBound.equals("END")) { |
| 166 |
// protocol error |
| 167 |
_socketOut.println("ERROR"); |
| 168 |
throw new IOException("protocol error - expected:END got:" + inBound); |
| 169 |
} |
| 170 |
else { |
| 171 |
// send ok |
| 172 |
_socketOut.println("OK"); |
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
} catch (Exception e) { |
| 178 |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e); |
| 179 |
} |
| 180 |
|
| 181 |
_socketOut.flush(); |
| 182 |
// Disconnect streams & socket |
| 183 |
try { |
| 184 |
_socketIn.close(); |
| 185 |
_socketOut.close(); |
| 186 |
_socket.close(); |
| 187 |
} catch (IOException e) { |
| 188 |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
| 189 |
} |
| 190 |
_logger.write(toString(), Logger.SYSMSG, "finished"); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 195 |
* method to provide clean logging (every class should have this). |
| 196 |
* |
| 197 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
| 198 |
* to format the toString() |
| 199 |
* |
| 200 |
* @return the name of this class and its CVS revision |
| 201 |
*/ |
| 202 |
public String toString() { |
| 203 |
return FormatName.getName( |
| 204 |
_name, |
| 205 |
getClass().getName(), |
| 206 |
REVISION); |
| 207 |
} |
| 208 |
|
| 209 |
//---PRIVATE METHODS--- |
| 210 |
|
| 211 |
//---ACCESSOR/MUTATOR METHODS--- |
| 212 |
|
| 213 |
//---ATTRIBUTES--- |
| 214 |
|
| 215 |
/** |
| 216 |
* This holds a reference to the |
| 217 |
* system logger that is being used. |
| 218 |
*/ |
| 219 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
| 220 |
|
| 221 |
/** |
| 222 |
* A reference to the Configuration Manager the system is using |
| 223 |
*/ |
| 224 |
private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
| 225 |
|
| 226 |
/** |
| 227 |
* This is the friendly identifier of the |
| 228 |
* component this class is running in. |
| 229 |
* eg, a Filter may be called "filter1", |
| 230 |
* If this class does not have an owning |
| 231 |
* component, a name from the configuration |
| 232 |
* can be placed here. This name could also |
| 233 |
* be changed to null for utility classes. |
| 234 |
*/ |
| 235 |
private String _name = FilterManager.NAME; |
| 236 |
|
| 237 |
/** |
| 238 |
* The socket this class uses |
| 239 |
*/ |
| 240 |
private Socket _socket; |
| 241 |
|
| 242 |
/** |
| 243 |
* Used for the input stream of this socket |
| 244 |
*/ |
| 245 |
private BufferedReader _socketIn; |
| 246 |
|
| 247 |
/** |
| 248 |
* Used for the output stream of this socket |
| 249 |
*/ |
| 250 |
private PrintWriter _socketOut; |
| 251 |
|
| 252 |
//---STATIC ATTRIBUTES--- |
| 253 |
|
| 254 |
} |