1 | #include <iostream> |
---|
2 | #include <sys/types.h> |
---|
3 | #include <sys/wait.h> |
---|
4 | #include "tinyxml.h" |
---|
5 | #include "tinystr.h" |
---|
6 | #include "socket/ServerSocket.h" |
---|
7 | #include "socket/SocketException.h" |
---|
8 | |
---|
9 | |
---|
10 | using namespace std; |
---|
11 | |
---|
12 | #define SERVER_PORT 30000 |
---|
13 | |
---|
14 | struct CE_Info { |
---|
15 | int numUtilities; |
---|
16 | int numParameters; |
---|
17 | int numObservables; |
---|
18 | }; |
---|
19 | |
---|
20 | struct Utility { |
---|
21 | string name; |
---|
22 | string units; |
---|
23 | string goal; |
---|
24 | float target; |
---|
25 | }; |
---|
26 | struct Affect { |
---|
27 | Utility * u; |
---|
28 | string relation; |
---|
29 | }; |
---|
30 | struct Parameter { |
---|
31 | string name; |
---|
32 | string units; |
---|
33 | float min; |
---|
34 | int numAffects; |
---|
35 | Affect affection_list[10]; |
---|
36 | float max; |
---|
37 | float step; |
---|
38 | }; |
---|
39 | |
---|
40 | struct Observable { |
---|
41 | string name; |
---|
42 | Affect affection_list[10]; |
---|
43 | int numAffects; |
---|
44 | }; |
---|
45 | |
---|
46 | void print_current_config(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { |
---|
47 | int i = 0; |
---|
48 | int j = 0; |
---|
49 | |
---|
50 | for(i = 0; i < ce_info->numUtilities ; i++) { |
---|
51 | cout << "Utility: " << uList[i]->name << endl; |
---|
52 | cout << " Units: " << uList[i]->units << endl; |
---|
53 | cout << " Goal: " << uList[i]->goal << endl; |
---|
54 | cout << " Target: " << uList[i]->target << endl; |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
59 | cout << "Parameter: " << pList[i]->name << endl; |
---|
60 | cout << " Units: " << pList[i]->units << endl; |
---|
61 | cout << " Min: " << pList[i]->min << endl; |
---|
62 | cout << " Max: " << pList[i]->max << endl; |
---|
63 | cout << " Step: " << pList[i]->step << endl; |
---|
64 | for(j = 0; j < pList[i]->numAffects; j++) { |
---|
65 | cout << " Affect: " << pList[i]->affection_list[j].u->name << " -> " << pList[i]->affection_list[j].relation << endl; |
---|
66 | } |
---|
67 | } |
---|
68 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
69 | cout << "Observable: " << oList[i]->name << endl; |
---|
70 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
71 | cout << " Affect: " << oList[i]->affection_list[j].u->name << " -> " << oList[i]->affection_list[j].relation << endl; |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | int parse_ce_config( TiXmlDocument * doc , Utility * u[], Parameter * p[], Observable * o[], CE_Info * ce_info) { |
---|
78 | |
---|
79 | TiXmlElement* pElem; //!current element |
---|
80 | TiXmlElement* pChild; //!current child of pElem |
---|
81 | TiXmlElement* pChild1; //!current child of pElem |
---|
82 | TiXmlElement* pSecondChild; //!current child of pElem |
---|
83 | TiXmlHandle hDoc(doc); //!handle to xml document |
---|
84 | TiXmlHandle hRoot(0); //! handle to root element |
---|
85 | |
---|
86 | int count = 0; |
---|
87 | int i = 0; |
---|
88 | int j = 0; |
---|
89 | int k = 0; |
---|
90 | |
---|
91 | pElem = hDoc.FirstChildElement().Element(); |
---|
92 | if(!pElem) { cout << "no valid root! quit-ing function!" << endl; return 0; } |
---|
93 | hRoot = TiXmlHandle(pElem); |
---|
94 | |
---|
95 | // Pull utility information from XML file. |
---|
96 | |
---|
97 | pElem = hRoot.FirstChild("utilities").Element(); |
---|
98 | pChild1 = hRoot.Child("utilities",count).Element(); |
---|
99 | |
---|
100 | |
---|
101 | for(pChild = pChild1->FirstChildElement("utility"); pChild; pChild = pChild->NextSiblingElement()) |
---|
102 | { |
---|
103 | u[i] = new Utility; |
---|
104 | const char *uName = pChild->Attribute("name"); |
---|
105 | if(uName) u[i]->name = uName; |
---|
106 | const char *uUnits = pChild->Attribute("units"); |
---|
107 | if(uUnits) u[i]->units = uUnits; |
---|
108 | const char *uGoal = pChild->Attribute("goal"); |
---|
109 | if(uGoal) u[i]->goal = uGoal; |
---|
110 | if(pChild->QueryFloatAttribute("target",&u[i]->target) != TIXML_SUCCESS) u[i]->target = -1; |
---|
111 | i++; |
---|
112 | } |
---|
113 | ce_info->numUtilities = i; |
---|
114 | cout << "Parsed " << ce_info->numUtilities << " utilities." << endl; |
---|
115 | |
---|
116 | // Pull observable information from XML file. |
---|
117 | i = 0; |
---|
118 | pElem = hRoot.FirstChild("observables").Element(); |
---|
119 | pChild1 = hRoot.Child("observables",count).Element(); |
---|
120 | |
---|
121 | for(pChild = pChild1->FirstChildElement("observable"); pChild; pChild = pChild->NextSiblingElement()) |
---|
122 | { |
---|
123 | |
---|
124 | const char *oName = pChild->Attribute("name"); |
---|
125 | o[i] = new Observable; |
---|
126 | |
---|
127 | if(oName) o[i]->name = oName; |
---|
128 | |
---|
129 | j = 0; |
---|
130 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) |
---|
131 | { |
---|
132 | const char *oUtilName = pSecondChild->Attribute("utility"); |
---|
133 | |
---|
134 | // If a utility affects this parameter find the utility object and assign it |
---|
135 | if(oUtilName) { |
---|
136 | // Search for correct utility |
---|
137 | for(k=0;k<10;k++){ |
---|
138 | if(u[k]->name == oUtilName) { |
---|
139 | o[i]->affection_list[j].u = u[k]; |
---|
140 | break; |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | // Set relationship |
---|
146 | const char *oRelate = pSecondChild->Attribute("relationship"); |
---|
147 | if(oRelate) o[i]->affection_list[j].relation = oRelate; |
---|
148 | j++; |
---|
149 | } |
---|
150 | o[i]->numAffects = j; |
---|
151 | i++; |
---|
152 | } |
---|
153 | ce_info->numObservables = i; |
---|
154 | cout << "Parsed " << ce_info->numObservables << " observables." << endl; |
---|
155 | |
---|
156 | |
---|
157 | // Pull parameter information from XML file. |
---|
158 | pElem = hRoot.FirstChild("parameters").Element(); |
---|
159 | pChild1 = hRoot.Child("parameters",count).Element(); |
---|
160 | |
---|
161 | i = 0; |
---|
162 | for(pChild = pChild1->FirstChildElement("parameter"); pChild; pChild = pChild->NextSiblingElement()) |
---|
163 | { |
---|
164 | p[i] = new Parameter; |
---|
165 | |
---|
166 | const char *pName = pChild->Attribute("name"); |
---|
167 | if(pName) p[i]->name = pName; |
---|
168 | const char *pUnits = pChild->Attribute("units"); |
---|
169 | if(pUnits) p[i]->units = pUnits; |
---|
170 | |
---|
171 | if(pChild->QueryFloatAttribute("min",&p[i]->min) != TIXML_SUCCESS) p[i]->min = -1; |
---|
172 | if(pChild->QueryFloatAttribute("max",&p[i]->max) != TIXML_SUCCESS) p[i]->max = -1; |
---|
173 | if(pChild->QueryFloatAttribute("step",&p[i]->step) != TIXML_SUCCESS) p[i]->step = -1; |
---|
174 | |
---|
175 | j = 0; |
---|
176 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) |
---|
177 | { |
---|
178 | const char *pUtilName = pSecondChild->Attribute("utility"); |
---|
179 | |
---|
180 | // If a utility affects this parameter find the utility object and assign it |
---|
181 | if(pUtilName) { |
---|
182 | // Search for correct utility |
---|
183 | for( k=0 ; u[k]!=NULL ; k++ ){ |
---|
184 | if(u[k]->name == pUtilName) { |
---|
185 | p[i]->affection_list[j].u = u[k]; |
---|
186 | break; |
---|
187 | } |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | // Set relationship |
---|
192 | const char *pRelate = pSecondChild->Attribute("relationship"); |
---|
193 | if(pRelate) { |
---|
194 | p[i]->affection_list[j].relation = pRelate; |
---|
195 | } else { |
---|
196 | cout << "Error: No relation found." << endl; |
---|
197 | } |
---|
198 | j++; |
---|
199 | |
---|
200 | } |
---|
201 | p[i]->numAffects = j; |
---|
202 | i++; |
---|
203 | |
---|
204 | } |
---|
205 | ce_info->numParameters = i; |
---|
206 | cout << "Parsed " << ce_info->numParameters << " parameters." << endl; |
---|
207 | return 1; |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | void error(char *msg) |
---|
212 | { |
---|
213 | perror(msg); |
---|
214 | exit(1); |
---|
215 | } |
---|
216 | |
---|
217 | int ReceiveMessage(int socketfd, string message) { |
---|
218 | |
---|
219 | char buffer[256]; |
---|
220 | int n; |
---|
221 | |
---|
222 | // Clear incoming data buffer |
---|
223 | bzero(buffer,256); |
---|
224 | |
---|
225 | // Wait for incoming message |
---|
226 | n = recv(socketfd,buffer,255,0); |
---|
227 | |
---|
228 | // Print message |
---|
229 | if (n > 0) |
---|
230 | printf("Here is the message: %s\n",buffer); |
---|
231 | //printf("n = %d\n",n); |
---|
232 | |
---|
233 | return n; |
---|
234 | } |
---|
235 | |
---|
236 | int SendMessage(int socketfd, string message) { |
---|
237 | int n; |
---|
238 | |
---|
239 | // Write message back to client |
---|
240 | n = send(socketfd,message.c_str(),(message.size()+1),MSG_NOSIGNAL); |
---|
241 | |
---|
242 | return n; |
---|
243 | } |
---|
244 | |
---|
245 | void StartMessaging(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { |
---|
246 | int n,i,j; |
---|
247 | char counter[55]; |
---|
248 | char var[50]; |
---|
249 | |
---|
250 | |
---|
251 | while(1) { |
---|
252 | // Send parameters from struct |
---|
253 | sprintf(counter,"%d",ce_info->numUtilities); |
---|
254 | SendMessage(socketfd,counter); |
---|
255 | |
---|
256 | // utilities |
---|
257 | for(i = 0; i < ce_info->numUtilities; i++) { |
---|
258 | SendMessage(socketfd,uList[i]->name); |
---|
259 | SendMessage(socketfd,uList[i]->units); |
---|
260 | SendMessage(socketfd,uList[i]->goal); |
---|
261 | sprintf(var,"%f",uList[i]->target); |
---|
262 | SendMessage(socketfd,var); |
---|
263 | } |
---|
264 | |
---|
265 | // parameters |
---|
266 | sprintf(counter,"%i",ce_info->numParameters); |
---|
267 | SendMessage(socketfd,counter); |
---|
268 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
269 | SendMessage(socketfd,pList[i]->name); |
---|
270 | SendMessage(socketfd,pList[i]->units); |
---|
271 | sprintf(var,"%f",pList[i]->min); |
---|
272 | SendMessage(socketfd,var); |
---|
273 | sprintf(var,"%f",pList[i]->max); |
---|
274 | SendMessage(socketfd,var); |
---|
275 | sprintf(var,"%f",pList[i]->step); |
---|
276 | SendMessage(socketfd,var); |
---|
277 | |
---|
278 | sprintf(counter,"%i",pList[i]->numAffects); |
---|
279 | SendMessage(socketfd,counter); |
---|
280 | for(j = 0; j < pList[i]->numAffects; j++) { |
---|
281 | SendMessage(socketfd,pList[i]->affection_list[j].u->name); |
---|
282 | SendMessage(socketfd,pList[i]->affection_list[j].relation); |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | // observables |
---|
287 | sprintf(counter,"%i",ce_info->numObservables); |
---|
288 | SendMessage(socketfd,counter); |
---|
289 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
290 | SendMessage(socketfd,oList[i]->name); |
---|
291 | |
---|
292 | sprintf(counter,"%i",oList[i]->numAffects); |
---|
293 | SendMessage(socketfd,counter); |
---|
294 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
295 | SendMessage(socketfd,oList[i]->affection_list[j].u->name); |
---|
296 | SendMessage(socketfd,oList[i]->affection_list[j].relation); |
---|
297 | } |
---|
298 | } |
---|
299 | |
---|
300 | |
---|
301 | // Receive a message |
---|
302 | string message; |
---|
303 | n = ReceiveMessage(socketfd, message); |
---|
304 | //cout << message << endl; |
---|
305 | |
---|
306 | |
---|
307 | //printf("n = %d\n", n); |
---|
308 | /*if (n < 0) { |
---|
309 | printf("ERROR reading from socket\n"); |
---|
310 | close(socketfd); |
---|
311 | return; |
---|
312 | } |
---|
313 | if (n == 0) { |
---|
314 | printf("Client has closed the connection.\n"); |
---|
315 | return; |
---|
316 | }*/ |
---|
317 | |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | int StartShell(int port,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { |
---|
322 | // Start socket server |
---|
323 | int sockfd, newsockfd, clilen; |
---|
324 | struct sockaddr_in serv_addr, cli_addr; |
---|
325 | |
---|
326 | // Setup socket connection |
---|
327 | |
---|
328 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
329 | if (sockfd < 0) |
---|
330 | error("ERROR opening socket"); |
---|
331 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
---|
332 | serv_addr.sin_family = AF_INET; |
---|
333 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
---|
334 | serv_addr.sin_port = htons(port); |
---|
335 | if (bind(sockfd, (struct sockaddr *) &serv_addr, |
---|
336 | sizeof(serv_addr)) < 0) |
---|
337 | error("ERROR on binding"); |
---|
338 | listen(sockfd,5); |
---|
339 | clilen = sizeof(cli_addr); |
---|
340 | |
---|
341 | //while(1) { |
---|
342 | newsockfd = accept(sockfd, |
---|
343 | (struct sockaddr *) &cli_addr, |
---|
344 | (socklen_t*)&clilen); |
---|
345 | if (newsockfd < 0) |
---|
346 | error("ERROR on accept"); |
---|
347 | |
---|
348 | // Begin parsing the messages |
---|
349 | StartMessaging(newsockfd,uList, pList, oList, ce_info); |
---|
350 | //} |
---|
351 | return 0; |
---|
352 | } |
---|
353 | |
---|
354 | int main(int argc, char* argv[]) { |
---|
355 | |
---|
356 | |
---|
357 | // CognitiveEngine CE; |
---|
358 | // CognitiveEngineShell Shell; |
---|
359 | string pFilename; |
---|
360 | int pid; |
---|
361 | |
---|
362 | Utility * uList[10]; |
---|
363 | Parameter * pList[10]; |
---|
364 | Observable * oList[10]; |
---|
365 | CE_Info ce_info; |
---|
366 | |
---|
367 | if(argc < 2) { |
---|
368 | cout << "Warning no XML file specific using default: example.xml" << endl; |
---|
369 | pFilename = "example.xml"; |
---|
370 | } else { |
---|
371 | pFilename = argv[1]; |
---|
372 | } |
---|
373 | |
---|
374 | TiXmlDocument doc( pFilename.c_str() ); |
---|
375 | bool loadOkay = doc.LoadFile(); |
---|
376 | if (!loadOkay) |
---|
377 | { |
---|
378 | cout << "Loading " << pFilename << " failed." << endl; |
---|
379 | return 0; |
---|
380 | } |
---|
381 | |
---|
382 | cout << "Attemping to parse " << pFilename << "." << endl; |
---|
383 | parse_ce_config( &doc , uList, pList, oList, &ce_info); |
---|
384 | cout << "Configuration file parsing completed." << endl; |
---|
385 | |
---|
386 | pid = fork(); |
---|
387 | if(pid == 0) { |
---|
388 | // In child process - open policy engine port. |
---|
389 | // StartShell(30003,uList, pList, oList, &ce_info); |
---|
390 | } else { |
---|
391 | // In parent process - open cognitive engine port. |
---|
392 | StartShell(30003,uList, pList, oList, &ce_info); |
---|
393 | } |
---|
394 | |
---|
395 | |
---|
396 | return 1; |
---|
397 | } |
---|