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