root/vtcross/branches/wrodgers/src/service_management_layer/ServiceManagementLayer_old.cpp @ 277

Revision 277, 9.0 KB (checked in by wrodgers, 15 years ago)

Updating SML functionality

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   recvmsg
192   while
193}
194
195
196void
197ServiceManagementLayer::TransferExperience(int32_t ID)
198{
199}
200
201
202void
203ServiceManagementLayer::ReceiveServices()
204{
205}
206
207
208void
209ServiceManagementLayer::SetActiveMission()
210{
211}
212
213
214void
215ServiceManagementLayer::ListServices()
216{
217}
218
219
220void
221ServiceManagementLayer::ReloadConfiguration()
222{
223    LOG("ServiceManagementLayer:: Reloading Configuration.\n");
224}
225
226
227void
228ServiceManagementLayer::LoadConfiguration()
229{
230    LOG("ServiceManagementLayer:: Loading Configuration.\n");
231}
232
233void
234ServiceManagementLayer::RegisterCognitiveEngine(int32_t ID)
235{
236    SendMessage(shellSocketFD, "register_engine_cognitive");
237    LOG("Cognitive Engine:: Registration message sent to shell.\n");
238
239    TransferRadioConfiguration(socketFD);
240    TransferExperience(socketFD);
241
242    numberOfCognitiveEngines++;
243    CE_Present = true;
244}
245
246
247void
248ServiceManagementLayer::DeregisterCognitiveEngine(int32_t ID)
249{
250    LOG("Cognitive Radio Shell:: Received deregistration message from Cognitive Engine.\n");
251
252    numberOfCognitiveEngines--;
253    if(numberOfCognitiveEngines == 0)
254        CE_Present = false;
255
256    SendMessage(socketFD, "deregister_ack");
257    shutdown(socketFD, 2);
258    close(socketFD);
259    LOG("Cognitive Radio Shell:: Socket closed.\n");
260}
261
262void
263ServiceManagementLayer::StartSMLServer()
264{
265    struct timeval selTimeout;
266    int32_t running = 1;
267    int32_t port, rc, new_sd = 1;
268    int32_t desc_ready = 1;
269    int32_t timeout = 10;
270    fd_set sockSet;
271
272    cogEngSrv = CreateTCPServerSocket(CEPort);
273    int32_t maxDescriptor = cogEngSrv;
274
275    if(InitializeTCPServerPort(cogEngSrv) == -1)
276        ERROR(1,"Error initializing primary port\n");
277
278    while (running) {
279        /* Zero socket descriptor vector and set for server sockets */
280        /* This must be reset every time select() is called */
281        FD_ZERO(&sockSet);
282        FD_SET(cogEngSrv, &sockSet);
283
284        /* Timeout specification */
285        /* This must be reset every time select() is called */
286        selTimeout.tv_sec = timeout;       /* timeout (secs.) */
287        selTimeout.tv_usec = 0;            /* 0 microseconds */
288
289        //Check if there is a message on the socket waiting to be read
290        rc = select(cogEngSrv + 1, &sockSet, NULL, NULL, &selTimeout);
291        if(rc == 0){
292            //If not, log that fact and check instead for messages from the shell
293            LOG("No echo requests for %i secs...Server still alive\n", timeout);
294            ShellSignalHandler();       
295        }
296        else {
297            //If so, process the address information and pass the component ID on to the handler
298            char buffer[256];
299            memset(buffer, 0, 256);
300            struct sockaddr_in sock_addr;
301            memset((void *) &sock_addr, 0, sizeof(sock_addr));
302            socklen_t sock_len;
303            //Peak at the next message on the socket to determine its address of orgin
304            recvfrom(cogEngSrv, buffer, 256, MSG_PEEK, (struct sockaddr*) &sock_addr, &sock_len);
305            bool found = false;
306            //Is it from a previously logged address?
307            for(int i = 0; i < Current_ID; i++){
308                if((*CE_List[i].sock_ptr).sin_addr.s_addr == sock_addr.sin_addr.s_addr){
309                        //If so, pass the ID number of that component into the signal handler to process the message
310                        found=true;
311                        CESignalHandler(i);
312                }
313            }
314            //If not, log the address and pass the ID number of that component into the signal handler
315            if(!found){
316                CE_List[Current_ID].sock_ptr = &sock_addr;
317                CE_List[Current_ID].ID_num = Current_ID;
318                CE_List[Current_ID].sock_len = sock_len;
319                CESignalHandler(Current_ID);
320                Current_ID++;
321            }
322        }
323    }       
324
325    /* Close sockets */
326    close(cogEngSrv);
327
328    return;
329}
330
331/*
332
333
334
335rc = select(cogEngSrv + 1, &sockSet, NULL, NULL, &selTimeout);
336        if(rc == 0){
337            LOG("No echo requests for %i secs...Server still alive\n", timeout);
338            ShellSignalHandler();       
339        }
340        else {
341            desc_ready = rc;
342
343            for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) {
344                if(FD_ISSET(port, &sockSet)) {
345                    desc_ready -= 1;
346
347                    //Check if request is new or on an existing open descriptor
348                    if(port == cogEngSrv) {
349                        do {
350                            new_sd = AcceptTCPConnection(port);
351                            if(new_sd < 0)
352                                break;
353                           
354                            CE_List[Current_ID].FD = new_sd;
355                            CE_List[Current_ID].IDNum = Current_ID;
356                            HandleMessage(CE_List[Current_ID]);
357                            Current_ID++;
358       
359                            FD_SET(new_sd,&sockSet);
360                            if(new_sd > maxDescriptor)
361                                maxDescriptor = new_sd;
362                            //LOG("New incoming connection - %i\n\n",new_sd);
363                        } while(new_sd != -1);
364                    }
365                    else {
366                        //LOG("Request on already open descriptor.\n\n");
367                        for(int16_t i = 0; i < Current_ID; i++)
368                        {
369                                if(CE_List[i].FD == port)
370                                        HandleMessage(CE_List[i]);
371                        }
372                    }
373                }
374            }
375    }
376*/
377
378
Note: See TracBrowser for help on using the browser.