[163] | 1 | /* Virginia Tech Cognitive Radio Open Source Systems |
---|
| 2 | * Virginia Tech, 2009 |
---|
| 3 | * |
---|
| 4 | * LICENSE INFORMATION GOES HERE |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /* DESCRIPTION OF FILE. |
---|
[247] | 8 | */ multiple clients |
---|
[163] | 9 | |
---|
| 10 | |
---|
| 11 | #include <cstdlib> |
---|
| 12 | #include <cstring> |
---|
| 13 | #include <stdint.h> |
---|
| 14 | |
---|
[247] | 15 | #include "../include/vtcross/common.h" |
---|
| 16 | #include "../include/vtcross/components.h" |
---|
| 17 | #include "../include/vtcross/containers.h" |
---|
| 18 | #include "../include/vtcross/debug.h" |
---|
| 19 | #include "../include/vtcross/error.h" |
---|
| 20 | #include "../include/vtcross/socketcomm.h" |
---|
[163] | 21 | |
---|
[247] | 22 | uint32_t Current_ID = 0; |
---|
| 23 | uint32_t numCogEnginesPresent = 0; |
---|
| 24 | bool cogEnginePresent = false; |
---|
[163] | 25 | |
---|
[247] | 26 | |
---|
[163] | 27 | ServiceManagementLayer::ServiceManagementLayer() |
---|
| 28 | { |
---|
[164] | 29 | LOG("Creating Service Management Layer.\n"); |
---|
[204] | 30 | shellSocketFD = -1; |
---|
[164] | 31 | LoadConfiguration(); |
---|
[163] | 32 | } |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | ServiceManagementLayer::~ServiceManagementLayer() |
---|
| 36 | { |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | |
---|
[204] | 40 | ServiceManagementLayer::ServiceManagementLayer(const char* serverName, \ |
---|
| 41 | const char* serverPort) |
---|
| 42 | { |
---|
| 43 | LOG("Creating Service Management Layer.\n"); |
---|
| 44 | |
---|
| 45 | ConnectToShell(serverName, serverPort); |
---|
[247] | 46 | CE_List = new CE_Reg[10]; |
---|
[204] | 47 | |
---|
| 48 | LoadConfiguration(); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
[163] | 52 | void |
---|
[200] | 53 | ServiceManagementLayer::SendComponentType() |
---|
[163] | 54 | { |
---|
[204] | 55 | SendMessage(shellSocketFD, "response_sml"); |
---|
| 56 | LOG("SML responded to GetRemoteComponentType query.\n"); |
---|
[163] | 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | void |
---|
[204] | 61 | ServiceManagementLayer::ConnectToShell(const char* serverName, \ |
---|
| 62 | const char* serverPort) |
---|
[200] | 63 | { |
---|
[204] | 64 | shellSocketFD = ClientSocket(serverName, serverPort); |
---|
| 65 | |
---|
| 66 | RegisterComponent(); |
---|
[200] | 67 | } |
---|
[164] | 68 | |
---|
[200] | 69 | |
---|
| 70 | void |
---|
[247] | 71 | ServiceManagementLayer::ShellSignalHandler() |
---|
[200] | 72 | { |
---|
[206] | 73 | char buffer[256]; |
---|
| 74 | |
---|
[247] | 75 | memset(buffer, 0, 256); |
---|
| 76 | ReadMessage(shellSocketFD, buffer); |
---|
[206] | 77 | |
---|
[247] | 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) { |
---|
[206] | 84 | } |
---|
[247] | 85 | else if(strcmp(buffer, "policy_time") == 0) { |
---|
[206] | 86 | } |
---|
[247] | 87 | else if(strcmp(buffer, "policy_spectrum") == 0) { |
---|
[206] | 88 | } |
---|
[247] | 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 | } |
---|
| 148 | } |
---|
[206] | 149 | else if(strcmp(buffer, "reset_sml") == 0) { |
---|
| 150 | Reset(); |
---|
| 151 | } |
---|
| 152 | else if(strcmp(buffer, "shutdown_sml") == 0) { |
---|
| 153 | Shutdown(); |
---|
| 154 | } |
---|
| 155 | } |
---|
[163] | 156 | } |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | void |
---|
| 160 | ServiceManagementLayer::Shutdown() |
---|
| 161 | { |
---|
[204] | 162 | DeregisterComponent(); |
---|
[163] | 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | void |
---|
| 167 | ServiceManagementLayer::Reset() |
---|
| 168 | { |
---|
[204] | 169 | DeregisterComponent(); |
---|
| 170 | LoadConfiguration(); |
---|
[163] | 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | void |
---|
[200] | 175 | ServiceManagementLayer::RegisterComponent() |
---|
[163] | 176 | { |
---|
[200] | 177 | SendMessage(shellSocketFD, "register_sml"); |
---|
[164] | 178 | LOG("ServiceManagementLayer:: Registration message sent.\n"); |
---|
[163] | 179 | } |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | void |
---|
[200] | 183 | ServiceManagementLayer::DeregisterComponent() |
---|
[163] | 184 | { |
---|
[204] | 185 | SendMessage(shellSocketFD, "deregister_sml"); |
---|
| 186 | LOG("ServiceManagementLayer:: Deregistration message sent.\n"); |
---|
| 187 | |
---|
| 188 | shutdown(shellSocketFD, 2); |
---|
| 189 | close(shellSocketFD); |
---|
| 190 | shellSocketFD = -1; |
---|
| 191 | LOG("ServiceManagementLayer:: Shell socket closed.\n"); |
---|
[163] | 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | void |
---|
[247] | 196 | ServiceManagementLayer::TransferRadioConfiguration(int32_t ID) |
---|
[163] | 197 | { |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | void |
---|
[247] | 202 | ServiceManagementLayer::TransferExperience(int32_t ID) |
---|
[163] | 203 | { |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | void |
---|
| 208 | ServiceManagementLayer::ReceiveServices() |
---|
| 209 | { |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | void |
---|
| 214 | ServiceManagementLayer::SetActiveMission() |
---|
| 215 | { |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | void |
---|
| 220 | ServiceManagementLayer::ListServices() |
---|
| 221 | { |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | void |
---|
| 226 | ServiceManagementLayer::ReloadConfiguration() |
---|
| 227 | { |
---|
[204] | 228 | LOG("ServiceManagementLayer:: Reloading Configuration.\n"); |
---|
[163] | 229 | } |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | void |
---|
| 233 | ServiceManagementLayer::LoadConfiguration() |
---|
| 234 | { |
---|
[164] | 235 | LOG("ServiceManagementLayer:: Loading Configuration.\n"); |
---|
[163] | 236 | } |
---|
| 237 | |
---|
[247] | 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 | |
---|