[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; |
---|
[80] | 24 | float value; |
---|
[35] | 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; |
---|
[80] | 38 | float value; |
---|
[35] | 39 | }; |
---|
| 40 | |
---|
| 41 | struct Observable { |
---|
| 42 | string name; |
---|
| 43 | Affect affection_list[10]; |
---|
| 44 | int numAffects; |
---|
[80] | 45 | float value; |
---|
[35] | 46 | }; |
---|
| 47 | |
---|
[65] | 48 | void print_current_config(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { |
---|
[35] | 49 | int i = 0; |
---|
| 50 | int j = 0; |
---|
| 51 | |
---|
| 52 | for(i = 0; i < ce_info->numUtilities ; i++) { |
---|
| 53 | cout << "Utility: " << uList[i]->name << endl; |
---|
| 54 | cout << " Units: " << uList[i]->units << endl; |
---|
| 55 | cout << " Goal: " << uList[i]->goal << endl; |
---|
| 56 | cout << " Target: " << uList[i]->target << endl; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
| 60 | cout << "Parameter: " << pList[i]->name << endl; |
---|
| 61 | cout << " Units: " << pList[i]->units << endl; |
---|
| 62 | cout << " Min: " << pList[i]->min << endl; |
---|
| 63 | cout << " Max: " << pList[i]->max << endl; |
---|
| 64 | cout << " Step: " << pList[i]->step << endl; |
---|
| 65 | for(j = 0; j < pList[i]->numAffects; j++) { |
---|
| 66 | cout << " Affect: " << pList[i]->affection_list[j].u->name << " -> " << pList[i]->affection_list[j].relation << endl; |
---|
| 67 | } |
---|
| 68 | } |
---|
[80] | 69 | |
---|
| 70 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
[35] | 71 | cout << "Observable: " << oList[i]->name << endl; |
---|
| 72 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
| 73 | cout << " Affect: " << oList[i]->affection_list[j].u->name << " -> " << oList[i]->affection_list[j].relation << endl; |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | int parse_ce_config( TiXmlDocument * doc , Utility * u[], Parameter * p[], Observable * o[], CE_Info * ce_info) { |
---|
| 80 | |
---|
| 81 | TiXmlElement* pElem; //!current element |
---|
| 82 | TiXmlElement* pChild; //!current child of pElem |
---|
| 83 | TiXmlElement* pChild1; //!current child of pElem |
---|
| 84 | TiXmlElement* pSecondChild; //!current child of pElem |
---|
| 85 | TiXmlHandle hDoc(doc); //!handle to xml document |
---|
| 86 | TiXmlHandle hRoot(0); //! handle to root element |
---|
| 87 | |
---|
| 88 | int count = 0; |
---|
| 89 | int i = 0; |
---|
| 90 | int j = 0; |
---|
| 91 | int k = 0; |
---|
[86] | 92 | int match_found = 0; |
---|
[35] | 93 | |
---|
| 94 | pElem = hDoc.FirstChildElement().Element(); |
---|
| 95 | if(!pElem) { cout << "no valid root! quit-ing function!" << endl; return 0; } |
---|
| 96 | hRoot = TiXmlHandle(pElem); |
---|
| 97 | |
---|
| 98 | // Pull utility information from XML file. |
---|
| 99 | |
---|
| 100 | pElem = hRoot.FirstChild("utilities").Element(); |
---|
| 101 | pChild1 = hRoot.Child("utilities",count).Element(); |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | for(pChild = pChild1->FirstChildElement("utility"); pChild; pChild = pChild->NextSiblingElement()) |
---|
| 105 | { |
---|
| 106 | u[i] = new Utility; |
---|
| 107 | const char *uName = pChild->Attribute("name"); |
---|
| 108 | if(uName) u[i]->name = uName; |
---|
| 109 | const char *uUnits = pChild->Attribute("units"); |
---|
| 110 | if(uUnits) u[i]->units = uUnits; |
---|
| 111 | const char *uGoal = pChild->Attribute("goal"); |
---|
| 112 | if(uGoal) u[i]->goal = uGoal; |
---|
| 113 | if(pChild->QueryFloatAttribute("target",&u[i]->target) != TIXML_SUCCESS) u[i]->target = -1; |
---|
| 114 | i++; |
---|
| 115 | } |
---|
| 116 | ce_info->numUtilities = i; |
---|
| 117 | cout << "Parsed " << ce_info->numUtilities << " utilities." << endl; |
---|
| 118 | |
---|
| 119 | // Pull observable information from XML file. |
---|
| 120 | i = 0; |
---|
| 121 | pElem = hRoot.FirstChild("observables").Element(); |
---|
| 122 | pChild1 = hRoot.Child("observables",count).Element(); |
---|
| 123 | |
---|
| 124 | for(pChild = pChild1->FirstChildElement("observable"); pChild; pChild = pChild->NextSiblingElement()) |
---|
| 125 | { |
---|
| 126 | |
---|
| 127 | const char *oName = pChild->Attribute("name"); |
---|
| 128 | o[i] = new Observable; |
---|
| 129 | |
---|
| 130 | if(oName) o[i]->name = oName; |
---|
| 131 | |
---|
| 132 | j = 0; |
---|
| 133 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) |
---|
| 134 | { |
---|
| 135 | const char *oUtilName = pSecondChild->Attribute("utility"); |
---|
| 136 | |
---|
| 137 | // If a utility affects this parameter find the utility object and assign it |
---|
| 138 | if(oUtilName) { |
---|
| 139 | // Search for correct utility |
---|
[86] | 140 | for( k=0 ; u[k]!=NULL ; k++ ){ |
---|
[35] | 141 | if(u[k]->name == oUtilName) { |
---|
| 142 | o[i]->affection_list[j].u = u[k]; |
---|
[86] | 143 | // Set relationship |
---|
| 144 | const char *oRelate = pSecondChild->Attribute("relationship"); |
---|
| 145 | if(oRelate) o[i]->affection_list[j].relation = oRelate; |
---|
| 146 | j++; |
---|
| 147 | match_found = 1; |
---|
[35] | 148 | break; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
[86] | 152 | if(!match_found) cout << "Error: " << o[i]->name << ": " << oUtilName << " not a valid utility: Affect not added." << endl; |
---|
| 153 | match_found = 0; |
---|
[35] | 154 | } |
---|
| 155 | o[i]->numAffects = j; |
---|
| 156 | i++; |
---|
| 157 | } |
---|
| 158 | ce_info->numObservables = i; |
---|
| 159 | cout << "Parsed " << ce_info->numObservables << " observables." << endl; |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | // Pull parameter information from XML file. |
---|
| 163 | pElem = hRoot.FirstChild("parameters").Element(); |
---|
| 164 | pChild1 = hRoot.Child("parameters",count).Element(); |
---|
| 165 | |
---|
| 166 | i = 0; |
---|
| 167 | for(pChild = pChild1->FirstChildElement("parameter"); pChild; pChild = pChild->NextSiblingElement()) |
---|
| 168 | { |
---|
| 169 | p[i] = new Parameter; |
---|
| 170 | |
---|
| 171 | const char *pName = pChild->Attribute("name"); |
---|
| 172 | if(pName) p[i]->name = pName; |
---|
| 173 | const char *pUnits = pChild->Attribute("units"); |
---|
| 174 | if(pUnits) p[i]->units = pUnits; |
---|
| 175 | |
---|
| 176 | if(pChild->QueryFloatAttribute("min",&p[i]->min) != TIXML_SUCCESS) p[i]->min = -1; |
---|
| 177 | if(pChild->QueryFloatAttribute("max",&p[i]->max) != TIXML_SUCCESS) p[i]->max = -1; |
---|
| 178 | if(pChild->QueryFloatAttribute("step",&p[i]->step) != TIXML_SUCCESS) p[i]->step = -1; |
---|
| 179 | |
---|
| 180 | j = 0; |
---|
| 181 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) |
---|
| 182 | { |
---|
| 183 | const char *pUtilName = pSecondChild->Attribute("utility"); |
---|
| 184 | |
---|
| 185 | // If a utility affects this parameter find the utility object and assign it |
---|
| 186 | if(pUtilName) { |
---|
| 187 | // Search for correct utility |
---|
| 188 | for( k=0 ; u[k]!=NULL ; k++ ){ |
---|
| 189 | if(u[k]->name == pUtilName) { |
---|
[86] | 190 | // If match found, assign it to this index |
---|
[35] | 191 | p[i]->affection_list[j].u = u[k]; |
---|
[86] | 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 | match_found = 1; |
---|
| 199 | j++; |
---|
[35] | 200 | break; |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | } |
---|
[86] | 204 | if(!match_found) cout << "Error: " << p[i]->name << ": " << pUtilName << " not a valid utility: Affect not added." << endl; |
---|
| 205 | match_found = 0; |
---|
[35] | 206 | } |
---|
| 207 | p[i]->numAffects = j; |
---|
| 208 | i++; |
---|
| 209 | |
---|
| 210 | } |
---|
| 211 | ce_info->numParameters = i; |
---|
| 212 | cout << "Parsed " << ce_info->numParameters << " parameters." << endl; |
---|
| 213 | return 1; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
[63] | 217 | void error(char *msg) |
---|
| 218 | { |
---|
| 219 | perror(msg); |
---|
| 220 | exit(1); |
---|
| 221 | } |
---|
[35] | 222 | |
---|
| 223 | |
---|
[81] | 224 | int ReceiveMessage(int socket,char * buffer) |
---|
| 225 | { |
---|
| 226 | int i,n; |
---|
[65] | 227 | |
---|
[81] | 228 | n = recv(socket,buffer,256,MSG_PEEK); |
---|
| 229 | for(i=0;i<256;i++){ |
---|
| 230 | if(strcmp(&buffer[i],"\0") == 0) break; |
---|
| 231 | } |
---|
| 232 | n = recv(socket,buffer,i+1,0); |
---|
| 233 | if (n < 0) |
---|
| 234 | error("ERROR reading from socket"); |
---|
| 235 | // printf("ReadMessage:%s %d\n",buffer,n); |
---|
| 236 | |
---|
| 237 | return n; |
---|
[65] | 238 | } |
---|
| 239 | |
---|
[81] | 240 | |
---|
[65] | 241 | int SendMessage(int socketfd, string message) { |
---|
| 242 | int n; |
---|
| 243 | |
---|
[85] | 244 | message.append("\0"); |
---|
[65] | 245 | // Write message back to client |
---|
[70] | 246 | n = write(socketfd,message.c_str(),(message.size()+1)); |
---|
| 247 | if (n<0) |
---|
| 248 | error("Error sending to client\n"); |
---|
[72] | 249 | if(n == 0) |
---|
| 250 | printf("Client closed the socket.\n"); |
---|
[85] | 251 | |
---|
| 252 | printf("SendMessage:%s %d\n",message.c_str(),n); |
---|
[70] | 253 | return n; |
---|
[65] | 254 | } |
---|
| 255 | |
---|
[72] | 256 | void GetEnvironment() { |
---|
| 257 | |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | void Policy_ValidateSettings() { |
---|
| 261 | |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | |
---|
| 265 | void LoadCEConfiguration(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info){ |
---|
[65] | 266 | int n,i,j; |
---|
| 267 | char counter[55]; |
---|
| 268 | char var[50]; |
---|
[82] | 269 | //int total_bytes; |
---|
[72] | 270 | |
---|
| 271 | printf("Sending configuration to CE.\n"); |
---|
| 272 | |
---|
[80] | 273 | // utilities |
---|
[72] | 274 | // Send number of utilities |
---|
| 275 | sprintf(counter,"%d",ce_info->numUtilities); |
---|
| 276 | SendMessage(socketfd,counter); |
---|
[80] | 277 | // send utility |
---|
| 278 | for(i = 0; i < ce_info->numUtilities; i++) { |
---|
[72] | 279 | SendMessage(socketfd,uList[i]->name); |
---|
| 280 | SendMessage(socketfd,uList[i]->units); |
---|
| 281 | SendMessage(socketfd,uList[i]->goal); |
---|
| 282 | sprintf(var,"%f",uList[i]->target); |
---|
| 283 | SendMessage(socketfd,var); |
---|
| 284 | } |
---|
[66] | 285 | |
---|
[72] | 286 | // parameters |
---|
[80] | 287 | sprintf(counter,"%i",ce_info->numParameters); |
---|
[72] | 288 | SendMessage(socketfd,counter); |
---|
| 289 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
| 290 | SendMessage(socketfd,pList[i]->name); |
---|
| 291 | SendMessage(socketfd,pList[i]->units); |
---|
| 292 | sprintf(var,"%f",pList[i]->min); |
---|
| 293 | SendMessage(socketfd,var); |
---|
| 294 | sprintf(var,"%f",pList[i]->max); |
---|
| 295 | SendMessage(socketfd,var); |
---|
| 296 | sprintf(var,"%f",pList[i]->step); |
---|
| 297 | SendMessage(socketfd,var); |
---|
| 298 | |
---|
| 299 | sprintf(counter,"%i",pList[i]->numAffects); |
---|
[65] | 300 | SendMessage(socketfd,counter); |
---|
[72] | 301 | for(j = 0; j < pList[i]->numAffects; j++) { |
---|
| 302 | SendMessage(socketfd,pList[i]->affection_list[j].u->name); |
---|
| 303 | SendMessage(socketfd,pList[i]->affection_list[j].relation); |
---|
[65] | 304 | } |
---|
[72] | 305 | } |
---|
[66] | 306 | |
---|
[80] | 307 | // observables |
---|
[72] | 308 | sprintf(counter,"%i",ce_info->numObservables); |
---|
| 309 | SendMessage(socketfd,counter); |
---|
| 310 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
| 311 | SendMessage(socketfd,oList[i]->name); |
---|
| 312 | |
---|
| 313 | sprintf(counter,"%i",oList[i]->numAffects); |
---|
[65] | 314 | SendMessage(socketfd,counter); |
---|
[72] | 315 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
| 316 | SendMessage(socketfd,oList[i]->affection_list[j].u->name); |
---|
| 317 | SendMessage(socketfd,oList[i]->affection_list[j].relation); |
---|
[71] | 318 | } |
---|
[72] | 319 | } |
---|
[66] | 320 | |
---|
[72] | 321 | printf("Configuration sent, waiting for ACK...\n"); |
---|
| 322 | // Receive ACK for utils |
---|
[81] | 323 | char buffer[256]; |
---|
[66] | 324 | string message; |
---|
[81] | 325 | n = ReceiveMessage(socketfd, buffer); |
---|
| 326 | printf("%s\n", buffer); |
---|
| 327 | //cout << message << endl; |
---|
[72] | 328 | printf("ACK received.\n"); |
---|
[66] | 329 | |
---|
[72] | 330 | } |
---|
[66] | 331 | |
---|
[72] | 332 | void UpdateCEConfiguration() { |
---|
| 333 | |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | void ResetCEConfiguration(){ |
---|
| 337 | |
---|
| 338 | } |
---|
| 339 | |
---|
[87] | 340 | void UpdateCEExperience(int socketfd, int num_rows, int num_cols, |
---|
| 341 | float * past_exp[]) |
---|
| 342 | { |
---|
| 343 | char buffer[256]; |
---|
| 344 | int i, j; |
---|
| 345 | char counter[55]; |
---|
| 346 | char var[50]; |
---|
[72] | 347 | |
---|
[87] | 348 | for (i = 0; i < num_rows; i++){ |
---|
| 349 | for (j = 0; j< num_cols; j++){ |
---|
| 350 | sprintf(var,"%f",past_exp[i][j]); |
---|
| 351 | printf("%f, \n", past_exp[i][j]); |
---|
| 352 | printf("%s, \n", var); |
---|
| 353 | } |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | // send the number of rows to the ce first |
---|
| 357 | sprintf(counter,"%d",num_rows); |
---|
| 358 | SendMessage(socketfd,counter); |
---|
| 359 | // send the number of columns to the ce |
---|
| 360 | sprintf(counter,"%d",num_cols); |
---|
| 361 | SendMessage(socketfd,counter); |
---|
| 362 | // update ce with experience |
---|
| 363 | for (i = 0; i < num_rows; i++){ |
---|
| 364 | for (j = 0; j< num_cols; j++){ |
---|
| 365 | sprintf(var,"%f",past_exp[i][j]); |
---|
| 366 | SendMessage(socketfd,var); |
---|
| 367 | } |
---|
| 368 | } |
---|
[72] | 369 | } |
---|
| 370 | |
---|
| 371 | void ResetCEExperience() { |
---|
| 372 | |
---|
| 373 | } |
---|
| 374 | |
---|
[82] | 375 | // Update operating settings |
---|
| 376 | // This function will interact with the hardware "drivers" |
---|
[87] | 377 | void UpdateRadioSettings() |
---|
| 378 | { |
---|
[72] | 379 | } |
---|
| 380 | |
---|
[82] | 381 | int RequestCEOptimization(int sockfd, Utility *uList[], |
---|
[80] | 382 | Parameter *pList[], Observable *oList[], |
---|
| 383 | CE_Info *ce_info) |
---|
| 384 | { |
---|
[81] | 385 | char buffer[256]; |
---|
[80] | 386 | int i; |
---|
| 387 | float var; |
---|
[72] | 388 | |
---|
[82] | 389 | /*// utility |
---|
[80] | 390 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
| 391 | bzero(buffer,256); |
---|
[81] | 392 | ReceiveMessage(sockfd,buffer); |
---|
| 393 | var = atof(buffer); |
---|
[80] | 394 | uList[i]->value = var; |
---|
[81] | 395 | printf("utility %s, value %f\n" |
---|
| 396 | , uList[i]->name.c_str(), uList[i]->value); |
---|
[82] | 397 | }*/ |
---|
[80] | 398 | |
---|
| 399 | // paramter |
---|
| 400 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
[81] | 401 | bzero(buffer,256); |
---|
| 402 | ReceiveMessage(sockfd,buffer); |
---|
| 403 | var = atof(buffer); |
---|
[80] | 404 | pList[i]->value = var; |
---|
[81] | 405 | printf("parameter %s, value %f\n" |
---|
| 406 | , pList[i]->name.c_str(), pList[i]->value); |
---|
[80] | 407 | } |
---|
| 408 | |
---|
[82] | 409 | /*// observable |
---|
[80] | 410 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
[81] | 411 | bzero(buffer,256); |
---|
| 412 | ReceiveMessage(sockfd,buffer); |
---|
| 413 | var = atof(buffer); |
---|
[80] | 414 | oList[i]->value = var; |
---|
[81] | 415 | printf("observable %s, value %f\n" |
---|
| 416 | , oList[i]->name.c_str(), oList[i]->value); |
---|
[82] | 417 | }*/ |
---|
| 418 | |
---|
[81] | 419 | return 1; |
---|
[80] | 420 | } |
---|
| 421 | |
---|
| 422 | void RunSimulator(int socketfd, Utility * uList[], |
---|
| 423 | Parameter * pList[], Observable * oList[], |
---|
| 424 | CE_Info * ce_info) { |
---|
[85] | 425 | |
---|
| 426 | float **past_exp; |
---|
[87] | 427 | int num_rows, num_cols; |
---|
[80] | 428 | |
---|
[78] | 429 | // Set fake current environment params = current environment |
---|
[82] | 430 | RequestCEOptimization(socketfd, uList, pList, oList, ce_info); |
---|
[72] | 431 | |
---|
| 432 | // Act like we are updating the hardware tranmission settings |
---|
[82] | 433 | UpdateRadioSettings(); |
---|
[72] | 434 | |
---|
| 435 | // Send back fake utility values |
---|
[87] | 436 | // need to initialize |
---|
| 437 | //UpdateCEExperience(socketfd, num_rows, num_cols, past_exp); |
---|
[63] | 438 | } |
---|
[35] | 439 | |
---|
[87] | 440 | void StartMessaging(int socketfd, Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) |
---|
| 441 | { |
---|
[80] | 442 | LoadCEConfiguration(socketfd, uList, pList, oList, ce_info); |
---|
[85] | 443 | |
---|
[87] | 444 | // cr experience |
---|
| 445 | float **past_exp; |
---|
| 446 | int num_cols; |
---|
| 447 | // get number of columns |
---|
| 448 | num_cols = ce_info->numUtilities + ce_info->numParameters; |
---|
| 449 | num_cols = num_cols + ce_info->numObservables; |
---|
| 450 | num_cols = num_cols + 1; // overall utility |
---|
| 451 | int num_rows = 1; |
---|
| 452 | past_exp = (float **)malloc(sizeof(float)*num_rows); |
---|
| 453 | int i; |
---|
| 454 | for (i=0; i<num_rows; i++){ |
---|
| 455 | past_exp[i] = (float*)malloc(sizeof(float)*num_cols); |
---|
| 456 | } |
---|
| 457 | // sample experience #1 |
---|
| 458 | past_exp[0][0] = 1e3f; // throughput |
---|
| 459 | past_exp[0][1] = 1; // spectral_efficiency |
---|
| 460 | past_exp[0][2] = -3.5; // log10_ber |
---|
| 461 | past_exp[0][3] = 1; // mod_scheme |
---|
| 462 | past_exp[0][4] = -3.5; // tx_power |
---|
| 463 | past_exp[0][5] = 10.0; // SNR |
---|
| 464 | past_exp[0][6] = 0.762; // overall utility |
---|
| 465 | /*// sample experience #2 |
---|
| 466 | past_exp[1][0] = 1e2f; // throughput |
---|
| 467 | past_exp[1][1] = 1; // spectral_efficiency |
---|
| 468 | past_exp[1][2] = -3.5; // log10_ber |
---|
| 469 | past_exp[1][3] = 1; // mod_scheme |
---|
| 470 | past_exp[1][4] = -3.5; // tx_power |
---|
| 471 | past_exp[1][5] = 3.0; // SNR |
---|
| 472 | past_exp[1][6] = 0.462; // overall utility*/ |
---|
[72] | 473 | |
---|
[87] | 474 | |
---|
| 475 | // update ce with experience |
---|
| 476 | UpdateCEExperience(socketfd, num_rows, num_cols, past_exp); |
---|
| 477 | |
---|
[80] | 478 | RunSimulator(socketfd, uList, pList, oList, ce_info); |
---|
[72] | 479 | } |
---|
| 480 | |
---|
[65] | 481 | int StartShell(int port,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { |
---|
[63] | 482 | // Start socket server |
---|
| 483 | int sockfd, newsockfd, clilen; |
---|
| 484 | struct sockaddr_in serv_addr, cli_addr; |
---|
[35] | 485 | |
---|
[73] | 486 | // Setup server socket connection |
---|
| 487 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
[63] | 488 | if (sockfd < 0) |
---|
| 489 | error("ERROR opening socket"); |
---|
| 490 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
---|
| 491 | serv_addr.sin_family = AF_INET; |
---|
| 492 | serv_addr.sin_port = htons(port); |
---|
[73] | 493 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
---|
[63] | 494 | if (bind(sockfd, (struct sockaddr *) &serv_addr, |
---|
| 495 | sizeof(serv_addr)) < 0) |
---|
| 496 | error("ERROR on binding"); |
---|
| 497 | listen(sockfd,5); |
---|
| 498 | clilen = sizeof(cli_addr); |
---|
[35] | 499 | |
---|
[66] | 500 | //while(1) { |
---|
[64] | 501 | newsockfd = accept(sockfd, |
---|
| 502 | (struct sockaddr *) &cli_addr, |
---|
| 503 | (socklen_t*)&clilen); |
---|
| 504 | if (newsockfd < 0) |
---|
| 505 | error("ERROR on accept"); |
---|
[63] | 506 | |
---|
[64] | 507 | // Begin parsing the messages |
---|
[80] | 508 | StartMessaging(newsockfd, uList, pList, oList, ce_info); |
---|
[66] | 509 | //} |
---|
[63] | 510 | return 0; |
---|
[35] | 511 | } |
---|
| 512 | |
---|
| 513 | int main(int argc, char* argv[]) { |
---|
| 514 | |
---|
| 515 | |
---|
| 516 | // CognitiveEngine CE; |
---|
| 517 | // CognitiveEngineShell Shell; |
---|
| 518 | string pFilename; |
---|
[64] | 519 | int pid; |
---|
[35] | 520 | |
---|
| 521 | Utility * uList[10]; |
---|
| 522 | Parameter * pList[10]; |
---|
| 523 | Observable * oList[10]; |
---|
| 524 | CE_Info ce_info; |
---|
| 525 | |
---|
| 526 | if(argc < 2) { |
---|
| 527 | cout << "Warning no XML file specific using default: example.xml" << endl; |
---|
| 528 | pFilename = "example.xml"; |
---|
| 529 | } else { |
---|
| 530 | pFilename = argv[1]; |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | TiXmlDocument doc( pFilename.c_str() ); |
---|
| 534 | bool loadOkay = doc.LoadFile(); |
---|
| 535 | if (!loadOkay) |
---|
| 536 | { |
---|
| 537 | cout << "Loading " << pFilename << " failed." << endl; |
---|
| 538 | return 0; |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | cout << "Attemping to parse " << pFilename << "." << endl; |
---|
| 542 | parse_ce_config( &doc , uList, pList, oList, &ce_info); |
---|
| 543 | cout << "Configuration file parsing completed." << endl; |
---|
| 544 | |
---|
[72] | 545 | //print_current_config(uList, pList, oList, &ce_info); |
---|
[69] | 546 | |
---|
| 547 | pid = fork(); |
---|
[64] | 548 | if(pid == 0) { |
---|
| 549 | // In child process - open policy engine port. |
---|
[71] | 550 | // StartShell(30000,uList, pList, oList, &ce_info); |
---|
[64] | 551 | } else { |
---|
| 552 | // In parent process - open cognitive engine port. |
---|
[82] | 553 | StartShell(30001,uList, pList, oList, &ce_info); |
---|
[64] | 554 | } |
---|
| 555 | |
---|
[35] | 556 | |
---|
| 557 | return 1; |
---|
[71] | 558 | } |
---|