1 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.5 |
package uk.ac.ukc.iscream.filter; |
3 |
tdb |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import uk.ac.ukc.iscream.core.*; |
6 |
|
|
import uk.ac.ukc.iscream.filter.*; |
7 |
|
|
import java.net.Socket; |
8 |
|
|
import java.io.InputStream; |
9 |
|
|
import java.io.OutputStream; |
10 |
|
|
import java.io.IOException; |
11 |
|
|
import java.io.*; |
12 |
ajm |
1.9 |
import uk.ac.ukc.iscream.util.*; |
13 |
tdb |
1.1 |
|
14 |
|
|
/** |
15 |
ajm |
1.10 |
* This provides Host heartbeat functionality |
16 |
tdb |
1.1 |
* |
17 |
ajm |
1.10 |
* @author $Author: ajm4 $ |
18 |
|
|
* @version $Id: TCPReaderInit.java,v 1.9 2000/11/30 02:38:09 ajm4 Exp $ |
19 |
tdb |
1.1 |
*/ |
20 |
|
|
class TCPReaderInit extends Thread { |
21 |
|
|
|
22 |
|
|
//---FINAL ATTRIBUTES--- |
23 |
|
|
|
24 |
|
|
/** |
25 |
|
|
* The current CVS revision of this class |
26 |
|
|
*/ |
27 |
ajm |
1.10 |
public final String REVISION = "$Revision: 1.9 $"; |
28 |
tdb |
1.1 |
|
29 |
|
|
//---STATIC METHODS--- |
30 |
|
|
|
31 |
|
|
//---CONSTRUCTORS--- |
32 |
|
|
|
33 |
tdb |
1.8 |
public TCPReaderInit(Socket socket, Filter parent) throws IOException { |
34 |
tdb |
1.1 |
_socket = socket; |
35 |
|
|
_parent = parent; |
36 |
|
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
37 |
|
|
_socketOut = new PrintWriter(_socket.getOutputStream()); |
38 |
tdb |
1.8 |
_logger.write(toString(), Logger.SYSINIT, "created"); |
39 |
tdb |
1.1 |
} |
40 |
|
|
|
41 |
|
|
//---PUBLIC METHODS--- |
42 |
|
|
|
43 |
|
|
public void run() { |
44 |
|
|
try { |
45 |
tdb |
1.3 |
//variables |
46 |
|
|
String filelist = ""; |
47 |
|
|
String lastModified = ""; |
48 |
|
|
String inBound = ""; |
49 |
tdb |
1.2 |
|
50 |
tdb |
1.3 |
inBound = _socketIn.readLine(); |
51 |
|
|
if(!inBound.equals("HEARTBEAT")) { |
52 |
|
|
_socketOut.println("ERROR"); |
53 |
|
|
_socketOut.flush(); |
54 |
|
|
throw new IOException("protocol error - expecting:HEARTBEAT got:" + inBound); |
55 |
|
|
} else { |
56 |
|
|
_socketOut.println("OK"); |
57 |
|
|
_socketOut.flush(); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
inBound = _socketIn.readLine(); |
61 |
|
|
if(!inBound.equals("CONFIG")) { |
62 |
|
|
_socketOut.println("ERROR"); |
63 |
|
|
_socketOut.flush(); |
64 |
|
|
throw new IOException("protocol error - expecting:CONFIG got:" + inBound); |
65 |
|
|
} else { |
66 |
|
|
_socketOut.println("OK"); |
67 |
|
|
_socketOut.flush(); |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
inBound = _socketIn.readLine(); |
71 |
|
|
filelist = inBound; |
72 |
|
|
_socketOut.println("OK"); |
73 |
|
|
_socketOut.flush(); |
74 |
|
|
|
75 |
|
|
inBound = _socketIn.readLine(); |
76 |
|
|
lastModified = inBound; |
77 |
|
|
|
78 |
|
|
boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified)); |
79 |
|
|
|
80 |
|
|
if(newConfig) { |
81 |
|
|
_socketOut.println("ERROR"); |
82 |
|
|
} |
83 |
|
|
else { |
84 |
|
|
_socketOut.println("OK"); |
85 |
|
|
} |
86 |
|
|
_socketOut.flush(); |
87 |
|
|
|
88 |
|
|
inBound = _socketIn.readLine(); |
89 |
|
|
if(!inBound.equals("ENDHEARTBEAT")) { |
90 |
|
|
_socketOut.println("ERROR"); |
91 |
|
|
_socketOut.flush(); |
92 |
|
|
throw new IOException("protocol error - expecting:ENDHEARTBEAT got:" + inBound); |
93 |
|
|
} else { |
94 |
|
|
_socketOut.println("OK"); |
95 |
|
|
_socketOut.flush(); |
96 |
|
|
} |
97 |
tdb |
1.4 |
|
98 |
tdb |
1.6 |
String date = new Long(System.currentTimeMillis()).toString(); |
99 |
|
|
String hostname = _socket.getInetAddress().getHostName(); |
100 |
|
|
String ipadd = _socket.getInetAddress().getHostAddress(); |
101 |
|
|
String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\"></packet>"; |
102 |
|
|
|
103 |
tdb |
1.8 |
FilterThread t = new FilterThread(xml, _parent); |
104 |
tdb |
1.6 |
t.start(); |
105 |
tdb |
1.1 |
|
106 |
|
|
} catch (Exception e) { |
107 |
tdb |
1.8 |
_logger.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
108 |
tdb |
1.1 |
} |
109 |
|
|
|
110 |
|
|
_socketOut.flush(); |
111 |
|
|
// Disconnect streams & socket |
112 |
|
|
try { |
113 |
|
|
_socketIn.close(); |
114 |
|
|
_socketOut.close(); |
115 |
|
|
_socket.close(); |
116 |
|
|
} catch (IOException e) { |
117 |
tdb |
1.8 |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
118 |
tdb |
1.1 |
} |
119 |
tdb |
1.8 |
_logger.write(toString(), Logger.SYSMSG, "finished"); |
120 |
tdb |
1.1 |
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
124 |
|
|
* method to provide clean logging (every class should have this). |
125 |
|
|
* |
126 |
ajm |
1.10 |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
127 |
|
|
* to format the toString() |
128 |
|
|
* |
129 |
tdb |
1.1 |
* @return the name of this class and its CVS revision |
130 |
|
|
*/ |
131 |
|
|
public String toString() { |
132 |
ajm |
1.10 |
return FormatName.getName( |
133 |
|
|
_name, |
134 |
|
|
getClass().getName(), |
135 |
|
|
REVISION); |
136 |
tdb |
1.1 |
} |
137 |
|
|
|
138 |
|
|
//---PRIVATE METHODS--- |
139 |
|
|
|
140 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
141 |
|
|
|
142 |
|
|
//---ATTRIBUTES--- |
143 |
|
|
|
144 |
ajm |
1.10 |
/** |
145 |
|
|
* A reference to the configuration manager |
146 |
|
|
*/ |
147 |
tdb |
1.8 |
ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
148 |
ajm |
1.10 |
|
149 |
|
|
/** |
150 |
|
|
* This is the friendly identifier of the |
151 |
|
|
* component this class is running in. |
152 |
|
|
* eg, a Filter may be called "filter1", |
153 |
|
|
* If this class does not have an owning |
154 |
|
|
* component, a name from the configuration |
155 |
|
|
* can be placed here. This name could also |
156 |
|
|
* be changed to null for utility classes. |
157 |
|
|
*/ |
158 |
|
|
private String _name = FilterMain.NAME; |
159 |
|
|
|
160 |
|
|
/** |
161 |
|
|
* This holds a reference to the |
162 |
|
|
* system logger that is being used. |
163 |
|
|
*/ |
164 |
|
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
165 |
|
|
|
166 |
|
|
/** |
167 |
|
|
* The socket we are talking on |
168 |
|
|
*/ |
169 |
tdb |
1.1 |
Socket _socket; |
170 |
ajm |
1.10 |
|
171 |
|
|
/** |
172 |
|
|
* The input from the socket |
173 |
|
|
*/ |
174 |
tdb |
1.1 |
BufferedReader _socketIn; |
175 |
ajm |
1.10 |
|
176 |
|
|
/** |
177 |
|
|
* The output from the socket |
178 |
|
|
*/ |
179 |
tdb |
1.1 |
PrintWriter _socketOut; |
180 |
ajm |
1.10 |
|
181 |
|
|
/** |
182 |
|
|
* A reference to our parent |
183 |
|
|
*/ |
184 |
tdb |
1.1 |
Filter _parent; |
185 |
|
|
//---STATIC ATTRIBUTES--- |
186 |
|
|
|
187 |
|
|
} |