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