3 |
|
Config::Config( string serverName, int serverPort, int printDebug ){ |
4 |
|
// the constructor for the class |
5 |
|
|
6 |
– |
|
6 |
|
configConnectionRetryTime = 1; // one second |
7 |
|
debug = printDebug; |
8 |
< |
MAX_CONNECTION_ATTEMPTS = 10; // |
8 |
> |
MAX_CONNECTION_ATTEMPTS = 10; |
9 |
|
|
10 |
|
configName = serverName; |
11 |
|
configPort = serverPort; |
12 |
|
|
13 |
|
if ( debug == 1 ){ |
14 |
|
std::cout << "Config Constructor\n"; |
15 |
< |
} |
15 |
> |
} // if |
16 |
|
|
17 |
|
|
18 |
|
// make a reference to smallnet |
20 |
|
std::cout << "Config::Constructor::Constructing SmallNet\n"; |
21 |
|
} |
22 |
|
net = new SmallNet(debug); |
23 |
< |
// net->setDebug(1); |
25 |
< |
|
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 |
23 |
> |
|
24 |
|
// sod that we are using defined values now.. |
25 |
|
propertyNames[0] = "UDPUpdateTime"; |
26 |
|
propertyNames[1] = "TCPUpdateTime"; |
27 |
|
propertyNames[2] = "FilterRetryTime"; |
28 |
|
propertyNames[3] = "ConfigRetryTime"; |
29 |
< |
numProperties = 4; |
29 |
> |
propertyNames[4] = "AVERAGERUpdateTime"; |
30 |
> |
propertyNames[5] = "MAXTCPFilterRetries"; |
31 |
> |
numProperties = 5; |
32 |
|
|
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 |
– |
|
33 |
|
if ( loadConfig() == 0 ){ |
34 |
|
// successful just continue |
35 |
|
if ( debug == 1 ){ |
36 |
|
std::cout << "Config::Constructor::loadConfig() Successful\n"; |
37 |
< |
} |
38 |
< |
} |
51 |
< |
else { |
37 |
> |
} // if |
38 |
> |
} else { |
39 |
|
if ( debug == 1 ){ |
40 |
|
std::cout << "Config::Constructor::loadConfig() failed-retrying\n"; |
41 |
< |
} |
41 |
> |
} // if |
42 |
> |
|
43 |
|
// woops something went wrong. we'll just keep retrying |
44 |
|
// and increasing the time between trys |
45 |
|
int loadConfigResult = -1; |
50 |
|
while ( loadConfigResult != 0 ){ |
51 |
|
if ( retrys > MAX_CONNECTION_ATTEMPTS ){ |
52 |
|
std::cout << "Maxium number of retries reached, Quitting"; |
53 |
< |
// exit("Failed to connect"); |
66 |
< |
} |
53 |
> |
} // if |
54 |
|
// make the connection retry time greater |
55 |
|
configConnectionRetryTime = configConnectionRetryTime * 2; |
56 |
|
std::cout << "Waiting " << configConnectionRetryTime << " seconds before retrying\n"; |
59 |
|
retrys++; |
60 |
|
loadConfigResult = loadConfig(); |
61 |
|
} // while |
75 |
– |
|
76 |
– |
|
62 |
|
} // if |
63 |
|
|
64 |
|
if ( debug == 1 ){ |
65 |
|
std::cout << "Config::Constructor - Finished!\n"; |
66 |
< |
} |
66 |
> |
} // if |
67 |
|
|
68 |
|
} // Config |
69 |
|
|
73 |
|
|
74 |
|
if ( debug == 1 ){ |
75 |
|
std::cout << "Config::loadConfig()\n"; |
76 |
< |
} |
76 |
> |
} // if |
77 |
|
|
78 |
|
// make a connection |
79 |
|
|
80 |
|
if ( debug == 1 ){ |
81 |
|
std::cout << "Config::loadConfig::establishing config connection\n"; |
82 |
< |
} |
82 |
> |
} // if |
83 |
> |
|
84 |
|
int established = establishConfig(); |
85 |
|
if ( established != 0 ){ |
86 |
+ |
destroyConfig(); |
87 |
|
return established; |
88 |
< |
} |
88 |
> |
} // if |
89 |
|
|
90 |
|
// do the talk ;) |
91 |
|
|
92 |
|
if ( debug == 1 ){ |
93 |
|
std::cout << "Config::loadConfig::Processing chat script\n"; |
94 |
< |
} |
94 |
> |
} // if |
95 |
> |
|
96 |
|
int chat = chatConfig(); |
97 |
|
if ( chat != 0 ){ |
98 |
|
// and close the connection |
99 |
|
destroyConfig(); |
100 |
< |
// return chat; |
113 |
< |
} |
100 |
> |
} // if |
101 |
|
|
102 |
|
// and close the connection |
116 |
– |
|
103 |
|
if ( debug == 1 ){ |
104 |
|
std::cout << "Config::loadConfig::closing config connection\n"; |
105 |
< |
} |
105 |
> |
} // if |
106 |
|
destroyConfig(); |
107 |
|
|
108 |
|
// return everything ok |
114 |
|
// just an interface onto smallnet |
115 |
|
if ( debug == 1 ){ |
116 |
|
std::cout << "Config::establishHeartbeat()\n"; |
117 |
< |
} |
117 |
> |
} // if |
118 |
|
|
119 |
< |
int establish = net->connectHeartBeat(configName, configPort); |
120 |
< |
|
119 |
> |
int establish = net->connectHeartBeat(configName, configPort); |
120 |
> |
|
121 |
|
if ( debug == 1 ){ |
122 |
|
std::cout << "->" << establish << "\n"; |
123 |
< |
} |
123 |
> |
} // if |
124 |
|
|
125 |
|
return establish; |
126 |
|
|
127 |
|
} // establishHeartBeat |
128 |
|
|
143 |
– |
|
129 |
|
int Config::destroyHeartbeat(){ |
130 |
|
// just hooks into the smallnet interface |
131 |
|
if ( debug == 1 ){ |
132 |
|
std::cout << "Config::destroyHeartbeat()\n"; |
133 |
< |
} |
133 |
> |
} // if |
134 |
|
|
135 |
|
net->closeHeartBeatConnection(); |
136 |
|
|
137 |
|
} // destroyHeartBeat |
138 |
|
|
139 |
+ |
int Config::chatHeartBeat(){ |
140 |
+ |
|
141 |
+ |
string response; |
142 |
+ |
|
143 |
+ |
response = net->heartBeatSend("HEARTBEAT"); |
144 |
+ |
if ( response != "OK" ){ |
145 |
+ |
// something went wrong |
146 |
+ |
if ( debug == 1 ){ |
147 |
+ |
std::cout << "Config::chatHeartBeat::ERROR in HEARTBEAT\n"; |
148 |
+ |
} // if |
149 |
+ |
return -1; |
150 |
+ |
} // if |
151 |
+ |
|
152 |
+ |
response = net->heartBeatSend("CONFIG"); |
153 |
+ |
if ( response != "OK" ){ |
154 |
+ |
// something went wrong |
155 |
+ |
if ( debug == 1 ){ |
156 |
+ |
std::cout << "Config::chatHeartBeat::ERROR in CONFIG\n"; |
157 |
+ |
} // if |
158 |
+ |
return -1; |
159 |
+ |
} // if |
160 |
+ |
|
161 |
+ |
response = net->heartBeatSend(fileList); |
162 |
+ |
if ( response != "OK" ){ |
163 |
+ |
// something went wrong |
164 |
+ |
if ( debug == 1 ){ |
165 |
+ |
std::cout << "Config::chatHeartBeat::ERROR in fileList\n"; |
166 |
+ |
} // if |
167 |
+ |
return -1; |
168 |
+ |
} // if |
169 |
+ |
|
170 |
+ |
response = net->heartBeatSend(lastModified); |
171 |
+ |
if ( response != "OK" ){ |
172 |
+ |
// something went wrong |
173 |
+ |
if ( debug == 1 ){ |
174 |
+ |
std::cout << "Config::chatHeartBeat::ERROR in lastModified\n"; |
175 |
+ |
} // if |
176 |
+ |
return -1; |
177 |
+ |
} // if |
178 |
+ |
|
179 |
+ |
response = net->heartBeatSend("ENDHEARTBEAT"); |
180 |
+ |
if ( response != "OK" ){ |
181 |
+ |
// something went wrong |
182 |
+ |
if ( debug == 1 ){ |
183 |
+ |
std::cout << "Config::chatHeartBeat::ERROR in ENDHEARTBEAT\n"; |
184 |
+ |
} // if |
185 |
+ |
return -1; |
186 |
+ |
} // if |
187 |
+ |
|
188 |
+ |
// seems ok to me |
189 |
+ |
return 0; |
190 |
+ |
|
191 |
+ |
} // chat Heart Beat |
192 |
|
|
193 |
+ |
|
194 |
|
int Config::establishConfig(){ |
195 |
|
// ask smallnet to make a connection to the filtermanager so we can get some config |
196 |
|
// details |
198 |
|
std::cout << "Config::establishConfig()\n"; |
199 |
|
} |
200 |
|
|
162 |
– |
|
163 |
– |
/* MODIFY THIS */ |
201 |
|
int response = net->connectConfig(configName, configPort); |
202 |
|
|
203 |
|
if ( debug == 1 ){ |
204 |
|
std::cout << "->" << response << "\n"; |
205 |
< |
} |
205 |
> |
} // if |
206 |
|
|
207 |
|
return response; |
208 |
|
|
213 |
|
// do some funky jibble.. ;) |
214 |
|
if ( debug == 1 ){ |
215 |
|
std::cout << "Config::chatConfig()\n"; |
216 |
< |
} |
216 |
> |
} // if |
217 |
|
|
218 |
|
string response; |
219 |
< |
|
183 |
< |
|
219 |
> |
|
220 |
|
// hard coeded for the moment |
221 |
|
if ( debug == 1 ){ |
222 |
|
std::cout << "Config::chatConfig::STARTCONFIG\n"; |
223 |
< |
} |
223 |
> |
} // if |
224 |
> |
|
225 |
|
response = net->heartBeatSend("STARTCONFIG"); |
226 |
|
if ( response != "OK" ){ |
227 |
|
// something went wrong |
191 |
– |
return -1; |
228 |
|
if ( debug == 1 ){ |
229 |
|
std::cout << "Config::chatConfig::ERROR\n"; |
230 |
< |
} |
231 |
< |
} |
230 |
> |
} // if |
231 |
> |
return -1; |
232 |
> |
} // if |
233 |
|
|
234 |
|
if ( debug == 1 ){ |
235 |
|
std::cout << "Config::chatConfig::LASTMODIFIED\n"; |
236 |
< |
} |
236 |
> |
} // if |
237 |
|
lastModified = net->heartBeatSend("LASTMODIFIED"); |
238 |
|
if ( debug == 1 ){ |
239 |
|
std::cout << "Config::chatConfig::FILELIST\n"; |
240 |
< |
} |
240 |
> |
} // if |
241 |
|
fileList = net->heartBeatSend("FILELIST"); |
242 |
|
|
243 |
|
fQDN = net->heartBeatSend("FQDN"); |
246 |
|
|
247 |
|
if ( debug == 1 ){ |
248 |
|
std::cout << "Config::chatConfig::requesting properties\n"; |
249 |
< |
} |
249 |
> |
} // if |
250 |
|
if ( numProperties > 0 ){ |
251 |
|
// we actually need to look for some properties |
252 |
|
for ( int i=0; i < numProperties; i++ ){ |
253 |
|
// lets hope that this works! ;) |
254 |
|
if ( debug == 1 ){ |
255 |
|
std::cout << "Config::chatConfig::" << propertyNames[i] << "\n"; |
256 |
< |
} |
256 |
> |
} // if |
257 |
|
propertyValues[i] = net->heartBeatSend(propertyNames[i]); |
258 |
|
} // for |
259 |
|
} // if |
260 |
|
|
261 |
|
if ( debug == 1 ){ |
262 |
|
std::cout << "Config::chatConfig::ENDCONFIG\n"; |
263 |
< |
} |
263 |
> |
} // if |
264 |
|
response = net->heartBeatSend("ENDCONFIG"); |
265 |
|
// doesn't matter what it returns |
266 |
|
|
267 |
|
if ( debug == 1 ){ |
268 |
|
std::cout << "Config::chatConfig::FILTER\n"; |
269 |
< |
} |
269 |
> |
} // if |
270 |
> |
|
271 |
|
response = net->heartBeatSend("FILTER"); |
272 |
|
// now we need to pull repsonse apart to get the filter information out |
273 |
|
int first = response.find(";",0); |
278 |
|
|
279 |
|
if ( debug == 1 ){ |
280 |
|
std::cout << "Config::chatConfig::END\n"; |
281 |
< |
} |
281 |
> |
} // if |
282 |
|
response = net->heartBeatSend("END"); |
283 |
|
|
284 |
|
return 0; |
288 |
|
int Config::destroyConfig(){ |
289 |
|
if ( debug == 1 ){ |
290 |
|
std::cout << "Config::destroyConfig()\n"; |
291 |
< |
} |
291 |
> |
} // if |
292 |
|
|
293 |
|
net->closeConfigConnection(); |
294 |
|
|
299 |
|
|
300 |
|
if ( debug == 1 ){ |
301 |
|
std::cout << "Config::getProperty()\n"; |
302 |
< |
} |
302 |
> |
} // if |
303 |
|
|
304 |
|
for ( int i=0; i<20; i++ ){ |
305 |
|
if ( propertyName == propertyNames[i] ){ |
318 |
|
|
319 |
|
if ( debug == 1 ){ |
320 |
|
std::cout << "Config::getProperty()\n"; |
321 |
< |
} |
321 |
> |
} // if |
322 |
|
|
323 |
|
string response = getStrProperty( propertyName ); |
324 |
|
if ( response == "ERROR" ){ |
328 |
|
int i = atoi(response.c_str()); |
329 |
|
|
330 |
|
if ( i > 0 ){ |
331 |
+ |
// valid |
332 |
|
return i; |
333 |
< |
} |
333 |
> |
} // if |
334 |
|
|
335 |
|
// otherwise return ERROR |
336 |
|
return -1; |