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