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

Revision 247, 9.1 KB (checked in by anonymous, 15 years ago)

Started work on SML-side CE socket

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 */ multiple clients
9
10
11#include <cstdlib>
12#include <cstring>
13#include <stdint.h>
14
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"
21
22uint32_t Current_ID = 0;
23uint32_t numCogEnginesPresent = 0;
24bool cogEnginePresent = false;
25
26
27ServiceManagementLayer::ServiceManagementLayer()
28{
29    LOG("Creating Service Management Layer.\n");
30    shellSocketFD = -1;
31    LoadConfiguration();
32}
33
34
35ServiceManagementLayer::~ServiceManagementLayer()
36{
37}
38
39
40ServiceManagementLayer::ServiceManagementLayer(const char* serverName, \
41        const char* serverPort)
42{
43    LOG("Creating Service Management Layer.\n");
44
45    ConnectToShell(serverName, serverPort);
46    CE_List = new CE_Reg[10];
47
48    LoadConfiguration();
49}
50
51
52void
53ServiceManagementLayer::SendComponentType()
54{
55    SendMessage(shellSocketFD, "response_sml");
56    LOG("SML responded to GetRemoteComponentType query.\n");
57}
58
59
60void
61ServiceManagementLayer::ConnectToShell(const char* serverName, \
62        const char* serverPort)
63{
64    shellSocketFD = ClientSocket(serverName, serverPort);
65
66    RegisterComponent();
67}
68
69
70void
71ServiceManagementLayer::ShellSignalHandler()
72{
73    char buffer[256];
74
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
107void
108ServiceManagementLayer::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
121void
122CognitiveRadioShell::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
134void
135CognitiveRadioShell::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        }
149        else if(strcmp(buffer, "reset_sml") == 0) {
150            Reset();
151        }
152        else if(strcmp(buffer, "shutdown_sml") == 0) {
153            Shutdown();
154        }
155    }
156}
157
158
159void
160ServiceManagementLayer::Shutdown()
161{
162    DeregisterComponent();
163}
164
165
166void
167ServiceManagementLayer::Reset()
168{
169    DeregisterComponent();
170    LoadConfiguration();
171}
172
173
174void
175ServiceManagementLayer::RegisterComponent()
176{
177    SendMessage(shellSocketFD, "register_sml");
178    LOG("ServiceManagementLayer:: Registration message sent.\n");
179}
180
181
182void
183ServiceManagementLayer::DeregisterComponent()
184{
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");
192}
193
194
195void
196ServiceManagementLayer::TransferRadioConfiguration(int32_t ID)
197{
198}
199
200
201void
202ServiceManagementLayer::TransferExperience(int32_t ID)
203{
204}
205
206
207void
208ServiceManagementLayer::ReceiveServices()
209{
210}
211
212
213void
214ServiceManagementLayer::SetActiveMission()
215{
216}
217
218
219void
220ServiceManagementLayer::ListServices()
221{
222}
223
224
225void
226ServiceManagementLayer::ReloadConfiguration()
227{
228    LOG("ServiceManagementLayer:: Reloading Configuration.\n");
229}
230
231
232void
233ServiceManagementLayer::LoadConfiguration()
234{
235    LOG("ServiceManagementLayer:: Loading Configuration.\n");
236}
237
238void
239CognitiveRadioShell::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
252void
253CognitiveRadioShell::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
267void
268CognitiveRadioShell::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
337rc = 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
Note: See TracBrowser for help on using the browser.