70 | | while(true) { |
71 | | memset(buffer, 0, 256); |
72 | | |
73 | | ReadMessage(shellSocketFD, buffer); |
74 | | |
75 | | // TODO |
76 | | // If we send integer op codes rather than strings, this process will be |
77 | | // MUCH faster since instead of donig string compares we can simply |
78 | | // switch on the integer value... |
79 | | if(strcmp(buffer, "register_service") == 0) { |
80 | | if(strcmp(buffer, "policy_geo") == 0) { |
81 | | } |
82 | | else if(strcmp(buffer, "policy_time") == 0) { |
83 | | } |
84 | | else if(strcmp(buffer, "policy_spectrum") == 0) { |
85 | | } |
86 | | else if(strcmp(buffer, "policy_spacial") == 0) { |
87 | | } |
88 | | } |
89 | | else if(strcmp(buffer, "deregister_service") == 0) { |
90 | | if(strcmp(buffer, "policy_geo") == 0) { |
91 | | } |
92 | | else if(strcmp(buffer, "policy_time") == 0) { |
93 | | } |
94 | | else if(strcmp(buffer, "policy_spectrum") == 0) { |
95 | | } |
96 | | else if(strcmp(buffer, "policy_spacial") == 0) { |
97 | | } |
98 | | } |
99 | | else if(strcmp(buffer, "query_component_type") == 0) { |
100 | | SendComponentType(); |
| 75 | memset(buffer, 0, 256); |
| 76 | ReadMessage(shellSocketFD, buffer); |
| 77 | |
| 78 | // TODO |
| 79 | // If we send integer op codes rather than strings, this process will be |
| 80 | // MUCH faster since instead of donig string compares we can simply |
| 81 | // switch on the integer value... |
| 82 | if(strcmp(buffer, "register_service") == 0) { |
| 83 | if(strcmp(buffer, "policy_geo") == 0) { |
| 84 | } |
| 85 | else if(strcmp(buffer, "policy_time") == 0) { |
| 86 | } |
| 87 | else if(strcmp(buffer, "policy_spectrum") == 0) { |
| 88 | } |
| 89 | else if(strcmp(buffer, "policy_spacial") == 0) { |
| 90 | } |
| 91 | } |
| 92 | else if(strcmp(buffer, "deregister_service") == 0) { |
| 93 | if(strcmp(buffer, "policy_geo") == 0) { |
| 94 | } |
| 95 | else if(strcmp(buffer, "policy_time") == 0) { |
| 96 | } |
| 97 | else if(strcmp(buffer, "policy_spectrum") == 0) { |
| 98 | } |
| 99 | else if(strcmp(buffer, "policy_spacial") == 0) { |
| 100 | } |
| 101 | } |
| 102 | else if(strcmp(buffer, "query_component_type") == 0) { |
| 103 | SendComponentType(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | ServiceManagementLayer::CESignalHandler(int32_t ID) |
| 109 | { |
| 110 | char buffer[256]; |
| 111 | memset(buffer, 0, 256); |
| 112 | // |
| 113 | ReadMessage(cogEngSrv, buffer); |
| 114 | if(strcmp(buffer, "register_engine_cognitive") == 0) { |
| 115 | RegisterCognitiveEngine(ID); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | CognitiveRadioShell::RegisterCognitiveEngine(int32_t ID) |
| 123 | { |
| 124 | LOG("Cognitive Radio Shell:: Received registration message from Cognitive Engine.\n"); |
| 125 | |
| 126 | TransferRadioConfiguration(ID); |
| 127 | TransferRadioExperience(ID); |
| 128 | |
| 129 | numberOfCognitiveEngines++; |
| 130 | CE_present = true; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | void |
| 135 | CognitiveRadioShell::DeregisterCognitiveEngine(int32_t socketFD) |
| 136 | { |
| 137 | LOG("Cognitive Radio Shell:: Received deregistration message from Cognitive Engine.\n"); |
| 138 | |
| 139 | numberOfCognitiveEngines--; |
| 140 | if(numberOfCognitiveEngines == 0) |
| 141 | CE_present = false; |
| 142 | |
| 143 | SendMessage(socketFD, "deregister_ack"); |
| 144 | shutdown(socketFD, 2); |
| 145 | close(socketFD); |
| 146 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
| 147 | } |
| 238 | void |
| 239 | CognitiveRadioShell::RegisterCognitiveEngine(int32_t socketFD) |
| 240 | { |
| 241 | SendMessage(commandSocketFD, "register_engine_cognitive"); |
| 242 | LOG("Cognitive Engine:: Registration message sent to shell.\n"); |
| 243 | |
| 244 | TransferRadioConfiguration(socketFD); |
| 245 | TransferRadioExperience(socketFD); |
| 246 | |
| 247 | numberOfCognitiveEngines++; |
| 248 | CE_present = true; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | void |
| 253 | CognitiveRadioShell::DeregisterCognitiveEngine(int32_t socketFD) |
| 254 | { |
| 255 | LOG("Cognitive Radio Shell:: Received deregistration message from Cognitive Engine.\n"); |
| 256 | |
| 257 | numberOfCognitiveEngines--; |
| 258 | if(numberOfCognitiveEngines == 0) |
| 259 | CE_present = false; |
| 260 | |
| 261 | SendMessage(socketFD, "deregister_ack"); |
| 262 | shutdown(socketFD, 2); |
| 263 | close(socketFD); |
| 264 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
| 265 | } |
| 266 | |
| 267 | void |
| 268 | CognitiveRadioShell::StartSMLServer() |
| 269 | { |
| 270 | struct timeval selTimeout; |
| 271 | int32_t running = 1; |
| 272 | int32_t port, rc, new_sd = 1; |
| 273 | int32_t desc_ready = 1; |
| 274 | int32_t timeout = 10; |
| 275 | fd_set sockSet[1]; |
| 276 | |
| 277 | cogEngSrv = CreateTCPServerSocket(CEPort); |
| 278 | int32_t maxDescriptor = cogEngSrv; |
| 279 | |
| 280 | if(InitializeTCPServerPort(cogEngSrv) == -1) |
| 281 | ERROR(1,"Error initializing primary port\n"); |
| 282 | |
| 283 | while (running) { |
| 284 | /* Zero socket descriptor vector and set for server sockets */ |
| 285 | /* This must be reset every time select() is called */ |
| 286 | FD_ZERO(&sockSet); |
| 287 | FD_SET(cogEngSrv, &sockSet); |
| 288 | |
| 289 | /* Timeout specification */ |
| 290 | /* This must be reset every time select() is called */ |
| 291 | selTimeout.tv_sec = timeout; /* timeout (secs.) */ |
| 292 | selTimeout.tv_usec = 0; /* 0 microseconds */ |
| 293 | |
| 294 | //Check if there is a message on the socket waiting to be read |
| 295 | rc = select(cogEngSrv + 1, &sockSet, NULL, NULL, &selTimeout); |
| 296 | if(rc == 0){ |
| 297 | //If not, log that fact and check instead for messages from the shell |
| 298 | LOG("No echo requests for %i secs...Server still alive\n", timeout); |
| 299 | ShellSignalHandler(); |
| 300 | } |
| 301 | else { |
| 302 | //If so, process the address information and pass the component ID on to the handler |
| 303 | char buffer[256]; |
| 304 | memset(buffer, 0, 256); |
| 305 | sockaddr_in sock_addr = new sockaddr_in; |
| 306 | //Peak at the next message on the socket to determine its address of orgin |
| 307 | recvfrom(cogEngSrv, buffer, 256, MSG_PEAK, (struct sockaddr*) &sock_addr, NULL); |
| 308 | bool found = false; |
| 309 | //Is it from a previously logged address? |
| 310 | for(int i = 0; i < Current_ID; i++){ |
| 311 | if(CE_List[i].(*sock_ptr).sin_addr.s_addr == sock_addr.sin_addr.s_addr){ |
| 312 | //If so, pass the ID number of that component into the signal handler to process the message |
| 313 | CESignalHandler(i); |
| 314 | found=true; |
| 315 | } |
| 316 | } |
| 317 | //If not, log the address and pass the ID number of that component into the signal handler |
| 318 | if(!found){ |
| 319 | CE_List[Current_ID].sock_ptr = &sock_addr; |
| 320 | CE_List[Current_ID].ID_num = Current_ID; |
| 321 | CESignalHandler(Current_ID); |
| 322 | CurrentID++; |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /* Close sockets */ |
| 328 | close(cogEngSrv); |
| 329 | |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | |
| 335 | |
| 336 | |
| 337 | rc = select(cogEngSrv + 1, &sockSet, NULL, NULL, &selTimeout); |
| 338 | if(rc == 0){ |
| 339 | LOG("No echo requests for %i secs...Server still alive\n", timeout); |
| 340 | ShellSignalHandler(); |
| 341 | } |
| 342 | else { |
| 343 | desc_ready = rc; |
| 344 | |
| 345 | for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) { |
| 346 | if(FD_ISSET(port, &sockSet)) { |
| 347 | desc_ready -= 1; |
| 348 | |
| 349 | //Check if request is new or on an existing open descriptor |
| 350 | if(port == cogEngSrv) { |
| 351 | do { |
| 352 | new_sd = AcceptTCPConnection(port); |
| 353 | if(new_sd < 0) |
| 354 | break; |
| 355 | |
| 356 | CE_List[Current_ID].FD = new_sd; |
| 357 | CE_List[Current_ID].IDNum = Current_ID; |
| 358 | HandleMessage(CE_List[Current_ID]); |
| 359 | Current_ID++; |
| 360 | |
| 361 | FD_SET(new_sd,&sockSet); |
| 362 | if(new_sd > maxDescriptor) |
| 363 | maxDescriptor = new_sd; |
| 364 | //LOG("New incoming connection - %i\n\n",new_sd); |
| 365 | } while(new_sd != -1); |
| 366 | } |
| 367 | else { |
| 368 | //LOG("Request on already open descriptor.\n\n"); |
| 369 | for(int16_t i = 0; i < Current_ID; i++) |
| 370 | { |
| 371 | if(CE_List[i].FD == port) |
| 372 | HandleMessage(CE_List[i]); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | */ |
| 379 | |
| 380 | |