[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. |
---|
[248] | 8 | */ |
---|
[163] | 9 | |
---|
| 10 | #include <cstdlib> |
---|
| 11 | #include <cstring> |
---|
| 12 | #include <stdint.h> |
---|
| 13 | |
---|
[248] | 14 | #include "../../trunk/src/include/vtcross/common.h" |
---|
| 15 | #include "../../trunk/src/include/vtcross/components.h" |
---|
| 16 | #include "../../trunk/src/include/vtcross/containers.h" |
---|
| 17 | #include "../../trunk/src/include/vtcross/debug.h" |
---|
| 18 | #include "../../trunk/src/include/vtcross/error.h" |
---|
| 19 | #include "../../trunk/src/include/vtcross/socketcomm.h" |
---|
[163] | 20 | |
---|
[248] | 21 | |
---|
| 22 | #include <cstdlib> |
---|
| 23 | #include <cstring> |
---|
| 24 | #include <stdint.h> |
---|
| 25 | #include <math.h> |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | #include <arpa/inet.h> |
---|
| 29 | #include <iostream> |
---|
| 30 | #include <netinet/in.h> |
---|
| 31 | #include <netdb.h> |
---|
| 32 | #include <fcntl.h> |
---|
| 33 | #include <sys/ioctl.h> |
---|
| 34 | #include <sys/mman.h> |
---|
| 35 | #include <sys/socket.h> |
---|
| 36 | #include <sys/types.h> |
---|
| 37 | #include <sys/wait.h> |
---|
| 38 | |
---|
[250] | 39 | #include "../../trunk/src/include/tinyxml/tinyxml.h" |
---|
| 40 | #include "../../trunk/src/include/tinyxml/tinystr.h" |
---|
[248] | 41 | |
---|
| 42 | |
---|
[163] | 43 | |
---|
[247] | 44 | |
---|
[163] | 45 | ServiceManagementLayer::ServiceManagementLayer() |
---|
| 46 | { |
---|
[164] | 47 | LOG("Creating Service Management Layer.\n"); |
---|
[204] | 48 | shellSocketFD = -1; |
---|
[248] | 49 | numberOfCognitiveEngines = 0; |
---|
[250] | 50 | CE_Present = false; |
---|
| 51 | cogEngSrv = 1; |
---|
| 52 | |
---|
| 53 | Current_ID = 0; |
---|
| 54 | LoadConfiguration(); |
---|
[163] | 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | ServiceManagementLayer::~ServiceManagementLayer() |
---|
| 59 | { |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | |
---|
[204] | 63 | ServiceManagementLayer::ServiceManagementLayer(const char* serverName, \ |
---|
| 64 | const char* serverPort) |
---|
| 65 | { |
---|
| 66 | LOG("Creating Service Management Layer.\n"); |
---|
| 67 | |
---|
| 68 | ConnectToShell(serverName, serverPort); |
---|
[247] | 69 | CE_List = new CE_Reg[10]; |
---|
[204] | 70 | |
---|
| 71 | LoadConfiguration(); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
[163] | 75 | void |
---|
[200] | 76 | ServiceManagementLayer::SendComponentType() |
---|
[163] | 77 | { |
---|
[204] | 78 | SendMessage(shellSocketFD, "response_sml"); |
---|
| 79 | LOG("SML responded to GetRemoteComponentType query.\n"); |
---|
[163] | 80 | } |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | void |
---|
[204] | 84 | ServiceManagementLayer::ConnectToShell(const char* serverName, \ |
---|
| 85 | const char* serverPort) |
---|
[200] | 86 | { |
---|
[204] | 87 | shellSocketFD = ClientSocket(serverName, serverPort); |
---|
| 88 | |
---|
| 89 | RegisterComponent(); |
---|
[200] | 90 | } |
---|
[164] | 91 | |
---|
[200] | 92 | |
---|
| 93 | void |
---|
[247] | 94 | ServiceManagementLayer::ShellSignalHandler() |
---|
[200] | 95 | { |
---|
[206] | 96 | char buffer[256]; |
---|
| 97 | |
---|
[247] | 98 | memset(buffer, 0, 256); |
---|
| 99 | ReadMessage(shellSocketFD, buffer); |
---|
[206] | 100 | |
---|
[247] | 101 | // TODO |
---|
| 102 | // If we send integer op codes rather than strings, this process will be |
---|
| 103 | // MUCH faster since instead of donig string compares we can simply |
---|
| 104 | // switch on the integer value... |
---|
| 105 | if(strcmp(buffer, "register_service") == 0) { |
---|
| 106 | if(strcmp(buffer, "policy_geo") == 0) { |
---|
[206] | 107 | } |
---|
[247] | 108 | else if(strcmp(buffer, "policy_time") == 0) { |
---|
[206] | 109 | } |
---|
[247] | 110 | else if(strcmp(buffer, "policy_spectrum") == 0) { |
---|
[206] | 111 | } |
---|
[247] | 112 | else if(strcmp(buffer, "policy_spacial") == 0) { |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | else if(strcmp(buffer, "deregister_service") == 0) { |
---|
| 116 | if(strcmp(buffer, "policy_geo") == 0) { |
---|
| 117 | } |
---|
| 118 | else if(strcmp(buffer, "policy_time") == 0) { |
---|
| 119 | } |
---|
| 120 | else if(strcmp(buffer, "policy_spectrum") == 0) { |
---|
| 121 | } |
---|
| 122 | else if(strcmp(buffer, "policy_spacial") == 0) { |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | else if(strcmp(buffer, "query_component_type") == 0) { |
---|
| 126 | SendComponentType(); |
---|
[248] | 127 | } |
---|
| 128 | else if(strcmp(buffer, "reset_sml") == 0) { |
---|
| 129 | Reset(); |
---|
| 130 | } |
---|
| 131 | else if(strcmp(buffer, "shutdown_sml") == 0) { |
---|
| 132 | Shutdown(); |
---|
| 133 | } |
---|
[247] | 134 | } |
---|
| 135 | |
---|
| 136 | void |
---|
| 137 | ServiceManagementLayer::CESignalHandler(int32_t ID) |
---|
| 138 | { |
---|
| 139 | char buffer[256]; |
---|
| 140 | memset(buffer, 0, 256); |
---|
| 141 | // |
---|
| 142 | ReadMessage(cogEngSrv, buffer); |
---|
| 143 | if(strcmp(buffer, "register_engine_cognitive") == 0) { |
---|
| 144 | RegisterCognitiveEngine(ID); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | void |
---|
[163] | 153 | ServiceManagementLayer::Shutdown() |
---|
| 154 | { |
---|
[204] | 155 | DeregisterComponent(); |
---|
[163] | 156 | } |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | void |
---|
| 160 | ServiceManagementLayer::Reset() |
---|
| 161 | { |
---|
[204] | 162 | DeregisterComponent(); |
---|
| 163 | LoadConfiguration(); |
---|
[163] | 164 | } |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | void |
---|
[200] | 168 | ServiceManagementLayer::RegisterComponent() |
---|
[163] | 169 | { |
---|
[200] | 170 | SendMessage(shellSocketFD, "register_sml"); |
---|
[164] | 171 | LOG("ServiceManagementLayer:: Registration message sent.\n"); |
---|
[163] | 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | void |
---|
[200] | 176 | ServiceManagementLayer::DeregisterComponent() |
---|
[163] | 177 | { |
---|
[204] | 178 | SendMessage(shellSocketFD, "deregister_sml"); |
---|
| 179 | LOG("ServiceManagementLayer:: Deregistration message sent.\n"); |
---|
| 180 | |
---|
| 181 | shutdown(shellSocketFD, 2); |
---|
| 182 | close(shellSocketFD); |
---|
| 183 | shellSocketFD = -1; |
---|
| 184 | LOG("ServiceManagementLayer:: Shell socket closed.\n"); |
---|
[163] | 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | void |
---|
[247] | 189 | ServiceManagementLayer::TransferRadioConfiguration(int32_t ID) |
---|
[163] | 190 | { |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | void |
---|
[247] | 195 | ServiceManagementLayer::TransferExperience(int32_t ID) |
---|
[163] | 196 | { |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | void |
---|
| 201 | ServiceManagementLayer::ReceiveServices() |
---|
| 202 | { |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | void |
---|
| 207 | ServiceManagementLayer::SetActiveMission() |
---|
| 208 | { |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | void |
---|
| 213 | ServiceManagementLayer::ListServices() |
---|
| 214 | { |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | void |
---|
| 219 | ServiceManagementLayer::ReloadConfiguration() |
---|
| 220 | { |
---|
[204] | 221 | LOG("ServiceManagementLayer:: Reloading Configuration.\n"); |
---|
[163] | 222 | } |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | void |
---|
| 226 | ServiceManagementLayer::LoadConfiguration() |
---|
| 227 | { |
---|
[164] | 228 | LOG("ServiceManagementLayer:: Loading Configuration.\n"); |
---|
[163] | 229 | } |
---|
| 230 | |
---|
[247] | 231 | void |
---|
[248] | 232 | ServiceManagementLayer::RegisterCognitiveEngine(int32_t socketFD) |
---|
[247] | 233 | { |
---|
[250] | 234 | SendMessage(shellSocketFD, "register_engine_cognitive"); |
---|
[247] | 235 | LOG("Cognitive Engine:: Registration message sent to shell.\n"); |
---|
| 236 | |
---|
| 237 | TransferRadioConfiguration(socketFD); |
---|
[248] | 238 | TransferExperience(socketFD); |
---|
[247] | 239 | |
---|
| 240 | numberOfCognitiveEngines++; |
---|
[250] | 241 | CE_Present = true; |
---|
[247] | 242 | } |
---|
| 243 | |
---|
| 244 | |
---|
| 245 | void |
---|
[248] | 246 | ServiceManagementLayer::DeregisterCognitiveEngine(int32_t socketFD) |
---|
[247] | 247 | { |
---|
| 248 | LOG("Cognitive Radio Shell:: Received deregistration message from Cognitive Engine.\n"); |
---|
| 249 | |
---|
| 250 | numberOfCognitiveEngines--; |
---|
| 251 | if(numberOfCognitiveEngines == 0) |
---|
[250] | 252 | CE_Present = false; |
---|
[247] | 253 | |
---|
| 254 | SendMessage(socketFD, "deregister_ack"); |
---|
| 255 | shutdown(socketFD, 2); |
---|
| 256 | close(socketFD); |
---|
| 257 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | void |
---|
[248] | 261 | ServiceManagementLayer::StartSMLServer() |
---|
[247] | 262 | { |
---|
| 263 | struct timeval selTimeout; |
---|
| 264 | int32_t running = 1; |
---|
| 265 | int32_t port, rc, new_sd = 1; |
---|
| 266 | int32_t desc_ready = 1; |
---|
| 267 | int32_t timeout = 10; |
---|
[250] | 268 | fd_set sockSet; |
---|
[247] | 269 | |
---|
| 270 | cogEngSrv = CreateTCPServerSocket(CEPort); |
---|
| 271 | int32_t maxDescriptor = cogEngSrv; |
---|
| 272 | |
---|
[250] | 273 | if(InitializeTCPServerPort(cogEngSrv) == -1) |
---|
[247] | 274 | ERROR(1,"Error initializing primary port\n"); |
---|
| 275 | |
---|
| 276 | while (running) { |
---|
| 277 | /* Zero socket descriptor vector and set for server sockets */ |
---|
| 278 | /* This must be reset every time select() is called */ |
---|
| 279 | FD_ZERO(&sockSet); |
---|
| 280 | FD_SET(cogEngSrv, &sockSet); |
---|
| 281 | |
---|
| 282 | /* Timeout specification */ |
---|
| 283 | /* This must be reset every time select() is called */ |
---|
| 284 | selTimeout.tv_sec = timeout; /* timeout (secs.) */ |
---|
| 285 | selTimeout.tv_usec = 0; /* 0 microseconds */ |
---|
| 286 | |
---|
| 287 | //Check if there is a message on the socket waiting to be read |
---|
| 288 | rc = select(cogEngSrv + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
| 289 | if(rc == 0){ |
---|
| 290 | //If not, log that fact and check instead for messages from the shell |
---|
| 291 | LOG("No echo requests for %i secs...Server still alive\n", timeout); |
---|
| 292 | ShellSignalHandler(); |
---|
| 293 | } |
---|
| 294 | else { |
---|
| 295 | //If so, process the address information and pass the component ID on to the handler |
---|
| 296 | char buffer[256]; |
---|
| 297 | memset(buffer, 0, 256); |
---|
[250] | 298 | struct sockaddr_in sock_addr; |
---|
| 299 | memset((void *) &sock_addr, 0, sizeof(sock_addr)); |
---|
[247] | 300 | //Peak at the next message on the socket to determine its address of orgin |
---|
[250] | 301 | recvfrom(cogEngSrv, buffer, 256, MSG_PEEK, (struct sockaddr*) &sock_addr, NULL); |
---|
[247] | 302 | bool found = false; |
---|
| 303 | //Is it from a previously logged address? |
---|
| 304 | for(int i = 0; i < Current_ID; i++){ |
---|
[250] | 305 | if((*CE_List[i].sock_ptr).sin_addr.s_addr == sock_addr.sin_addr.s_addr){ |
---|
[247] | 306 | //If so, pass the ID number of that component into the signal handler to process the message |
---|
| 307 | found=true; |
---|
[250] | 308 | CESignalHandler(i); |
---|
[247] | 309 | } |
---|
| 310 | } |
---|
| 311 | //If not, log the address and pass the ID number of that component into the signal handler |
---|
| 312 | if(!found){ |
---|
| 313 | CE_List[Current_ID].sock_ptr = &sock_addr; |
---|
| 314 | CE_List[Current_ID].ID_num = Current_ID; |
---|
| 315 | CESignalHandler(Current_ID); |
---|
[250] | 316 | Current_ID++; |
---|
[247] | 317 | } |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | /* Close sockets */ |
---|
| 322 | close(cogEngSrv); |
---|
| 323 | |
---|
| 324 | return; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | /* |
---|
| 328 | |
---|
| 329 | |
---|
| 330 | |
---|
| 331 | rc = select(cogEngSrv + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
| 332 | if(rc == 0){ |
---|
| 333 | LOG("No echo requests for %i secs...Server still alive\n", timeout); |
---|
| 334 | ShellSignalHandler(); |
---|
| 335 | } |
---|
| 336 | else { |
---|
| 337 | desc_ready = rc; |
---|
| 338 | |
---|
| 339 | for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) { |
---|
| 340 | if(FD_ISSET(port, &sockSet)) { |
---|
| 341 | desc_ready -= 1; |
---|
| 342 | |
---|
| 343 | //Check if request is new or on an existing open descriptor |
---|
| 344 | if(port == cogEngSrv) { |
---|
| 345 | do { |
---|
| 346 | new_sd = AcceptTCPConnection(port); |
---|
| 347 | if(new_sd < 0) |
---|
| 348 | break; |
---|
| 349 | |
---|
| 350 | CE_List[Current_ID].FD = new_sd; |
---|
| 351 | CE_List[Current_ID].IDNum = Current_ID; |
---|
| 352 | HandleMessage(CE_List[Current_ID]); |
---|
| 353 | Current_ID++; |
---|
| 354 | |
---|
| 355 | FD_SET(new_sd,&sockSet); |
---|
| 356 | if(new_sd > maxDescriptor) |
---|
| 357 | maxDescriptor = new_sd; |
---|
| 358 | //LOG("New incoming connection - %i\n\n",new_sd); |
---|
| 359 | } while(new_sd != -1); |
---|
| 360 | } |
---|
| 361 | else { |
---|
| 362 | //LOG("Request on already open descriptor.\n\n"); |
---|
| 363 | for(int16_t i = 0; i < Current_ID; i++) |
---|
| 364 | { |
---|
| 365 | if(CE_List[i].FD == port) |
---|
| 366 | HandleMessage(CE_List[i]); |
---|
| 367 | } |
---|
| 368 | } |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | } |
---|
| 372 | */ |
---|
| 373 | |
---|
| 374 | |
---|