1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.25 |
package uk.org.iscream.cms.server.filter; |
3 |
tdb |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
tdb |
1.25 |
import uk.org.iscream.cms.server.core.*; |
6 |
|
|
import uk.org.iscream.cms.server.filter.*; |
7 |
|
|
import uk.org.iscream.cms.server.componentmanager.*; |
8 |
tdb |
1.1 |
import java.net.Socket; |
9 |
|
|
import java.io.*; |
10 |
tdb |
1.27 |
import java.util.Random; |
11 |
tdb |
1.25 |
import uk.org.iscream.cms.server.util.*; |
12 |
tdb |
1.1 |
|
13 |
|
|
/** |
14 |
ajm |
1.10 |
* This provides Host heartbeat functionality |
15 |
tdb |
1.1 |
* |
16 |
tdb |
1.26 |
* @author $Author: tdb $ |
17 |
tdb |
1.27 |
* @version $Id: TCPReaderInit.java,v 1.26 2002/03/21 13:01:21 tdb Exp $ |
18 |
tdb |
1.1 |
*/ |
19 |
|
|
class TCPReaderInit extends Thread { |
20 |
|
|
|
21 |
|
|
//---FINAL ATTRIBUTES--- |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* The current CVS revision of this class |
25 |
|
|
*/ |
26 |
tdb |
1.27 |
public final String REVISION = "$Revision: 1.26 $"; |
27 |
|
|
|
28 |
|
|
/** |
29 |
|
|
* The set of characters to be used for our key |
30 |
|
|
*/ |
31 |
|
|
private final char[] KEYSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); |
32 |
|
|
|
33 |
|
|
/** |
34 |
|
|
* The length of our key |
35 |
|
|
*/ |
36 |
|
|
private final int KEYLEN = 15; |
37 |
tdb |
1.1 |
|
38 |
|
|
//---STATIC METHODS--- |
39 |
|
|
|
40 |
|
|
//---CONSTRUCTORS--- |
41 |
tdb |
1.13 |
|
42 |
|
|
/** |
43 |
|
|
* Construct a new TCPReaderInit. |
44 |
|
|
* |
45 |
|
|
* @param socket the Socket to which the host is connected |
46 |
|
|
* @param queue the Queue to which we'll add data |
47 |
|
|
* @throws IOException if something goes badly wrong |
48 |
|
|
*/ |
49 |
tdb |
1.11 |
public TCPReaderInit(Socket socket, Queue queue) throws IOException { |
50 |
tdb |
1.21 |
// set the Thread name |
51 |
|
|
setName("filter.TCPReaderInit"); |
52 |
|
|
|
53 |
tdb |
1.1 |
_socket = socket; |
54 |
tdb |
1.11 |
_queue = queue; |
55 |
tdb |
1.13 |
// setup the reader & writer |
56 |
tdb |
1.1 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
57 |
tdb |
1.15 |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
58 |
tdb |
1.8 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
59 |
tdb |
1.1 |
} |
60 |
|
|
|
61 |
|
|
//---PUBLIC METHODS--- |
62 |
tdb |
1.13 |
|
63 |
|
|
/** |
64 |
|
|
* Main run method. Will communicate with the host, inform it |
65 |
|
|
* if any updates to it's configuration are needed, and send |
66 |
|
|
* a heartbeat packet into the system. |
67 |
|
|
*/ |
68 |
tdb |
1.1 |
public void run() { |
69 |
|
|
try { |
70 |
tdb |
1.27 |
// get an instance of the KeyManager |
71 |
|
|
KeyManager keyman = KeyManager.getInstance(); |
72 |
|
|
|
73 |
|
|
// get some information about the host |
74 |
|
|
String hostname = _socket.getInetAddress().getHostName().toLowerCase(); |
75 |
|
|
String ipadd = _socket.getInetAddress().getHostAddress(); |
76 |
tdb |
1.2 |
|
77 |
tdb |
1.22 |
// try for HEARTBEAT |
78 |
tdb |
1.23 |
getInBound("HEARTBEAT"); |
79 |
tdb |
1.22 |
_socketOut.println("OK"); |
80 |
tdb |
1.3 |
|
81 |
tdb |
1.27 |
// look for a command: |
82 |
|
|
// CONFIG - to check config |
83 |
|
|
// KEY - to get the key |
84 |
|
|
// ENDHEARTBEAT - to finish |
85 |
|
|
String cmd = getInBound(); |
86 |
|
|
while(!cmd.equals("ENDHEARTBEAT")) { |
87 |
|
|
if(cmd.equals("CONFIG")) { |
88 |
|
|
// respond to CONFIG |
89 |
|
|
_socketOut.println("OK"); |
90 |
|
|
|
91 |
|
|
// try for {filelist} |
92 |
|
|
String filelist = getInBound(); |
93 |
|
|
_socketOut.println("OK"); |
94 |
|
|
|
95 |
|
|
// try for {lastModified} |
96 |
|
|
String lastModified = getInBound(); |
97 |
|
|
// check to see if a config update has happen |
98 |
|
|
boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified)); |
99 |
|
|
if(newConfig) { |
100 |
|
|
// new config ! |
101 |
|
|
_socketOut.println("ERROR"); |
102 |
|
|
} |
103 |
|
|
else { |
104 |
|
|
// nothing has changed |
105 |
|
|
_socketOut.println("OK"); |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
else if(cmd.equals("KEY")) { |
109 |
|
|
// repsond to KEY |
110 |
|
|
// generate a key |
111 |
|
|
String key = keyman.genKey(); |
112 |
|
|
// add it to the key manager |
113 |
|
|
keyman.addKey(hostname, key); |
114 |
|
|
_socketOut.println(key); |
115 |
|
|
} |
116 |
|
|
else { |
117 |
|
|
_socketOut.println("ERROR"); |
118 |
|
|
} |
119 |
|
|
// get the next command |
120 |
|
|
cmd = getInBound(); |
121 |
tdb |
1.3 |
} |
122 |
|
|
|
123 |
tdb |
1.27 |
// respond to ENDHEARTBEAT |
124 |
tdb |
1.22 |
_socketOut.println("OK"); |
125 |
|
|
|
126 |
|
|
// work out some information for our heartbeat packet |
127 |
|
|
String date = new Long(System.currentTimeMillis()/((long) 1000)).toString(); //seconds |
128 |
tdb |
1.6 |
|
129 |
ajm |
1.16 |
// run the service checks for this host |
130 |
ajm |
1.17 |
_logger.write(toString(), Logger.DEBUG, "Running service checks"); |
131 |
ajm |
1.16 |
String checks = PluginServiceCheckManager.getInstance().runServiceChecks(hostname); |
132 |
|
|
|
133 |
|
|
// build the heartbeat packet |
134 |
|
|
String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\">" + checks + "</packet>"; |
135 |
|
|
|
136 |
|
|
// get it to be sent on |
137 |
tdb |
1.11 |
_queue.add(xml); |
138 |
tdb |
1.1 |
|
139 |
|
|
} catch (Exception e) { |
140 |
tdb |
1.20 |
_logger.write(toString(), Logger.ERROR, "ERROR: " + e); |
141 |
tdb |
1.1 |
} |
142 |
|
|
|
143 |
|
|
// Disconnect streams & socket |
144 |
|
|
try { |
145 |
|
|
_socketIn.close(); |
146 |
|
|
_socketOut.close(); |
147 |
|
|
_socket.close(); |
148 |
|
|
} catch (IOException e) { |
149 |
tdb |
1.8 |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
150 |
tdb |
1.1 |
} |
151 |
tdb |
1.14 |
_logger.write(toString(), Logger.DEBUG, "finished"); |
152 |
tdb |
1.1 |
} |
153 |
|
|
|
154 |
|
|
/** |
155 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
156 |
|
|
* method to provide clean logging (every class should have this). |
157 |
|
|
* |
158 |
tdb |
1.25 |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
159 |
ajm |
1.10 |
* to format the toString() |
160 |
|
|
* |
161 |
tdb |
1.1 |
* @return the name of this class and its CVS revision |
162 |
|
|
*/ |
163 |
|
|
public String toString() { |
164 |
ajm |
1.10 |
return FormatName.getName( |
165 |
|
|
_name, |
166 |
|
|
getClass().getName(), |
167 |
|
|
REVISION); |
168 |
tdb |
1.1 |
} |
169 |
|
|
|
170 |
|
|
//---PRIVATE METHODS--- |
171 |
tdb |
1.22 |
|
172 |
|
|
private String getInBound(String expected) throws IOException { |
173 |
|
|
// grab the input |
174 |
|
|
String inBound = getInBound(); |
175 |
|
|
// check if it's what we're expecting |
176 |
|
|
if(!inBound.equals(expected)) { |
177 |
tdb |
1.26 |
throw new IOException("protocol error from "+_socket.getInetAddress().getHostName()+" - expected:"+expected+" got:" + inBound); |
178 |
tdb |
1.22 |
} |
179 |
|
|
// it should be ok then |
180 |
|
|
return inBound; |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
private String getInBound() throws IOException { |
184 |
|
|
// grab the input |
185 |
|
|
String inBound = _socketIn.readLine(); |
186 |
|
|
// check for null's, likely disconnection |
187 |
|
|
if(inBound == null) { |
188 |
|
|
throw new IOException("got null from host, maybe it died"); |
189 |
|
|
} |
190 |
|
|
// it's a valid message it seems |
191 |
|
|
return inBound; |
192 |
|
|
} |
193 |
tdb |
1.27 |
|
194 |
tdb |
1.1 |
//---ACCESSOR/MUTATOR METHODS--- |
195 |
|
|
|
196 |
|
|
//---ATTRIBUTES--- |
197 |
|
|
|
198 |
ajm |
1.10 |
/** |
199 |
|
|
* A reference to the configuration manager |
200 |
|
|
*/ |
201 |
tdb |
1.8 |
ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
202 |
ajm |
1.10 |
|
203 |
|
|
/** |
204 |
|
|
* This is the friendly identifier of the |
205 |
|
|
* component this class is running in. |
206 |
|
|
* eg, a Filter may be called "filter1", |
207 |
|
|
* If this class does not have an owning |
208 |
|
|
* component, a name from the configuration |
209 |
|
|
* can be placed here. This name could also |
210 |
|
|
* be changed to null for utility classes. |
211 |
|
|
*/ |
212 |
|
|
private String _name = FilterMain.NAME; |
213 |
|
|
|
214 |
|
|
/** |
215 |
|
|
* This holds a reference to the |
216 |
|
|
* system logger that is being used. |
217 |
|
|
*/ |
218 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
219 |
|
|
|
220 |
|
|
/** |
221 |
|
|
* The socket we are talking on |
222 |
|
|
*/ |
223 |
tdb |
1.1 |
Socket _socket; |
224 |
ajm |
1.10 |
|
225 |
|
|
/** |
226 |
|
|
* The input from the socket |
227 |
|
|
*/ |
228 |
tdb |
1.1 |
BufferedReader _socketIn; |
229 |
ajm |
1.10 |
|
230 |
|
|
/** |
231 |
|
|
* The output from the socket |
232 |
|
|
*/ |
233 |
tdb |
1.1 |
PrintWriter _socketOut; |
234 |
ajm |
1.10 |
|
235 |
|
|
/** |
236 |
tdb |
1.11 |
* A reference to our Queue |
237 |
ajm |
1.10 |
*/ |
238 |
tdb |
1.11 |
Queue _queue; |
239 |
tdb |
1.27 |
|
240 |
tdb |
1.1 |
//---STATIC ATTRIBUTES--- |
241 |
|
|
|
242 |
|
|
} |