[35] | 1 | #include <iostream> |
---|
[63] | 2 | #include <sys/types.h> |
---|
| 3 | #include <sys/wait.h> |
---|
[35] | 4 | #include "tinyxml.h" |
---|
| 5 | #include "tinystr.h" |
---|
| 6 | #include "socket/ServerSocket.h" |
---|
| 7 | #include "socket/SocketException.h" |
---|
| 8 | |
---|
[65] | 9 | |
---|
[35] | 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 | |
---|
[65] | 46 | void print_current_config(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { |
---|
[35] | 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 | |
---|
[63] | 211 | void error(char *msg) |
---|
| 212 | { |
---|
| 213 | perror(msg); |
---|
| 214 | exit(1); |
---|
| 215 | } |
---|
[35] | 216 | |
---|
[66] | 217 | int ReceiveMessage(int socketfd, string message) { |
---|
[65] | 218 | |
---|
[63] | 219 | char buffer[256]; |
---|
| 220 | int n; |
---|
[35] | 221 | |
---|
[65] | 222 | // Clear incoming data buffer |
---|
[66] | 223 | bzero(buffer,256); |
---|
[65] | 224 | |
---|
| 225 | // Wait for incoming message |
---|
| 226 | n = recv(socketfd,buffer,255,0); |
---|
| 227 | |
---|
| 228 | // Print message |
---|
[66] | 229 | if (n > 0) |
---|
| 230 | printf("Here is the message: %s\n",buffer); |
---|
| 231 | //printf("n = %d\n",n); |
---|
| 232 | |
---|
| 233 | return n; |
---|
[65] | 234 | } |
---|
| 235 | |
---|
| 236 | int SendMessage(int socketfd, string message) { |
---|
| 237 | int n; |
---|
| 238 | |
---|
[70] | 239 | message.append("\0000"); |
---|
[65] | 240 | // Write message back to client |
---|
[70] | 241 | n = write(socketfd,message.c_str(),(message.size()+1)); |
---|
| 242 | if (n<0) |
---|
| 243 | error("Error sending to client\n"); |
---|
| 244 | printf("sent: %s %i\n", message.c_str(),n); |
---|
[66] | 245 | |
---|
[70] | 246 | return n; |
---|
[65] | 247 | } |
---|
| 248 | |
---|
| 249 | void StartMessaging(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { |
---|
| 250 | int n,i,j; |
---|
| 251 | char counter[55]; |
---|
| 252 | char var[50]; |
---|
| 253 | |
---|
[66] | 254 | |
---|
[69] | 255 | // while(1) { |
---|
[65] | 256 | // Send parameters from struct |
---|
| 257 | sprintf(counter,"%d",ce_info->numUtilities); |
---|
| 258 | SendMessage(socketfd,counter); |
---|
| 259 | |
---|
[66] | 260 | // utilities |
---|
| 261 | for(i = 0; i < ce_info->numUtilities; i++) { |
---|
[69] | 262 | //for(i = 0; i < 1; i++) { |
---|
[65] | 263 | SendMessage(socketfd,uList[i]->name); |
---|
| 264 | SendMessage(socketfd,uList[i]->units); |
---|
| 265 | SendMessage(socketfd,uList[i]->goal); |
---|
| 266 | sprintf(var,"%f",uList[i]->target); |
---|
| 267 | SendMessage(socketfd,var); |
---|
| 268 | } |
---|
[66] | 269 | |
---|
[69] | 270 | /*// parameters |
---|
[66] | 271 | sprintf(counter,"%i",ce_info->numParameters); |
---|
[65] | 272 | SendMessage(socketfd,counter); |
---|
| 273 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
| 274 | SendMessage(socketfd,pList[i]->name); |
---|
| 275 | SendMessage(socketfd,pList[i]->units); |
---|
| 276 | sprintf(var,"%f",pList[i]->min); |
---|
| 277 | SendMessage(socketfd,var); |
---|
| 278 | sprintf(var,"%f",pList[i]->max); |
---|
| 279 | SendMessage(socketfd,var); |
---|
| 280 | sprintf(var,"%f",pList[i]->step); |
---|
| 281 | SendMessage(socketfd,var); |
---|
| 282 | |
---|
| 283 | sprintf(counter,"%i",pList[i]->numAffects); |
---|
| 284 | SendMessage(socketfd,counter); |
---|
| 285 | for(j = 0; j < pList[i]->numAffects; j++) { |
---|
| 286 | SendMessage(socketfd,pList[i]->affection_list[j].u->name); |
---|
| 287 | SendMessage(socketfd,pList[i]->affection_list[j].relation); |
---|
| 288 | } |
---|
| 289 | } |
---|
[66] | 290 | |
---|
| 291 | // observables |
---|
[65] | 292 | sprintf(counter,"%i",ce_info->numObservables); |
---|
| 293 | SendMessage(socketfd,counter); |
---|
| 294 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
| 295 | SendMessage(socketfd,oList[i]->name); |
---|
| 296 | |
---|
| 297 | sprintf(counter,"%i",oList[i]->numAffects); |
---|
| 298 | SendMessage(socketfd,counter); |
---|
| 299 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
| 300 | SendMessage(socketfd,oList[i]->affection_list[j].u->name); |
---|
| 301 | SendMessage(socketfd,oList[i]->affection_list[j].relation); |
---|
| 302 | } |
---|
[69] | 303 | }*/ |
---|
[66] | 304 | |
---|
[65] | 305 | |
---|
| 306 | // Receive a message |
---|
[66] | 307 | string message; |
---|
| 308 | n = ReceiveMessage(socketfd, message); |
---|
| 309 | //cout << message << endl; |
---|
| 310 | |
---|
| 311 | |
---|
| 312 | //printf("n = %d\n", n); |
---|
| 313 | /*if (n < 0) { |
---|
| 314 | printf("ERROR reading from socket\n"); |
---|
[65] | 315 | close(socketfd); |
---|
| 316 | return; |
---|
| 317 | } |
---|
| 318 | if (n == 0) { |
---|
[64] | 319 | printf("Client has closed the connection.\n"); |
---|
| 320 | return; |
---|
[66] | 321 | }*/ |
---|
| 322 | |
---|
[69] | 323 | // } |
---|
[63] | 324 | } |
---|
[35] | 325 | |
---|
[65] | 326 | int StartShell(int port,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { |
---|
[63] | 327 | // Start socket server |
---|
| 328 | int sockfd, newsockfd, clilen; |
---|
| 329 | struct sockaddr_in serv_addr, cli_addr; |
---|
[35] | 330 | |
---|
[63] | 331 | // Setup socket connection |
---|
| 332 | |
---|
| 333 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
| 334 | if (sockfd < 0) |
---|
| 335 | error("ERROR opening socket"); |
---|
| 336 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
---|
| 337 | serv_addr.sin_family = AF_INET; |
---|
| 338 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
---|
| 339 | serv_addr.sin_port = htons(port); |
---|
| 340 | if (bind(sockfd, (struct sockaddr *) &serv_addr, |
---|
| 341 | sizeof(serv_addr)) < 0) |
---|
| 342 | error("ERROR on binding"); |
---|
| 343 | listen(sockfd,5); |
---|
| 344 | clilen = sizeof(cli_addr); |
---|
[35] | 345 | |
---|
[66] | 346 | //while(1) { |
---|
[64] | 347 | newsockfd = accept(sockfd, |
---|
| 348 | (struct sockaddr *) &cli_addr, |
---|
| 349 | (socklen_t*)&clilen); |
---|
| 350 | if (newsockfd < 0) |
---|
| 351 | error("ERROR on accept"); |
---|
[63] | 352 | |
---|
[64] | 353 | // Begin parsing the messages |
---|
[65] | 354 | StartMessaging(newsockfd,uList, pList, oList, ce_info); |
---|
[66] | 355 | //} |
---|
[63] | 356 | return 0; |
---|
[35] | 357 | } |
---|
| 358 | |
---|
| 359 | int main(int argc, char* argv[]) { |
---|
| 360 | |
---|
| 361 | |
---|
| 362 | // CognitiveEngine CE; |
---|
| 363 | // CognitiveEngineShell Shell; |
---|
| 364 | string pFilename; |
---|
[64] | 365 | int pid; |
---|
[35] | 366 | |
---|
| 367 | Utility * uList[10]; |
---|
| 368 | Parameter * pList[10]; |
---|
| 369 | Observable * oList[10]; |
---|
| 370 | CE_Info ce_info; |
---|
| 371 | |
---|
| 372 | if(argc < 2) { |
---|
| 373 | cout << "Warning no XML file specific using default: example.xml" << endl; |
---|
| 374 | pFilename = "example.xml"; |
---|
| 375 | } else { |
---|
| 376 | pFilename = argv[1]; |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | TiXmlDocument doc( pFilename.c_str() ); |
---|
| 380 | bool loadOkay = doc.LoadFile(); |
---|
| 381 | if (!loadOkay) |
---|
| 382 | { |
---|
| 383 | cout << "Loading " << pFilename << " failed." << endl; |
---|
| 384 | return 0; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | cout << "Attemping to parse " << pFilename << "." << endl; |
---|
| 388 | parse_ce_config( &doc , uList, pList, oList, &ce_info); |
---|
| 389 | cout << "Configuration file parsing completed." << endl; |
---|
| 390 | |
---|
[69] | 391 | print_current_config(uList, pList, oList, &ce_info); |
---|
| 392 | |
---|
| 393 | pid = fork(); |
---|
[64] | 394 | if(pid == 0) { |
---|
| 395 | // In child process - open policy engine port. |
---|
[65] | 396 | // StartShell(30003,uList, pList, oList, &ce_info); |
---|
[64] | 397 | } else { |
---|
| 398 | // In parent process - open cognitive engine port. |
---|
[70] | 399 | StartShell(30002,uList, pList, oList, &ce_info); |
---|
[64] | 400 | } |
---|
| 401 | |
---|
[35] | 402 | |
---|
| 403 | return 1; |
---|
| 404 | } |
---|