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 uk.ac.ukc.iscream.util.*; |
14 |
|
15 |
/** |
16 |
* This provides Host heartbeat functionality |
17 |
* |
18 |
* @author $Author: tdb1 $ |
19 |
* @version $Id: TCPReaderInit.java,v 1.12 2001/01/18 23:13:36 tdb1 Exp $ |
20 |
*/ |
21 |
class TCPReaderInit extends Thread { |
22 |
|
23 |
//---FINAL ATTRIBUTES--- |
24 |
|
25 |
/** |
26 |
* The current CVS revision of this class |
27 |
*/ |
28 |
public final String REVISION = "$Revision: 1.12 $"; |
29 |
|
30 |
//---STATIC METHODS--- |
31 |
|
32 |
//---CONSTRUCTORS--- |
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 |
_queue = queue; |
44 |
// setup the reader & writer |
45 |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
46 |
_socketOut = new PrintWriter(_socket.getOutputStream()); |
47 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
48 |
} |
49 |
|
50 |
//---PUBLIC METHODS--- |
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 |
//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 - expecting:HEARTBEAT got:" + inBound); |
69 |
} else { |
70 |
_socketOut.println("OK"); |
71 |
_socketOut.flush(); |
72 |
} |
73 |
|
74 |
inBound = _socketIn.readLine(); |
75 |
if(!inBound.equals("CONFIG")) { |
76 |
_socketOut.println("ERROR"); |
77 |
_socketOut.flush(); |
78 |
throw new IOException("protocol error - expecting:CONFIG got:" + inBound); |
79 |
} else { |
80 |
_socketOut.println("OK"); |
81 |
_socketOut.flush(); |
82 |
} |
83 |
|
84 |
inBound = _socketIn.readLine(); |
85 |
filelist = inBound; |
86 |
_socketOut.println("OK"); |
87 |
_socketOut.flush(); |
88 |
|
89 |
inBound = _socketIn.readLine(); |
90 |
lastModified = inBound; |
91 |
|
92 |
boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified)); |
93 |
|
94 |
if(newConfig) { |
95 |
_socketOut.println("ERROR"); |
96 |
} |
97 |
else { |
98 |
_socketOut.println("OK"); |
99 |
} |
100 |
_socketOut.flush(); |
101 |
|
102 |
inBound = _socketIn.readLine(); |
103 |
if(!inBound.equals("ENDHEARTBEAT")) { |
104 |
_socketOut.println("ERROR"); |
105 |
_socketOut.flush(); |
106 |
throw new IOException("protocol error - expecting:ENDHEARTBEAT got:" + inBound); |
107 |
} else { |
108 |
_socketOut.println("OK"); |
109 |
_socketOut.flush(); |
110 |
} |
111 |
|
112 |
String date = new Long(System.currentTimeMillis()).toString(); |
113 |
String hostname = _socket.getInetAddress().getHostName(); |
114 |
String ipadd = _socket.getInetAddress().getHostAddress(); |
115 |
String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\"></packet>"; |
116 |
|
117 |
_queue.add(xml); |
118 |
|
119 |
} catch (Exception e) { |
120 |
_logger.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
121 |
} |
122 |
|
123 |
_socketOut.flush(); |
124 |
// Disconnect streams & socket |
125 |
try { |
126 |
_socketIn.close(); |
127 |
_socketOut.close(); |
128 |
_socket.close(); |
129 |
} catch (IOException e) { |
130 |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
131 |
} |
132 |
_logger.write(toString(), Logger.SYSMSG, "finished"); |
133 |
} |
134 |
|
135 |
/** |
136 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
137 |
* method to provide clean logging (every class should have this). |
138 |
* |
139 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
140 |
* to format the toString() |
141 |
* |
142 |
* @return the name of this class and its CVS revision |
143 |
*/ |
144 |
public String toString() { |
145 |
return FormatName.getName( |
146 |
_name, |
147 |
getClass().getName(), |
148 |
REVISION); |
149 |
} |
150 |
|
151 |
//---PRIVATE METHODS--- |
152 |
|
153 |
//---ACCESSOR/MUTATOR METHODS--- |
154 |
|
155 |
//---ATTRIBUTES--- |
156 |
|
157 |
/** |
158 |
* A reference to the configuration manager |
159 |
*/ |
160 |
ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
161 |
|
162 |
/** |
163 |
* This is the friendly identifier of the |
164 |
* component this class is running in. |
165 |
* eg, a Filter may be called "filter1", |
166 |
* If this class does not have an owning |
167 |
* component, a name from the configuration |
168 |
* can be placed here. This name could also |
169 |
* be changed to null for utility classes. |
170 |
*/ |
171 |
private String _name = FilterMain.NAME; |
172 |
|
173 |
/** |
174 |
* This holds a reference to the |
175 |
* system logger that is being used. |
176 |
*/ |
177 |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
178 |
|
179 |
/** |
180 |
* The socket we are talking on |
181 |
*/ |
182 |
Socket _socket; |
183 |
|
184 |
/** |
185 |
* The input from the socket |
186 |
*/ |
187 |
BufferedReader _socketIn; |
188 |
|
189 |
/** |
190 |
* The output from the socket |
191 |
*/ |
192 |
PrintWriter _socketOut; |
193 |
|
194 |
/** |
195 |
* A reference to our Queue |
196 |
*/ |
197 |
Queue _queue; |
198 |
//---STATIC ATTRIBUTES--- |
199 |
|
200 |
} |