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$ |
31 |
|
|
32 |
|
//---CONSTRUCTORS--- |
33 |
|
|
34 |
< |
public TCPReaderInit(Socket socket, ConfigurationManager configManager, Logger logRef, Filter parent) throws IOException { |
35 |
< |
_configManager = configManager; |
36 |
< |
_logRef = logRef; |
34 |
> |
public TCPReaderInit(Socket socket, Queue queue) throws IOException { |
35 |
|
_socket = socket; |
36 |
< |
_parent = parent; |
36 |
> |
_queue = queue; |
37 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
38 |
|
_socketOut = new PrintWriter(_socket.getOutputStream()); |
39 |
< |
_logRef.write(toString(), Logger.SYSINIT, "created"); |
39 |
> |
_logger.write(toString(), Logger.SYSINIT, "created"); |
40 |
|
} |
41 |
|
|
42 |
|
//---PUBLIC METHODS--- |
43 |
|
|
46 |
– |
//NEED TO MAJORILY CHANGE THIS BIT ! |
44 |
|
public void run() { |
45 |
|
try { |
46 |
< |
String inBound = _socketIn.readLine(); |
47 |
< |
if (!inBound.equals("STARTCONFIG")) { |
46 |
> |
//variables |
47 |
> |
String filelist = ""; |
48 |
> |
String lastModified = ""; |
49 |
> |
String inBound = ""; |
50 |
> |
|
51 |
> |
inBound = _socketIn.readLine(); |
52 |
> |
if(!inBound.equals("HEARTBEAT")) { |
53 |
|
_socketOut.println("ERROR"); |
54 |
|
_socketOut.flush(); |
55 |
< |
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
55 |
> |
throw new IOException("protocol error - expecting:HEARTBEAT got:" + inBound); |
56 |
> |
} else { |
57 |
> |
_socketOut.println("OK"); |
58 |
> |
_socketOut.flush(); |
59 |
|
} |
60 |
< |
|
61 |
< |
Configuration myConfig = _configManager.getConfiguration(_socket.getInetAddress().getHostName().toLowerCase()); |
62 |
< |
if (myConfig == null) { |
60 |
> |
|
61 |
> |
inBound = _socketIn.readLine(); |
62 |
> |
if(!inBound.equals("CONFIG")) { |
63 |
|
_socketOut.println("ERROR"); |
64 |
< |
throw new IOException("error in host configuration"); |
64 |
> |
_socketOut.flush(); |
65 |
> |
throw new IOException("protocol error - expecting:CONFIG got:" + inBound); |
66 |
|
} else { |
67 |
|
_socketOut.println("OK"); |
68 |
|
_socketOut.flush(); |
69 |
< |
|
70 |
< |
// get lastmodified |
71 |
< |
inBound = _socketIn.readLine(); |
72 |
< |
if(!inBound.equals("LASTMODIFIED")) { |
73 |
< |
// protocol error |
74 |
< |
_socketOut.println("ERROR"); |
75 |
< |
_socketOut.flush(); |
76 |
< |
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
77 |
< |
} |
78 |
< |
else { |
79 |
< |
// send info |
80 |
< |
_socketOut.println(myConfig.getLastModified()); |
81 |
< |
_socketOut.flush(); |
82 |
< |
} |
83 |
< |
|
84 |
< |
// 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"); |
69 |
> |
} |
70 |
> |
|
71 |
> |
inBound = _socketIn.readLine(); |
72 |
> |
filelist = inBound; |
73 |
> |
_socketOut.println("OK"); |
74 |
> |
_socketOut.flush(); |
75 |
> |
|
76 |
> |
inBound = _socketIn.readLine(); |
77 |
> |
lastModified = inBound; |
78 |
> |
|
79 |
> |
boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified)); |
80 |
> |
|
81 |
> |
if(newConfig) { |
82 |
> |
_socketOut.println("ERROR"); |
83 |
> |
} |
84 |
> |
else { |
85 |
|
_socketOut.println("OK"); |
86 |
+ |
} |
87 |
+ |
_socketOut.flush(); |
88 |
+ |
|
89 |
+ |
inBound = _socketIn.readLine(); |
90 |
+ |
if(!inBound.equals("ENDHEARTBEAT")) { |
91 |
+ |
_socketOut.println("ERROR"); |
92 |
|
_socketOut.flush(); |
93 |
< |
|
94 |
< |
// get filter reference |
95 |
< |
inBound = _socketIn.readLine(); |
96 |
< |
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 |
< |
|
93 |
> |
throw new IOException("protocol error - expecting:ENDHEARTBEAT got:" + inBound); |
94 |
> |
} else { |
95 |
> |
_socketOut.println("OK"); |
96 |
> |
_socketOut.flush(); |
97 |
|
} |
98 |
+ |
|
99 |
+ |
String date = new Long(System.currentTimeMillis()).toString(); |
100 |
+ |
String hostname = _socket.getInetAddress().getHostName(); |
101 |
+ |
String ipadd = _socket.getInetAddress().getHostAddress(); |
102 |
+ |
String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\"></packet>"; |
103 |
|
|
104 |
+ |
_queue.add(xml); |
105 |
+ |
|
106 |
|
} catch (Exception e) { |
107 |
< |
_logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
107 |
> |
_logger.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
108 |
|
} |
109 |
|
|
110 |
|
_socketOut.flush(); |
114 |
|
_socketOut.close(); |
115 |
|
_socket.close(); |
116 |
|
} catch (IOException e) { |
117 |
< |
_logRef.write(toString(), Logger.ERROR, "exception on socket close"); |
117 |
> |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
118 |
|
} |
119 |
< |
_logRef.write(toString(), Logger.SYSMSG, "finished"); |
119 |
> |
_logger.write(toString(), Logger.SYSMSG, "finished"); |
120 |
|
} |
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 |
+ |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
127 |
+ |
* to format the toString() |
128 |
+ |
* |
129 |
|
* @return the name of this class and its CVS revision |
130 |
|
*/ |
131 |
|
public String toString() { |
132 |
< |
return this.getClass().getName() + "{" + _socket.getInetAddress().getHostName() |
133 |
< |
+ "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
134 |
< |
|
132 |
> |
return FormatName.getName( |
133 |
> |
_name, |
134 |
> |
getClass().getName(), |
135 |
> |
REVISION); |
136 |
|
} |
137 |
|
|
138 |
|
//---PRIVATE METHODS--- |
141 |
|
|
142 |
|
//---ATTRIBUTES--- |
143 |
|
|
144 |
< |
ConfigurationManager _configManager; |
145 |
< |
Logger _logRef; |
144 |
> |
/** |
145 |
> |
* A reference to the configuration manager |
146 |
> |
*/ |
147 |
> |
ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
148 |
> |
|
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 |
|
Socket _socket; |
170 |
+ |
|
171 |
+ |
/** |
172 |
+ |
* The input from the socket |
173 |
+ |
*/ |
174 |
|
BufferedReader _socketIn; |
175 |
+ |
|
176 |
+ |
/** |
177 |
+ |
* The output from the socket |
178 |
+ |
*/ |
179 |
|
PrintWriter _socketOut; |
180 |
< |
Filter _parent; |
180 |
> |
|
181 |
> |
/** |
182 |
> |
* A reference to our Queue |
183 |
> |
*/ |
184 |
> |
Queue _queue; |
185 |
|
//---STATIC ATTRIBUTES--- |
186 |
|
|
187 |
|
} |