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; |
30 |
|
//---STATIC METHODS--- |
31 |
|
|
32 |
|
//---CONSTRUCTORS--- |
33 |
< |
|
34 |
< |
public TCPReaderInit(Socket socket, Filter parent) throws IOException { |
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()); |
46 |
> |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
47 |
|
_logger.write(toString(), Logger.SYSINIT, "created"); |
48 |
|
} |
49 |
|
|
50 |
|
//---PUBLIC METHODS--- |
51 |
< |
|
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 |
64 |
|
inBound = _socketIn.readLine(); |
65 |
|
if(!inBound.equals("HEARTBEAT")) { |
66 |
|
_socketOut.println("ERROR"); |
53 |
– |
_socketOut.flush(); |
67 |
|
throw new IOException("protocol error - expecting:HEARTBEAT got:" + inBound); |
68 |
|
} else { |
69 |
|
_socketOut.println("OK"); |
57 |
– |
_socketOut.flush(); |
70 |
|
} |
71 |
|
|
72 |
|
inBound = _socketIn.readLine(); |
73 |
|
if(!inBound.equals("CONFIG")) { |
74 |
|
_socketOut.println("ERROR"); |
63 |
– |
_socketOut.flush(); |
75 |
|
throw new IOException("protocol error - expecting:CONFIG got:" + inBound); |
76 |
|
} else { |
77 |
|
_socketOut.println("OK"); |
67 |
– |
_socketOut.flush(); |
78 |
|
} |
79 |
|
|
80 |
|
inBound = _socketIn.readLine(); |
81 |
|
filelist = inBound; |
82 |
|
_socketOut.println("OK"); |
73 |
– |
_socketOut.flush(); |
83 |
|
|
84 |
|
inBound = _socketIn.readLine(); |
85 |
|
lastModified = inBound; |
92 |
|
else { |
93 |
|
_socketOut.println("OK"); |
94 |
|
} |
86 |
– |
_socketOut.flush(); |
95 |
|
|
96 |
|
inBound = _socketIn.readLine(); |
97 |
|
if(!inBound.equals("ENDHEARTBEAT")) { |
98 |
|
_socketOut.println("ERROR"); |
91 |
– |
_socketOut.flush(); |
99 |
|
throw new IOException("protocol error - expecting:ENDHEARTBEAT got:" + inBound); |
100 |
|
} else { |
101 |
|
_socketOut.println("OK"); |
95 |
– |
_socketOut.flush(); |
102 |
|
} |
103 |
|
|
104 |
|
String date = new Long(System.currentTimeMillis()).toString(); |
105 |
|
String hostname = _socket.getInetAddress().getHostName(); |
106 |
|
String ipadd = _socket.getInetAddress().getHostAddress(); |
101 |
– |
String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\"></packet>"; |
107 |
|
|
108 |
< |
FilterThread t = new FilterThread(xml, _parent); |
109 |
< |
t.start(); |
108 |
> |
// run the service checks for this host |
109 |
> |
String checks = PluginServiceCheckManager.getInstance().runServiceChecks(hostname); |
110 |
|
|
111 |
+ |
// build the heartbeat packet |
112 |
+ |
String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\">" + checks + "</packet>"; |
113 |
+ |
|
114 |
+ |
// get it to be sent on |
115 |
+ |
_queue.add(xml); |
116 |
+ |
|
117 |
|
} catch (Exception e) { |
118 |
|
_logger.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
119 |
|
} |
120 |
|
|
110 |
– |
_socketOut.flush(); |
121 |
|
// Disconnect streams & socket |
122 |
|
try { |
123 |
|
_socketIn.close(); |
126 |
|
} catch (IOException e) { |
127 |
|
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
128 |
|
} |
129 |
< |
_logger.write(toString(), Logger.SYSMSG, "finished"); |
129 |
> |
_logger.write(toString(), Logger.DEBUG, "finished"); |
130 |
|
} |
131 |
|
|
132 |
|
/** |
189 |
|
PrintWriter _socketOut; |
190 |
|
|
191 |
|
/** |
192 |
< |
* A reference to our parent |
192 |
> |
* A reference to our Queue |
193 |
|
*/ |
194 |
< |
Filter _parent; |
194 |
> |
Queue _queue; |
195 |
|
//---STATIC ATTRIBUTES--- |
196 |
|
|
197 |
|
} |