| 1 |
|
//---PACKAGE DECLARATION--- |
| 2 |
+ |
package uk.ac.ukc.iscream.filter; |
| 3 |
|
|
| 4 |
|
//---IMPORTS--- |
| 5 |
|
import uk.ac.ukc.iscream.core.*; |
| 6 |
|
import uk.ac.ukc.iscream.filter.*; |
| 7 |
+ |
import uk.ac.ukc.iscream.componentmanager.*; |
| 8 |
|
import java.net.Socket; |
| 9 |
|
import java.io.InputStream; |
| 10 |
|
import java.io.OutputStream; |
| 11 |
|
import java.io.IOException; |
| 12 |
|
import java.io.*; |
| 13 |
< |
import org.omg.CORBA.*; |
| 12 |
< |
import org.omg.CosNaming.*; |
| 13 |
> |
import uk.ac.ukc.iscream.util.*; |
| 14 |
|
|
| 15 |
|
/** |
| 16 |
< |
* <ONE LINE DESCRIPTION> |
| 16 |
< |
* <DETAILED DESCRIPTION> |
| 16 |
> |
* This provides Host heartbeat functionality |
| 17 |
|
* |
| 18 |
|
* @author $Author$ |
| 19 |
|
* @version $Id$ |
| 30 |
|
//---STATIC METHODS--- |
| 31 |
|
|
| 32 |
|
//---CONSTRUCTORS--- |
| 33 |
< |
|
| 34 |
< |
public TCPReaderInit(Socket socket, ConfigurationManager configManager, Logger logRef, Filter parent) throws IOException { |
| 35 |
< |
_configManager = configManager; |
| 36 |
< |
_logRef = logRef; |
| 33 |
> |
|
| 34 |
> |
/** |
| 35 |
> |
* Construct a new TCPReaderInit. |
| 36 |
> |
* |
| 37 |
> |
* @param socket the Socket to which the host is connected |
| 38 |
> |
* @param queue the Queue to which we'll add data |
| 39 |
> |
* @throws IOException if something goes badly wrong |
| 40 |
> |
*/ |
| 41 |
> |
public TCPReaderInit(Socket socket, Queue queue) throws IOException { |
| 42 |
> |
// set the Thread name |
| 43 |
> |
setName("filter.TCPReaderInit"); |
| 44 |
> |
|
| 45 |
|
_socket = socket; |
| 46 |
< |
_parent = parent; |
| 46 |
> |
_queue = queue; |
| 47 |
> |
// setup the reader & writer |
| 48 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
| 49 |
< |
_socketOut = new PrintWriter(_socket.getOutputStream()); |
| 50 |
< |
_logRef.write(toString(), Logger.SYSINIT, "created"); |
| 49 |
> |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
| 50 |
> |
_logger.write(toString(), Logger.SYSINIT, "created"); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
//---PUBLIC METHODS--- |
| 54 |
< |
|
| 55 |
< |
//NEED TO MAJORILY CHANGE THIS BIT ! |
| 54 |
> |
|
| 55 |
> |
/** |
| 56 |
> |
* Main run method. Will communicate with the host, inform it |
| 57 |
> |
* if any updates to it's configuration are needed, and send |
| 58 |
> |
* a heartbeat packet into the system. |
| 59 |
> |
*/ |
| 60 |
|
public void run() { |
| 61 |
|
try { |
| 62 |
< |
String inBound = _socketIn.readLine(); |
| 63 |
< |
if (!inBound.equals("STARTCONFIG")) { |
| 62 |
> |
//variables |
| 63 |
> |
String filelist = ""; |
| 64 |
> |
String lastModified = ""; |
| 65 |
> |
String inBound = ""; |
| 66 |
> |
|
| 67 |
> |
inBound = _socketIn.readLine(); |
| 68 |
> |
if(!inBound.equals("HEARTBEAT")) { |
| 69 |
|
_socketOut.println("ERROR"); |
| 70 |
< |
_socketOut.flush(); |
| 71 |
< |
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
| 70 |
> |
throw new IOException("protocol error - expecting:HEARTBEAT got:" + inBound); |
| 71 |
> |
} else { |
| 72 |
> |
_socketOut.println("OK"); |
| 73 |
|
} |
| 74 |
< |
|
| 75 |
< |
Configuration myConfig = _configManager.getConfiguration(_socket.getInetAddress().getHostName().toLowerCase()); |
| 76 |
< |
if (myConfig == null) { |
| 74 |
> |
|
| 75 |
> |
inBound = _socketIn.readLine(); |
| 76 |
> |
if(!inBound.equals("CONFIG")) { |
| 77 |
|
_socketOut.println("ERROR"); |
| 78 |
< |
throw new IOException("error in host configuration"); |
| 78 |
> |
throw new IOException("protocol error - expecting:CONFIG got:" + inBound); |
| 79 |
|
} else { |
| 80 |
|
_socketOut.println("OK"); |
| 81 |
< |
_socketOut.flush(); |
| 82 |
< |
|
| 83 |
< |
// get lastmodified |
| 84 |
< |
inBound = _socketIn.readLine(); |
| 85 |
< |
if(!inBound.equals("LASTMODIFIED")) { |
| 86 |
< |
// protocol error |
| 87 |
< |
_socketOut.println("ERROR"); |
| 88 |
< |
_socketOut.flush(); |
| 89 |
< |
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
| 90 |
< |
} |
| 91 |
< |
else { |
| 92 |
< |
// send info |
| 93 |
< |
_socketOut.println(myConfig.getLastModified()); |
| 94 |
< |
_socketOut.flush(); |
| 95 |
< |
} |
| 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")) { |
| 95 |
< |
|
| 96 |
< |
// get the property |
| 97 |
< |
try { |
| 98 |
< |
String returnedProperty = myConfig.getProperty(inBound); |
| 99 |
< |
|
| 100 |
< |
_socketOut.println(returnedProperty); |
| 101 |
< |
_socketOut.flush(); |
| 102 |
< |
|
| 103 |
< |
} catch (org.omg.CORBA.MARSHAL e) { |
| 104 |
< |
_socketOut.println("ERROR"); |
| 105 |
< |
_socketOut.flush(); |
| 106 |
< |
} |
| 107 |
< |
inBound = _socketIn.readLine(); |
| 108 |
< |
} |
| 109 |
< |
_logRef.write(toString(), Logger.SYSMSG, "configured host"); |
| 81 |
> |
} |
| 82 |
> |
|
| 83 |
> |
inBound = _socketIn.readLine(); |
| 84 |
> |
filelist = inBound; |
| 85 |
> |
_socketOut.println("OK"); |
| 86 |
> |
|
| 87 |
> |
inBound = _socketIn.readLine(); |
| 88 |
> |
lastModified = inBound; |
| 89 |
> |
|
| 90 |
> |
boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified)); |
| 91 |
> |
|
| 92 |
> |
if(newConfig) { |
| 93 |
> |
_socketOut.println("ERROR"); |
| 94 |
> |
} |
| 95 |
> |
else { |
| 96 |
|
_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 |
– |
|
| 97 |
|
} |
| 98 |
|
|
| 99 |
+ |
inBound = _socketIn.readLine(); |
| 100 |
+ |
if(!inBound.equals("ENDHEARTBEAT")) { |
| 101 |
+ |
_socketOut.println("ERROR"); |
| 102 |
+ |
throw new IOException("protocol error - expecting:ENDHEARTBEAT got:" + inBound); |
| 103 |
+ |
} else { |
| 104 |
+ |
_socketOut.println("OK"); |
| 105 |
+ |
} |
| 106 |
+ |
|
| 107 |
+ |
String date = new Long(System.currentTimeMillis()/((long) 1000)).toString(); |
| 108 |
+ |
String hostname = _socket.getInetAddress().getHostName().toLowerCase(); |
| 109 |
+ |
String ipadd = _socket.getInetAddress().getHostAddress(); |
| 110 |
+ |
|
| 111 |
+ |
// run the service checks for this host |
| 112 |
+ |
_logger.write(toString(), Logger.DEBUG, "Running service checks"); |
| 113 |
+ |
String checks = PluginServiceCheckManager.getInstance().runServiceChecks(hostname); |
| 114 |
+ |
|
| 115 |
+ |
// build the heartbeat packet |
| 116 |
+ |
String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\">" + checks + "</packet>"; |
| 117 |
+ |
|
| 118 |
+ |
// get it to be sent on |
| 119 |
+ |
_queue.add(xml); |
| 120 |
+ |
|
| 121 |
|
} catch (Exception e) { |
| 122 |
< |
_logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
| 122 |
> |
_logger.write(toString(), Logger.ERROR, "ERROR: " + e); |
| 123 |
|
} |
| 124 |
|
|
| 153 |
– |
_socketOut.flush(); |
| 125 |
|
// Disconnect streams & socket |
| 126 |
|
try { |
| 127 |
|
_socketIn.close(); |
| 128 |
|
_socketOut.close(); |
| 129 |
|
_socket.close(); |
| 130 |
|
} catch (IOException e) { |
| 131 |
< |
_logRef.write(toString(), Logger.ERROR, "exception on socket close"); |
| 131 |
> |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
| 132 |
|
} |
| 133 |
< |
_logRef.write(toString(), Logger.SYSMSG, "finished"); |
| 133 |
> |
_logger.write(toString(), Logger.DEBUG, "finished"); |
| 134 |
|
} |
| 135 |
|
|
| 136 |
|
/** |
| 137 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 138 |
|
* method to provide clean logging (every class should have this). |
| 139 |
|
* |
| 140 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
| 141 |
+ |
* to format the toString() |
| 142 |
+ |
* |
| 143 |
|
* @return the name of this class and its CVS revision |
| 144 |
|
*/ |
| 145 |
|
public String toString() { |
| 146 |
< |
return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName() |
| 147 |
< |
+ "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
| 148 |
< |
|
| 146 |
> |
return FormatName.getName( |
| 147 |
> |
_name, |
| 148 |
> |
getClass().getName(), |
| 149 |
> |
REVISION); |
| 150 |
|
} |
| 151 |
|
|
| 152 |
|
//---PRIVATE METHODS--- |
| 155 |
|
|
| 156 |
|
//---ATTRIBUTES--- |
| 157 |
|
|
| 158 |
< |
ConfigurationManager _configManager; |
| 159 |
< |
Logger _logRef; |
| 158 |
> |
/** |
| 159 |
> |
* A reference to the configuration manager |
| 160 |
> |
*/ |
| 161 |
> |
ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
| 162 |
> |
|
| 163 |
> |
/** |
| 164 |
> |
* This is the friendly identifier of the |
| 165 |
> |
* component this class is running in. |
| 166 |
> |
* eg, a Filter may be called "filter1", |
| 167 |
> |
* If this class does not have an owning |
| 168 |
> |
* component, a name from the configuration |
| 169 |
> |
* can be placed here. This name could also |
| 170 |
> |
* be changed to null for utility classes. |
| 171 |
> |
*/ |
| 172 |
> |
private String _name = FilterMain.NAME; |
| 173 |
> |
|
| 174 |
> |
/** |
| 175 |
> |
* This holds a reference to the |
| 176 |
> |
* system logger that is being used. |
| 177 |
> |
*/ |
| 178 |
> |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
| 179 |
> |
|
| 180 |
> |
/** |
| 181 |
> |
* The socket we are talking on |
| 182 |
> |
*/ |
| 183 |
|
Socket _socket; |
| 184 |
+ |
|
| 185 |
+ |
/** |
| 186 |
+ |
* The input from the socket |
| 187 |
+ |
*/ |
| 188 |
|
BufferedReader _socketIn; |
| 189 |
+ |
|
| 190 |
+ |
/** |
| 191 |
+ |
* The output from the socket |
| 192 |
+ |
*/ |
| 193 |
|
PrintWriter _socketOut; |
| 194 |
< |
Filter _parent; |
| 194 |
> |
|
| 195 |
> |
/** |
| 196 |
> |
* A reference to our Queue |
| 197 |
> |
*/ |
| 198 |
> |
Queue _queue; |
| 199 |
|
//---STATIC ATTRIBUTES--- |
| 200 |
|
|
| 201 |
|
} |