[166] | 1 | /* Virginia Tech Cognitive Radio Open Source Systems |
---|
| 2 | * Virginia Tech, 2009 |
---|
| 3 | * |
---|
| 4 | * TODO LICENSE INFORMATION GOES HERE |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /* TODO DESCRIPTION OF FILE. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | #include <arpa/inet.h> |
---|
[35] | 12 | #include <iostream> |
---|
[96] | 13 | #include <netinet/in.h> |
---|
| 14 | #include <netdb.h> |
---|
[97] | 15 | #include <fcntl.h> |
---|
| 16 | #include <sys/ioctl.h> |
---|
[166] | 17 | #include <sys/mman.h> |
---|
| 18 | #include <sys/socket.h> |
---|
| 19 | #include <sys/types.h> |
---|
| 20 | #include <sys/wait.h> |
---|
[97] | 21 | |
---|
[161] | 22 | #include "tinyxml/tinyxml.h" |
---|
| 23 | #include "tinyxml/tinystr.h" |
---|
| 24 | |
---|
[166] | 25 | #include "vtcross/common.h" |
---|
| 26 | #include "vtcross/components.h" |
---|
| 27 | #include "vtcross/containers.h" |
---|
| 28 | #include "vtcross/debug.h" |
---|
| 29 | #include "vtcross/error.h" |
---|
| 30 | #include "vtcross/socketcomm.h" |
---|
| 31 | |
---|
| 32 | |
---|
[35] | 33 | using namespace std; |
---|
| 34 | |
---|
[166] | 35 | |
---|
[97] | 36 | #define CE_SERVER_PORT 30001 |
---|
[166] | 37 | #define PE_SERVER_PORT 30003 |
---|
[35] | 38 | |
---|
[166] | 39 | |
---|
| 40 | void |
---|
| 41 | print_current_config(Utility* uList[], Parameter* pList[], \ |
---|
| 42 | Observable* oList[], CE_Info* ce_info) |
---|
[97] | 43 | { |
---|
[166] | 44 | for(size_t i = 0; i < ce_info->numUtilities ; i++) { |
---|
[175] | 45 | LOG("Shell:: Utility: %s\n\tUnits: %s\n\tGoal: %s\n\tTarget: %f\n", \ |
---|
| 46 | uList[i]->name.c_str(), uList[i]->units.c_str(), \ |
---|
| 47 | uList[i]->goal.c_str(), uList[i]->target); |
---|
[166] | 48 | } |
---|
[97] | 49 | |
---|
[166] | 50 | for(size_t i = 0; i < ce_info->numParameters; i++) { |
---|
| 51 | LOG("Shell:: Radio Operation Profile has been sucessfully sent.\n"); |
---|
[175] | 52 | LOG("Shell:: Parameter: %s\n\tUnits: %s\n\tMin: %f\n\t", \ |
---|
| 53 | pList[i]->name.c_str(), pList[i]->units.c_str(), \ |
---|
| 54 | pList[i]->min); |
---|
| 55 | LOG("\tMax: %f\n\tStep: %f\n", pList[i]->max, pList[i]->step); |
---|
[35] | 56 | |
---|
[166] | 57 | for(size_t j = 0; j < pList[i]->numAffects; j++) { |
---|
[175] | 58 | LOG("\t\tAffect %s -> %s\n", pList[i]->affection_list[j].u->name.c_str(), \ |
---|
| 59 | pList[i]->affection_list[j].relation.c_str()); |
---|
[35] | 60 | } |
---|
| 61 | } |
---|
[80] | 62 | |
---|
[166] | 63 | for(size_t i = 0; i < ce_info->numObservables; i++) { |
---|
[175] | 64 | LOG("Observable: %s\n", oList[i]->name.c_str()); |
---|
[169] | 65 | |
---|
[166] | 66 | for(size_t j = 0; j < oList[i]->numAffects; j++) { |
---|
[175] | 67 | LOG("\t\tAffect %s -> %s ", oList[i]->affection_list[j].u->name.c_str(), \ |
---|
| 68 | oList[i]->affection_list[j].relation.c_str()); |
---|
[35] | 69 | } |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
[175] | 74 | // TODO should this function really be called parse 'ce' config? Seems like it |
---|
| 75 | // affects the entire radio? |
---|
| 76 | // TODO Why are we always returning 1? Is this just a success variable? It's not |
---|
| 77 | // even being used when we call it... |
---|
| 78 | // TODO this function needs some serious comments |
---|
[166] | 79 | int32_t |
---|
| 80 | parse_ce_config(TiXmlDocument* doc, Utility* u[], Parameter* p[], \ |
---|
| 81 | Observable* o[], CE_Info* ce_info) |
---|
| 82 | { |
---|
[35] | 83 | |
---|
[169] | 84 | TiXmlElement *pElem; |
---|
| 85 | TiXmlElement *pChild; |
---|
| 86 | TiXmlElement *pChild1; |
---|
| 87 | TiXmlElement *pSecondChild; |
---|
| 88 | TiXmlHandle hDoc(doc); |
---|
| 89 | TiXmlHandle hRoot(0); |
---|
[35] | 90 | |
---|
[166] | 91 | int32_t count = 0; |
---|
[169] | 92 | size_t item_count = 0; |
---|
| 93 | size_t affect_count = 0; |
---|
| 94 | int32_t attribute_count = 0; |
---|
| 95 | bool match_found = false; |
---|
[35] | 96 | |
---|
| 97 | pElem = hDoc.FirstChildElement().Element(); |
---|
[169] | 98 | |
---|
| 99 | if(!pElem) |
---|
| 100 | ERROR(1, "No valid root!"); |
---|
| 101 | |
---|
[35] | 102 | hRoot = TiXmlHandle(pElem); |
---|
| 103 | |
---|
| 104 | pElem = hRoot.FirstChild("utilities").Element(); |
---|
[169] | 105 | pChild1 = hRoot.Child("utilities", count).Element(); |
---|
[35] | 106 | |
---|
[169] | 107 | for(pChild = pChild1->FirstChildElement("utility"); pChild; \ |
---|
| 108 | pChild = pChild->NextSiblingElement()) { |
---|
[35] | 109 | |
---|
[169] | 110 | u[item_count] = new Utility; |
---|
| 111 | |
---|
[35] | 112 | const char *uName = pChild->Attribute("name"); |
---|
[169] | 113 | if(uName) |
---|
| 114 | u[item_count]->name = uName; |
---|
| 115 | |
---|
[35] | 116 | const char *uUnits = pChild->Attribute("units"); |
---|
[169] | 117 | if(uUnits) |
---|
| 118 | u[item_count]->units = uUnits; |
---|
| 119 | |
---|
[35] | 120 | const char *uGoal = pChild->Attribute("goal"); |
---|
[169] | 121 | if(uGoal) |
---|
| 122 | u[item_count]->goal = uGoal; |
---|
| 123 | |
---|
[175] | 124 | if(pChild->QueryFloatAttribute("target", &u[item_count]->target) != TIXML_SUCCESS) |
---|
| 125 | u[item_count]->target = -1; |
---|
[169] | 126 | |
---|
| 127 | item_count++; |
---|
[35] | 128 | } |
---|
| 129 | |
---|
[169] | 130 | ce_info->numUtilities = item_count; |
---|
| 131 | LOG("Initialize:: Parsed %d utilities.\n", ce_info->numUtilities); |
---|
| 132 | |
---|
| 133 | item_count = 0; |
---|
[35] | 134 | pElem = hRoot.FirstChild("observables").Element(); |
---|
[169] | 135 | pChild1 = hRoot.Child("observables", count).Element(); |
---|
[35] | 136 | |
---|
[169] | 137 | for(pChild = pChild1->FirstChildElement("observable"); pChild; \ |
---|
| 138 | pChild = pChild->NextSiblingElement()) { |
---|
| 139 | |
---|
[35] | 140 | const char *oName = pChild->Attribute("name"); |
---|
[169] | 141 | o[item_count] = new Observable; |
---|
| 142 | if(oName) |
---|
| 143 | o[item_count]->name = oName; |
---|
| 144 | |
---|
| 145 | affect_count = 0; |
---|
| 146 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ |
---|
| 147 | pSecondChild = pSecondChild->NextSiblingElement()) { |
---|
[35] | 148 | |
---|
| 149 | const char *oUtilName = pSecondChild->Attribute("utility"); |
---|
[169] | 150 | if(oUtilName) { |
---|
[175] | 151 | for(attribute_count = 0; u[attribute_count] != NULL; attribute_count++ ) { |
---|
[169] | 152 | if(u[attribute_count]->name == oUtilName) { |
---|
[175] | 153 | o[item_count]->affection_list[affect_count].u = u[attribute_count]; |
---|
[35] | 154 | |
---|
[86] | 155 | const char *oRelate = pSecondChild->Attribute("relationship"); |
---|
[169] | 156 | if(oRelate) |
---|
| 157 | o[item_count]->affection_list[affect_count].relation = oRelate; |
---|
| 158 | |
---|
| 159 | affect_count++; |
---|
| 160 | match_found = true; |
---|
[35] | 161 | break; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | } |
---|
[169] | 165 | |
---|
[175] | 166 | if(!match_found) { |
---|
[169] | 167 | ERROR(1, "Error: %s: %s is not a valid utility.\n", \ |
---|
[175] | 168 | o[item_count]->name.c_str(), oUtilName); |
---|
| 169 | } |
---|
[169] | 170 | else |
---|
| 171 | match_found = false; |
---|
[35] | 172 | } |
---|
[169] | 173 | |
---|
| 174 | o[item_count]->numAffects = affect_count; |
---|
| 175 | item_count++; |
---|
[35] | 176 | } |
---|
| 177 | |
---|
[169] | 178 | ce_info->numObservables = item_count; |
---|
| 179 | LOG("Initialize:: Parsed %d observables.\n", ce_info->numObservables); |
---|
| 180 | |
---|
[35] | 181 | pElem = hRoot.FirstChild("parameters").Element(); |
---|
[169] | 182 | pChild1 = hRoot.Child("parameters", count).Element(); |
---|
[35] | 183 | |
---|
[169] | 184 | item_count = 0; |
---|
| 185 | for(pChild = pChild1->FirstChildElement("parameter"); pChild; \ |
---|
| 186 | pChild = pChild->NextSiblingElement()) { |
---|
[35] | 187 | |
---|
[169] | 188 | p[item_count] = new Parameter; |
---|
| 189 | |
---|
[35] | 190 | const char *pName = pChild->Attribute("name"); |
---|
[169] | 191 | if(pName) |
---|
| 192 | p[item_count]->name = pName; |
---|
| 193 | |
---|
[35] | 194 | const char *pUnits = pChild->Attribute("units"); |
---|
[169] | 195 | if(pUnits) |
---|
| 196 | p[item_count]->units = pUnits; |
---|
[35] | 197 | |
---|
[175] | 198 | if(pChild->QueryFloatAttribute("min", &p[item_count]->min) != TIXML_SUCCESS) |
---|
[169] | 199 | p[item_count]->min = -1; |
---|
| 200 | |
---|
[175] | 201 | if(pChild->QueryFloatAttribute("max", &p[item_count]->max) != TIXML_SUCCESS) |
---|
[169] | 202 | p[item_count]->max = -1; |
---|
| 203 | |
---|
[175] | 204 | if(pChild->QueryFloatAttribute("step", &p[item_count]->step) != TIXML_SUCCESS) |
---|
[169] | 205 | p[item_count]->step = -1; |
---|
[35] | 206 | |
---|
[169] | 207 | affect_count = 0; |
---|
| 208 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ |
---|
| 209 | pSecondChild = pSecondChild->NextSiblingElement()) { |
---|
| 210 | |
---|
[35] | 211 | const char *pUtilName = pSecondChild->Attribute("utility"); |
---|
| 212 | if(pUtilName) { |
---|
[169] | 213 | for(attribute_count = 0; u[attribute_count] != NULL; attribute_count++) { |
---|
| 214 | if(u[attribute_count]->name == pUtilName) { |
---|
| 215 | p[item_count]->affection_list[affect_count].u = u[attribute_count]; |
---|
| 216 | |
---|
[86] | 217 | const char *pRelate = pSecondChild->Attribute("relationship"); |
---|
[169] | 218 | if(pRelate) |
---|
| 219 | p[item_count]->affection_list[affect_count].relation = pRelate; |
---|
| 220 | else |
---|
| 221 | LOG("Error: No relation found.\n"); |
---|
| 222 | |
---|
| 223 | match_found = true; |
---|
| 224 | affect_count++; |
---|
[35] | 225 | break; |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | } |
---|
[169] | 229 | |
---|
| 230 | if(!match_found) { |
---|
| 231 | ERROR(1, "Error: %s: %s is not a valid utility.\n", \ |
---|
[175] | 232 | p[item_count]->name.c_str(), pUtilName); |
---|
[169] | 233 | } |
---|
| 234 | |
---|
| 235 | match_found = false; |
---|
[35] | 236 | } |
---|
| 237 | |
---|
[169] | 238 | p[item_count]->numAffects = affect_count; |
---|
| 239 | item_count++; |
---|
[35] | 240 | } |
---|
[169] | 241 | |
---|
| 242 | ce_info->numParameters = item_count; |
---|
| 243 | LOG("Initialize:: Parsed %d parameters.\n", ce_info->numParameters); |
---|
| 244 | |
---|
[35] | 245 | return 1; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
[166] | 249 | void |
---|
[178] | 250 | LoadCEConfiguration(int32_t socketfd,Utility* uList[], Parameter* pList[], \ |
---|
| 251 | Observable* oList[], CE_Info* ce_info) |
---|
| 252 | { |
---|
[166] | 253 | int32_t n,i,j; |
---|
[65] | 254 | char counter[55]; |
---|
| 255 | char var[50]; |
---|
[166] | 256 | //int32_t total_bytes; |
---|
[72] | 257 | |
---|
[168] | 258 | printf("Cognitive Radio:: Sending Radio Operating Profile to Cognitive Engine.\n\n"); |
---|
[72] | 259 | |
---|
[80] | 260 | // utilities |
---|
[72] | 261 | // Send number of utilities |
---|
[167] | 262 | sprintf(counter,"%d",ce_info->numUtilities); |
---|
[72] | 263 | SendMessage(socketfd,counter); |
---|
[80] | 264 | // send utility |
---|
| 265 | for(i = 0; i < ce_info->numUtilities; i++) { |
---|
[175] | 266 | SendMessage(socketfd, uList[i]->name.c_str()); |
---|
| 267 | SendMessage(socketfd, uList[i]->units.c_str()); |
---|
| 268 | SendMessage(socketfd, uList[i]->goal.c_str()); |
---|
| 269 | sprintf(var,"%f", uList[i]->target); |
---|
[72] | 270 | SendMessage(socketfd,var); |
---|
| 271 | } |
---|
[66] | 272 | |
---|
[72] | 273 | // parameters |
---|
[167] | 274 | sprintf(counter,"%i",ce_info->numParameters); |
---|
[72] | 275 | SendMessage(socketfd,counter); |
---|
| 276 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
[175] | 277 | SendMessage(socketfd,pList[i]->name.c_str()); |
---|
| 278 | SendMessage(socketfd,pList[i]->units.c_str()); |
---|
[167] | 279 | sprintf(var,"%f",pList[i]->min); |
---|
[72] | 280 | SendMessage(socketfd,var); |
---|
[167] | 281 | sprintf(var,"%f",pList[i]->max); |
---|
[72] | 282 | SendMessage(socketfd,var); |
---|
[167] | 283 | sprintf(var,"%f",pList[i]->step); |
---|
[72] | 284 | SendMessage(socketfd,var); |
---|
| 285 | |
---|
[167] | 286 | sprintf(counter,"%i",pList[i]->numAffects); |
---|
[65] | 287 | SendMessage(socketfd,counter); |
---|
[72] | 288 | for(j = 0; j < pList[i]->numAffects; j++) { |
---|
[175] | 289 | SendMessage(socketfd,pList[i]->affection_list[j].u->name.c_str()); |
---|
| 290 | SendMessage(socketfd,pList[i]->affection_list[j].relation.c_str()); |
---|
[65] | 291 | } |
---|
[72] | 292 | } |
---|
[66] | 293 | |
---|
[80] | 294 | // observables |
---|
[167] | 295 | sprintf(counter,"%i",ce_info->numObservables); |
---|
[72] | 296 | SendMessage(socketfd,counter); |
---|
| 297 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
[175] | 298 | SendMessage(socketfd,oList[i]->name.c_str()); |
---|
[72] | 299 | |
---|
[167] | 300 | sprintf(counter,"%i",oList[i]->numAffects); |
---|
[65] | 301 | SendMessage(socketfd,counter); |
---|
[72] | 302 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
[175] | 303 | SendMessage(socketfd,oList[i]->affection_list[j].u->name.c_str()); |
---|
| 304 | SendMessage(socketfd,oList[i]->affection_list[j].relation.c_str()); |
---|
[71] | 305 | } |
---|
[72] | 306 | } |
---|
[66] | 307 | |
---|
[72] | 308 | // Receive ACK for utils |
---|
[81] | 309 | char buffer[256]; |
---|
[66] | 310 | string message; |
---|
[175] | 311 | ReadMessage(socketfd, buffer); |
---|
[167] | 312 | //printf("%s\n", buffer); |
---|
[81] | 313 | //cout << message << endl; |
---|
[167] | 314 | //printf("ACK received.\n"); |
---|
[66] | 315 | |
---|
[72] | 316 | } |
---|
[66] | 317 | |
---|
[72] | 318 | |
---|
[178] | 319 | void |
---|
| 320 | UpdateCEExperience(int32_t socketfd, int32_t num_rows, int32_t num_cols, |
---|
[87] | 321 | float * past_exp[]) |
---|
| 322 | { |
---|
[166] | 323 | int32_t i, j; |
---|
[87] | 324 | char counter[55]; |
---|
| 325 | char var[50]; |
---|
[72] | 326 | |
---|
[87] | 327 | for (i = 0; i < num_rows; i++){ |
---|
| 328 | for (j = 0; j< num_cols; j++){ |
---|
[167] | 329 | sprintf(var,"%f",past_exp[i][j]); |
---|
| 330 | //printf("%f, \n", past_exp[i][j]); |
---|
| 331 | //printf("%s, \n", var); |
---|
[87] | 332 | } |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | // send the number of rows to the ce first |
---|
[167] | 336 | sprintf(counter,"%d",num_rows); |
---|
[87] | 337 | SendMessage(socketfd,counter); |
---|
| 338 | // send the number of columns to the ce |
---|
[167] | 339 | sprintf(counter,"%d",num_cols); |
---|
[87] | 340 | SendMessage(socketfd,counter); |
---|
| 341 | // update ce with experience |
---|
| 342 | for (i = 0; i < num_rows; i++){ |
---|
| 343 | for (j = 0; j< num_cols; j++){ |
---|
[167] | 344 | sprintf(var,"%f",past_exp[i][j]); |
---|
[87] | 345 | SendMessage(socketfd,var); |
---|
| 346 | } |
---|
| 347 | } |
---|
[90] | 348 | |
---|
[72] | 349 | } |
---|
| 350 | |
---|
| 351 | |
---|
[178] | 352 | int32_t |
---|
| 353 | RequestPolicyValidation(Parameter * pList[], CE_Info *ce_info) |
---|
[87] | 354 | { |
---|
[97] | 355 | char counter[55]; |
---|
| 356 | char var[50]; |
---|
[166] | 357 | int32_t i; |
---|
[175] | 358 | char* control_msg = "val"; |
---|
[97] | 359 | |
---|
[166] | 360 | int32_t socketfd = ce_info->policy_socket; |
---|
[97] | 361 | |
---|
| 362 | // Control message that validation request is coming |
---|
| 363 | SendMessage(socketfd,control_msg); |
---|
| 364 | |
---|
[167] | 365 | printf("Cognitive Radio:: Here. %i\n\n", socketfd); |
---|
[97] | 366 | |
---|
| 367 | // Send parameter information |
---|
[167] | 368 | sprintf(counter,"%i",ce_info->numParameters); |
---|
[97] | 369 | SendMessage(socketfd,counter); |
---|
| 370 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
[175] | 371 | SendMessage(socketfd,pList[i]->name.c_str()); |
---|
| 372 | SendMessage(socketfd,pList[i]->units.c_str()); |
---|
[167] | 373 | sprintf(var,"%f",pList[i]->min); |
---|
[97] | 374 | SendMessage(socketfd,var); |
---|
[167] | 375 | sprintf(var,"%f",pList[i]->max); |
---|
[97] | 376 | SendMessage(socketfd,var); |
---|
[167] | 377 | sprintf(var,"%f",pList[i]->step); |
---|
[97] | 378 | SendMessage(socketfd,var); |
---|
[167] | 379 | sprintf(var,"%f",pList[i]->value); |
---|
[97] | 380 | SendMessage(socketfd,var); |
---|
| 381 | |
---|
| 382 | } |
---|
| 383 | return 1; |
---|
| 384 | |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | |
---|
[178] | 388 | int32_t |
---|
| 389 | RequestCEOptimization(int32_t sockfd, Utility *uList[], |
---|
[80] | 390 | Parameter *pList[], Observable *oList[], |
---|
| 391 | CE_Info *ce_info) |
---|
| 392 | { |
---|
[81] | 393 | char buffer[256]; |
---|
[166] | 394 | int32_t i; |
---|
[80] | 395 | float var; |
---|
[72] | 396 | |
---|
[95] | 397 | // Send request optimization message followed by the current environment parameters. |
---|
| 398 | /* |
---|
| 399 | SendMessage(sockfd,"request"); |
---|
| 400 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
| 401 | SendMessage(sockfd,..); |
---|
| 402 | } |
---|
| 403 | */ |
---|
| 404 | |
---|
| 405 | // Receive optimized values from the Cognitive Engine |
---|
[80] | 406 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
[81] | 407 | bzero(buffer,256); |
---|
[175] | 408 | ReadMessage(sockfd,buffer); |
---|
[81] | 409 | var = atof(buffer); |
---|
[80] | 410 | pList[i]->value = var; |
---|
| 411 | } |
---|
| 412 | |
---|
[82] | 413 | |
---|
[97] | 414 | // If policy engine is connect, validate new values |
---|
| 415 | if(ce_info->policy_engine == 1) { |
---|
| 416 | |
---|
[167] | 417 | printf("Cognitive Radio:: Found Policy Engine!\n"); |
---|
| 418 | printf("Cognitive Radio:: Validating parameters with Policy Engine\n\n"); |
---|
[97] | 419 | RequestPolicyValidation(pList,ce_info); |
---|
[167] | 420 | printf("Cognitive Radio:: Done\n\n"); |
---|
[97] | 421 | |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | |
---|
[81] | 425 | return 1; |
---|
[80] | 426 | } |
---|
| 427 | |
---|
[178] | 428 | void |
---|
| 429 | RunSimulator(int32_t socketfd, Utility * uList[], |
---|
[80] | 430 | Parameter * pList[], Observable * oList[], |
---|
| 431 | CE_Info * ce_info) { |
---|
[85] | 432 | |
---|
| 433 | float **past_exp; |
---|
[166] | 434 | int32_t num_rows, num_cols; |
---|
[80] | 435 | |
---|
[78] | 436 | // Set fake current environment params = current environment |
---|
[82] | 437 | RequestCEOptimization(socketfd, uList, pList, oList, ce_info); |
---|
[72] | 438 | |
---|
| 439 | // Act like we are updating the hardware tranmission settings |
---|
[179] | 440 | //UpdateRadioSettings(); |
---|
[72] | 441 | |
---|
| 442 | // Send back fake utility values |
---|
[87] | 443 | // need to initialize |
---|
| 444 | //UpdateCEExperience(socketfd, num_rows, num_cols, past_exp); |
---|
[63] | 445 | } |
---|
[35] | 446 | |
---|
[178] | 447 | void |
---|
| 448 | InitializePE(int32_t socket, CE_Info * ce_info) |
---|
[87] | 449 | { |
---|
[97] | 450 | // Policy Engine is connected |
---|
| 451 | // Set global policy engine value to 1 |
---|
| 452 | ce_info->policy_engine = 1; |
---|
| 453 | ce_info->policy_socket = socket; |
---|
| 454 | |
---|
| 455 | return; |
---|
| 456 | } |
---|
| 457 | |
---|
[178] | 458 | void |
---|
| 459 | InitializeCE(int32_t socketfd, Utility* uList[], Parameter* pList[], \ |
---|
| 460 | Observable* oList[], CE_Info* ce_info) |
---|
[97] | 461 | { |
---|
[80] | 462 | LoadCEConfiguration(socketfd, uList, pList, oList, ce_info); |
---|
[85] | 463 | |
---|
[87] | 464 | // cr experience |
---|
| 465 | float **past_exp; |
---|
[166] | 466 | int32_t num_cols; |
---|
[87] | 467 | // get number of columns |
---|
| 468 | num_cols = ce_info->numUtilities + ce_info->numParameters; |
---|
| 469 | num_cols = num_cols + ce_info->numObservables; |
---|
| 470 | num_cols = num_cols + 1; // overall utility |
---|
[166] | 471 | int32_t num_rows = 2; |
---|
[87] | 472 | past_exp = (float **)malloc(sizeof(float)*num_rows); |
---|
[166] | 473 | int32_t i; |
---|
[87] | 474 | for (i=0; i<num_rows; i++){ |
---|
| 475 | past_exp[i] = (float*)malloc(sizeof(float)*num_cols); |
---|
| 476 | } |
---|
| 477 | // sample experience #1 |
---|
| 478 | past_exp[0][0] = 1e3f; // throughput |
---|
| 479 | past_exp[0][1] = 1; // spectral_efficiency |
---|
| 480 | past_exp[0][2] = -3.5; // log10_ber |
---|
| 481 | past_exp[0][3] = 1; // mod_scheme |
---|
[90] | 482 | past_exp[0][4] = -10; // tx_power |
---|
[87] | 483 | past_exp[0][5] = 10.0; // SNR |
---|
[90] | 484 | past_exp[0][6] = 0.762; // overall utility*/ |
---|
| 485 | // sample experience #2 |
---|
[87] | 486 | past_exp[1][0] = 1e2f; // throughput |
---|
| 487 | past_exp[1][1] = 1; // spectral_efficiency |
---|
| 488 | past_exp[1][2] = -3.5; // log10_ber |
---|
| 489 | past_exp[1][3] = 1; // mod_scheme |
---|
[90] | 490 | past_exp[1][4] = -14; // tx_power |
---|
| 491 | past_exp[1][5] = 3.0; // SNR |
---|
| 492 | past_exp[1][6] = 0.462; // overall utility |
---|
[72] | 493 | |
---|
[87] | 494 | // update ce with experience |
---|
[167] | 495 | printf("Cognitive Radio:: Sending Previous Experience to New Cognitive Engine.\n\n"); |
---|
[87] | 496 | UpdateCEExperience(socketfd, num_rows, num_cols, past_exp); |
---|
| 497 | |
---|
[80] | 498 | RunSimulator(socketfd, uList, pList, oList, ce_info); |
---|
[72] | 499 | } |
---|
| 500 | |
---|
[35] | 501 | |
---|
[179] | 502 | void |
---|
| 503 | HandleTCPClient(int32_t socketFD, Utility* uList[], Parameter* pList[], \ |
---|
| 504 | Observable* oList[], CE_Info* ce_info) |
---|
| 505 | { |
---|
| 506 | char buffer[256]; |
---|
| 507 | |
---|
| 508 | /* Receive message from client */ |
---|
| 509 | bzero(buffer, 256); |
---|
| 510 | ReadMessage(socketFD, buffer); |
---|
| 511 | |
---|
| 512 | LOG("Cognitive Radio:: Message Received - %s.\n\n", buffer); |
---|
| 513 | |
---|
| 514 | if(strcmp(buffer,"c_register") == 0) |
---|
| 515 | InitializeCE(socketFD, uList, pList, oList, ce_info); |
---|
| 516 | |
---|
| 517 | if(strcmp(buffer,"p_register") == 0) |
---|
| 518 | InitializePE(socketFD, ce_info); |
---|
| 519 | |
---|
| 520 | if(strcmp(buffer,"optimize") == 0) |
---|
| 521 | RunSimulator(socketFD, uList, pList, oList, ce_info); |
---|
| 522 | |
---|
| 523 | // TODO why aren't we doing this anymore? |
---|
| 524 | //close(socketfd); /* Close client socket */ |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | |
---|
[178] | 528 | int32_t |
---|
| 529 | StartServers(Utility* uList[], Parameter* pList[], Observable* oList[], \ |
---|
| 530 | CE_Info* ce_info) |
---|
[97] | 531 | { |
---|
[166] | 532 | int32_t * servSock; |
---|
| 533 | int32_t running = 1; |
---|
[97] | 534 | struct timeval selTimeout; |
---|
[166] | 535 | int32_t timeout = 10; |
---|
| 536 | int32_t cognitive_engine = 0; |
---|
| 537 | int32_t policy_engine = 1; |
---|
| 538 | int32_t port, rc, on = 1; |
---|
| 539 | int32_t new_sd; |
---|
| 540 | int32_t desc_ready = 0; |
---|
[97] | 541 | fd_set sockSet; |
---|
| 542 | |
---|
[166] | 543 | servSock = (int32_t *) malloc(2 * sizeof(int32_t)); |
---|
[97] | 544 | |
---|
| 545 | servSock[cognitive_engine] = CreateTCPServerSocket(CE_SERVER_PORT); |
---|
[166] | 546 | servSock[policy_engine] = CreateTCPServerSocket(PE_SERVER_PORT); |
---|
[97] | 547 | |
---|
| 548 | |
---|
[166] | 549 | int32_t maxDescriptor = servSock[cognitive_engine]; |
---|
[97] | 550 | |
---|
| 551 | if(servSock[cognitive_engine] < servSock[policy_engine]) |
---|
| 552 | maxDescriptor = servSock[policy_engine]; |
---|
| 553 | |
---|
| 554 | rc = setsockopt(servSock[cognitive_engine], SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)); |
---|
| 555 | if(rc < 0) |
---|
| 556 | { |
---|
| 557 | perror("setsockopt() failed"); |
---|
| 558 | close(servSock[cognitive_engine]); |
---|
| 559 | exit(-1); |
---|
| 560 | } |
---|
| 561 | |
---|
| 562 | rc = setsockopt(servSock[policy_engine], SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)); |
---|
| 563 | if(rc < 0) |
---|
| 564 | { |
---|
| 565 | perror("setsockopt() failed"); |
---|
| 566 | close(servSock[policy_engine]); |
---|
| 567 | exit(-1); |
---|
| 568 | } |
---|
| 569 | |
---|
| 570 | rc = ioctl(servSock[cognitive_engine], FIONBIO, (char*)&on); |
---|
| 571 | if(rc < 0) |
---|
| 572 | { |
---|
| 573 | perror("ioctl() failed"); |
---|
| 574 | close(servSock[cognitive_engine]); |
---|
| 575 | exit(-1); |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | rc = ioctl(servSock[policy_engine], FIONBIO, (char*)&on); |
---|
| 579 | if(rc < 0) |
---|
| 580 | { |
---|
| 581 | perror("ioctl() failed"); |
---|
| 582 | close(servSock[policy_engine]); |
---|
| 583 | exit(-1); |
---|
| 584 | } |
---|
| 585 | |
---|
[167] | 586 | printf("Starting server: Hit return to shutdown\n"); |
---|
[97] | 587 | while (running) |
---|
| 588 | { |
---|
| 589 | /* Zero socket descriptor vector and set for server sockets */ |
---|
| 590 | /* This must be reset every time select() is called */ |
---|
| 591 | FD_ZERO(&sockSet); |
---|
| 592 | /* Add keyboard to descriptor vector */ |
---|
| 593 | FD_SET(STDIN_FILENO, &sockSet); |
---|
| 594 | FD_SET(servSock[cognitive_engine], &sockSet); |
---|
| 595 | FD_SET(servSock[policy_engine], &sockSet); |
---|
| 596 | |
---|
| 597 | /* Timeout specification */ |
---|
| 598 | /* This must be reset every time select() is called */ |
---|
| 599 | selTimeout.tv_sec = timeout; /* timeout (secs.) */ |
---|
| 600 | selTimeout.tv_usec = 0; /* 0 microseconds */ |
---|
| 601 | |
---|
| 602 | /* Suspend program until descriptor is ready or timeout */ |
---|
| 603 | rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
| 604 | if (rc == 0) |
---|
[167] | 605 | printf("No echo requests for %i secs...Server still alive\n", timeout); |
---|
[97] | 606 | else |
---|
| 607 | { |
---|
| 608 | if (FD_ISSET(0, &sockSet)) /* Check keyboard */ |
---|
| 609 | { |
---|
[167] | 610 | printf("Shutting down server\n"); |
---|
[97] | 611 | getchar(); |
---|
| 612 | running = 0; |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | desc_ready = rc; |
---|
| 616 | |
---|
| 617 | for (port = 0; port <= maxDescriptor && desc_ready > 0; port++) { |
---|
| 618 | if (FD_ISSET(port, &sockSet)) |
---|
| 619 | { |
---|
[167] | 620 | printf("Request on port %d: ", port); |
---|
[97] | 621 | desc_ready -= 1; |
---|
| 622 | |
---|
| 623 | |
---|
| 624 | if( (port == servSock[cognitive_engine]) || (port == servSock[policy_engine])) { |
---|
| 625 | |
---|
| 626 | do |
---|
| 627 | { |
---|
| 628 | new_sd = AcceptTCPConnection(port); |
---|
| 629 | if(new_sd < 0) |
---|
| 630 | { |
---|
| 631 | break; |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | HandleTCPClient(new_sd, uList, pList, oList, ce_info); |
---|
| 635 | FD_SET(new_sd,&sockSet); |
---|
| 636 | if(new_sd > maxDescriptor) |
---|
| 637 | maxDescriptor = new_sd; |
---|
[167] | 638 | printf("New incoming connection - %i\n\n",new_sd); |
---|
[97] | 639 | } while(new_sd != -1); |
---|
| 640 | } else { |
---|
| 641 | |
---|
[167] | 642 | printf("Request on already open descriptor.\n\n"); |
---|
[97] | 643 | HandleTCPClient(port, uList, pList, oList, ce_info); |
---|
| 644 | |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | } |
---|
| 648 | } |
---|
| 649 | } |
---|
| 650 | } |
---|
| 651 | |
---|
| 652 | /* Close sockets */ |
---|
| 653 | for (port = 0; port < 2; port++) |
---|
| 654 | close(servSock[port]); |
---|
| 655 | |
---|
| 656 | /* Free list of sockets */ |
---|
| 657 | free(servSock); |
---|
| 658 | |
---|
[63] | 659 | return 0; |
---|
[35] | 660 | } |
---|
| 661 | |
---|
[166] | 662 | int32_t |
---|
| 663 | main(int32_t argc, char* argv[]) |
---|
| 664 | { |
---|
[35] | 665 | // CognitiveEngine CE; |
---|
| 666 | // CognitiveEngineShell Shell; |
---|
| 667 | string pFilename; |
---|
[166] | 668 | int32_t fd; |
---|
[35] | 669 | |
---|
| 670 | Utility * uList[10]; |
---|
| 671 | Parameter * pList[10]; |
---|
| 672 | Observable * oList[10]; |
---|
[97] | 673 | struct CE_Info *ce_info; |
---|
[35] | 674 | |
---|
[97] | 675 | if((fd = open("/dev/zero", O_RDWR)) == -1) |
---|
| 676 | return 1; |
---|
| 677 | |
---|
| 678 | ce_info = (struct CE_Info *)mmap(0,sizeof(CE_Info),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); |
---|
| 679 | |
---|
| 680 | close(fd); |
---|
| 681 | |
---|
[35] | 682 | if(argc < 2) { |
---|
| 683 | cout << "Warning no XML file specific using default: example.xml" << endl; |
---|
| 684 | pFilename = "example.xml"; |
---|
| 685 | } else { |
---|
| 686 | pFilename = argv[1]; |
---|
| 687 | } |
---|
| 688 | |
---|
| 689 | TiXmlDocument doc( pFilename.c_str() ); |
---|
| 690 | bool loadOkay = doc.LoadFile(); |
---|
| 691 | if (!loadOkay) |
---|
| 692 | { |
---|
| 693 | cout << "Loading " << pFilename << " failed." << endl; |
---|
| 694 | return 0; |
---|
| 695 | } |
---|
| 696 | |
---|
[95] | 697 | cout << "\n\nInitialize:: Attemping to parse " << pFilename << "." << endl; |
---|
[97] | 698 | parse_ce_config( &doc , uList, pList, oList, ce_info); |
---|
[95] | 699 | cout << "Initialize:: Configuration file parsing completed.\n" << endl; |
---|
[35] | 700 | |
---|
[167] | 701 | //print_current_config(uList, pList, oList, &ce_info); |
---|
[69] | 702 | |
---|
[97] | 703 | StartServers(uList, pList, oList, ce_info); |
---|
[35] | 704 | return 1; |
---|
[71] | 705 | } |
---|