root/vtcross/branches/wrodgers/ServiceManagementLayer.cpp @ 250

Revision 250, 8.9 KB (checked in by wrodgers, 15 years ago)

Fixed SML compile errors

Line 
1/* Virginia Tech Cognitive Radio Open Source Systems
2 * Virginia Tech, 2009
3 *
4 * LICENSE INFORMATION GOES HERE
5 */
6
7/* DESCRIPTION OF FILE.
8 */
9
10#include <cstdlib>
11#include <cstring>
12#include <stdint.h>
13
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"
20
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
39#include "../../trunk/src/include/tinyxml/tinyxml.h"
40#include "../../trunk/src/include/tinyxml/tinystr.h"
41
42
43
44
45ServiceManagementLayer::ServiceManagementLayer()
46{
47    LOG("Creating Service Management Layer.\n");
48    shellSocketFD = -1;
49    numberOfCognitiveEngines = 0;
50    CE_Present = false;
51    cogEngSrv = 1;
52
53    Current_ID = 0;
54    LoadConfiguration();
55}
56
57
58ServiceManagementLayer::~ServiceManagementLayer()
59{
60}
61
62
63ServiceManagementLayer::ServiceManagementLayer(const char* serverName, \
64        const char* serverPort)
65{
66    LOG("Creating Service Management Layer.\n");
67
68    ConnectToShell(serverName, serverPort);
69    CE_List = new CE_Reg[10];
70
71    LoadConfiguration();
72}
73
74
75void
76ServiceManagementLayer::SendComponentType()
77{
78    SendMessage(shellSocketFD, "response_sml");
79    LOG("SML responded to GetRemoteComponentType query.\n");
80}
81
82
83void
84ServiceManagementLayer::ConnectToShell(const char* serverName, \
85        const char* serverPort)
86{
87    shellSocketFD = ClientSocket(serverName, serverPort);
88
89    RegisterComponent();
90}
91
92
93void
94ServiceManagementLayer::ShellSignalHandler()
95{
96    char buffer[256];
97
98    memset(buffer, 0, 256);
99    ReadMessage(shellSocketFD, buffer);
100
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) {
107        }
108        else if(strcmp(buffer, "policy_time") == 0) {
109        }
110        else if(strcmp(buffer, "policy_spectrum") == 0) {
111        }
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();
127    }
128    else if(strcmp(buffer, "reset_sml") == 0) {
129        Reset();
130    }
131    else if(strcmp(buffer, "shutdown_sml") == 0) {
132        Shutdown();
133    }
134}
135
136void
137ServiceManagementLayer::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
152void
153ServiceManagementLayer::Shutdown()
154{
155    DeregisterComponent();
156}
157
158
159void
160ServiceManagementLayer::Reset()
161{
162    DeregisterComponent();
163    LoadConfiguration();
164}
165
166
167void
168ServiceManagementLayer::RegisterComponent()
169{
170    SendMessage(shellSocketFD, "register_sml");
171    LOG("ServiceManagementLayer:: Registration message sent.\n");
172}
173
174
175void
176ServiceManagementLayer::DeregisterComponent()
177{
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");
185}
186
187
188void
189ServiceManagementLayer::TransferRadioConfiguration(int32_t ID)
190{
191}
192
193
194void
195ServiceManagementLayer::TransferExperience(int32_t ID)
196{
197}
198
199
200void
201ServiceManagementLayer::ReceiveServices()
202{
203}
204
205
206void
207ServiceManagementLayer::SetActiveMission()
208{
209}
210
211
212void
213ServiceManagementLayer::ListServices()
214{
215}
216
217
218void
219ServiceManagementLayer::ReloadConfiguration()
220{
221    LOG("ServiceManagementLayer:: Reloading Configuration.\n");
222}
223
224
225void
226ServiceManagementLayer::LoadConfiguration()
227{
228    LOG("ServiceManagementLayer:: Loading Configuration.\n");
229}
230
231void
232ServiceManagementLayer::RegisterCognitiveEngine(int32_t socketFD)
233{
234    SendMessage(shellSocketFD, "register_engine_cognitive");
235    LOG("Cognitive Engine:: Registration message sent to shell.\n");
236
237    TransferRadioConfiguration(socketFD);
238    TransferExperience(socketFD);
239
240    numberOfCognitiveEngines++;
241    CE_Present = true;
242}
243
244
245void
246ServiceManagementLayer::DeregisterCognitiveEngine(int32_t socketFD)
247{
248    LOG("Cognitive Radio Shell:: Received deregistration message from Cognitive Engine.\n");
249
250    numberOfCognitiveEngines--;
251    if(numberOfCognitiveEngines == 0)
252        CE_Present = false;
253
254    SendMessage(socketFD, "deregister_ack");
255    shutdown(socketFD, 2);
256    close(socketFD);
257    LOG("Cognitive Radio Shell:: Socket closed.\n");
258}
259
260void
261ServiceManagementLayer::StartSMLServer()
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;
268    fd_set sockSet;
269
270    cogEngSrv = CreateTCPServerSocket(CEPort);
271    int32_t maxDescriptor = cogEngSrv;
272
273    if(InitializeTCPServerPort(cogEngSrv) == -1)
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);
298            struct sockaddr_in sock_addr;
299            memset((void *) &sock_addr, 0, sizeof(sock_addr));
300            //Peak at the next message on the socket to determine its address of orgin
301            recvfrom(cogEngSrv, buffer, 256, MSG_PEEK, (struct sockaddr*) &sock_addr, NULL);
302            bool found = false;
303            //Is it from a previously logged address?
304            for(int i = 0; i < Current_ID; i++){
305                if((*CE_List[i].sock_ptr).sin_addr.s_addr == sock_addr.sin_addr.s_addr){
306                        //If so, pass the ID number of that component into the signal handler to process the message
307                        found=true;
308                        CESignalHandler(i);
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);
316                Current_ID++;
317            }
318        }
319    }       
320
321    /* Close sockets */
322    close(cogEngSrv);
323
324    return;
325}
326
327/*
328
329
330
331rc = 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
Note: See TracBrowser for help on using the browser.