[411] | 1 | /* |
---|
| 2 | Copyright 2009 Virginia Polytechnic Institute and State University |
---|
[207] | 3 | |
---|
[411] | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 5 | you may not use this file except in compliance with the License. |
---|
| 6 | You may obtain a copy of the License at |
---|
| 7 | |
---|
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 9 | |
---|
| 10 | Unless required by applicable law or agreed to in writing, software |
---|
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 13 | See the License for the specific language governing permissions and |
---|
| 14 | limitations under the License. |
---|
| 15 | */ |
---|
| 16 | |
---|
[207] | 17 | /* DESCRIPTION OF FILE. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | #include <cstdlib> |
---|
| 22 | #include <cstring> |
---|
| 23 | #include <stdint.h> |
---|
[212] | 24 | #include <string> |
---|
[207] | 25 | |
---|
| 26 | #include <arpa/inet.h> |
---|
| 27 | #include <iostream> |
---|
| 28 | #include <netinet/in.h> |
---|
| 29 | #include <netdb.h> |
---|
| 30 | #include <fcntl.h> |
---|
| 31 | #include <sys/ioctl.h> |
---|
| 32 | #include <sys/mman.h> |
---|
| 33 | #include <sys/socket.h> |
---|
| 34 | #include <sys/types.h> |
---|
| 35 | #include <sys/wait.h> |
---|
| 36 | |
---|
| 37 | #include "tinyxml/tinyxml.h" |
---|
| 38 | #include "tinyxml/tinystr.h" |
---|
| 39 | |
---|
| 40 | #include "vtcross/common.h" |
---|
| 41 | #include "vtcross/components.h" |
---|
| 42 | #include "vtcross/containers.h" |
---|
| 43 | #include "vtcross/debug.h" |
---|
| 44 | #include "vtcross/error.h" |
---|
| 45 | #include "vtcross/socketcomm.h" |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | CognitiveRadioShell::CognitiveRadioShell() |
---|
| 49 | { |
---|
| 50 | LOG("Creating Cognitive Radio Shell.\n"); |
---|
| 51 | SML_present = false; |
---|
| 52 | PE_present = false; |
---|
| 53 | CE_present = false; |
---|
[213] | 54 | |
---|
| 55 | // TODO =BUG= The params, observables, and utils arrays are not being |
---|
| 56 | // allocated here. If an shell object is constructed, and then immediately |
---|
| 57 | // destructed, there will be no allocated memory, and the delete operater |
---|
| 58 | // will receive a null pointer, which will segfault. |
---|
[207] | 59 | } |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | CognitiveRadioShell::~CognitiveRadioShell() |
---|
| 63 | { |
---|
[213] | 64 | delete [] params; |
---|
| 65 | delete [] observables; |
---|
| 66 | delete [] utils; |
---|
[207] | 67 | } |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | CognitiveRadioShell::CognitiveRadioShell(const char* radioConfig, int16_t p1, \ |
---|
| 71 | int16_t p2, int16_t p3) |
---|
| 72 | { |
---|
| 73 | LOG("Creating Cognitive Radio Shell.\n"); |
---|
| 74 | |
---|
[213] | 75 | params = new Parameter[10]; |
---|
| 76 | observables = new Observable[10]; |
---|
| 77 | utils = new Utility[10]; |
---|
[207] | 78 | radio_info = new Radio_Info; |
---|
| 79 | |
---|
[213] | 80 | LoadRadioConfiguration(radioConfig, params, utils, observables, radio_info); |
---|
[207] | 81 | |
---|
| 82 | primaryPort = p1; |
---|
| 83 | policyPort = p2; |
---|
| 84 | commandPort = p3; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | void |
---|
| 89 | CognitiveRadioShell::SendComponentType(int32_t socketFD) |
---|
| 90 | { |
---|
| 91 | SendMessage(socketFD, "response_shell"); |
---|
| 92 | LOG("Cognitive Radio Shell responded to GetRemoteComponentType query.\n"); |
---|
| 93 | } |
---|
| 94 | |
---|
[209] | 95 | |
---|
[212] | 96 | std::string |
---|
| 97 | CognitiveRadioShell::GetRemoteComponentType(int32_t socketFD) |
---|
| 98 | { |
---|
| 99 | SendMessage(socketFD, "request_component_type"); |
---|
| 100 | |
---|
| 101 | char buffer[256]; |
---|
| 102 | memset(buffer, 0, 256); |
---|
| 103 | ReadMessage(socketFD, buffer); |
---|
| 104 | |
---|
| 105 | return std::string(buffer); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | |
---|
[207] | 109 | void |
---|
| 110 | CognitiveRadioShell::Shutdown() |
---|
| 111 | { |
---|
| 112 | // TODO should something else be happening here? |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | void |
---|
| 117 | CognitiveRadioShell::Reset() |
---|
| 118 | { |
---|
| 119 | LOG("Resetting Cognitive Radio Shell.\n"); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
[231] | 123 | // TODO what is the point of always returning a 1? |
---|
[218] | 124 | bool |
---|
[231] | 125 | CognitiveRadioShell::SendRadioConfiguration(int32_t socketFD) |
---|
[207] | 126 | { |
---|
| 127 | LOG("Cognitive Radio Shell:: Sending radio configuration to Cognitive Engine.\n"); |
---|
[231] | 128 | |
---|
[218] | 129 | char counter[55]; |
---|
| 130 | char var[50]; |
---|
| 131 | |
---|
[231] | 132 | /* Send utilities */ |
---|
| 133 | sprintf(counter, "%d", radio_info->numUtilities); |
---|
| 134 | SendMessage(socketFD, counter); |
---|
| 135 | for(size_t i = 0; i < radio_info->numUtilities; i++) { |
---|
| 136 | SendMessage(socketFD, utils[i].name.c_str()); |
---|
| 137 | SendMessage(socketFD, utils[i].units.c_str()); |
---|
| 138 | SendMessage(socketFD, utils[i].goal.c_str()); |
---|
[222] | 139 | sprintf(var,"%f", utils[i].target); |
---|
[231] | 140 | SendMessage(socketFD, var); |
---|
[218] | 141 | } |
---|
| 142 | |
---|
[231] | 143 | /* Send parameters */ |
---|
[218] | 144 | sprintf(counter,"%i",radio_info->numParameters); |
---|
[231] | 145 | SendMessage(socketFD,counter); |
---|
| 146 | for(size_t i = 0; i < radio_info->numParameters; i++) { |
---|
| 147 | SendMessage(socketFD, params[i].name.c_str()); |
---|
| 148 | SendMessage(socketFD, params[i].units.c_str()); |
---|
[227] | 149 | sprintf(var, "%f", params[i].min); |
---|
[231] | 150 | SendMessage(socketFD,var); |
---|
[227] | 151 | sprintf(var, "%f", params[i].max); |
---|
[231] | 152 | SendMessage(socketFD, var); |
---|
[227] | 153 | sprintf(var, "%f", params[i].step); |
---|
[231] | 154 | SendMessage(socketFD, var); |
---|
[218] | 155 | |
---|
[227] | 156 | sprintf(counter, "%i", params[i].numAffects); |
---|
[231] | 157 | SendMessage(socketFD, counter); |
---|
| 158 | for(size_t j = 0; j < params[i].numAffects; j++) { |
---|
| 159 | SendMessage(socketFD, params[i].affection_list[j].u->name.c_str()); |
---|
| 160 | SendMessage(socketFD, params[i].affection_list[j].relation.c_str()); |
---|
[218] | 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
[231] | 164 | /* Send observables */ |
---|
[218] | 165 | sprintf(counter,"%i",radio_info->numObservables); |
---|
[231] | 166 | SendMessage(socketFD, counter); |
---|
| 167 | for(size_t i = 0; i < radio_info->numObservables; i++) { |
---|
| 168 | SendMessage(socketFD, observables[i].name.c_str()); |
---|
[232] | 169 | |
---|
[227] | 170 | sprintf(counter, "%i", observables[i].numAffects); |
---|
[231] | 171 | SendMessage(socketFD, counter); |
---|
| 172 | for(size_t j = 0; j < observables[i].numAffects; j++) { |
---|
| 173 | SendMessage(socketFD, observables[i].affection_list[j].u->name.c_str()); |
---|
| 174 | SendMessage(socketFD, observables[i].affection_list[j].relation.c_str()); |
---|
[218] | 175 | } |
---|
| 176 | } |
---|
[232] | 177 | |
---|
[231] | 178 | /* Receive ACK for radio configuration */ |
---|
[218] | 179 | char buffer[256]; |
---|
| 180 | memset(buffer, 0, 256); |
---|
[231] | 181 | ReadMessage(socketFD, buffer); |
---|
[218] | 182 | |
---|
| 183 | if(strcmp(buffer, "receive_config_ack") != 0) { |
---|
[227] | 184 | LOG("Cognitive Radio Shell:: Unexpected response: %s\n", buffer); |
---|
[218] | 185 | return 0; |
---|
| 186 | } |
---|
[227] | 187 | |
---|
[218] | 188 | return 1; |
---|
[207] | 189 | } |
---|
| 190 | |
---|
[218] | 191 | bool |
---|
[207] | 192 | CognitiveRadioShell::SendRadioExperience(int32_t socketFD) |
---|
| 193 | { |
---|
| 194 | |
---|
| 195 | LOG("Cognitive Radio Shell:: Sending radio experience to Cognitive Engine.\n"); |
---|
[218] | 196 | int32_t numberExp = 4; |
---|
[232] | 197 | char numberExpString[50]; |
---|
[218] | 198 | |
---|
[285] | 199 | sprintf(numberExpString, "%d", numberExp); |
---|
| 200 | SendMessage(socketFD, "test"); //TODO modified |
---|
[222] | 201 | |
---|
[218] | 202 | char buffer[256]; |
---|
| 203 | memset(buffer, 0, 256); |
---|
| 204 | ReadMessage(socketFD, buffer); |
---|
| 205 | if(strcmp(buffer, "receive_exp_ack") != 0) { |
---|
[231] | 206 | // TODO perhaps this should be a WARNING instead of a LOG? |
---|
| 207 | LOG("Cognitive Radio Shell:: Unexpected response: %s\n", buffer); |
---|
[218] | 208 | return 0; |
---|
| 209 | } |
---|
[285] | 210 | //printf("done sending exp \n"); |
---|
[218] | 211 | return 1; |
---|
[207] | 212 | } |
---|
| 213 | |
---|
[209] | 214 | |
---|
[207] | 215 | void |
---|
| 216 | CognitiveRadioShell::RegisterCognitiveEngine(int32_t socketFD) |
---|
| 217 | { |
---|
| 218 | LOG("Cognitive Radio Shell:: Received registration message from Cognitive Engine.\n"); |
---|
| 219 | |
---|
[279] | 220 | SendMessage(socketFD, "register_ack"); |
---|
[207] | 221 | SendRadioConfiguration(socketFD); |
---|
| 222 | SendRadioExperience(socketFD); |
---|
| 223 | |
---|
| 224 | numberOfCognitiveEngines++; |
---|
| 225 | CE_present = true; |
---|
| 226 | } |
---|
| 227 | |
---|
[209] | 228 | |
---|
[207] | 229 | void |
---|
| 230 | CognitiveRadioShell::DeregisterCognitiveEngine(int32_t socketFD) |
---|
| 231 | { |
---|
| 232 | LOG("Cognitive Radio Shell:: Received deregistration message from Cognitive Engine.\n"); |
---|
| 233 | |
---|
| 234 | numberOfCognitiveEngines--; |
---|
| 235 | if(numberOfCognitiveEngines == 0) |
---|
[209] | 236 | CE_present = false; |
---|
[207] | 237 | |
---|
| 238 | SendMessage(socketFD, "deregister_ack"); |
---|
| 239 | shutdown(socketFD, 2); |
---|
| 240 | close(socketFD); |
---|
| 241 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
| 242 | } |
---|
| 243 | |
---|
[209] | 244 | |
---|
[207] | 245 | void |
---|
| 246 | CognitiveRadioShell::RegisterPolicyEngine(int32_t socketFD) |
---|
| 247 | { |
---|
| 248 | LOG("Cognitive Radio Shell:: Received registration message from Policy Engine.\n"); |
---|
| 249 | PE_present = true; |
---|
| 250 | } |
---|
| 251 | |
---|
[209] | 252 | |
---|
[207] | 253 | void |
---|
| 254 | CognitiveRadioShell::DeregisterPolicyEngine(int32_t socketFD) |
---|
| 255 | { |
---|
| 256 | LOG("Cognitive Radio Shell:: Received deregistration message from Policy Engine.\n"); |
---|
| 257 | |
---|
| 258 | PE_present = false; |
---|
| 259 | |
---|
| 260 | SendMessage(socketFD, "deregister_ack"); |
---|
| 261 | shutdown(socketFD, 2); |
---|
| 262 | close(socketFD); |
---|
| 263 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
| 264 | } |
---|
| 265 | |
---|
[209] | 266 | |
---|
[207] | 267 | void |
---|
| 268 | CognitiveRadioShell::RegisterSML(int32_t socketFD) |
---|
| 269 | { |
---|
| 270 | LOG("Cognitive Radio Shell:: Received registration message from SML.\n"); |
---|
| 271 | |
---|
| 272 | SML_present = true; |
---|
| 273 | } |
---|
| 274 | |
---|
[209] | 275 | |
---|
[207] | 276 | void |
---|
| 277 | CognitiveRadioShell::DeregisterSML(int32_t socketFD) |
---|
| 278 | { |
---|
| 279 | LOG("Cognitive Radio Shell:: Received deregistration message from SML.\n"); |
---|
| 280 | |
---|
| 281 | SML_present = false; |
---|
| 282 | |
---|
| 283 | SendMessage(socketFD, "deregister_ack"); |
---|
| 284 | shutdown(socketFD, 2); |
---|
| 285 | close(socketFD); |
---|
| 286 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
| 287 | } |
---|
| 288 | |
---|
[281] | 289 | void |
---|
| 290 | CognitiveRadioShell::SetActiveMission(int32_t socketFD) |
---|
| 291 | { |
---|
| 292 | char buffer[256]; |
---|
[209] | 293 | |
---|
[281] | 294 | LOG("Cognitive Radio Shell:: Received Set Active Mission command from host.\n"); |
---|
| 295 | |
---|
| 296 | // Read the name of the active mission to be set from the host. |
---|
| 297 | memset(buffer, 0, 256); |
---|
| 298 | ReadMessage(commandSocketFD, buffer); |
---|
| 299 | |
---|
| 300 | // Send command to SML |
---|
| 301 | SendMessage(ceSocketFD, "set_active_mission"); |
---|
| 302 | SendMessage(ceSocketFD, buffer); |
---|
| 303 | |
---|
| 304 | // Get ack from SML saying the mission was set properly |
---|
| 305 | memset(buffer, 0, 256); |
---|
| 306 | ReadMessage(ceSocketFD, buffer); |
---|
| 307 | |
---|
| 308 | // Forward ack to host |
---|
| 309 | SendMessage(commandSocketFD, buffer); |
---|
| 310 | } |
---|
| 311 | |
---|
[207] | 312 | int32_t |
---|
[209] | 313 | CognitiveRadioShell::LoadRadioConfiguration(const char* radioConfig, \ |
---|
| 314 | Parameter* &pList, Utility* &uList, Observable* &oList, \ |
---|
| 315 | Radio_Info* radioInfo) |
---|
[207] | 316 | { |
---|
| 317 | TiXmlElement *pElem; |
---|
| 318 | TiXmlElement *pChild; |
---|
| 319 | TiXmlElement *pChild1; |
---|
| 320 | TiXmlElement *pSecondChild; |
---|
| 321 | TiXmlHandle hRoot(0); |
---|
| 322 | |
---|
| 323 | int32_t count = 0; |
---|
| 324 | size_t item_count = 0; |
---|
| 325 | size_t affect_count = 0; |
---|
[218] | 326 | uint32_t attribute_count = 0; |
---|
[207] | 327 | bool match_found = false; |
---|
| 328 | |
---|
| 329 | LOG("Cognitive Radio Shell:: Loading radio configuration.\n"); |
---|
| 330 | |
---|
| 331 | TiXmlDocument doc( radioConfig ); |
---|
| 332 | bool loadOkay = doc.LoadFile(); |
---|
[215] | 333 | if(!loadOkay) |
---|
[207] | 334 | ERROR(1,"Loading radio configuration failed: %s\n", radioConfig); |
---|
| 335 | |
---|
| 336 | TiXmlHandle hDoc(&doc); |
---|
| 337 | |
---|
| 338 | pElem = hDoc.FirstChildElement().Element(); |
---|
| 339 | |
---|
| 340 | if(!pElem) |
---|
| 341 | ERROR(1, "No valid root!"); |
---|
| 342 | |
---|
| 343 | hRoot = TiXmlHandle(pElem); |
---|
| 344 | |
---|
| 345 | pElem = hRoot.FirstChild("utilities").Element(); |
---|
| 346 | pChild1 = hRoot.Child("utilities", count).Element(); |
---|
| 347 | |
---|
| 348 | for(pChild = pChild1->FirstChildElement("utility"); pChild; \ |
---|
[209] | 349 | pChild = pChild->NextSiblingElement()) { |
---|
| 350 | |
---|
[207] | 351 | const char *uName = pChild->Attribute("name"); |
---|
| 352 | if(uName) |
---|
[208] | 353 | uList[item_count].name = uName; |
---|
[207] | 354 | |
---|
| 355 | const char *uUnits = pChild->Attribute("units"); |
---|
| 356 | if(uUnits) |
---|
| 357 | uList[item_count].units = uUnits; |
---|
| 358 | |
---|
| 359 | const char *uGoal = pChild->Attribute("goal"); |
---|
| 360 | if(uGoal) |
---|
| 361 | uList[item_count].goal = uGoal; |
---|
| 362 | |
---|
| 363 | if(pChild->QueryFloatAttribute("target", &uList[item_count].target) != TIXML_SUCCESS) |
---|
| 364 | uList[item_count].target = -1; |
---|
| 365 | |
---|
| 366 | item_count++; |
---|
| 367 | } |
---|
| 368 | |
---|
[208] | 369 | radio_info->numUtilities = item_count; |
---|
[207] | 370 | LOG("Cognitive Radio Shell:: Parsed %d utilities.\n", radioInfo->numUtilities); |
---|
| 371 | |
---|
| 372 | item_count = 0; |
---|
| 373 | pElem = hRoot.FirstChild("observables").Element(); |
---|
| 374 | pChild1 = hRoot.Child("observables", count).Element(); |
---|
| 375 | |
---|
| 376 | for(pChild = pChild1->FirstChildElement("observable"); pChild; \ |
---|
[209] | 377 | pChild = pChild->NextSiblingElement()) { |
---|
| 378 | |
---|
[207] | 379 | const char *oName = pChild->Attribute("name"); |
---|
| 380 | if(oName) |
---|
| 381 | oList[item_count].name = oName; |
---|
[208] | 382 | |
---|
[207] | 383 | affect_count = 0; |
---|
| 384 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ |
---|
[209] | 385 | pSecondChild = pSecondChild->NextSiblingElement()) { |
---|
| 386 | |
---|
[207] | 387 | const char *oUtilName = pSecondChild->Attribute("utility"); |
---|
[209] | 388 | if(oUtilName) { |
---|
| 389 | for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++ ) { |
---|
| 390 | if(uList[attribute_count].name == oUtilName) { |
---|
| 391 | |
---|
[207] | 392 | oList[item_count].affection_list[affect_count].u = &uList[attribute_count]; |
---|
| 393 | const char *oRelate = pSecondChild->Attribute("relationship"); |
---|
| 394 | if(oRelate) |
---|
| 395 | oList[item_count].affection_list[affect_count].relation = oRelate; |
---|
[209] | 396 | |
---|
[207] | 397 | affect_count++; |
---|
| 398 | match_found = true; |
---|
| 399 | break; |
---|
| 400 | } |
---|
| 401 | } |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | if(!match_found) { |
---|
| 405 | ERROR(1, "Error: %s: %s is not a valid utility.\n", \ |
---|
| 406 | oList[item_count].name.c_str(), oUtilName); |
---|
| 407 | } |
---|
| 408 | else |
---|
[208] | 409 | match_found = false; |
---|
[207] | 410 | } |
---|
| 411 | oList[item_count].numAffects = affect_count; |
---|
| 412 | item_count++; |
---|
| 413 | } |
---|
| 414 | |
---|
[208] | 415 | radioInfo->numObservables = item_count; |
---|
[207] | 416 | LOG("Cognitive Radio Shell:: Parsed %d observables.\n", radioInfo->numObservables); |
---|
| 417 | |
---|
| 418 | pElem = hRoot.FirstChild("parameters").Element(); |
---|
| 419 | pChild1 = hRoot.Child("parameters", count).Element(); |
---|
[208] | 420 | |
---|
[207] | 421 | item_count = 0; |
---|
| 422 | for(pChild = pChild1->FirstChildElement("parameter"); pChild; \ |
---|
[209] | 423 | pChild = pChild->NextSiblingElement()) { |
---|
| 424 | |
---|
[207] | 425 | const char *pName = pChild->Attribute("name"); |
---|
| 426 | if(pName) |
---|
[208] | 427 | pList[item_count].name = pName; |
---|
[207] | 428 | |
---|
| 429 | const char *pUnits = pChild->Attribute("units"); |
---|
| 430 | if(pUnits) |
---|
| 431 | pList[item_count].units = pUnits; |
---|
| 432 | |
---|
| 433 | if(pChild->QueryFloatAttribute("min", &pList[item_count].min) != TIXML_SUCCESS) |
---|
| 434 | pList[item_count].min = -1; |
---|
| 435 | |
---|
| 436 | if(pChild->QueryFloatAttribute("max", &pList[item_count].max) != TIXML_SUCCESS) |
---|
| 437 | pList[item_count].max = -1; |
---|
| 438 | |
---|
| 439 | if(pChild->QueryFloatAttribute("step", &pList[item_count].step) != TIXML_SUCCESS) |
---|
| 440 | pList[item_count].step = -1; |
---|
[208] | 441 | |
---|
[207] | 442 | affect_count = 0; |
---|
| 443 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ |
---|
[209] | 444 | |
---|
| 445 | pSecondChild = pSecondChild->NextSiblingElement()) { |
---|
[207] | 446 | const char *pUtilName = pSecondChild->Attribute("utility"); |
---|
[209] | 447 | if(pUtilName) { |
---|
| 448 | for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++) { |
---|
| 449 | if(uList[attribute_count].name == pUtilName) { |
---|
[208] | 450 | pList[item_count].affection_list[affect_count].u = &uList[attribute_count]; |
---|
[207] | 451 | |
---|
| 452 | const char *pRelate = pSecondChild->Attribute("relationship"); |
---|
| 453 | if(pRelate) |
---|
| 454 | pList[item_count].affection_list[affect_count].relation = pRelate; |
---|
| 455 | else |
---|
| 456 | LOG("Error: No relation found.\n"); |
---|
| 457 | |
---|
| 458 | match_found = true; |
---|
| 459 | affect_count++; |
---|
| 460 | break; |
---|
| 461 | } |
---|
| 462 | } |
---|
| 463 | } |
---|
| 464 | |
---|
[209] | 465 | if(!match_found) { |
---|
[207] | 466 | ERROR(1, "Error: %s: %s is not a valid utility.\n", \ |
---|
| 467 | pList[item_count].name.c_str(), pUtilName); |
---|
| 468 | } |
---|
| 469 | |
---|
[208] | 470 | match_found = false; |
---|
[207] | 471 | } |
---|
| 472 | |
---|
| 473 | pList[item_count].numAffects = affect_count; |
---|
| 474 | item_count++; |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | radioInfo->numParameters = item_count; |
---|
| 478 | LOG("Cognitive Radio Shell:: Parsed %d parameters.\n", radioInfo->numParameters); |
---|
| 479 | |
---|
| 480 | return 1; |
---|
| 481 | } |
---|
| 482 | |
---|
[209] | 483 | |
---|
[207] | 484 | void |
---|
| 485 | CognitiveRadioShell::GetOptimalParameters(int32_t socketFD) |
---|
| 486 | { |
---|
[222] | 487 | char buffer[256]; |
---|
| 488 | char counter[55]; |
---|
| 489 | char var[50]; |
---|
| 490 | |
---|
| 491 | /* Receive Set of Observables */ |
---|
| 492 | LOG("Cognitive Radio Shell:: Got request for optimization.\n"); |
---|
| 493 | memset(buffer, 0, 256); |
---|
[227] | 494 | ReadMessage(commandSocketFD, buffer); |
---|
[231] | 495 | uint32_t numObservables = atoi(buffer); |
---|
[222] | 496 | |
---|
[232] | 497 | LOG("Cognitive Radio Shell:: Attempting to get %i observables.\n", numObservables); |
---|
[227] | 498 | Observable *o = new Observable[numObservables]; |
---|
[222] | 499 | |
---|
[227] | 500 | for(size_t i = 0; i < numObservables; i++) { |
---|
[222] | 501 | memset(buffer, 0, 256); |
---|
[227] | 502 | ReadMessage(commandSocketFD, buffer); |
---|
[222] | 503 | o[i].name = std::string(buffer); |
---|
[229] | 504 | |
---|
[222] | 505 | memset(buffer, 0, 256); |
---|
[227] | 506 | ReadMessage(commandSocketFD, buffer); |
---|
[222] | 507 | o[i].value = atof(buffer); |
---|
| 508 | } |
---|
[231] | 509 | |
---|
[232] | 510 | /* Receive Set of Current Parameters */ |
---|
[228] | 511 | memset(buffer, 0, 256); |
---|
| 512 | ReadMessage(commandSocketFD,buffer); |
---|
[231] | 513 | uint32_t numCurrentParameters = atoi(buffer); |
---|
[228] | 514 | |
---|
[232] | 515 | LOG("Cognitive Radio Shell:: Attempting to get %i parameters.\n",numCurrentParameters); |
---|
[228] | 516 | Parameter * cp = new Parameter[numCurrentParameters]; |
---|
[229] | 517 | |
---|
| 518 | for (size_t i = 0; i < numCurrentParameters; i++){ |
---|
[228] | 519 | memset(buffer, 0, 256); |
---|
| 520 | ReadMessage(commandSocketFD,buffer); |
---|
| 521 | cp[i].name = std::string(buffer); |
---|
| 522 | memset(buffer, 0, 256); |
---|
| 523 | ReadMessage(commandSocketFD,buffer); |
---|
| 524 | cp[i].value = atof(buffer); |
---|
| 525 | } |
---|
[231] | 526 | |
---|
[222] | 527 | /* Send to Cognitive Engine |
---|
[232] | 528 | * TODO: With multiple CEs we need to make a decision about where |
---|
| 529 | * to send this information |
---|
| 530 | */ |
---|
[281] | 531 | LOG("Cognitive Radio Shell:: Passing on observables.\n"); |
---|
| 532 | SendMessage(ceSocketFD,"request_optimization"); |
---|
| 533 | sprintf(counter,"%i",numObservables); |
---|
| 534 | SendMessage(ceSocketFD,counter); |
---|
| 535 | for(size_t i = 0; i < numObservables; i++) { |
---|
| 536 | SendMessage(ceSocketFD,o[i].name.c_str()); |
---|
| 537 | sprintf(var,"%f",o[i].value); |
---|
| 538 | SendMessage(ceSocketFD,var); |
---|
| 539 | } |
---|
[232] | 540 | |
---|
[281] | 541 | LOG("Cognitive Radio Shell:: Passing on current parameters.\n"); |
---|
| 542 | sprintf(counter,"%i",numCurrentParameters); |
---|
| 543 | SendMessage(ceSocketFD,counter); |
---|
| 544 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
| 545 | SendMessage(ceSocketFD,cp[i].name.c_str()); |
---|
| 546 | sprintf(var,"%f",cp[i].value); |
---|
[316] | 547 | SendMessage(ceSocketFD,var); |
---|
[232] | 548 | } |
---|
[222] | 549 | |
---|
[281] | 550 | LOG("Cognitive Radio Shell:: Receiving optimized parameters.\n"); |
---|
[222] | 551 | /* Receive Set of Parameters */ |
---|
| 552 | memset(buffer, 0, 256); |
---|
[227] | 553 | ReadMessage(ceSocketFD, buffer); |
---|
[231] | 554 | uint32_t numParameters = atoi(buffer); |
---|
[222] | 555 | |
---|
[227] | 556 | Parameter *p = new Parameter[numParameters]; |
---|
[222] | 557 | |
---|
[227] | 558 | for(size_t i = 0; i < numParameters; i++) { |
---|
[222] | 559 | memset(buffer, 0, 256); |
---|
[227] | 560 | ReadMessage(ceSocketFD, buffer); |
---|
[222] | 561 | p[i].name = std::string(buffer); |
---|
| 562 | |
---|
| 563 | memset(buffer, 0, 256); |
---|
[227] | 564 | ReadMessage(ceSocketFD, buffer); |
---|
[222] | 565 | p[i].value = atof(buffer); |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | /* Send to Application |
---|
[232] | 569 | */ |
---|
| 570 | LOG("Cognitive Radio Shell:: Sending optimized parameters to Application.\n"); |
---|
[222] | 571 | memset(counter, 0, 55); |
---|
[227] | 572 | sprintf(counter, "%i", numParameters); |
---|
| 573 | SendMessage(commandSocketFD, counter); |
---|
| 574 | for(size_t i = 0; i < numParameters; i++) { |
---|
| 575 | SendMessage(commandSocketFD, p[i].name.c_str()); |
---|
| 576 | sprintf(var, "%f", p[i].value); |
---|
[232] | 577 | SendMessage(commandSocketFD, var); |
---|
[222] | 578 | } |
---|
[227] | 579 | |
---|
| 580 | delete [] o; |
---|
| 581 | delete [] p; |
---|
[207] | 582 | } |
---|
| 583 | |
---|
[231] | 584 | // TODO point of always returning 1? |
---|
[230] | 585 | bool |
---|
| 586 | CognitiveRadioShell::UpdateParameterPerformance(int32_t socketFD) |
---|
| 587 | { |
---|
| 588 | char counter[55]; |
---|
| 589 | char var[50]; |
---|
| 590 | char buffer[256]; |
---|
[209] | 591 | |
---|
[232] | 592 | /* Receive Set of Parameters */ |
---|
[230] | 593 | memset(buffer, 0, 256); |
---|
| 594 | ReadMessage(commandSocketFD,buffer); |
---|
[231] | 595 | uint32_t numParameters = atoi(buffer); |
---|
[230] | 596 | |
---|
| 597 | Parameter *p = new Parameter[numParameters]; |
---|
| 598 | |
---|
| 599 | for (size_t i = 0; i < numParameters; i++){ |
---|
| 600 | memset(buffer, 0, 256); |
---|
| 601 | ReadMessage(commandSocketFD,buffer); |
---|
| 602 | p[i].name = std::string(buffer); |
---|
| 603 | memset(buffer, 0, 256); |
---|
| 604 | ReadMessage(commandSocketFD,buffer); |
---|
| 605 | p[i].value = atof(buffer); |
---|
| 606 | } |
---|
| 607 | |
---|
| 608 | /* Receive Set of Observables */ |
---|
| 609 | memset(buffer, 0, 256); |
---|
| 610 | ReadMessage(commandSocketFD, buffer); |
---|
[231] | 611 | uint32_t numObservables = atoi(buffer); |
---|
[230] | 612 | |
---|
| 613 | Observable *o = new Observable[numObservables]; |
---|
| 614 | |
---|
| 615 | for(size_t i = 0; i < numObservables; i++) { |
---|
| 616 | memset(buffer, 0, 256); |
---|
| 617 | ReadMessage(commandSocketFD, buffer); |
---|
| 618 | o[i].name = std::string(buffer); |
---|
| 619 | |
---|
| 620 | memset(buffer, 0, 256); |
---|
| 621 | ReadMessage(commandSocketFD, buffer); |
---|
| 622 | o[i].value = atof(buffer); |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | SendMessage(ceSocketFD, "update_performance"); |
---|
| 626 | |
---|
[231] | 627 | /* Send Parameters */ |
---|
[230] | 628 | memset(counter, 0, 55); |
---|
| 629 | sprintf(counter, "%i", numParameters); |
---|
| 630 | SendMessage(ceSocketFD, counter); |
---|
| 631 | |
---|
| 632 | for(size_t i = 0; i < numParameters; i++) { |
---|
| 633 | SendMessage(ceSocketFD,p[i].name.c_str()); |
---|
| 634 | sprintf(var,"%f",p[i].value); |
---|
| 635 | SendMessage(ceSocketFD,var); |
---|
| 636 | } |
---|
| 637 | |
---|
[231] | 638 | /* Send Observables */ |
---|
[230] | 639 | sprintf(counter, "%i", numObservables); |
---|
| 640 | SendMessage(ceSocketFD, counter); |
---|
| 641 | for(size_t i = 0; i < numObservables; i++) { |
---|
| 642 | SendMessage(ceSocketFD, o[i].name.c_str()); |
---|
| 643 | sprintf(var, "%f", o[i].value); |
---|
| 644 | SendMessage(ceSocketFD, var); |
---|
| 645 | } |
---|
[231] | 646 | |
---|
| 647 | delete [] p; |
---|
[239] | 648 | delete [] o; |
---|
[230] | 649 | |
---|
| 650 | return 1; |
---|
| 651 | } |
---|
[231] | 652 | |
---|
| 653 | |
---|
[389] | 654 | int |
---|
[207] | 655 | CognitiveRadioShell::HandleMessage(int32_t socketFD) |
---|
| 656 | { |
---|
| 657 | char buffer[256]; |
---|
[389] | 658 | int ret = 0; |
---|
| 659 | |
---|
| 660 | ret = ReadMessage(socketFD, buffer); |
---|
| 661 | if(ret == -1) { |
---|
| 662 | return ret; |
---|
| 663 | } |
---|
[207] | 664 | |
---|
[231] | 665 | // TODO trying to read this code lock makes my eyes bleed |
---|
[227] | 666 | if(strcmp(buffer, "register_engine_cognitive") == 0) { |
---|
[208] | 667 | RegisterCognitiveEngine(socketFD); |
---|
[227] | 668 | } else if(strcmp(buffer, "deregister_engine_cognitive") == 0) { |
---|
[208] | 669 | DeregisterCognitiveEngine(socketFD); |
---|
[227] | 670 | } else if(strcmp(buffer, "register_engine_policy") == 0) { |
---|
[208] | 671 | RegisterPolicyEngine(socketFD); |
---|
[227] | 672 | } else if(strcmp(buffer, "deregister_engine_policy") == 0) { |
---|
[208] | 673 | DeregisterPolicyEngine(socketFD); |
---|
[227] | 674 | } else if(strcmp(buffer, "register_sml") == 0) { |
---|
[208] | 675 | RegisterSML(socketFD); |
---|
[227] | 676 | } else if(strcmp(buffer, "deregister_sml") == 0) { |
---|
[208] | 677 | DeregisterSML(socketFD); |
---|
[230] | 678 | } else if(strcmp(buffer, "update_performance") == 0) { |
---|
| 679 | UpdateParameterPerformance(socketFD); |
---|
[231] | 680 | } else if(strcmp(buffer, "get_number_utilities") == 0) { |
---|
[232] | 681 | char numUtilities[20]; |
---|
| 682 | sprintf(numUtilities, "%i", radio_info->numUtilities); |
---|
[231] | 683 | SendMessage(commandSocketFD, numUtilities); |
---|
| 684 | } else if(strcmp(buffer, "get_number_observables") == 0) { |
---|
[232] | 685 | char numObservables[20]; |
---|
| 686 | sprintf(numObservables, "%i", radio_info->numObservables); |
---|
[231] | 687 | SendMessage(commandSocketFD, numObservables); |
---|
| 688 | } else if(strcmp(buffer, "get_number_parameters") == 0) { |
---|
[232] | 689 | char numParameters[20]; |
---|
| 690 | sprintf(numParameters, "%i", radio_info->numParameters); |
---|
[231] | 691 | SendMessage(commandSocketFD, numParameters); |
---|
| 692 | } else if(strcmp(buffer, "request_optimization") == 0) { |
---|
[208] | 693 | /* Receive optimization request and current environment */ |
---|
| 694 | GetOptimalParameters(socketFD); |
---|
[281] | 695 | } else if(strcmp(buffer, "set_active_mission") == 0) { |
---|
| 696 | SetActiveMission(socketFD); |
---|
[231] | 697 | } else if(strcmp(buffer, "request_optimization_service") == 0) { |
---|
[228] | 698 | /* Receive optimization request and current environment */ |
---|
| 699 | //GetOptimalParametersService(socketFD); |
---|
[207] | 700 | } |
---|
[389] | 701 | |
---|
| 702 | return ret; |
---|
[207] | 703 | } |
---|
| 704 | |
---|
[209] | 705 | |
---|
[207] | 706 | void |
---|
| 707 | CognitiveRadioShell::StartShellServer() |
---|
| 708 | { |
---|
| 709 | struct timeval selTimeout; |
---|
| 710 | int32_t primary = 0; |
---|
| 711 | int32_t policy = 1; |
---|
| 712 | int32_t command = 2; |
---|
| 713 | int32_t running = 1; |
---|
| 714 | int32_t port, rc, new_sd = 1; |
---|
| 715 | int32_t desc_ready = 1; |
---|
| 716 | int32_t timeout = 10; |
---|
[389] | 717 | int32_t ret = 0;; |
---|
[207] | 718 | fd_set sockSet; |
---|
[389] | 719 | |
---|
[207] | 720 | |
---|
[389] | 721 | |
---|
[210] | 722 | int32_t *servSock = new int32_t[3]; |
---|
[207] | 723 | |
---|
| 724 | servSock[primary] = CreateTCPServerSocket(primaryPort); |
---|
| 725 | servSock[policy] = CreateTCPServerSocket(policyPort); |
---|
| 726 | servSock[command] = CreateTCPServerSocket(commandPort); |
---|
| 727 | |
---|
[222] | 728 | int32_t maxDescriptor; |
---|
[207] | 729 | |
---|
[232] | 730 | if(servSock[primary] > servSock[policy]) |
---|
| 731 | maxDescriptor = servSock[primary]; |
---|
[222] | 732 | else |
---|
[232] | 733 | maxDescriptor = servSock[policy]; |
---|
[222] | 734 | |
---|
[232] | 735 | if(servSock[command] > maxDescriptor) |
---|
| 736 | maxDescriptor = servSock[command]; |
---|
[222] | 737 | |
---|
[215] | 738 | if(InitializeTCPServerPort(servSock[primary]) == -1) |
---|
| 739 | ERROR(1,"Error initializing primary port\n"); |
---|
[207] | 740 | |
---|
[215] | 741 | if(InitializeTCPServerPort(servSock[policy]) == -1) |
---|
| 742 | ERROR(1,"Error initializing policy port\n"); |
---|
[207] | 743 | |
---|
[215] | 744 | if(InitializeTCPServerPort(servSock[command]) == -1) |
---|
| 745 | ERROR(1,"Error initializing command port\n"); |
---|
[207] | 746 | |
---|
[389] | 747 | FD_ZERO(&sockSet); |
---|
| 748 | |
---|
[209] | 749 | while (running) { |
---|
[207] | 750 | /* Zero socket descriptor vector and set for server sockets */ |
---|
| 751 | /* This must be reset every time select() is called */ |
---|
| 752 | FD_SET(servSock[primary], &sockSet); |
---|
| 753 | FD_SET(servSock[policy], &sockSet); |
---|
| 754 | FD_SET(servSock[command], &sockSet); |
---|
| 755 | |
---|
| 756 | /* Timeout specification */ |
---|
| 757 | /* This must be reset every time select() is called */ |
---|
| 758 | selTimeout.tv_sec = timeout; /* timeout (secs.) */ |
---|
| 759 | selTimeout.tv_usec = 0; /* 0 microseconds */ |
---|
| 760 | |
---|
| 761 | /* Suspend program until descriptor is ready or timeout */ |
---|
[389] | 762 | rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
[209] | 763 | if(rc == 0) |
---|
[207] | 764 | LOG("No echo requests for %i secs...Server still alive\n", timeout); |
---|
[209] | 765 | else { |
---|
[207] | 766 | desc_ready = rc; |
---|
| 767 | |
---|
[209] | 768 | for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) { |
---|
| 769 | if(FD_ISSET(port, &sockSet)) { |
---|
| 770 | desc_ready -= 1; |
---|
[207] | 771 | /* Check if request is new or on an existing open descriptor */ |
---|
[209] | 772 | if((port == servSock[primary]) || (port == servSock[policy]) || (port == servSock[command])) { |
---|
| 773 | do { |
---|
[207] | 774 | new_sd = AcceptTCPConnection(port); |
---|
[389] | 775 | if(new_sd < 0) { |
---|
[207] | 776 | break; |
---|
[389] | 777 | } |
---|
[222] | 778 | |
---|
| 779 | if(port == servSock[primary]) |
---|
| 780 | ceSocketFD = new_sd; |
---|
| 781 | if(port == servSock[command]) |
---|
| 782 | commandSocketFD = new_sd; |
---|
| 783 | if(port == servSock[policy]) |
---|
| 784 | policySocketFD = new_sd; |
---|
| 785 | |
---|
[389] | 786 | ret = HandleMessage(new_sd); |
---|
| 787 | |
---|
| 788 | if(ret == -1) { |
---|
| 789 | FD_CLR(new_sd,&sockSet); |
---|
| 790 | close(new_sd); |
---|
| 791 | } |
---|
| 792 | |
---|
| 793 | FD_SET(new_sd,&sockSet); |
---|
[207] | 794 | if(new_sd > maxDescriptor) |
---|
| 795 | maxDescriptor = new_sd; |
---|
| 796 | } while(new_sd != -1); |
---|
[389] | 797 | } |
---|
[209] | 798 | else { |
---|
[389] | 799 | ret = HandleMessage(port); |
---|
| 800 | if(ret == -1) { |
---|
| 801 | FD_CLR(port,&sockSet); |
---|
| 802 | close(port); |
---|
| 803 | } |
---|
[207] | 804 | } |
---|
| 805 | } |
---|
| 806 | } |
---|
| 807 | } |
---|
| 808 | } |
---|
| 809 | |
---|
[389] | 810 | LOG("Closing it all.\n\n"); |
---|
[207] | 811 | /* Close sockets */ |
---|
| 812 | close(servSock[primary]); |
---|
| 813 | close(servSock[policy]); |
---|
| 814 | close(servSock[command]); |
---|
| 815 | |
---|
| 816 | /* Free list of sockets */ |
---|
[210] | 817 | delete servSock; |
---|
[207] | 818 | |
---|
| 819 | return; |
---|
| 820 | } |
---|