| 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.Socket; |
| 8 |
– |
import java.io.InputStream; |
| 9 |
– |
import java.io.OutputStream; |
| 10 |
– |
import java.io.IOException; |
| 10 |
|
import java.io.*; |
| 12 |
– |
import org.omg.CORBA.*; |
| 13 |
– |
import org.omg.CosNaming.*; |
| 11 |
|
|
| 12 |
|
/** |
| 13 |
< |
* <ONE LINE DESCRIPTION> |
| 14 |
< |
* <DETAILED DESCRIPTION> |
| 13 |
> |
* Handles setting up a host. |
| 14 |
> |
* This class provides a host with appropriate configuration |
| 15 |
> |
* and a reference to a Filter to which it should pass data. |
| 16 |
|
* |
| 17 |
|
* @author $Author$ |
| 18 |
|
* @version $Id$ |
| 30 |
|
|
| 31 |
|
//---CONSTRUCTORS--- |
| 32 |
|
|
| 33 |
< |
public HostInit(Socket socket, ConfigurationManager configManager, Logger logRef, NamingContextExt ncRef) throws IOException { |
| 34 |
< |
_configManager = configManager; |
| 35 |
< |
_logRef = logRef; |
| 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 |
< |
_ncRef = ncRef; |
| 40 |
> |
// setup reader & writer |
| 41 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
| 42 |
< |
_socketOut = new PrintWriter(_socket.getOutputStream()); |
| 43 |
< |
_logRef.write(toString(), Logger.SYSINIT, "created"); |
| 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"); |
| 52 |
– |
_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"); |
| 62 |
– |
_socketOut.flush(); |
| 66 |
|
|
| 67 |
|
// get lastmodified |
| 68 |
|
inBound = _socketIn.readLine(); |
| 69 |
|
if(!inBound.equals("LASTMODIFIED")) { |
| 70 |
|
// protocol error |
| 71 |
|
_socketOut.println("ERROR"); |
| 69 |
– |
_socketOut.flush(); |
| 72 |
|
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
| 73 |
|
} |
| 74 |
|
else { |
| 75 |
|
// send info |
| 76 |
|
_socketOut.println(myConfig.getLastModified()); |
| 75 |
– |
_socketOut.flush(); |
| 77 |
|
} |
| 78 |
|
|
| 79 |
|
// get config fileList |
| 81 |
|
if(!inBound.equals("FILELIST")) { |
| 82 |
|
// protocol error |
| 83 |
|
_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(); |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
// get properties |
| 97 |
|
String returnedProperty = myConfig.getProperty("Host."+inBound); |
| 98 |
|
|
| 99 |
|
_socketOut.println(returnedProperty); |
| 101 |
– |
_socketOut.flush(); |
| 100 |
|
|
| 101 |
|
} catch (org.omg.CORBA.MARSHAL e) { |
| 102 |
|
_socketOut.println("ERROR"); |
| 105 |
– |
_socketOut.flush(); |
| 103 |
|
} |
| 104 |
|
inBound = _socketIn.readLine(); |
| 105 |
|
} |
| 106 |
< |
_logRef.write(toString(), Logger.SYSMSG, "configured host"); |
| 106 |
> |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
| 107 |
|
_socketOut.println("OK"); |
| 111 |
– |
_socketOut.flush(); |
| 108 |
|
|
| 109 |
|
// get filter reference |
| 110 |
|
inBound = _socketIn.readLine(); |
| 111 |
|
if(!inBound.equals("FILTER")) { |
| 112 |
|
// protocol error |
| 113 |
|
_socketOut.println("ERROR"); |
| 118 |
– |
_socketOut.flush(); |
| 114 |
|
throw new IOException("protocol error - expected:FILTER got:" + inBound); |
| 115 |
|
} |
| 116 |
|
else { |
| 117 |
|
// send info |
| 118 |
|
String parentFilter = myConfig.getProperty("Host.filter"); |
| 119 |
< |
_logRef.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter); |
| 120 |
< |
org.omg.CORBA.Object objRef = _ncRef.resolve(_ncRef.to_name("iscream.Filter." + parentFilter)); |
| 126 |
< |
Filter filter = FilterHelper.narrow(objRef); |
| 119 |
> |
_logger.write(toString(), Logger.DEBUG, " looking for parent - " + parentFilter); |
| 120 |
> |
Filter filter = FilterHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.Filter." + parentFilter)); |
| 121 |
|
_socketOut.println(filter.getHostName() + ";" |
| 122 |
|
+ filter.getUDPPort() + ";" |
| 123 |
|
+ filter.getTCPPort()); |
| 130 |
– |
_socketOut.flush(); |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
// confirm that all is ok |
| 128 |
|
if(!inBound.equals("END")) { |
| 129 |
|
// protocol error |
| 130 |
|
_socketOut.println("ERROR"); |
| 138 |
– |
_socketOut.flush(); |
| 131 |
|
throw new IOException("protocol error - expected:END got:" + inBound); |
| 132 |
|
} |
| 133 |
|
else { |
| 134 |
|
// send ok |
| 135 |
|
_socketOut.println("OK"); |
| 144 |
– |
_socketOut.flush(); |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
} catch (Exception e) { |
| 141 |
< |
_logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
| 141 |
> |
_logger.write(toString(), Logger.ERROR, "ERROR - " + e.getMessage()); |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
_socketOut.flush(); |
| 148 |
|
_socketOut.close(); |
| 149 |
|
_socket.close(); |
| 150 |
|
} catch (IOException e) { |
| 151 |
< |
_logRef.write(toString(), Logger.ERROR, "exception on socket close"); |
| 151 |
> |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
| 152 |
|
} |
| 153 |
< |
_logRef.write(toString(), Logger.SYSMSG, "finished"); |
| 153 |
> |
_logger.write(toString(), Logger.SYSMSG, "finished"); |
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
/** |
| 157 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 158 |
|
* method to provide clean logging (every class should have this). |
| 159 |
|
* |
| 160 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
| 161 |
+ |
* to format the toString() |
| 162 |
+ |
* |
| 163 |
|
* @return the name of this class and its CVS revision |
| 164 |
|
*/ |
| 165 |
|
public String toString() { |
| 166 |
< |
return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName() |
| 167 |
< |
+ "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
| 168 |
< |
|
| 166 |
> |
return FormatName.getName( |
| 167 |
> |
_name, |
| 168 |
> |
getClass().getName(), |
| 169 |
> |
REVISION); |
| 170 |
|
} |
| 171 |
|
|
| 172 |
|
//---PRIVATE METHODS--- |
| 175 |
|
|
| 176 |
|
//---ATTRIBUTES--- |
| 177 |
|
|
| 178 |
< |
ConfigurationManager _configManager; |
| 179 |
< |
Logger _logRef; |
| 180 |
< |
Socket _socket; |
| 181 |
< |
BufferedReader _socketIn; |
| 182 |
< |
PrintWriter _socketOut; |
| 183 |
< |
NamingContextExt _ncRef; |
| 178 |
> |
/** |
| 179 |
> |
* This holds a reference to the |
| 180 |
> |
* system logger that is being used. |
| 181 |
> |
*/ |
| 182 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
| 183 |
> |
|
| 184 |
> |
/** |
| 185 |
> |
* A reference to the Configuration Manager the system is using |
| 186 |
> |
*/ |
| 187 |
> |
private ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
| 188 |
> |
|
| 189 |
> |
/** |
| 190 |
> |
* This is the friendly identifier of the |
| 191 |
> |
* component this class is running in. |
| 192 |
> |
* eg, a Filter may be called "filter1", |
| 193 |
> |
* If this class does not have an owning |
| 194 |
> |
* component, a name from the configuration |
| 195 |
> |
* can be placed here. This name could also |
| 196 |
> |
* be changed to null for utility classes. |
| 197 |
> |
*/ |
| 198 |
> |
private String _name = FilterManager.NAME; |
| 199 |
> |
|
| 200 |
> |
/** |
| 201 |
> |
* The socket this class uses |
| 202 |
> |
*/ |
| 203 |
> |
private Socket _socket; |
| 204 |
> |
|
| 205 |
> |
/** |
| 206 |
> |
* Used for the input stream of this socket |
| 207 |
> |
*/ |
| 208 |
> |
private BufferedReader _socketIn; |
| 209 |
> |
|
| 210 |
> |
/** |
| 211 |
> |
* Used for the output stream of this socket |
| 212 |
> |
*/ |
| 213 |
> |
private PrintWriter _socketOut; |
| 214 |
> |
|
| 215 |
|
//---STATIC ATTRIBUTES--- |
| 216 |
|
|
| 217 |
|
} |