| 1 |
+ |
/* |
| 2 |
+ |
* i-scream central monitoring system |
| 3 |
+ |
* http://www.i-scream.org.uk |
| 4 |
+ |
* Copyright (C) 2000-2002 i-scream |
| 5 |
+ |
* |
| 6 |
+ |
* This program is free software; you can redistribute it and/or |
| 7 |
+ |
* modify it under the terms of the GNU General Public License |
| 8 |
+ |
* as published by the Free Software Foundation; either version 2 |
| 9 |
+ |
* of the License, or (at your option) any later version. |
| 10 |
+ |
* |
| 11 |
+ |
* This program is distributed in the hope that it will be useful, |
| 12 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
+ |
* GNU General Public License for more details. |
| 15 |
+ |
* |
| 16 |
+ |
* You should have received a copy of the GNU General Public License |
| 17 |
+ |
* along with this program; if not, write to the Free Software |
| 18 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
+ |
*/ |
| 20 |
+ |
|
| 21 |
|
//---PACKAGE DECLARATION--- |
| 22 |
< |
package uk.ac.ukc.iscream.filtermanager; |
| 22 |
> |
package uk.org.iscream.cms.server.filtermanager; |
| 23 |
|
|
| 24 |
|
//---IMPORTS--- |
| 25 |
< |
import uk.ac.ukc.iscream.core.*; |
| 26 |
< |
import uk.ac.ukc.iscream.filter.*; |
| 27 |
< |
import uk.ac.ukc.iscream.util.*; |
| 28 |
< |
import uk.ac.ukc.iscream.componentmanager.*; |
| 25 |
> |
import uk.org.iscream.cms.server.core.*; |
| 26 |
> |
import uk.org.iscream.cms.server.filter.*; |
| 27 |
> |
import uk.org.iscream.cms.server.util.*; |
| 28 |
> |
import uk.org.iscream.cms.server.componentmanager.*; |
| 29 |
|
import java.net.*; |
| 30 |
|
import java.io.*; |
| 31 |
|
import java.util.*; |
| 57 |
|
* @param socket The socket to which the host is connected |
| 58 |
|
*/ |
| 59 |
|
public HostInit(Socket socket) throws IOException { |
| 60 |
+ |
// set the Thread name |
| 61 |
+ |
setName("filtermanager.HostInit"); |
| 62 |
+ |
|
| 63 |
|
_socket = socket; |
| 64 |
|
// setup reader & writer |
| 65 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
| 74 |
|
* the host to determine it's setup. |
| 75 |
|
*/ |
| 76 |
|
public void run() { |
| 77 |
+ |
// get a hook on the config proxy |
| 78 |
+ |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
| 79 |
+ |
// this is our config name |
| 80 |
+ |
String configName = "Host." + _socket.getInetAddress().getHostName().toLowerCase(); |
| 81 |
|
try { |
| 82 |
< |
String inBound = _socketIn.readLine(); |
| 83 |
< |
if (!inBound.equals("STARTCONFIG")) { |
| 84 |
< |
_socketOut.println("ERROR"); |
| 85 |
< |
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
| 86 |
< |
} |
| 87 |
< |
|
| 88 |
< |
Configuration myConfig = _configManager.getConfiguration("Host." + _socket.getInetAddress().getHostName().toLowerCase()); |
| 89 |
< |
if (myConfig == null) { |
| 90 |
< |
_socketOut.println("ERROR"); |
| 91 |
< |
throw new IOException("error in host configuration"); |
| 92 |
< |
} else { |
| 93 |
< |
_socketOut.println("OK"); |
| 94 |
< |
|
| 95 |
< |
// get lastmodified |
| 96 |
< |
inBound = _socketIn.readLine(); |
| 97 |
< |
if(!inBound.equals("LASTMODIFIED")) { |
| 98 |
< |
// protocol error |
| 82 |
> |
// try for STARTCONFIG |
| 83 |
> |
getInBound("STARTCONFIG"); |
| 84 |
> |
_socketOut.println("OK"); |
| 85 |
> |
|
| 86 |
> |
// try for LASTMODIFIED |
| 87 |
> |
getInBound("LASTMODIFIED"); |
| 88 |
> |
_socketOut.println(cp.getLastModified(configName)); |
| 89 |
> |
|
| 90 |
> |
// try for FILELIST |
| 91 |
> |
getInBound("FILELIST"); |
| 92 |
> |
_socketOut.println(cp.getFileList(configName)); |
| 93 |
> |
|
| 94 |
> |
// try for FQDN |
| 95 |
> |
getInBound("FQDN"); |
| 96 |
> |
_socketOut.println(_socket.getInetAddress().getHostName().toLowerCase()); |
| 97 |
> |
|
| 98 |
> |
// get properties |
| 99 |
> |
String reqProperty = getInBound(); |
| 100 |
> |
while(!reqProperty.equals("ENDCONFIG")) { |
| 101 |
> |
// get the property |
| 102 |
> |
try { |
| 103 |
> |
String returnedProperty = cp.getProperty(configName, "Host."+reqProperty); |
| 104 |
> |
_socketOut.println(returnedProperty); |
| 105 |
> |
} catch (PropertyNotFoundException e) { |
| 106 |
|
_socketOut.println("ERROR"); |
| 73 |
– |
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
| 107 |
|
} |
| 108 |
< |
else { |
| 109 |
< |
// send info |
| 110 |
< |
_socketOut.println(myConfig.getLastModified()); |
| 108 |
> |
// get the next request |
| 109 |
> |
reqProperty = _socketIn.readLine(); |
| 110 |
> |
} |
| 111 |
> |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
| 112 |
> |
_socketOut.println("OK"); |
| 113 |
> |
|
| 114 |
> |
// get filter reference |
| 115 |
> |
getInBound("FILTER"); |
| 116 |
> |
// send info |
| 117 |
> |
String filterList = cp.getProperty(configName, "Host.filter"); |
| 118 |
> |
FilterInfo filterInfoRef = null; |
| 119 |
> |
String filter = null; |
| 120 |
> |
StringTokenizer st = new StringTokenizer(filterList, ";"); |
| 121 |
> |
while (filterInfoRef==null && st.hasMoreTokens()) { |
| 122 |
> |
filter = st.nextToken(); |
| 123 |
> |
_logger.write(toString(), Logger.DEBUG, " looking for filter- " + filter); |
| 124 |
> |
try { |
| 125 |
> |
filterInfoRef = FilterInfoHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.FilterInfo." + filter)); |
| 126 |
> |
} catch (ComponentCORBAException e) { |
| 127 |
> |
_logger.write(toString(), Logger.DEBUG, " unable to find filter- " + filter); |
| 128 |
|
} |
| 79 |
– |
|
| 80 |
– |
// get config fileList |
| 81 |
– |
inBound = _socketIn.readLine(); |
| 82 |
– |
if(!inBound.equals("FILELIST")) { |
| 83 |
– |
// protocol error |
| 84 |
– |
_socketOut.println("ERROR"); |
| 85 |
– |
throw new IOException("protocol error - expected:FILELIST got:" + inBound); |
| 86 |
– |
} |
| 87 |
– |
else { |
| 88 |
– |
// send info |
| 89 |
– |
_socketOut.println(myConfig.getFileList()); |
| 90 |
– |
} |
| 91 |
– |
|
| 92 |
– |
// send the FQDN to the host |
| 93 |
– |
inBound = _socketIn.readLine(); |
| 94 |
– |
if(!inBound.equals("FQDN")) { |
| 95 |
– |
// protocol error |
| 96 |
– |
_socketOut.println("ERROR"); |
| 97 |
– |
throw new IOException("protocol error - expected:FQDN got:" + inBound); |
| 98 |
– |
} |
| 99 |
– |
else { |
| 100 |
– |
// send the fqdn (of the host) to the host |
| 101 |
– |
_socketOut.println(_socket.getInetAddress().getHostName().toLowerCase()); |
| 102 |
– |
} |
| 103 |
– |
|
| 104 |
– |
// get properties |
| 105 |
– |
inBound = _socketIn.readLine(); |
| 106 |
– |
while(!inBound.equals("ENDCONFIG")) { |
| 107 |
– |
|
| 108 |
– |
// get the property |
| 109 |
– |
try { |
| 110 |
– |
String returnedProperty = myConfig.getProperty("Host."+inBound); |
| 111 |
– |
|
| 112 |
– |
_socketOut.println(returnedProperty); |
| 113 |
– |
|
| 114 |
– |
} catch (org.omg.CORBA.MARSHAL e) { |
| 115 |
– |
_socketOut.println("ERROR"); |
| 116 |
– |
} |
| 117 |
– |
inBound = _socketIn.readLine(); |
| 118 |
– |
} |
| 119 |
– |
_logger.write(toString(), Logger.SYSMSG, "configured host"); |
| 120 |
– |
_socketOut.println("OK"); |
| 121 |
– |
|
| 122 |
– |
// get filter reference |
| 123 |
– |
inBound = _socketIn.readLine(); |
| 124 |
– |
if(!inBound.equals("FILTER")) { |
| 125 |
– |
// protocol error |
| 126 |
– |
_socketOut.println("ERROR"); |
| 127 |
– |
throw new IOException("protocol error - expected:FILTER got:" + inBound); |
| 128 |
– |
} |
| 129 |
– |
else { |
| 130 |
– |
// send info |
| 131 |
– |
String filterList = myConfig.getProperty("Host.filter"); |
| 132 |
– |
Filter filterRef = null; |
| 133 |
– |
String filter = null; |
| 134 |
– |
StringTokenizer st = new StringTokenizer(filterList, ";"); |
| 135 |
– |
while (filterRef==null && st.hasMoreTokens()) { |
| 136 |
– |
filter = st.nextToken(); |
| 137 |
– |
_logger.write(toString(), Logger.DEBUG, " looking for filter- " + filter); |
| 138 |
– |
try { |
| 139 |
– |
filterRef = FilterHelper.narrow(ReferenceManager.getInstance().getCORBARef("iscream.Filter." + filter)); |
| 140 |
– |
} catch (ComponentCORBAException e) { |
| 141 |
– |
_logger.write(toString(), Logger.DEBUG, " unable to find filter- " + filter); |
| 142 |
– |
} |
| 143 |
– |
} |
| 144 |
– |
|
| 145 |
– |
// hopefully we found a filter |
| 146 |
– |
if(filterRef != null) { |
| 147 |
– |
_logger.write(toString(), Logger.DEBUG, " found filter- " + filter); |
| 148 |
– |
// tell the host about it... |
| 149 |
– |
_socketOut.println(filterRef.getHostName() + ";" |
| 150 |
– |
+ filterRef.getUDPPort() + ";" |
| 151 |
– |
+ filterRef.getTCPPort()); |
| 152 |
– |
} |
| 153 |
– |
else { |
| 154 |
– |
// ...or throw a wobbly (and tell the host!) |
| 155 |
– |
_socketOut.println("ERROR"); |
| 156 |
– |
throw new IOException("unable to find filter for host"); |
| 157 |
– |
} |
| 158 |
– |
} |
| 159 |
– |
|
| 160 |
– |
// confirm that all is ok |
| 161 |
– |
inBound = _socketIn.readLine(); |
| 162 |
– |
if(!inBound.equals("END")) { |
| 163 |
– |
// protocol error |
| 164 |
– |
_socketOut.println("ERROR"); |
| 165 |
– |
throw new IOException("protocol error - expected:END got:" + inBound); |
| 166 |
– |
} |
| 167 |
– |
else { |
| 168 |
– |
// send ok |
| 169 |
– |
_socketOut.println("OK"); |
| 170 |
– |
} |
| 171 |
– |
|
| 129 |
|
} |
| 130 |
+ |
|
| 131 |
+ |
// hopefully we found a filter |
| 132 |
+ |
if(filterInfoRef != null) { |
| 133 |
+ |
_logger.write(toString(), Logger.DEBUG, " found filter- " + filter); |
| 134 |
+ |
try { |
| 135 |
+ |
// tell the host about it... |
| 136 |
+ |
_socketOut.println(filterInfoRef.getHostName() + ";" |
| 137 |
+ |
+ filterInfoRef.getUDPPort() + ";" |
| 138 |
+ |
+ filterInfoRef.getTCPPort()); |
| 139 |
+ |
} |
| 140 |
+ |
catch(org.omg.CORBA.COMM_FAILURE e) { |
| 141 |
+ |
// failed to talk to filter, lets signal an error |
| 142 |
+ |
_socketOut.println("ERROR"); |
| 143 |
+ |
throw new IOException("error communicating with filter - " + e); |
| 144 |
+ |
} |
| 145 |
+ |
} |
| 146 |
+ |
else { |
| 147 |
+ |
// ...or throw a wobbly (and tell the host!) |
| 148 |
+ |
_socketOut.println("ERROR"); |
| 149 |
+ |
throw new IOException("unable to find filter for host"); |
| 150 |
+ |
} |
| 151 |
|
|
| 152 |
+ |
// confirm ok with END |
| 153 |
+ |
getInBound("END"); |
| 154 |
+ |
_socketOut.println("OK"); |
| 155 |
+ |
|
| 156 |
|
} catch (Exception e) { |
| 157 |
|
_logger.write(toString(), Logger.ERROR, "ERROR - " + e); |
| 158 |
|
} |
| 159 |
|
|
| 178 |
– |
_socketOut.flush(); |
| 160 |
|
// Disconnect streams & socket |
| 161 |
|
try { |
| 162 |
|
_socketIn.close(); |
| 172 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 173 |
|
* method to provide clean logging (every class should have this). |
| 174 |
|
* |
| 175 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
| 175 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
| 176 |
|
* to format the toString() |
| 177 |
|
* |
| 178 |
|
* @return the name of this class and its CVS revision |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
//---PRIVATE METHODS--- |
| 188 |
+ |
|
| 189 |
+ |
private String getInBound(String expected) throws IOException { |
| 190 |
+ |
// grab the input |
| 191 |
+ |
String inBound = getInBound(); |
| 192 |
+ |
// check if it's what we're expecting |
| 193 |
+ |
if(!inBound.equals(expected)) { |
| 194 |
+ |
throw new IOException("protocol error - expected:"+expected+" got:" + inBound); |
| 195 |
+ |
} |
| 196 |
+ |
// it should be ok then |
| 197 |
+ |
return inBound; |
| 198 |
+ |
} |
| 199 |
+ |
|
| 200 |
+ |
private String getInBound() throws IOException { |
| 201 |
+ |
// grab the input |
| 202 |
+ |
String inBound = _socketIn.readLine(); |
| 203 |
+ |
// check for null's, likely disconnection |
| 204 |
+ |
if(inBound == null) { |
| 205 |
+ |
throw new IOException("got null from host, maybe it died"); |
| 206 |
+ |
} |
| 207 |
+ |
// it's a valid message it seems |
| 208 |
+ |
return inBound; |
| 209 |
+ |
} |
| 210 |
|
|
| 211 |
|
//---ACCESSOR/MUTATOR METHODS--- |
| 212 |
|
|