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