1 |
|
//---PACKAGE DECLARATION--- |
2 |
+ |
package uk.ac.ukc.iscream.filter; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import uk.ac.ukc.iscream.core.*; |
11 |
|
import java.io.*; |
12 |
|
import org.omg.CORBA.*; |
13 |
|
import org.omg.CosNaming.*; |
14 |
+ |
import uk.ac.ukc.iscream.refman.*; |
15 |
|
|
16 |
|
/** |
17 |
|
* <ONE LINE DESCRIPTION> |
33 |
|
|
34 |
|
//---CONSTRUCTORS--- |
35 |
|
|
36 |
< |
public TCPReaderInit(Socket socket, ConfigurationManager configManager, Logger logRef, Filter parent) throws IOException { |
35 |
< |
_configManager = configManager; |
36 |
< |
_logRef = logRef; |
36 |
> |
public TCPReaderInit(Socket socket, Filter parent) throws IOException { |
37 |
|
_socket = socket; |
38 |
|
_parent = parent; |
39 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
40 |
|
_socketOut = new PrintWriter(_socket.getOutputStream()); |
41 |
< |
_logRef.write(toString(), Logger.SYSINIT, "created"); |
41 |
> |
_logger.write(toString(), Logger.SYSINIT, "created"); |
42 |
|
} |
43 |
|
|
44 |
|
//---PUBLIC METHODS--- |
45 |
|
|
46 |
– |
//NEED TO MAJORILY CHANGE THIS BIT ! |
46 |
|
public void run() { |
47 |
|
try { |
48 |
< |
String inBound = _socketIn.readLine(); |
49 |
< |
if (!inBound.equals("STARTCONFIG")) { |
48 |
> |
//variables |
49 |
> |
String filelist = ""; |
50 |
> |
String lastModified = ""; |
51 |
> |
String inBound = ""; |
52 |
> |
|
53 |
> |
inBound = _socketIn.readLine(); |
54 |
> |
if(!inBound.equals("HEARTBEAT")) { |
55 |
|
_socketOut.println("ERROR"); |
56 |
|
_socketOut.flush(); |
57 |
< |
throw new IOException("protocol error - expected:STARTCONFIG got:" + inBound); |
57 |
> |
throw new IOException("protocol error - expecting:HEARTBEAT got:" + inBound); |
58 |
> |
} else { |
59 |
> |
_socketOut.println("OK"); |
60 |
> |
_socketOut.flush(); |
61 |
|
} |
62 |
< |
|
63 |
< |
Configuration myConfig = _configManager.getConfiguration(_socket.getInetAddress().getHostName().toLowerCase()); |
64 |
< |
if (myConfig == null) { |
62 |
> |
|
63 |
> |
inBound = _socketIn.readLine(); |
64 |
> |
if(!inBound.equals("CONFIG")) { |
65 |
|
_socketOut.println("ERROR"); |
66 |
< |
throw new IOException("error in host configuration"); |
66 |
> |
_socketOut.flush(); |
67 |
> |
throw new IOException("protocol error - expecting:CONFIG got:" + inBound); |
68 |
|
} else { |
69 |
|
_socketOut.println("OK"); |
70 |
|
_socketOut.flush(); |
71 |
< |
|
72 |
< |
// get lastmodified |
73 |
< |
inBound = _socketIn.readLine(); |
74 |
< |
if(!inBound.equals("LASTMODIFIED")) { |
75 |
< |
// protocol error |
76 |
< |
_socketOut.println("ERROR"); |
77 |
< |
_socketOut.flush(); |
78 |
< |
throw new IOException("protocol error - expected:LASTMODIFIED got:" + inBound); |
79 |
< |
} |
80 |
< |
else { |
81 |
< |
// send info |
82 |
< |
_socketOut.println(myConfig.getLastModified()); |
83 |
< |
_socketOut.flush(); |
84 |
< |
} |
85 |
< |
|
86 |
< |
// 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"); |
71 |
> |
} |
72 |
> |
|
73 |
> |
inBound = _socketIn.readLine(); |
74 |
> |
filelist = inBound; |
75 |
> |
_socketOut.println("OK"); |
76 |
> |
_socketOut.flush(); |
77 |
> |
|
78 |
> |
inBound = _socketIn.readLine(); |
79 |
> |
lastModified = inBound; |
80 |
> |
|
81 |
> |
boolean newConfig = _configManager.isModified(filelist, Long.parseLong(lastModified)); |
82 |
> |
|
83 |
> |
if(newConfig) { |
84 |
> |
_socketOut.println("ERROR"); |
85 |
> |
} |
86 |
> |
else { |
87 |
|
_socketOut.println("OK"); |
88 |
+ |
} |
89 |
+ |
_socketOut.flush(); |
90 |
+ |
|
91 |
+ |
inBound = _socketIn.readLine(); |
92 |
+ |
if(!inBound.equals("ENDHEARTBEAT")) { |
93 |
+ |
_socketOut.println("ERROR"); |
94 |
|
_socketOut.flush(); |
95 |
< |
|
96 |
< |
// get filter reference |
97 |
< |
inBound = _socketIn.readLine(); |
98 |
< |
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 |
< |
|
95 |
> |
throw new IOException("protocol error - expecting:ENDHEARTBEAT got:" + inBound); |
96 |
> |
} else { |
97 |
> |
_socketOut.println("OK"); |
98 |
> |
_socketOut.flush(); |
99 |
|
} |
100 |
+ |
|
101 |
+ |
String date = new Long(System.currentTimeMillis()).toString(); |
102 |
+ |
String hostname = _socket.getInetAddress().getHostName(); |
103 |
+ |
String ipadd = _socket.getInetAddress().getHostAddress(); |
104 |
+ |
String xml = "<packet type=\"heartbeat\" machine_name=\""+hostname+"\" date=\""+date+"\" ip=\""+ipadd+"\"></packet>"; |
105 |
|
|
106 |
+ |
FilterThread t = new FilterThread(xml, _parent); |
107 |
+ |
t.start(); |
108 |
+ |
|
109 |
|
} catch (Exception e) { |
110 |
< |
_logRef.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
110 |
> |
_logger.write(toString(), Logger.ERROR, "ERROR: " + e.getMessage()); |
111 |
|
} |
112 |
|
|
113 |
|
_socketOut.flush(); |
117 |
|
_socketOut.close(); |
118 |
|
_socket.close(); |
119 |
|
} catch (IOException e) { |
120 |
< |
_logRef.write(toString(), Logger.ERROR, "exception on socket close"); |
120 |
> |
_logger.write(toString(), Logger.ERROR, "exception on socket close"); |
121 |
|
} |
122 |
< |
_logRef.write(toString(), Logger.SYSMSG, "finished"); |
122 |
> |
_logger.write(toString(), Logger.SYSMSG, "finished"); |
123 |
|
} |
124 |
|
|
125 |
|
/** |
140 |
|
|
141 |
|
//---ATTRIBUTES--- |
142 |
|
|
143 |
< |
ConfigurationManager _configManager; |
144 |
< |
Logger _logRef; |
143 |
> |
ConfigurationManager _configManager = ReferenceManager.getInstance().getCM(); |
144 |
> |
Logger _logger = ReferenceManager.getInstance().getLogger(); |
145 |
|
Socket _socket; |
146 |
|
BufferedReader _socketIn; |
147 |
|
PrintWriter _socketOut; |