[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 | |
---|
[521] | 17 | /*! This file provides the default implementation of the Cognitive Radio Shell. |
---|
| 18 | * |
---|
| 19 | * The CRS acts as the central control component in a CROSS radio system. It |
---|
| 20 | * handles component registration and message passing. |
---|
[207] | 21 | */ |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | #include <cstdlib> |
---|
| 25 | #include <cstring> |
---|
| 26 | #include <stdint.h> |
---|
[212] | 27 | #include <string> |
---|
[207] | 28 | |
---|
| 29 | #include <arpa/inet.h> |
---|
| 30 | #include <iostream> |
---|
| 31 | #include <netinet/in.h> |
---|
| 32 | #include <netdb.h> |
---|
| 33 | #include <fcntl.h> |
---|
| 34 | #include <sys/ioctl.h> |
---|
| 35 | #include <sys/mman.h> |
---|
| 36 | #include <sys/socket.h> |
---|
| 37 | #include <sys/types.h> |
---|
| 38 | #include <sys/wait.h> |
---|
| 39 | |
---|
| 40 | #include "tinyxml/tinyxml.h" |
---|
| 41 | #include "tinyxml/tinystr.h" |
---|
| 42 | |
---|
| 43 | #include "vtcross/common.h" |
---|
| 44 | #include "vtcross/containers.h" |
---|
[465] | 45 | #include "vtcross/cross_shell.h" |
---|
[207] | 46 | #include "vtcross/debug.h" |
---|
| 47 | #include "vtcross/error.h" |
---|
| 48 | #include "vtcross/socketcomm.h" |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | CognitiveRadioShell::CognitiveRadioShell() |
---|
| 52 | { |
---|
| 53 | LOG("Creating Cognitive Radio Shell.\n"); |
---|
[433] | 54 | |
---|
[207] | 55 | SML_present = false; |
---|
| 56 | PE_present = false; |
---|
| 57 | CE_present = false; |
---|
[213] | 58 | |
---|
[432] | 59 | params = new Parameter[10]; |
---|
| 60 | observables = new Observable[10]; |
---|
| 61 | utils = new Utility[10]; |
---|
| 62 | radio_info = new Radio_Info; |
---|
[207] | 63 | } |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | CognitiveRadioShell::~CognitiveRadioShell() |
---|
| 67 | { |
---|
[213] | 68 | delete [] params; |
---|
| 69 | delete [] observables; |
---|
| 70 | delete [] utils; |
---|
[437] | 71 | delete radio_info; |
---|
[207] | 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | CognitiveRadioShell::CognitiveRadioShell(const char* radioConfig, int16_t p1, \ |
---|
| 76 | int16_t p2, int16_t p3) |
---|
| 77 | { |
---|
| 78 | LOG("Creating Cognitive Radio Shell.\n"); |
---|
[433] | 79 | |
---|
| 80 | SML_present = false; |
---|
| 81 | PE_present = false; |
---|
| 82 | CE_present = false; |
---|
| 83 | |
---|
| 84 | params = new Parameter[10]; |
---|
| 85 | observables = new Observable[10]; |
---|
| 86 | utils = new Utility[10]; |
---|
| 87 | radio_info = new Radio_Info; |
---|
| 88 | |
---|
[213] | 89 | LoadRadioConfiguration(radioConfig, params, utils, observables, radio_info); |
---|
[207] | 90 | |
---|
| 91 | primaryPort = p1; |
---|
| 92 | policyPort = p2; |
---|
| 93 | commandPort = p3; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | void |
---|
| 98 | CognitiveRadioShell::SendComponentType(int32_t socketFD) |
---|
| 99 | { |
---|
| 100 | SendMessage(socketFD, "response_shell"); |
---|
| 101 | LOG("Cognitive Radio Shell responded to GetRemoteComponentType query.\n"); |
---|
| 102 | } |
---|
| 103 | |
---|
[209] | 104 | |
---|
[212] | 105 | std::string |
---|
| 106 | CognitiveRadioShell::GetRemoteComponentType(int32_t socketFD) |
---|
| 107 | { |
---|
| 108 | SendMessage(socketFD, "request_component_type"); |
---|
| 109 | |
---|
| 110 | char buffer[256]; |
---|
| 111 | memset(buffer, 0, 256); |
---|
| 112 | ReadMessage(socketFD, buffer); |
---|
| 113 | |
---|
| 114 | return std::string(buffer); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | |
---|
[207] | 118 | void |
---|
| 119 | CognitiveRadioShell::Shutdown() |
---|
| 120 | { |
---|
[435] | 121 | LOG("Shutting down Cognitive Radio Shell.\n"); |
---|
[207] | 122 | } |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | void |
---|
| 126 | CognitiveRadioShell::Reset() |
---|
| 127 | { |
---|
| 128 | LOG("Resetting Cognitive Radio Shell.\n"); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
[218] | 132 | bool |
---|
[231] | 133 | CognitiveRadioShell::SendRadioConfiguration(int32_t socketFD) |
---|
[207] | 134 | { |
---|
| 135 | LOG("Cognitive Radio Shell:: Sending radio configuration to Cognitive Engine.\n"); |
---|
[231] | 136 | |
---|
[218] | 137 | char counter[55]; |
---|
| 138 | char var[50]; |
---|
| 139 | |
---|
[231] | 140 | /* Send utilities */ |
---|
| 141 | sprintf(counter, "%d", radio_info->numUtilities); |
---|
| 142 | SendMessage(socketFD, counter); |
---|
| 143 | for(size_t i = 0; i < radio_info->numUtilities; i++) { |
---|
| 144 | SendMessage(socketFD, utils[i].name.c_str()); |
---|
| 145 | SendMessage(socketFD, utils[i].units.c_str()); |
---|
| 146 | SendMessage(socketFD, utils[i].goal.c_str()); |
---|
[222] | 147 | sprintf(var,"%f", utils[i].target); |
---|
[231] | 148 | SendMessage(socketFD, var); |
---|
[218] | 149 | } |
---|
| 150 | |
---|
[231] | 151 | /* Send parameters */ |
---|
[218] | 152 | sprintf(counter,"%i",radio_info->numParameters); |
---|
[231] | 153 | SendMessage(socketFD,counter); |
---|
| 154 | for(size_t i = 0; i < radio_info->numParameters; i++) { |
---|
| 155 | SendMessage(socketFD, params[i].name.c_str()); |
---|
| 156 | SendMessage(socketFD, params[i].units.c_str()); |
---|
[227] | 157 | sprintf(var, "%f", params[i].min); |
---|
[231] | 158 | SendMessage(socketFD,var); |
---|
[227] | 159 | sprintf(var, "%f", params[i].max); |
---|
[231] | 160 | SendMessage(socketFD, var); |
---|
[227] | 161 | sprintf(var, "%f", params[i].step); |
---|
[231] | 162 | SendMessage(socketFD, var); |
---|
[218] | 163 | |
---|
[227] | 164 | sprintf(counter, "%i", params[i].numAffects); |
---|
[231] | 165 | SendMessage(socketFD, counter); |
---|
| 166 | for(size_t j = 0; j < params[i].numAffects; j++) { |
---|
| 167 | SendMessage(socketFD, params[i].affection_list[j].u->name.c_str()); |
---|
| 168 | SendMessage(socketFD, params[i].affection_list[j].relation.c_str()); |
---|
[218] | 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
[231] | 172 | /* Send observables */ |
---|
[218] | 173 | sprintf(counter,"%i",radio_info->numObservables); |
---|
[231] | 174 | SendMessage(socketFD, counter); |
---|
| 175 | for(size_t i = 0; i < radio_info->numObservables; i++) { |
---|
| 176 | SendMessage(socketFD, observables[i].name.c_str()); |
---|
[232] | 177 | |
---|
[227] | 178 | sprintf(counter, "%i", observables[i].numAffects); |
---|
[231] | 179 | SendMessage(socketFD, counter); |
---|
| 180 | for(size_t j = 0; j < observables[i].numAffects; j++) { |
---|
| 181 | SendMessage(socketFD, observables[i].affection_list[j].u->name.c_str()); |
---|
| 182 | SendMessage(socketFD, observables[i].affection_list[j].relation.c_str()); |
---|
[218] | 183 | } |
---|
| 184 | } |
---|
[232] | 185 | |
---|
[231] | 186 | /* Receive ACK for radio configuration */ |
---|
[218] | 187 | char buffer[256]; |
---|
| 188 | memset(buffer, 0, 256); |
---|
[231] | 189 | ReadMessage(socketFD, buffer); |
---|
[218] | 190 | |
---|
| 191 | if(strcmp(buffer, "receive_config_ack") != 0) { |
---|
[227] | 192 | LOG("Cognitive Radio Shell:: Unexpected response: %s\n", buffer); |
---|
[435] | 193 | return false; |
---|
[218] | 194 | } |
---|
[227] | 195 | |
---|
[435] | 196 | return true; |
---|
[207] | 197 | } |
---|
| 198 | |
---|
[218] | 199 | bool |
---|
[207] | 200 | CognitiveRadioShell::SendRadioExperience(int32_t socketFD) |
---|
| 201 | { |
---|
| 202 | |
---|
| 203 | LOG("Cognitive Radio Shell:: Sending radio experience to Cognitive Engine.\n"); |
---|
[218] | 204 | int32_t numberExp = 4; |
---|
[232] | 205 | char numberExpString[50]; |
---|
[218] | 206 | |
---|
[285] | 207 | sprintf(numberExpString, "%d", numberExp); |
---|
[435] | 208 | SendMessage(socketFD, "test"); |
---|
[222] | 209 | |
---|
[218] | 210 | char buffer[256]; |
---|
| 211 | memset(buffer, 0, 256); |
---|
| 212 | ReadMessage(socketFD, buffer); |
---|
| 213 | if(strcmp(buffer, "receive_exp_ack") != 0) { |
---|
[435] | 214 | WARNING("Cognitive Radio Shell:: Unexpected response: %s\n", buffer); |
---|
| 215 | return false; |
---|
[218] | 216 | } |
---|
[435] | 217 | return true; |
---|
[207] | 218 | } |
---|
| 219 | |
---|
[209] | 220 | |
---|
[207] | 221 | void |
---|
| 222 | CognitiveRadioShell::RegisterCognitiveEngine(int32_t socketFD) |
---|
| 223 | { |
---|
[437] | 224 | LOG("Cognitive Radio Shell:: Received registration from Cognitive Engine on socket %d.\n", \ |
---|
[435] | 225 | socketFD); |
---|
[207] | 226 | |
---|
[279] | 227 | SendMessage(socketFD, "register_ack"); |
---|
[207] | 228 | SendRadioConfiguration(socketFD); |
---|
| 229 | SendRadioExperience(socketFD); |
---|
| 230 | |
---|
| 231 | numberOfCognitiveEngines++; |
---|
[435] | 232 | |
---|
| 233 | /* More efficient to always set to true than add a conditional branch to the |
---|
| 234 | * code. */ |
---|
[207] | 235 | CE_present = true; |
---|
| 236 | } |
---|
| 237 | |
---|
[209] | 238 | |
---|
[207] | 239 | void |
---|
| 240 | CognitiveRadioShell::DeregisterCognitiveEngine(int32_t socketFD) |
---|
| 241 | { |
---|
| 242 | LOG("Cognitive Radio Shell:: Received deregistration message from Cognitive Engine.\n"); |
---|
| 243 | |
---|
| 244 | numberOfCognitiveEngines--; |
---|
| 245 | if(numberOfCognitiveEngines == 0) |
---|
[209] | 246 | CE_present = false; |
---|
[207] | 247 | |
---|
| 248 | SendMessage(socketFD, "deregister_ack"); |
---|
| 249 | shutdown(socketFD, 2); |
---|
| 250 | close(socketFD); |
---|
[435] | 251 | LOG("Cognitive Radio Shell:: Socket %d closed.\n", socketFD); |
---|
[207] | 252 | } |
---|
| 253 | |
---|
[209] | 254 | |
---|
[207] | 255 | void |
---|
| 256 | CognitiveRadioShell::RegisterPolicyEngine(int32_t socketFD) |
---|
| 257 | { |
---|
[435] | 258 | LOG("Cognitive Radio Shell:: Received registration from Policy Engine on socket %d.\n", \ |
---|
| 259 | socketFD); |
---|
| 260 | |
---|
[207] | 261 | PE_present = true; |
---|
| 262 | } |
---|
| 263 | |
---|
[209] | 264 | |
---|
[207] | 265 | void |
---|
| 266 | CognitiveRadioShell::DeregisterPolicyEngine(int32_t socketFD) |
---|
| 267 | { |
---|
| 268 | LOG("Cognitive Radio Shell:: Received deregistration message from Policy Engine.\n"); |
---|
| 269 | |
---|
| 270 | PE_present = false; |
---|
| 271 | |
---|
| 272 | SendMessage(socketFD, "deregister_ack"); |
---|
| 273 | shutdown(socketFD, 2); |
---|
| 274 | close(socketFD); |
---|
[435] | 275 | LOG("Cognitive Radio Shell:: Socket %d closed.\n", socketFD); |
---|
[207] | 276 | } |
---|
| 277 | |
---|
[209] | 278 | |
---|
[207] | 279 | void |
---|
| 280 | CognitiveRadioShell::RegisterSML(int32_t socketFD) |
---|
| 281 | { |
---|
[435] | 282 | LOG("Cognitive Radio Shell:: Received registration from SML on socket %d.\n", \ |
---|
| 283 | socketFD); |
---|
[207] | 284 | |
---|
| 285 | SML_present = true; |
---|
| 286 | } |
---|
| 287 | |
---|
[209] | 288 | |
---|
[207] | 289 | void |
---|
| 290 | CognitiveRadioShell::DeregisterSML(int32_t socketFD) |
---|
| 291 | { |
---|
| 292 | LOG("Cognitive Radio Shell:: Received deregistration message from SML.\n"); |
---|
| 293 | |
---|
| 294 | SML_present = false; |
---|
| 295 | |
---|
| 296 | SendMessage(socketFD, "deregister_ack"); |
---|
| 297 | shutdown(socketFD, 2); |
---|
| 298 | close(socketFD); |
---|
[435] | 299 | LOG("Cognitive Radio Shell:: Socket %d closed.\n", socketFD); |
---|
[207] | 300 | } |
---|
| 301 | |
---|
[281] | 302 | void |
---|
| 303 | CognitiveRadioShell::SetActiveMission(int32_t socketFD) |
---|
| 304 | { |
---|
| 305 | char buffer[256]; |
---|
[209] | 306 | |
---|
[281] | 307 | LOG("Cognitive Radio Shell:: Received Set Active Mission command from host.\n"); |
---|
| 308 | |
---|
[437] | 309 | /* Read the name of the active mission to be set from the host. */ |
---|
[281] | 310 | memset(buffer, 0, 256); |
---|
| 311 | ReadMessage(commandSocketFD, buffer); |
---|
| 312 | |
---|
[437] | 313 | /* Send command to SML. */ |
---|
[281] | 314 | SendMessage(ceSocketFD, "set_active_mission"); |
---|
| 315 | SendMessage(ceSocketFD, buffer); |
---|
| 316 | |
---|
[437] | 317 | /* Get ack from SML saying the mission was set properly */ |
---|
[281] | 318 | memset(buffer, 0, 256); |
---|
| 319 | ReadMessage(ceSocketFD, buffer); |
---|
| 320 | |
---|
[437] | 321 | /* Forward ack to host */ |
---|
[281] | 322 | SendMessage(commandSocketFD, buffer); |
---|
| 323 | } |
---|
| 324 | |
---|
[537] | 325 | void |
---|
[209] | 326 | CognitiveRadioShell::LoadRadioConfiguration(const char* radioConfig, \ |
---|
| 327 | Parameter* &pList, Utility* &uList, Observable* &oList, \ |
---|
| 328 | Radio_Info* radioInfo) |
---|
[207] | 329 | { |
---|
| 330 | TiXmlElement *pElem; |
---|
| 331 | TiXmlElement *pChild; |
---|
| 332 | TiXmlElement *pChild1; |
---|
| 333 | TiXmlElement *pSecondChild; |
---|
| 334 | TiXmlHandle hRoot(0); |
---|
| 335 | |
---|
| 336 | int32_t count = 0; |
---|
| 337 | size_t item_count = 0; |
---|
| 338 | size_t affect_count = 0; |
---|
[218] | 339 | uint32_t attribute_count = 0; |
---|
[207] | 340 | bool match_found = false; |
---|
| 341 | |
---|
| 342 | LOG("Cognitive Radio Shell:: Loading radio configuration.\n"); |
---|
| 343 | |
---|
| 344 | TiXmlDocument doc( radioConfig ); |
---|
| 345 | bool loadOkay = doc.LoadFile(); |
---|
[215] | 346 | if(!loadOkay) |
---|
[437] | 347 | ERROR(1, "Loading radio configuration failed: %s\n", radioConfig); |
---|
[207] | 348 | |
---|
| 349 | TiXmlHandle hDoc(&doc); |
---|
| 350 | |
---|
| 351 | pElem = hDoc.FirstChildElement().Element(); |
---|
| 352 | |
---|
| 353 | if(!pElem) |
---|
| 354 | ERROR(1, "No valid root!"); |
---|
| 355 | |
---|
| 356 | hRoot = TiXmlHandle(pElem); |
---|
| 357 | |
---|
| 358 | pElem = hRoot.FirstChild("utilities").Element(); |
---|
| 359 | pChild1 = hRoot.Child("utilities", count).Element(); |
---|
| 360 | |
---|
| 361 | for(pChild = pChild1->FirstChildElement("utility"); pChild; \ |
---|
[209] | 362 | pChild = pChild->NextSiblingElement()) { |
---|
| 363 | |
---|
[207] | 364 | const char *uName = pChild->Attribute("name"); |
---|
| 365 | if(uName) |
---|
[208] | 366 | uList[item_count].name = uName; |
---|
[207] | 367 | |
---|
| 368 | const char *uUnits = pChild->Attribute("units"); |
---|
| 369 | if(uUnits) |
---|
| 370 | uList[item_count].units = uUnits; |
---|
| 371 | |
---|
| 372 | const char *uGoal = pChild->Attribute("goal"); |
---|
| 373 | if(uGoal) |
---|
| 374 | uList[item_count].goal = uGoal; |
---|
| 375 | |
---|
| 376 | if(pChild->QueryFloatAttribute("target", &uList[item_count].target) != TIXML_SUCCESS) |
---|
| 377 | uList[item_count].target = -1; |
---|
| 378 | |
---|
| 379 | item_count++; |
---|
| 380 | } |
---|
| 381 | |
---|
[208] | 382 | radio_info->numUtilities = item_count; |
---|
[207] | 383 | LOG("Cognitive Radio Shell:: Parsed %d utilities.\n", radioInfo->numUtilities); |
---|
| 384 | |
---|
| 385 | item_count = 0; |
---|
| 386 | pElem = hRoot.FirstChild("observables").Element(); |
---|
| 387 | pChild1 = hRoot.Child("observables", count).Element(); |
---|
| 388 | |
---|
| 389 | for(pChild = pChild1->FirstChildElement("observable"); pChild; \ |
---|
[209] | 390 | pChild = pChild->NextSiblingElement()) { |
---|
| 391 | |
---|
[207] | 392 | const char *oName = pChild->Attribute("name"); |
---|
| 393 | if(oName) |
---|
| 394 | oList[item_count].name = oName; |
---|
[208] | 395 | |
---|
[207] | 396 | affect_count = 0; |
---|
| 397 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ |
---|
[209] | 398 | pSecondChild = pSecondChild->NextSiblingElement()) { |
---|
| 399 | |
---|
[207] | 400 | const char *oUtilName = pSecondChild->Attribute("utility"); |
---|
[209] | 401 | if(oUtilName) { |
---|
| 402 | for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++ ) { |
---|
| 403 | if(uList[attribute_count].name == oUtilName) { |
---|
| 404 | |
---|
[207] | 405 | oList[item_count].affection_list[affect_count].u = &uList[attribute_count]; |
---|
| 406 | const char *oRelate = pSecondChild->Attribute("relationship"); |
---|
| 407 | if(oRelate) |
---|
| 408 | oList[item_count].affection_list[affect_count].relation = oRelate; |
---|
[209] | 409 | |
---|
[207] | 410 | affect_count++; |
---|
| 411 | match_found = true; |
---|
| 412 | break; |
---|
| 413 | } |
---|
| 414 | } |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | if(!match_found) { |
---|
| 418 | ERROR(1, "Error: %s: %s is not a valid utility.\n", \ |
---|
| 419 | oList[item_count].name.c_str(), oUtilName); |
---|
| 420 | } |
---|
| 421 | else |
---|
[208] | 422 | match_found = false; |
---|
[207] | 423 | } |
---|
| 424 | oList[item_count].numAffects = affect_count; |
---|
| 425 | item_count++; |
---|
| 426 | } |
---|
| 427 | |
---|
[208] | 428 | radioInfo->numObservables = item_count; |
---|
[207] | 429 | LOG("Cognitive Radio Shell:: Parsed %d observables.\n", radioInfo->numObservables); |
---|
| 430 | |
---|
| 431 | pElem = hRoot.FirstChild("parameters").Element(); |
---|
| 432 | pChild1 = hRoot.Child("parameters", count).Element(); |
---|
[208] | 433 | |
---|
[207] | 434 | item_count = 0; |
---|
| 435 | for(pChild = pChild1->FirstChildElement("parameter"); pChild; \ |
---|
[209] | 436 | pChild = pChild->NextSiblingElement()) { |
---|
| 437 | |
---|
[207] | 438 | const char *pName = pChild->Attribute("name"); |
---|
| 439 | if(pName) |
---|
[208] | 440 | pList[item_count].name = pName; |
---|
[207] | 441 | |
---|
| 442 | const char *pUnits = pChild->Attribute("units"); |
---|
| 443 | if(pUnits) |
---|
| 444 | pList[item_count].units = pUnits; |
---|
| 445 | |
---|
| 446 | if(pChild->QueryFloatAttribute("min", &pList[item_count].min) != TIXML_SUCCESS) |
---|
| 447 | pList[item_count].min = -1; |
---|
| 448 | |
---|
| 449 | if(pChild->QueryFloatAttribute("max", &pList[item_count].max) != TIXML_SUCCESS) |
---|
| 450 | pList[item_count].max = -1; |
---|
| 451 | |
---|
| 452 | if(pChild->QueryFloatAttribute("step", &pList[item_count].step) != TIXML_SUCCESS) |
---|
| 453 | pList[item_count].step = -1; |
---|
[208] | 454 | |
---|
[207] | 455 | affect_count = 0; |
---|
| 456 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ |
---|
[209] | 457 | |
---|
| 458 | pSecondChild = pSecondChild->NextSiblingElement()) { |
---|
[207] | 459 | const char *pUtilName = pSecondChild->Attribute("utility"); |
---|
[209] | 460 | if(pUtilName) { |
---|
| 461 | for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++) { |
---|
| 462 | if(uList[attribute_count].name == pUtilName) { |
---|
[208] | 463 | pList[item_count].affection_list[affect_count].u = &uList[attribute_count]; |
---|
[207] | 464 | |
---|
| 465 | const char *pRelate = pSecondChild->Attribute("relationship"); |
---|
| 466 | if(pRelate) |
---|
| 467 | pList[item_count].affection_list[affect_count].relation = pRelate; |
---|
| 468 | else |
---|
| 469 | LOG("Error: No relation found.\n"); |
---|
| 470 | |
---|
| 471 | match_found = true; |
---|
| 472 | affect_count++; |
---|
| 473 | break; |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | } |
---|
| 477 | |
---|
[209] | 478 | if(!match_found) { |
---|
[207] | 479 | ERROR(1, "Error: %s: %s is not a valid utility.\n", \ |
---|
| 480 | pList[item_count].name.c_str(), pUtilName); |
---|
| 481 | } |
---|
| 482 | |
---|
[208] | 483 | match_found = false; |
---|
[207] | 484 | } |
---|
| 485 | |
---|
| 486 | pList[item_count].numAffects = affect_count; |
---|
| 487 | item_count++; |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | radioInfo->numParameters = item_count; |
---|
| 491 | LOG("Cognitive Radio Shell:: Parsed %d parameters.\n", radioInfo->numParameters); |
---|
| 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 | |
---|
[537] | 596 | |
---|
| 597 | void |
---|
[230] | 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 | } |
---|
[231] | 663 | |
---|
| 664 | |
---|
[432] | 665 | int32_t |
---|
[207] | 666 | CognitiveRadioShell::HandleMessage(int32_t socketFD) |
---|
| 667 | { |
---|
| 668 | char buffer[256]; |
---|
[389] | 669 | int ret = 0; |
---|
| 670 | |
---|
| 671 | ret = ReadMessage(socketFD, buffer); |
---|
[437] | 672 | if(ret == -1) |
---|
| 673 | return ret; |
---|
[207] | 674 | |
---|
[437] | 675 | // TODO trying to read this code block makes my eyes bleed |
---|
[227] | 676 | if(strcmp(buffer, "register_engine_cognitive") == 0) { |
---|
[208] | 677 | RegisterCognitiveEngine(socketFD); |
---|
[227] | 678 | } else if(strcmp(buffer, "deregister_engine_cognitive") == 0) { |
---|
[208] | 679 | DeregisterCognitiveEngine(socketFD); |
---|
[227] | 680 | } else if(strcmp(buffer, "register_engine_policy") == 0) { |
---|
[208] | 681 | RegisterPolicyEngine(socketFD); |
---|
[227] | 682 | } else if(strcmp(buffer, "deregister_engine_policy") == 0) { |
---|
[208] | 683 | DeregisterPolicyEngine(socketFD); |
---|
[227] | 684 | } else if(strcmp(buffer, "register_sml") == 0) { |
---|
[208] | 685 | RegisterSML(socketFD); |
---|
[227] | 686 | } else if(strcmp(buffer, "deregister_sml") == 0) { |
---|
[208] | 687 | DeregisterSML(socketFD); |
---|
[230] | 688 | } else if(strcmp(buffer, "update_performance") == 0) { |
---|
| 689 | UpdateParameterPerformance(socketFD); |
---|
[231] | 690 | } else if(strcmp(buffer, "get_number_utilities") == 0) { |
---|
[232] | 691 | char numUtilities[20]; |
---|
| 692 | sprintf(numUtilities, "%i", radio_info->numUtilities); |
---|
[231] | 693 | SendMessage(commandSocketFD, numUtilities); |
---|
| 694 | } else if(strcmp(buffer, "get_number_observables") == 0) { |
---|
[232] | 695 | char numObservables[20]; |
---|
| 696 | sprintf(numObservables, "%i", radio_info->numObservables); |
---|
[231] | 697 | SendMessage(commandSocketFD, numObservables); |
---|
| 698 | } else if(strcmp(buffer, "get_number_parameters") == 0) { |
---|
[232] | 699 | char numParameters[20]; |
---|
| 700 | sprintf(numParameters, "%i", radio_info->numParameters); |
---|
[231] | 701 | SendMessage(commandSocketFD, numParameters); |
---|
| 702 | } else if(strcmp(buffer, "request_optimization") == 0) { |
---|
[208] | 703 | /* Receive optimization request and current environment */ |
---|
| 704 | GetOptimalParameters(socketFD); |
---|
[281] | 705 | } else if(strcmp(buffer, "set_active_mission") == 0) { |
---|
| 706 | SetActiveMission(socketFD); |
---|
[231] | 707 | } else if(strcmp(buffer, "request_optimization_service") == 0) { |
---|
[228] | 708 | /* Receive optimization request and current environment */ |
---|
| 709 | //GetOptimalParametersService(socketFD); |
---|
[207] | 710 | } |
---|
[389] | 711 | |
---|
| 712 | return ret; |
---|
[207] | 713 | } |
---|
| 714 | |
---|
[209] | 715 | |
---|
[207] | 716 | void |
---|
| 717 | CognitiveRadioShell::StartShellServer() |
---|
| 718 | { |
---|
| 719 | struct timeval selTimeout; |
---|
| 720 | int32_t primary = 0; |
---|
| 721 | int32_t policy = 1; |
---|
| 722 | int32_t command = 2; |
---|
| 723 | int32_t running = 1; |
---|
| 724 | int32_t port, rc, new_sd = 1; |
---|
| 725 | int32_t desc_ready = 1; |
---|
[420] | 726 | int32_t timeout = 50; |
---|
[389] | 727 | int32_t ret = 0;; |
---|
[207] | 728 | fd_set sockSet; |
---|
[389] | 729 | |
---|
[210] | 730 | int32_t *servSock = new int32_t[3]; |
---|
[207] | 731 | |
---|
| 732 | servSock[primary] = CreateTCPServerSocket(primaryPort); |
---|
| 733 | servSock[policy] = CreateTCPServerSocket(policyPort); |
---|
| 734 | servSock[command] = CreateTCPServerSocket(commandPort); |
---|
| 735 | |
---|
[222] | 736 | int32_t maxDescriptor; |
---|
[207] | 737 | |
---|
[232] | 738 | if(servSock[primary] > servSock[policy]) |
---|
| 739 | maxDescriptor = servSock[primary]; |
---|
[222] | 740 | else |
---|
[232] | 741 | maxDescriptor = servSock[policy]; |
---|
[222] | 742 | |
---|
[232] | 743 | if(servSock[command] > maxDescriptor) |
---|
| 744 | maxDescriptor = servSock[command]; |
---|
[222] | 745 | |
---|
[215] | 746 | if(InitializeTCPServerPort(servSock[primary]) == -1) |
---|
| 747 | ERROR(1,"Error initializing primary port\n"); |
---|
[207] | 748 | |
---|
[215] | 749 | if(InitializeTCPServerPort(servSock[policy]) == -1) |
---|
| 750 | ERROR(1,"Error initializing policy port\n"); |
---|
[207] | 751 | |
---|
[215] | 752 | if(InitializeTCPServerPort(servSock[command]) == -1) |
---|
| 753 | ERROR(1,"Error initializing command port\n"); |
---|
[207] | 754 | |
---|
[389] | 755 | FD_ZERO(&sockSet); |
---|
| 756 | |
---|
[437] | 757 | while(running) { |
---|
[207] | 758 | /* Zero socket descriptor vector and set for server sockets */ |
---|
| 759 | /* This must be reset every time select() is called */ |
---|
| 760 | FD_SET(servSock[primary], &sockSet); |
---|
| 761 | FD_SET(servSock[policy], &sockSet); |
---|
| 762 | FD_SET(servSock[command], &sockSet); |
---|
| 763 | |
---|
| 764 | /* Timeout specification */ |
---|
| 765 | /* This must be reset every time select() is called */ |
---|
| 766 | selTimeout.tv_sec = timeout; /* timeout (secs.) */ |
---|
| 767 | selTimeout.tv_usec = 0; /* 0 microseconds */ |
---|
| 768 | |
---|
| 769 | /* Suspend program until descriptor is ready or timeout */ |
---|
[389] | 770 | rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
[209] | 771 | if(rc == 0) |
---|
[207] | 772 | LOG("No echo requests for %i secs...Server still alive\n", timeout); |
---|
[209] | 773 | else { |
---|
[207] | 774 | desc_ready = rc; |
---|
| 775 | |
---|
[209] | 776 | for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) { |
---|
| 777 | if(FD_ISSET(port, &sockSet)) { |
---|
| 778 | desc_ready -= 1; |
---|
[207] | 779 | /* Check if request is new or on an existing open descriptor */ |
---|
[437] | 780 | if((port == servSock[primary]) || \ |
---|
| 781 | (port == servSock[policy]) || \ |
---|
| 782 | (port == servSock[command])) { |
---|
[209] | 783 | do { |
---|
[207] | 784 | new_sd = AcceptTCPConnection(port); |
---|
[437] | 785 | if(new_sd < 0) |
---|
[207] | 786 | break; |
---|
[222] | 787 | if(port == servSock[primary]) |
---|
| 788 | ceSocketFD = new_sd; |
---|
| 789 | if(port == servSock[command]) |
---|
| 790 | commandSocketFD = new_sd; |
---|
| 791 | if(port == servSock[policy]) |
---|
| 792 | policySocketFD = new_sd; |
---|
| 793 | |
---|
[389] | 794 | ret = HandleMessage(new_sd); |
---|
| 795 | |
---|
[437] | 796 | if(ret == -1) { |
---|
| 797 | FD_CLR(new_sd,&sockSet); |
---|
| 798 | close(new_sd); |
---|
| 799 | break; |
---|
| 800 | } |
---|
[389] | 801 | |
---|
[437] | 802 | FD_SET(new_sd,&sockSet); |
---|
[207] | 803 | if(new_sd > maxDescriptor) |
---|
| 804 | maxDescriptor = new_sd; |
---|
[437] | 805 | |
---|
[207] | 806 | } while(new_sd != -1); |
---|
[437] | 807 | } |
---|
[209] | 808 | else { |
---|
[389] | 809 | ret = HandleMessage(port); |
---|
[437] | 810 | if(ret == -1) { |
---|
| 811 | FD_CLR(port,&sockSet); |
---|
| 812 | close(port); |
---|
| 813 | } |
---|
[207] | 814 | } |
---|
| 815 | } |
---|
| 816 | } |
---|
| 817 | } |
---|
| 818 | } |
---|
| 819 | |
---|
[389] | 820 | LOG("Closing it all.\n\n"); |
---|
[437] | 821 | |
---|
[207] | 822 | /* Close sockets */ |
---|
| 823 | close(servSock[primary]); |
---|
| 824 | close(servSock[policy]); |
---|
| 825 | close(servSock[command]); |
---|
| 826 | |
---|
| 827 | /* Free list of sockets */ |
---|
[210] | 828 | delete servSock; |
---|
[207] | 829 | |
---|
| 830 | return; |
---|
| 831 | } |
---|
[521] | 832 | |
---|