1 |
#include "Config.h" |
2 |
|
3 |
Config::Config( string serverName, int serverPort ){ |
4 |
// the constructor for the class |
5 |
|
6 |
configConnectionRetryTime = 1; // one second |
7 |
|
8 |
// make a reference to smallnet |
9 |
SmallNet net(); |
10 |
|
11 |
// read the values which we want to retrieve from the |
12 |
// config manager from the "config.values.txt" file |
13 |
//propertyNames = new string[20]; |
14 |
//propertyValues = new string[20]; |
15 |
|
16 |
// FIX ME |
17 |
// sod that we are using defined values now.. |
18 |
propertyNames[0] = "UDPUpdateTime"; |
19 |
propertyNames[1] = "TCPUpdateTime"; |
20 |
propertyNames[2] = "FilterRetryTime"; |
21 |
propertyNames[3] = "ConfigRetryTime"; |
22 |
|
23 |
// read the Chat dialog we 'expect' to have with the config |
24 |
// manager from disk. This is a new idea so that the client |
25 |
// won't have to be hard coded with the dialog (incase it changes) |
26 |
|
27 |
// FIX ME |
28 |
|
29 |
if ( loadConfig() == 0 ){ |
30 |
// successful just continue |
31 |
} |
32 |
else { |
33 |
// woops something went wrong. we'll just keep retrying |
34 |
// and increasing the time between trys |
35 |
int loadConfigResult = -1; |
36 |
|
37 |
// the following could cause problems if it is fed the |
38 |
// incorrect filter name and/or port it'll never stop trying |
39 |
while ( loadConfigResult != 0 ){ |
40 |
// make the connection retry time greater |
41 |
configConnectionRetryTime = configConnectionRetryTime * 2; |
42 |
sleep(configConnectionRetryTime); // seconds |
43 |
loadConfigResult = loadConfig(); |
44 |
} // while |
45 |
|
46 |
|
47 |
} // if |
48 |
|
49 |
} // Config |
50 |
|
51 |
int Config::loadConfig(){ |
52 |
// establishes a connection to the config manager (filter manager?) and |
53 |
// tries to load all the info which it needs, if it fails returns !0 |
54 |
|
55 |
// make a connection |
56 |
int established = establishConfig(); |
57 |
if ( established != 0 ){ |
58 |
return established; |
59 |
} |
60 |
|
61 |
// do the talk ;) |
62 |
int chat = chatConfig(); |
63 |
if ( chat != 0 ){ |
64 |
// and close the connection |
65 |
destroyConfig(); |
66 |
return chat; |
67 |
} |
68 |
|
69 |
// and close the connection |
70 |
destroyConfig(); |
71 |
|
72 |
// return everything ok |
73 |
return 0; |
74 |
|
75 |
} // loadConfig |
76 |
|
77 |
int Config::establishHeartbeat(){ |
78 |
// just an interface onto smallnet |
79 |
|
80 |
int establish = 0; //net.connectHeartbeat(configName, configPort); |
81 |
|
82 |
return establish; |
83 |
|
84 |
} // establishHeartBeat |
85 |
|
86 |
|
87 |
int Config::destroyHeartbeat(){ |
88 |
// just hooks into the smallnet interface |
89 |
|
90 |
net.closeHeartBeatConnection(); |
91 |
|
92 |
} // destroyHeartBeat |
93 |
|
94 |
|
95 |
int Config::establishConfig(){ |
96 |
// ask smallnet to make a connection to the filtermanager so we can get some config |
97 |
// details |
98 |
|
99 |
net.connectConfig(); |
100 |
|
101 |
|
102 |
return 0; |
103 |
|
104 |
} // establishConfig |
105 |
|
106 |
|
107 |
int Config::chatConfig(){ |
108 |
// do some funky jibble.. ;) |
109 |
|
110 |
string response; |
111 |
|
112 |
// hard coeded for the moment |
113 |
response = net.heartBeatSend("STARTCONFIG"); |
114 |
if ( response != "OK" ){ |
115 |
// something went wrong |
116 |
return -1; |
117 |
} |
118 |
|
119 |
lastModified = net.heartBeatSend("LASTMODIFIED"); |
120 |
fileList = net.heartBeatSend("FILELIST"); |
121 |
|
122 |
// now send the properties we want |
123 |
|
124 |
if ( numProperties > 0 ){ |
125 |
// we actually need to look for some properties |
126 |
for ( int i=0; i < numProperties; i++ ){ |
127 |
// lets hope that this works! ;) |
128 |
propertyValues[i] = net.heartBeatSend(propertyNames[i]); |
129 |
} // for |
130 |
} // if |
131 |
|
132 |
response = net.heartBeatSend("ENDCONFIG"); |
133 |
// doesn't matter what it returns |
134 |
|
135 |
response = net.heartBeatSend("FILTER"); |
136 |
// now we need to pull repsonse apart to get the filter information out |
137 |
|
138 |
// FIX ME |
139 |
|
140 |
response = net.heartBeatSend("END"); |
141 |
|
142 |
return 0; |
143 |
|
144 |
} // chatConfig |
145 |
|
146 |
int Config::destroyConfig(){ |
147 |
|
148 |
net.closeConfigConnection(); |
149 |
|
150 |
} // destroyConfig |
151 |
|
152 |
string getProperty(string propertyName){ |
153 |
|
154 |
/* // doesn't compile with this for some reason! |
155 |
for ( int i=0; i<20; i++ ){ |
156 |
if ( propertyName == propertyNames[i] ){ |
157 |
return propertyValues[i]; |
158 |
} // if |
159 |
} // for |
160 |
*/ |
161 |
|
162 |
// otherwise return ERROR |
163 |
return "ERROR"; |
164 |
|
165 |
} // getProperty |