[207] | 1 | /* Virginia Tech Cognitive Radio Open Source Systems |
---|
| 2 | * Virginia Tech, 2009 |
---|
| 3 | * |
---|
| 4 | * LICENSE INFORMATION GOES HERE |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /* DESCRIPTION OF FILE. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | #include <cstdlib> |
---|
| 12 | #include <cstring> |
---|
| 13 | #include <stdint.h> |
---|
| 14 | |
---|
| 15 | #include <arpa/inet.h> |
---|
| 16 | #include <iostream> |
---|
| 17 | #include <netinet/in.h> |
---|
| 18 | #include <netdb.h> |
---|
| 19 | #include <fcntl.h> |
---|
| 20 | #include <sys/ioctl.h> |
---|
| 21 | #include <sys/mman.h> |
---|
| 22 | #include <sys/socket.h> |
---|
| 23 | #include <sys/types.h> |
---|
| 24 | #include <sys/wait.h> |
---|
| 25 | |
---|
| 26 | #include "tinyxml/tinyxml.h" |
---|
| 27 | #include "tinyxml/tinystr.h" |
---|
| 28 | |
---|
| 29 | #include "vtcross/common.h" |
---|
| 30 | #include "vtcross/components.h" |
---|
| 31 | #include "vtcross/containers.h" |
---|
| 32 | #include "vtcross/debug.h" |
---|
| 33 | #include "vtcross/error.h" |
---|
| 34 | #include "vtcross/socketcomm.h" |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | CognitiveRadioShell::CognitiveRadioShell() |
---|
| 38 | { |
---|
| 39 | LOG("Creating Cognitive Radio Shell.\n"); |
---|
| 40 | SML_present = false; |
---|
| 41 | PE_present = false; |
---|
| 42 | CE_present = false; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | CognitiveRadioShell::~CognitiveRadioShell() |
---|
| 47 | { |
---|
| 48 | delete [] p; |
---|
| 49 | delete [] o; |
---|
| 50 | delete [] u; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | CognitiveRadioShell::CognitiveRadioShell(const char* radioConfig, int16_t p1, \ |
---|
| 55 | int16_t p2, int16_t p3) |
---|
| 56 | { |
---|
| 57 | LOG("Creating Cognitive Radio Shell.\n"); |
---|
| 58 | |
---|
| 59 | p = new Parameter[10]; |
---|
| 60 | o = new Observable[10]; |
---|
| 61 | u = new Utility[10]; |
---|
| 62 | radio_info = new Radio_Info; |
---|
| 63 | |
---|
| 64 | LoadRadioConfiguration(radioConfig,p,u,o,radio_info); |
---|
| 65 | |
---|
| 66 | primaryPort = p1; |
---|
| 67 | policyPort = p2; |
---|
| 68 | commandPort = p3; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | void |
---|
| 73 | CognitiveRadioShell::SendComponentType(int32_t socketFD) |
---|
| 74 | { |
---|
| 75 | SendMessage(socketFD, "response_shell"); |
---|
| 76 | LOG("Cognitive Radio Shell responded to GetRemoteComponentType query.\n"); |
---|
| 77 | } |
---|
| 78 | |
---|
[209] | 79 | |
---|
[207] | 80 | void |
---|
| 81 | CognitiveRadioShell::Shutdown() |
---|
| 82 | { |
---|
| 83 | // TODO should something else be happening here? |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | void |
---|
| 88 | CognitiveRadioShell::Reset() |
---|
| 89 | { |
---|
| 90 | LOG("Resetting Cognitive Radio Shell.\n"); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | void |
---|
| 95 | CognitiveRadioShell::SendRadioConfiguration(int32_t socketFD) |
---|
| 96 | { |
---|
| 97 | LOG("Cognitive Radio Shell:: Sending radio configuration to Cognitive Engine.\n"); |
---|
| 98 | } |
---|
| 99 | |
---|
[209] | 100 | |
---|
[207] | 101 | void |
---|
| 102 | CognitiveRadioShell::SendRadioExperience(int32_t socketFD) |
---|
| 103 | { |
---|
| 104 | |
---|
| 105 | LOG("Cognitive Radio Shell:: Sending radio experience to Cognitive Engine.\n"); |
---|
| 106 | } |
---|
| 107 | |
---|
[209] | 108 | |
---|
[207] | 109 | void |
---|
| 110 | CognitiveRadioShell::RegisterCognitiveEngine(int32_t socketFD) |
---|
| 111 | { |
---|
| 112 | LOG("Cognitive Radio Shell:: Received registration message from Cognitive Engine.\n"); |
---|
| 113 | |
---|
| 114 | SendRadioConfiguration(socketFD); |
---|
| 115 | SendRadioExperience(socketFD); |
---|
| 116 | |
---|
| 117 | numberOfCognitiveEngines++; |
---|
| 118 | CE_present = true; |
---|
| 119 | } |
---|
| 120 | |
---|
[209] | 121 | |
---|
[207] | 122 | void |
---|
| 123 | CognitiveRadioShell::DeregisterCognitiveEngine(int32_t socketFD) |
---|
| 124 | { |
---|
| 125 | LOG("Cognitive Radio Shell:: Received deregistration message from Cognitive Engine.\n"); |
---|
| 126 | |
---|
| 127 | numberOfCognitiveEngines--; |
---|
| 128 | if(numberOfCognitiveEngines == 0) |
---|
[209] | 129 | CE_present = false; |
---|
[207] | 130 | |
---|
| 131 | SendMessage(socketFD, "deregister_ack"); |
---|
| 132 | shutdown(socketFD, 2); |
---|
| 133 | close(socketFD); |
---|
| 134 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
| 135 | } |
---|
| 136 | |
---|
[209] | 137 | |
---|
[207] | 138 | void |
---|
| 139 | CognitiveRadioShell::RegisterPolicyEngine(int32_t socketFD) |
---|
| 140 | { |
---|
| 141 | LOG("Cognitive Radio Shell:: Received registration message from Policy Engine.\n"); |
---|
| 142 | PE_present = true; |
---|
| 143 | } |
---|
| 144 | |
---|
[209] | 145 | |
---|
[207] | 146 | void |
---|
| 147 | CognitiveRadioShell::DeregisterPolicyEngine(int32_t socketFD) |
---|
| 148 | { |
---|
| 149 | LOG("Cognitive Radio Shell:: Received deregistration message from Policy Engine.\n"); |
---|
| 150 | |
---|
| 151 | PE_present = false; |
---|
| 152 | |
---|
| 153 | SendMessage(socketFD, "deregister_ack"); |
---|
| 154 | shutdown(socketFD, 2); |
---|
| 155 | close(socketFD); |
---|
| 156 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
| 157 | } |
---|
| 158 | |
---|
[209] | 159 | |
---|
[207] | 160 | void |
---|
| 161 | CognitiveRadioShell::RegisterSML(int32_t socketFD) |
---|
| 162 | { |
---|
| 163 | LOG("Cognitive Radio Shell:: Received registration message from SML.\n"); |
---|
| 164 | |
---|
| 165 | SML_present = true; |
---|
| 166 | } |
---|
| 167 | |
---|
[209] | 168 | |
---|
[207] | 169 | void |
---|
| 170 | CognitiveRadioShell::DeregisterSML(int32_t socketFD) |
---|
| 171 | { |
---|
| 172 | LOG("Cognitive Radio Shell:: Received deregistration message from SML.\n"); |
---|
| 173 | |
---|
| 174 | SML_present = false; |
---|
| 175 | |
---|
| 176 | SendMessage(socketFD, "deregister_ack"); |
---|
| 177 | shutdown(socketFD, 2); |
---|
| 178 | close(socketFD); |
---|
| 179 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
| 180 | } |
---|
| 181 | |
---|
[209] | 182 | |
---|
[207] | 183 | int32_t |
---|
[209] | 184 | CognitiveRadioShell::LoadRadioConfiguration(const char* radioConfig, \ |
---|
| 185 | Parameter* &pList, Utility* &uList, Observable* &oList, \ |
---|
| 186 | Radio_Info* radioInfo) |
---|
[207] | 187 | { |
---|
| 188 | TiXmlElement *pElem; |
---|
| 189 | TiXmlElement *pChild; |
---|
| 190 | TiXmlElement *pChild1; |
---|
| 191 | TiXmlElement *pSecondChild; |
---|
| 192 | TiXmlHandle hRoot(0); |
---|
| 193 | |
---|
| 194 | int32_t count = 0; |
---|
| 195 | size_t item_count = 0; |
---|
| 196 | size_t affect_count = 0; |
---|
| 197 | int32_t attribute_count = 0; |
---|
| 198 | bool match_found = false; |
---|
| 199 | |
---|
| 200 | LOG("Cognitive Radio Shell:: Loading radio configuration.\n"); |
---|
| 201 | |
---|
| 202 | TiXmlDocument doc( radioConfig ); |
---|
| 203 | bool loadOkay = doc.LoadFile(); |
---|
[209] | 204 | if(!loadOkay) { |
---|
[207] | 205 | ERROR(1,"Loading radio configuration failed: %s\n", radioConfig); |
---|
| 206 | return -1; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | TiXmlHandle hDoc(&doc); |
---|
| 210 | |
---|
| 211 | pElem = hDoc.FirstChildElement().Element(); |
---|
| 212 | |
---|
| 213 | if(!pElem) |
---|
| 214 | ERROR(1, "No valid root!"); |
---|
| 215 | |
---|
| 216 | hRoot = TiXmlHandle(pElem); |
---|
| 217 | |
---|
| 218 | pElem = hRoot.FirstChild("utilities").Element(); |
---|
| 219 | pChild1 = hRoot.Child("utilities", count).Element(); |
---|
| 220 | |
---|
| 221 | for(pChild = pChild1->FirstChildElement("utility"); pChild; \ |
---|
[209] | 222 | pChild = pChild->NextSiblingElement()) { |
---|
| 223 | |
---|
[207] | 224 | const char *uName = pChild->Attribute("name"); |
---|
| 225 | if(uName) |
---|
[208] | 226 | uList[item_count].name = uName; |
---|
[207] | 227 | |
---|
| 228 | const char *uUnits = pChild->Attribute("units"); |
---|
| 229 | if(uUnits) |
---|
| 230 | uList[item_count].units = uUnits; |
---|
| 231 | |
---|
| 232 | const char *uGoal = pChild->Attribute("goal"); |
---|
| 233 | if(uGoal) |
---|
| 234 | uList[item_count].goal = uGoal; |
---|
| 235 | |
---|
| 236 | if(pChild->QueryFloatAttribute("target", &uList[item_count].target) != TIXML_SUCCESS) |
---|
| 237 | uList[item_count].target = -1; |
---|
| 238 | |
---|
| 239 | item_count++; |
---|
| 240 | } |
---|
| 241 | |
---|
[208] | 242 | radio_info->numUtilities = item_count; |
---|
[207] | 243 | LOG("Cognitive Radio Shell:: Parsed %d utilities.\n", radioInfo->numUtilities); |
---|
| 244 | |
---|
| 245 | item_count = 0; |
---|
| 246 | pElem = hRoot.FirstChild("observables").Element(); |
---|
| 247 | pChild1 = hRoot.Child("observables", count).Element(); |
---|
| 248 | |
---|
| 249 | for(pChild = pChild1->FirstChildElement("observable"); pChild; \ |
---|
[209] | 250 | pChild = pChild->NextSiblingElement()) { |
---|
| 251 | |
---|
[207] | 252 | const char *oName = pChild->Attribute("name"); |
---|
| 253 | if(oName) |
---|
| 254 | oList[item_count].name = oName; |
---|
[208] | 255 | |
---|
[207] | 256 | affect_count = 0; |
---|
| 257 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ |
---|
[209] | 258 | pSecondChild = pSecondChild->NextSiblingElement()) { |
---|
| 259 | |
---|
[207] | 260 | const char *oUtilName = pSecondChild->Attribute("utility"); |
---|
[209] | 261 | if(oUtilName) { |
---|
| 262 | for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++ ) { |
---|
| 263 | if(uList[attribute_count].name == oUtilName) { |
---|
| 264 | |
---|
[207] | 265 | oList[item_count].affection_list[affect_count].u = &uList[attribute_count]; |
---|
| 266 | const char *oRelate = pSecondChild->Attribute("relationship"); |
---|
| 267 | if(oRelate) |
---|
| 268 | oList[item_count].affection_list[affect_count].relation = oRelate; |
---|
[209] | 269 | |
---|
[207] | 270 | affect_count++; |
---|
| 271 | match_found = true; |
---|
| 272 | break; |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | if(!match_found) { |
---|
| 278 | ERROR(1, "Error: %s: %s is not a valid utility.\n", \ |
---|
| 279 | oList[item_count].name.c_str(), oUtilName); |
---|
| 280 | } |
---|
| 281 | else |
---|
[208] | 282 | match_found = false; |
---|
[207] | 283 | } |
---|
| 284 | oList[item_count].numAffects = affect_count; |
---|
| 285 | item_count++; |
---|
| 286 | } |
---|
| 287 | |
---|
[208] | 288 | radioInfo->numObservables = item_count; |
---|
[207] | 289 | LOG("Cognitive Radio Shell:: Parsed %d observables.\n", radioInfo->numObservables); |
---|
| 290 | |
---|
| 291 | pElem = hRoot.FirstChild("parameters").Element(); |
---|
| 292 | pChild1 = hRoot.Child("parameters", count).Element(); |
---|
[208] | 293 | |
---|
[207] | 294 | item_count = 0; |
---|
| 295 | for(pChild = pChild1->FirstChildElement("parameter"); pChild; \ |
---|
[209] | 296 | pChild = pChild->NextSiblingElement()) { |
---|
| 297 | |
---|
[207] | 298 | const char *pName = pChild->Attribute("name"); |
---|
| 299 | if(pName) |
---|
[208] | 300 | pList[item_count].name = pName; |
---|
[207] | 301 | |
---|
| 302 | const char *pUnits = pChild->Attribute("units"); |
---|
| 303 | if(pUnits) |
---|
| 304 | pList[item_count].units = pUnits; |
---|
| 305 | |
---|
| 306 | if(pChild->QueryFloatAttribute("min", &pList[item_count].min) != TIXML_SUCCESS) |
---|
| 307 | pList[item_count].min = -1; |
---|
| 308 | |
---|
| 309 | if(pChild->QueryFloatAttribute("max", &pList[item_count].max) != TIXML_SUCCESS) |
---|
| 310 | pList[item_count].max = -1; |
---|
| 311 | |
---|
| 312 | if(pChild->QueryFloatAttribute("step", &pList[item_count].step) != TIXML_SUCCESS) |
---|
| 313 | pList[item_count].step = -1; |
---|
[208] | 314 | |
---|
[207] | 315 | affect_count = 0; |
---|
| 316 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ |
---|
[209] | 317 | |
---|
| 318 | pSecondChild = pSecondChild->NextSiblingElement()) { |
---|
[207] | 319 | const char *pUtilName = pSecondChild->Attribute("utility"); |
---|
[209] | 320 | if(pUtilName) { |
---|
| 321 | for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++) { |
---|
| 322 | if(uList[attribute_count].name == pUtilName) { |
---|
[208] | 323 | pList[item_count].affection_list[affect_count].u = &uList[attribute_count]; |
---|
[207] | 324 | |
---|
| 325 | const char *pRelate = pSecondChild->Attribute("relationship"); |
---|
| 326 | if(pRelate) |
---|
| 327 | pList[item_count].affection_list[affect_count].relation = pRelate; |
---|
| 328 | else |
---|
| 329 | LOG("Error: No relation found.\n"); |
---|
| 330 | |
---|
| 331 | match_found = true; |
---|
| 332 | affect_count++; |
---|
| 333 | break; |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | } |
---|
| 337 | |
---|
[209] | 338 | if(!match_found) { |
---|
[207] | 339 | ERROR(1, "Error: %s: %s is not a valid utility.\n", \ |
---|
| 340 | pList[item_count].name.c_str(), pUtilName); |
---|
| 341 | } |
---|
| 342 | |
---|
[208] | 343 | match_found = false; |
---|
[207] | 344 | } |
---|
| 345 | |
---|
| 346 | pList[item_count].numAffects = affect_count; |
---|
| 347 | item_count++; |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | radioInfo->numParameters = item_count; |
---|
| 351 | LOG("Cognitive Radio Shell:: Parsed %d parameters.\n", radioInfo->numParameters); |
---|
| 352 | |
---|
| 353 | return 1; |
---|
| 354 | |
---|
| 355 | } |
---|
| 356 | |
---|
[209] | 357 | |
---|
[207] | 358 | void |
---|
| 359 | CognitiveRadioShell::GetOptimalParameters(int32_t socketFD) |
---|
| 360 | { |
---|
| 361 | } |
---|
| 362 | |
---|
[209] | 363 | |
---|
[207] | 364 | void |
---|
| 365 | CognitiveRadioShell::HandleMessage(int32_t socketFD) |
---|
| 366 | { |
---|
| 367 | char buffer[256]; |
---|
| 368 | |
---|
| 369 | ReadMessage(socketFD, buffer); |
---|
| 370 | |
---|
| 371 | if(strcmp(buffer,"register_engine_cognitive") == 0) { |
---|
[208] | 372 | RegisterCognitiveEngine(socketFD); |
---|
[207] | 373 | } else if(strcmp(buffer,"deregister_engine_cognitive") == 0) { |
---|
[208] | 374 | DeregisterCognitiveEngine(socketFD); |
---|
[207] | 375 | } else if(strcmp(buffer,"register_engine_policy") == 0) { |
---|
[208] | 376 | RegisterPolicyEngine(socketFD); |
---|
[207] | 377 | } else if(strcmp(buffer,"deregister_engine_policy") == 0) { |
---|
[208] | 378 | DeregisterPolicyEngine(socketFD); |
---|
[207] | 379 | } else if(strcmp(buffer,"register_sml") == 0) { |
---|
[208] | 380 | RegisterSML(socketFD); |
---|
[207] | 381 | } else if(strcmp(buffer,"deregister_sml") == 0) { |
---|
[208] | 382 | DeregisterSML(socketFD); |
---|
[207] | 383 | } else if(strcmp(buffer,"optimize") == 0) { |
---|
[208] | 384 | /* Receive optimization request and current environment */ |
---|
| 385 | GetOptimalParameters(socketFD); |
---|
[207] | 386 | } |
---|
| 387 | } |
---|
| 388 | |
---|
[209] | 389 | |
---|
[207] | 390 | void |
---|
| 391 | CognitiveRadioShell::StartShellServer() |
---|
| 392 | { |
---|
| 393 | int32_t * servSock; |
---|
| 394 | struct timeval selTimeout; |
---|
| 395 | int32_t primary = 0; |
---|
| 396 | int32_t policy = 1; |
---|
| 397 | int32_t command = 2; |
---|
| 398 | int32_t running = 1; |
---|
| 399 | int32_t port, rc, new_sd = 1; |
---|
| 400 | int32_t desc_ready = 1; |
---|
| 401 | int32_t timeout = 10; |
---|
| 402 | fd_set sockSet; |
---|
| 403 | |
---|
| 404 | servSock = (int32_t *) malloc(3 * sizeof(int32_t)); |
---|
| 405 | |
---|
| 406 | servSock[primary] = CreateTCPServerSocket(primaryPort); |
---|
| 407 | servSock[policy] = CreateTCPServerSocket(policyPort); |
---|
| 408 | servSock[command] = CreateTCPServerSocket(commandPort); |
---|
| 409 | |
---|
| 410 | int32_t maxDescriptor = servSock[command]; |
---|
| 411 | |
---|
| 412 | if(InitializeTCPServerPort(servSock[primary]) == -1) { |
---|
[208] | 413 | ERROR(1,"Error initializing primary port\n"); |
---|
| 414 | return; |
---|
[207] | 415 | } |
---|
| 416 | |
---|
| 417 | if(InitializeTCPServerPort(servSock[policy]) == -1) { |
---|
[208] | 418 | ERROR(1,"Error initializing policy port\n"); |
---|
| 419 | return; |
---|
[207] | 420 | } |
---|
| 421 | |
---|
| 422 | if(InitializeTCPServerPort(servSock[command]) == -1) { |
---|
[208] | 423 | ERROR(1,"Error initializing command port\n"); |
---|
| 424 | return; |
---|
[207] | 425 | } |
---|
| 426 | |
---|
[209] | 427 | while (running) { |
---|
[207] | 428 | /* Zero socket descriptor vector and set for server sockets */ |
---|
| 429 | /* This must be reset every time select() is called */ |
---|
| 430 | FD_ZERO(&sockSet); |
---|
| 431 | FD_SET(servSock[primary], &sockSet); |
---|
| 432 | FD_SET(servSock[policy], &sockSet); |
---|
| 433 | FD_SET(servSock[command], &sockSet); |
---|
| 434 | |
---|
| 435 | /* Timeout specification */ |
---|
| 436 | /* This must be reset every time select() is called */ |
---|
| 437 | selTimeout.tv_sec = timeout; /* timeout (secs.) */ |
---|
| 438 | selTimeout.tv_usec = 0; /* 0 microseconds */ |
---|
| 439 | |
---|
| 440 | /* Suspend program until descriptor is ready or timeout */ |
---|
| 441 | rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
[209] | 442 | if(rc == 0) |
---|
[207] | 443 | LOG("No echo requests for %i secs...Server still alive\n", timeout); |
---|
[209] | 444 | else { |
---|
[207] | 445 | desc_ready = rc; |
---|
| 446 | |
---|
[209] | 447 | for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) { |
---|
| 448 | if(FD_ISSET(port, &sockSet)) { |
---|
| 449 | desc_ready -= 1; |
---|
[207] | 450 | |
---|
| 451 | /* Check if request is new or on an existing open descriptor */ |
---|
[209] | 452 | if((port == servSock[primary]) || (port == servSock[policy]) || (port == servSock[command])) { |
---|
| 453 | do { |
---|
[207] | 454 | new_sd = AcceptTCPConnection(port); |
---|
| 455 | if(new_sd < 0) |
---|
| 456 | break; |
---|
| 457 | |
---|
| 458 | HandleMessage(new_sd); |
---|
| 459 | FD_SET(new_sd,&sockSet); |
---|
| 460 | if(new_sd > maxDescriptor) |
---|
| 461 | maxDescriptor = new_sd; |
---|
| 462 | //LOG("New incoming connection - %i\n\n",new_sd); |
---|
| 463 | } while(new_sd != -1); |
---|
[209] | 464 | } |
---|
| 465 | else { |
---|
[207] | 466 | //LOG("Request on already open descriptor.\n\n"); |
---|
| 467 | HandleMessage(port); |
---|
| 468 | } |
---|
| 469 | } |
---|
| 470 | } |
---|
| 471 | } |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | |
---|
| 475 | /* Close sockets */ |
---|
| 476 | close(servSock[primary]); |
---|
| 477 | close(servSock[policy]); |
---|
| 478 | close(servSock[command]); |
---|
| 479 | |
---|
| 480 | /* Free list of sockets */ |
---|
| 481 | free(servSock); |
---|
| 482 | |
---|
| 483 | return; |
---|
| 484 | } |
---|