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 |
|
|
// now send the properties we want |
207 |
|
|
|
208 |
ab11 |
1.3 |
if ( debug == 1 ){ |
209 |
|
|
std::cout << "Config::chatConfig::requesting properties\n"; |
210 |
|
|
} |
211 |
ab11 |
1.1 |
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 |
ab11 |
1.3 |
if ( debug == 1 ){ |
216 |
|
|
std::cout << "Config::chatConfig::" << propertyNames[i] << "\n"; |
217 |
|
|
} |
218 |
|
|
propertyValues[i] = net->heartBeatSend(propertyNames[i]); |
219 |
ab11 |
1.1 |
} // for |
220 |
|
|
} // if |
221 |
|
|
|
222 |
ab11 |
1.3 |
if ( debug == 1 ){ |
223 |
|
|
std::cout << "Config::chatConfig::ENDCONFIG\n"; |
224 |
|
|
} |
225 |
|
|
response = net->heartBeatSend("ENDCONFIG"); |
226 |
ab11 |
1.1 |
// doesn't matter what it returns |
227 |
|
|
|
228 |
ab11 |
1.3 |
if ( debug == 1 ){ |
229 |
|
|
std::cout << "Config::chatConfig::FILTER\n"; |
230 |
|
|
} |
231 |
|
|
response = net->heartBeatSend("FILTER"); |
232 |
ab11 |
1.1 |
// now we need to pull repsonse apart to get the filter information out |
233 |
ab11 |
1.3 |
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 |
ab11 |
1.1 |
|
239 |
ab11 |
1.3 |
if ( debug == 1 ){ |
240 |
|
|
std::cout << "Config::chatConfig::END\n"; |
241 |
|
|
} |
242 |
|
|
response = net->heartBeatSend("END"); |
243 |
ab11 |
1.1 |
|
244 |
|
|
return 0; |
245 |
|
|
|
246 |
|
|
} // chatConfig |
247 |
|
|
|
248 |
|
|
int Config::destroyConfig(){ |
249 |
ab11 |
1.3 |
if ( debug == 1 ){ |
250 |
|
|
std::cout << "Config::destroyConfig()\n"; |
251 |
|
|
} |
252 |
ab11 |
1.1 |
|
253 |
ab11 |
1.3 |
net->closeConfigConnection(); |
254 |
ab11 |
1.1 |
|
255 |
|
|
} // destroyConfig |
256 |
|
|
|
257 |
ab11 |
1.3 |
string Config::getStrProperty(string propertyName){ |
258 |
|
|
|
259 |
|
|
|
260 |
|
|
if ( debug == 1 ){ |
261 |
|
|
std::cout << "Config::getProperty()\n"; |
262 |
|
|
} |
263 |
ab11 |
1.1 |
|
264 |
|
|
for ( int i=0; i<20; i++ ){ |
265 |
|
|
if ( propertyName == propertyNames[i] ){ |
266 |
ab11 |
1.3 |
if ( propertyValues[i] != "" ){ |
267 |
|
|
return propertyValues[i]; |
268 |
|
|
} // if |
269 |
ab11 |
1.1 |
} // if |
270 |
|
|
} // for |
271 |
|
|
|
272 |
|
|
// otherwise return ERROR |
273 |
|
|
return "ERROR"; |
274 |
|
|
|
275 |
ab11 |
1.3 |
} // 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 |