1 |
ab11 |
1.1 |
#include "Config.h" |
2 |
|
|
|
3 |
ab11 |
1.3 |
Config::Config( string serverName, int serverPort, int printDebug ){ |
4 |
ab11 |
1.1 |
// the constructor for the class |
5 |
|
|
|
6 |
ab11 |
1.3 |
|
7 |
ab11 |
1.1 |
configConnectionRetryTime = 1; // one second |
8 |
ab11 |
1.3 |
debug = printDebug; |
9 |
|
|
MAX_CONNECTION_ATTEMPTS = 10; // |
10 |
|
|
|
11 |
|
|
configName = serverName; |
12 |
|
|
configPort = serverPort; |
13 |
|
|
|
14 |
|
|
if ( debug == 1 ){ |
15 |
|
|
std::cout << "Config Constructor\n"; |
16 |
|
|
} |
17 |
|
|
|
18 |
ab11 |
1.1 |
|
19 |
|
|
// make a reference to smallnet |
20 |
ab11 |
1.3 |
if ( debug == 1 ){ |
21 |
|
|
std::cout << "Config::Constructor::Constructing SmallNet\n"; |
22 |
|
|
} |
23 |
|
|
net = new SmallNet(debug); |
24 |
|
|
// net->setDebug(1); |
25 |
ab11 |
1.1 |
|
26 |
|
|
// read the values which we want to retrieve from the |
27 |
|
|
// config manager from the "config.values.txt" file |
28 |
|
|
//propertyNames = new string[20]; |
29 |
|
|
//propertyValues = new string[20]; |
30 |
|
|
|
31 |
|
|
// FIX ME |
32 |
|
|
// sod that we are using defined values now.. |
33 |
|
|
propertyNames[0] = "UDPUpdateTime"; |
34 |
|
|
propertyNames[1] = "TCPUpdateTime"; |
35 |
|
|
propertyNames[2] = "FilterRetryTime"; |
36 |
|
|
propertyNames[3] = "ConfigRetryTime"; |
37 |
ab11 |
1.3 |
numProperties = 4; |
38 |
ab11 |
1.1 |
|
39 |
|
|
// read the Chat dialog we 'expect' to have with the config |
40 |
|
|
// manager from disk. This is a new idea so that the client |
41 |
|
|
// won't have to be hard coded with the dialog (incase it changes) |
42 |
|
|
|
43 |
|
|
// FIX ME |
44 |
|
|
|
45 |
|
|
if ( loadConfig() == 0 ){ |
46 |
|
|
// successful just continue |
47 |
ab11 |
1.3 |
if ( debug == 1 ){ |
48 |
|
|
std::cout << "Config::Constructor::loadConfig() Successful\n"; |
49 |
|
|
} |
50 |
ab11 |
1.1 |
} |
51 |
|
|
else { |
52 |
ab11 |
1.3 |
if ( debug == 1 ){ |
53 |
|
|
std::cout << "Config::Constructor::loadConfig() failed-retrying\n"; |
54 |
|
|
} |
55 |
ab11 |
1.1 |
// woops something went wrong. we'll just keep retrying |
56 |
|
|
// and increasing the time between trys |
57 |
|
|
int loadConfigResult = -1; |
58 |
|
|
|
59 |
|
|
// the following could cause problems if it is fed the |
60 |
|
|
// incorrect filter name and/or port it'll never stop trying |
61 |
ab11 |
1.3 |
int retrys = 1; // # of times we have tried to connect but failed.. |
62 |
ab11 |
1.1 |
while ( loadConfigResult != 0 ){ |
63 |
ab11 |
1.3 |
if ( retrys > MAX_CONNECTION_ATTEMPTS ){ |
64 |
|
|
std::cout << "Maxium number of retries reached, Quitting"; |
65 |
|
|
// exit("Failed to connect"); |
66 |
|
|
} |
67 |
ab11 |
1.1 |
// make the connection retry time greater |
68 |
|
|
configConnectionRetryTime = configConnectionRetryTime * 2; |
69 |
ab11 |
1.3 |
std::cout << "Waiting " << configConnectionRetryTime << " seconds before retrying\n"; |
70 |
ab11 |
1.1 |
sleep(configConnectionRetryTime); // seconds |
71 |
ab11 |
1.3 |
// increment the number of times we have tried to connect |
72 |
|
|
retrys++; |
73 |
ab11 |
1.1 |
loadConfigResult = loadConfig(); |
74 |
|
|
} // while |
75 |
|
|
|
76 |
|
|
|
77 |
|
|
} // if |
78 |
|
|
|
79 |
ab11 |
1.3 |
if ( debug == 1 ){ |
80 |
|
|
std::cout << "Config::Constructor - Finished!\n"; |
81 |
|
|
} |
82 |
|
|
|
83 |
ab11 |
1.1 |
} // Config |
84 |
|
|
|
85 |
|
|
int Config::loadConfig(){ |
86 |
|
|
// establishes a connection to the config manager (filter manager?) and |
87 |
|
|
// tries to load all the info which it needs, if it fails returns !0 |
88 |
|
|
|
89 |
ab11 |
1.3 |
if ( debug == 1 ){ |
90 |
|
|
std::cout << "Config::loadConfig()\n"; |
91 |
|
|
} |
92 |
|
|
|
93 |
ab11 |
1.1 |
// make a connection |
94 |
ab11 |
1.3 |
|
95 |
|
|
if ( debug == 1 ){ |
96 |
|
|
std::cout << "Config::loadConfig::establishing config connection\n"; |
97 |
|
|
} |
98 |
ab11 |
1.1 |
int established = establishConfig(); |
99 |
|
|
if ( established != 0 ){ |
100 |
|
|
return established; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
// do the talk ;) |
104 |
ab11 |
1.3 |
|
105 |
|
|
if ( debug == 1 ){ |
106 |
|
|
std::cout << "Config::loadConfig::Processing chat script\n"; |
107 |
|
|
} |
108 |
ab11 |
1.1 |
int chat = chatConfig(); |
109 |
|
|
if ( chat != 0 ){ |
110 |
|
|
// and close the connection |
111 |
|
|
destroyConfig(); |
112 |
ab11 |
1.3 |
// return chat; |
113 |
ab11 |
1.1 |
} |
114 |
|
|
|
115 |
|
|
// and close the connection |
116 |
ab11 |
1.3 |
|
117 |
|
|
if ( debug == 1 ){ |
118 |
|
|
std::cout << "Config::loadConfig::closing config connection\n"; |
119 |
|
|
} |
120 |
ab11 |
1.1 |
destroyConfig(); |
121 |
|
|
|
122 |
|
|
// return everything ok |
123 |
|
|
return 0; |
124 |
|
|
|
125 |
|
|
} // loadConfig |
126 |
|
|
|
127 |
|
|
int Config::establishHeartbeat(){ |
128 |
|
|
// just an interface onto smallnet |
129 |
ab11 |
1.3 |
if ( debug == 1 ){ |
130 |
|
|
std::cout << "Config::establishHeartbeat()\n"; |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
int establish = net->connectHeartBeat(configName, configPort); |
134 |
ab11 |
1.1 |
|
135 |
ab11 |
1.3 |
if ( debug == 1 ){ |
136 |
|
|
std::cout << "->" << establish << "\n"; |
137 |
|
|
} |
138 |
ab11 |
1.1 |
|
139 |
|
|
return establish; |
140 |
|
|
|
141 |
|
|
} // establishHeartBeat |
142 |
|
|
|
143 |
|
|
|
144 |
|
|
int Config::destroyHeartbeat(){ |
145 |
|
|
// just hooks into the smallnet interface |
146 |
ab11 |
1.3 |
if ( debug == 1 ){ |
147 |
|
|
std::cout << "Config::destroyHeartbeat()\n"; |
148 |
|
|
} |
149 |
ab11 |
1.1 |
|
150 |
ab11 |
1.3 |
net->closeHeartBeatConnection(); |
151 |
ab11 |
1.1 |
|
152 |
|
|
} // destroyHeartBeat |
153 |
|
|
|
154 |
|
|
|
155 |
|
|
int Config::establishConfig(){ |
156 |
|
|
// ask smallnet to make a connection to the filtermanager so we can get some config |
157 |
|
|
// details |
158 |
ab11 |
1.3 |
if ( debug == 1 ){ |
159 |
|
|
std::cout << "Config::establishConfig()\n"; |
160 |
|
|
} |
161 |
|
|
|
162 |
ab11 |
1.1 |
|
163 |
ab11 |
1.3 |
/* MODIFY THIS */ |
164 |
|
|
int response = net->connectConfig(configName, configPort); |
165 |
ab11 |
1.1 |
|
166 |
ab11 |
1.3 |
if ( debug == 1 ){ |
167 |
|
|
std::cout << "->" << response << "\n"; |
168 |
|
|
} |
169 |
ab11 |
1.1 |
|
170 |
ab11 |
1.3 |
return response; |
171 |
ab11 |
1.1 |
|
172 |
|
|
} // establishConfig |
173 |
|
|
|
174 |
|
|
|
175 |
|
|
int Config::chatConfig(){ |
176 |
|
|
// do some funky jibble.. ;) |
177 |
ab11 |
1.3 |
if ( debug == 1 ){ |
178 |
|
|
std::cout << "Config::chatConfig()\n"; |
179 |
|
|
} |
180 |
ab11 |
1.1 |
|
181 |
|
|
string response; |
182 |
|
|
|
183 |
ab11 |
1.3 |
|
184 |
ab11 |
1.1 |
// hard coeded for the moment |
185 |
ab11 |
1.3 |
if ( debug == 1 ){ |
186 |
|
|
std::cout << "Config::chatConfig::STARTCONFIG\n"; |
187 |
|
|
} |
188 |
|
|
response = net->heartBeatSend("STARTCONFIG"); |
189 |
ab11 |
1.1 |
if ( response != "OK" ){ |
190 |
|
|
// something went wrong |
191 |
|
|
return -1; |
192 |
ab11 |
1.3 |
if ( debug == 1 ){ |
193 |
|
|
std::cout << "Config::chatConfig::ERROR\n"; |
194 |
|
|
} |
195 |
ab11 |
1.1 |
} |
196 |
|
|
|
197 |
ab11 |
1.3 |
if ( debug == 1 ){ |
198 |
|
|
std::cout << "Config::chatConfig::LASTMODIFIED\n"; |
199 |
|
|
} |
200 |
|
|
lastModified = net->heartBeatSend("LASTMODIFIED"); |
201 |
|
|
if ( debug == 1 ){ |
202 |
|
|
std::cout << "Config::chatConfig::FILELIST\n"; |
203 |
|
|
} |
204 |
|
|
fileList = net->heartBeatSend("FILELIST"); |
205 |
ab11 |
1.1 |
|
206 |
ab11 |
1.4 |
fQDN = net->heartBeatSend("FQDN"); |
207 |
|
|
|
208 |
ab11 |
1.1 |
// now send the properties we want |
209 |
|
|
|
210 |
ab11 |
1.3 |
if ( debug == 1 ){ |
211 |
|
|
std::cout << "Config::chatConfig::requesting properties\n"; |
212 |
|
|
} |
213 |
ab11 |
1.1 |
if ( numProperties > 0 ){ |
214 |
|
|
// we actually need to look for some properties |
215 |
|
|
for ( int i=0; i < numProperties; i++ ){ |
216 |
|
|
// lets hope that this works! ;) |
217 |
ab11 |
1.3 |
if ( debug == 1 ){ |
218 |
|
|
std::cout << "Config::chatConfig::" << propertyNames[i] << "\n"; |
219 |
|
|
} |
220 |
|
|
propertyValues[i] = net->heartBeatSend(propertyNames[i]); |
221 |
ab11 |
1.1 |
} // for |
222 |
|
|
} // if |
223 |
|
|
|
224 |
ab11 |
1.3 |
if ( debug == 1 ){ |
225 |
|
|
std::cout << "Config::chatConfig::ENDCONFIG\n"; |
226 |
|
|
} |
227 |
|
|
response = net->heartBeatSend("ENDCONFIG"); |
228 |
ab11 |
1.1 |
// doesn't matter what it returns |
229 |
|
|
|
230 |
ab11 |
1.3 |
if ( debug == 1 ){ |
231 |
|
|
std::cout << "Config::chatConfig::FILTER\n"; |
232 |
|
|
} |
233 |
|
|
response = net->heartBeatSend("FILTER"); |
234 |
ab11 |
1.1 |
// now we need to pull repsonse apart to get the filter information out |
235 |
ab11 |
1.3 |
int first = response.find(";",0); |
236 |
|
|
int second = response.find(";",first+1); |
237 |
|
|
UDPFilterName = response.substr(0,first); |
238 |
|
|
UDPFilterPort = atoi( response.substr( first+1 ,second ).c_str()); |
239 |
|
|
TCPFilterPort = atoi(response.substr(second+1).c_str()); |
240 |
ab11 |
1.1 |
|
241 |
ab11 |
1.3 |
if ( debug == 1 ){ |
242 |
|
|
std::cout << "Config::chatConfig::END\n"; |
243 |
|
|
} |
244 |
|
|
response = net->heartBeatSend("END"); |
245 |
ab11 |
1.1 |
|
246 |
|
|
return 0; |
247 |
|
|
|
248 |
|
|
} // chatConfig |
249 |
|
|
|
250 |
|
|
int Config::destroyConfig(){ |
251 |
ab11 |
1.3 |
if ( debug == 1 ){ |
252 |
|
|
std::cout << "Config::destroyConfig()\n"; |
253 |
|
|
} |
254 |
ab11 |
1.1 |
|
255 |
ab11 |
1.3 |
net->closeConfigConnection(); |
256 |
ab11 |
1.1 |
|
257 |
|
|
} // destroyConfig |
258 |
|
|
|
259 |
ab11 |
1.3 |
string Config::getStrProperty(string propertyName){ |
260 |
|
|
|
261 |
|
|
|
262 |
|
|
if ( debug == 1 ){ |
263 |
|
|
std::cout << "Config::getProperty()\n"; |
264 |
|
|
} |
265 |
ab11 |
1.1 |
|
266 |
|
|
for ( int i=0; i<20; i++ ){ |
267 |
|
|
if ( propertyName == propertyNames[i] ){ |
268 |
ab11 |
1.3 |
if ( propertyValues[i] != "" ){ |
269 |
|
|
return propertyValues[i]; |
270 |
|
|
} // if |
271 |
ab11 |
1.1 |
} // if |
272 |
|
|
} // for |
273 |
|
|
|
274 |
|
|
// otherwise return ERROR |
275 |
|
|
return "ERROR"; |
276 |
|
|
|
277 |
ab11 |
1.3 |
} // getStrProperty |
278 |
|
|
|
279 |
|
|
int Config::getIntProperty( string propertyName ){ |
280 |
|
|
|
281 |
|
|
if ( debug == 1 ){ |
282 |
|
|
std::cout << "Config::getProperty()\n"; |
283 |
|
|
} |
284 |
|
|
|
285 |
|
|
string response = getStrProperty( propertyName ); |
286 |
|
|
if ( response == "ERROR" ){ |
287 |
|
|
return -1; |
288 |
|
|
} // if |
289 |
|
|
|
290 |
|
|
int i = atoi(response.c_str()); |
291 |
|
|
|
292 |
|
|
if ( i > 0 ){ |
293 |
|
|
return i; |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
// otherwise return ERROR |
297 |
|
|
return -1; |
298 |
|
|
|
299 |
|
|
} // getIntProperty |