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