root/vtcross/trunk/src/shell/CognitiveRadioShell.cpp @ 207

Revision 207, 13.7 KB (checked in by jgaeddert, 15 years ago)

Added CognitiveRadioShell? class and Shell demo.

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
11#include <cstdlib>
12#include <cstring>
13#include <stdint.h>
14
15#include <arpa/inet.h>
16#include <iostream>
17#include <netinet/in.h>
18#include <netdb.h>
19#include <fcntl.h>
20#include <sys/ioctl.h>
21#include <sys/mman.h>
22#include <sys/socket.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25
26#include "tinyxml/tinyxml.h"
27#include "tinyxml/tinystr.h"
28
29#include "vtcross/common.h"
30#include "vtcross/components.h"
31#include "vtcross/containers.h"
32#include "vtcross/debug.h"
33#include "vtcross/error.h"
34#include "vtcross/socketcomm.h"
35
36
37CognitiveRadioShell::CognitiveRadioShell()
38{
39    LOG("Creating Cognitive Radio Shell.\n");
40    SML_present = false;
41    PE_present = false;
42    CE_present = false;
43}
44
45
46CognitiveRadioShell::~CognitiveRadioShell()
47{
48    delete [] p;
49    delete [] o;
50    delete [] u;
51}
52
53
54CognitiveRadioShell::CognitiveRadioShell(const char* radioConfig, int16_t p1, \
55        int16_t p2, int16_t p3)
56{
57    LOG("Creating Cognitive Radio Shell.\n");
58
59    p = new Parameter[10];
60    o = new Observable[10];
61    u = new Utility[10];
62    radio_info = new Radio_Info;
63
64
65    LoadRadioConfiguration(radioConfig,p,u,o,radio_info);
66
67    primaryPort = p1;
68    policyPort = p2;
69    commandPort = p3;
70}
71
72
73void
74CognitiveRadioShell::SendComponentType(int32_t socketFD)
75{
76    SendMessage(socketFD, "response_shell");
77    LOG("Cognitive Radio Shell responded to GetRemoteComponentType query.\n");
78}
79
80void
81CognitiveRadioShell::Shutdown()
82{
83    // TODO should something else be happening here?
84}
85
86
87void
88CognitiveRadioShell::Reset()
89{
90    LOG("Resetting Cognitive Radio Shell.\n");
91}
92
93
94void
95CognitiveRadioShell::SendRadioConfiguration(int32_t socketFD)
96{
97    LOG("Cognitive Radio Shell:: Sending radio configuration to Cognitive Engine.\n");
98}
99
100void
101CognitiveRadioShell::SendRadioExperience(int32_t socketFD)
102{
103
104    LOG("Cognitive Radio Shell:: Sending radio experience to Cognitive Engine.\n");
105}
106
107void
108CognitiveRadioShell::RegisterCognitiveEngine(int32_t socketFD)
109{
110    LOG("Cognitive Radio Shell:: Received registration message from Cognitive Engine.\n");
111   
112    SendRadioConfiguration(socketFD);
113    SendRadioExperience(socketFD);
114
115    numberOfCognitiveEngines++;
116    CE_present = true;
117}
118
119void
120CognitiveRadioShell::DeregisterCognitiveEngine(int32_t socketFD)
121{
122    LOG("Cognitive Radio Shell:: Received deregistration message from Cognitive Engine.\n");
123
124    numberOfCognitiveEngines--;
125    if(numberOfCognitiveEngines == 0)
126        CE_present = false;
127
128    SendMessage(socketFD, "deregister_ack");
129    shutdown(socketFD, 2);
130    close(socketFD);
131    LOG("Cognitive Radio Shell:: Socket closed.\n");
132}
133
134void
135CognitiveRadioShell::RegisterPolicyEngine(int32_t socketFD)
136{
137    LOG("Cognitive Radio Shell:: Received registration message from Policy Engine.\n");
138    PE_present = true;
139}
140
141void
142CognitiveRadioShell::DeregisterPolicyEngine(int32_t socketFD)
143{
144    LOG("Cognitive Radio Shell:: Received deregistration message from Policy Engine.\n");
145
146    PE_present = false;
147   
148    SendMessage(socketFD, "deregister_ack");
149    shutdown(socketFD, 2);
150    close(socketFD);
151    LOG("Cognitive Radio Shell:: Socket closed.\n");
152}
153
154void
155CognitiveRadioShell::RegisterSML(int32_t socketFD)
156{
157    LOG("Cognitive Radio Shell:: Received registration message from SML.\n");
158
159    SML_present = true;
160}
161
162void
163CognitiveRadioShell::DeregisterSML(int32_t socketFD)
164{
165    LOG("Cognitive Radio Shell:: Received deregistration message from SML.\n");
166
167    SML_present = false;
168
169    SendMessage(socketFD, "deregister_ack");
170    shutdown(socketFD, 2);
171    close(socketFD);
172    LOG("Cognitive Radio Shell:: Socket closed.\n");
173}
174
175int32_t
176CognitiveRadioShell::LoadRadioConfiguration(const char* radioConfig, Parameter* &pList, \
177    Utility* &uList, Observable* &oList, Radio_Info * radioInfo)
178{
179    TiXmlElement *pElem;
180    TiXmlElement *pChild;
181    TiXmlElement *pChild1;
182    TiXmlElement *pSecondChild;
183    TiXmlHandle hRoot(0);
184
185    int32_t count = 0;
186    size_t item_count = 0;
187    size_t affect_count = 0;
188    int32_t attribute_count = 0;
189    bool match_found = false;
190
191    LOG("Cognitive Radio Shell:: Loading radio configuration.\n");
192
193    TiXmlDocument doc( radioConfig );
194    bool loadOkay = doc.LoadFile();
195    if(!loadOkay)
196    {
197        ERROR(1,"Loading radio configuration failed: %s\n", radioConfig);
198        return -1;
199    }
200
201    TiXmlHandle hDoc(&doc);
202   
203    pElem = hDoc.FirstChildElement().Element();
204
205    if(!pElem)
206        ERROR(1, "No valid root!");
207
208    hRoot = TiXmlHandle(pElem);
209
210    pElem = hRoot.FirstChild("utilities").Element();
211    pChild1 = hRoot.Child("utilities", count).Element();
212
213    for(pChild = pChild1->FirstChildElement("utility"); pChild; \
214        pChild = pChild->NextSiblingElement())
215    {
216        const char *uName = pChild->Attribute("name");
217        if(uName)
218            uList[item_count].name = uName;     
219
220        const char *uUnits = pChild->Attribute("units");
221        if(uUnits)
222            uList[item_count].units = uUnits;
223
224        const char *uGoal = pChild->Attribute("goal");
225        if(uGoal)
226            uList[item_count].goal = uGoal;
227
228        if(pChild->QueryFloatAttribute("target", &uList[item_count].target) != TIXML_SUCCESS)
229            uList[item_count].target = -1;
230
231        item_count++;
232    }
233
234    radio_info->numUtilities = item_count;     
235    LOG("Cognitive Radio Shell:: Parsed %d utilities.\n", radioInfo->numUtilities);
236
237    item_count = 0;
238    pElem = hRoot.FirstChild("observables").Element();
239    pChild1 = hRoot.Child("observables", count).Element();
240
241    for(pChild = pChild1->FirstChildElement("observable"); pChild; \
242        pChild = pChild->NextSiblingElement())
243    {
244        const char *oName = pChild->Attribute("name");
245        if(oName)
246            oList[item_count].name = oName;
247               
248        affect_count = 0;
249        for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \
250            pSecondChild = pSecondChild->NextSiblingElement())
251        {
252            const char *oUtilName = pSecondChild->Attribute("utility");
253            if(oUtilName)
254            {
255                for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++ )
256                {
257                    if(uList[attribute_count].name == oUtilName)
258                    {
259                        oList[item_count].affection_list[affect_count].u = &uList[attribute_count];
260                        const char *oRelate = pSecondChild->Attribute("relationship");
261                        if(oRelate)
262                            oList[item_count].affection_list[affect_count].relation = oRelate;
263                        affect_count++;
264                        match_found = true;
265                        break;
266                    }
267                }
268            }
269
270            if(!match_found) {
271                ERROR(1, "Error: %s: %s is not a valid utility.\n", \
272                    oList[item_count].name.c_str(), oUtilName);
273            }
274            else
275                match_found = false;   
276        }
277        oList[item_count].numAffects = affect_count;
278        item_count++;
279    }
280
281    radioInfo->numObservables = item_count;     
282    LOG("Cognitive Radio Shell:: Parsed %d observables.\n", radioInfo->numObservables);
283
284    pElem = hRoot.FirstChild("parameters").Element();
285    pChild1 = hRoot.Child("parameters", count).Element();
286       
287    item_count = 0;
288    for(pChild = pChild1->FirstChildElement("parameter"); pChild; \
289        pChild = pChild->NextSiblingElement())
290    {
291        const char *pName = pChild->Attribute("name");
292        if(pName)
293            pList[item_count].name = pName;     
294
295        const char *pUnits = pChild->Attribute("units");
296        if(pUnits)
297            pList[item_count].units = pUnits;
298
299        if(pChild->QueryFloatAttribute("min", &pList[item_count].min) != TIXML_SUCCESS)
300            pList[item_count].min = -1;
301
302        if(pChild->QueryFloatAttribute("max", &pList[item_count].max) != TIXML_SUCCESS)
303            pList[item_count].max = -1;
304
305        if(pChild->QueryFloatAttribute("step", &pList[item_count].step) != TIXML_SUCCESS)
306            pList[item_count].step = -1;
307               
308        affect_count = 0;
309        for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \
310            pSecondChild = pSecondChild->NextSiblingElement())
311        {
312            const char *pUtilName = pSecondChild->Attribute("utility");
313            if(pUtilName)
314            {
315                for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++)
316                {
317                    if(uList[attribute_count].name == pUtilName)
318                    {
319                        pList[item_count].affection_list[affect_count].u = &uList[attribute_count];     
320
321                        const char *pRelate = pSecondChild->Attribute("relationship");
322                        if(pRelate)
323                            pList[item_count].affection_list[affect_count].relation = pRelate;
324                        else
325                            LOG("Error: No relation found.\n");
326
327                        match_found = true;
328                        affect_count++;
329                        break;
330                    }
331                }
332            }
333
334            if(!match_found)
335            {
336                ERROR(1, "Error: %s: %s is not a valid utility.\n", \
337                    pList[item_count].name.c_str(), pUtilName);
338            }
339
340            match_found = false;       
341        }
342
343        pList[item_count].numAffects = affect_count;
344        item_count++;
345    }
346
347    radioInfo->numParameters = item_count;
348    LOG("Cognitive Radio Shell:: Parsed %d parameters.\n", radioInfo->numParameters);
349
350    return 1;
351
352}
353
354void
355CognitiveRadioShell::GetOptimalParameters(int32_t socketFD)
356{
357
358
359}
360
361void
362CognitiveRadioShell::HandleMessage(int32_t socketFD)
363{
364    char buffer[256];
365
366    ReadMessage(socketFD, buffer);
367
368    if(strcmp(buffer,"register_engine_cognitive") == 0) {
369            RegisterCognitiveEngine(socketFD);
370    } else if(strcmp(buffer,"deregister_engine_cognitive") == 0) {
371            DeregisterCognitiveEngine(socketFD);
372    } else if(strcmp(buffer,"register_engine_policy") == 0) {
373            RegisterPolicyEngine(socketFD);
374    } else if(strcmp(buffer,"deregister_engine_policy") == 0) {
375            DeregisterPolicyEngine(socketFD);
376    } else if(strcmp(buffer,"register_sml") == 0) {
377            RegisterSML(socketFD);
378    } else if(strcmp(buffer,"deregister_sml") == 0) {
379            DeregisterSML(socketFD);
380    } else if(strcmp(buffer,"optimize") == 0) {
381            /* Receive optimization request and current environment */
382            GetOptimalParameters(socketFD); 
383    }
384}
385
386void
387CognitiveRadioShell::StartShellServer()
388{
389    int32_t * servSock;
390    struct timeval selTimeout;
391    int32_t primary = 0;
392    int32_t policy = 1;
393    int32_t command = 2;
394    int32_t running = 1;
395    int32_t port, rc, new_sd = 1;
396    int32_t desc_ready = 1;
397    int32_t timeout = 10;
398    fd_set sockSet;
399
400    servSock = (int32_t *) malloc(3 * sizeof(int32_t));
401
402    servSock[primary] = CreateTCPServerSocket(primaryPort);
403    servSock[policy] = CreateTCPServerSocket(policyPort);
404    servSock[command] = CreateTCPServerSocket(commandPort);
405
406    int32_t maxDescriptor = servSock[command];
407
408    if(InitializeTCPServerPort(servSock[primary]) == -1) {
409        ERROR(1,"Error initializing primary port\n");
410        return;
411    }
412 
413    if(InitializeTCPServerPort(servSock[policy]) == -1) {
414        ERROR(1,"Error initializing policy port\n");
415        return;
416    }
417
418    if(InitializeTCPServerPort(servSock[command]) == -1) {
419        ERROR(1,"Error initializing command port\n");
420        return;
421    }
422
423    while (running)
424    {
425        /* Zero socket descriptor vector and set for server sockets */
426        /* This must be reset every time select() is called */
427        FD_ZERO(&sockSet);
428        FD_SET(servSock[primary], &sockSet);
429        FD_SET(servSock[policy], &sockSet);
430        FD_SET(servSock[command], &sockSet);
431
432        /* Timeout specification */
433        /* This must be reset every time select() is called */
434        selTimeout.tv_sec = timeout;       /* timeout (secs.) */
435        selTimeout.tv_usec = 0;            /* 0 microseconds */
436
437        /* Suspend program until descriptor is ready or timeout */
438        rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout);
439        if (rc == 0)
440            LOG("No echo requests for %i secs...Server still alive\n", timeout);
441        else
442        {
443            desc_ready = rc;
444
445            for (port = 0; port <= maxDescriptor && desc_ready > 0; port++) {
446                if (FD_ISSET(port, &sockSet))
447                {
448                    desc_ready -= 1;
449
450                    /* Check if request is new or on an existing open descriptor */
451                    if( (port == servSock[primary]) || (port == servSock[policy]) || (port == servSock[command])) {
452                        do
453                        {
454                            new_sd = AcceptTCPConnection(port);
455                            if(new_sd < 0)
456                            {
457                                break;
458                            }
459                           
460                            HandleMessage(new_sd);
461                            FD_SET(new_sd,&sockSet);
462                            if(new_sd > maxDescriptor)
463                                maxDescriptor = new_sd;
464                            //LOG("New incoming connection - %i\n\n",new_sd);
465                        } while(new_sd != -1);
466                    } else {
467                       
468                        //LOG("Request on already open descriptor.\n\n");
469                        HandleMessage(port);
470                    }
471                }
472            }
473        }
474    }
475
476
477    /* Close sockets */
478    close(servSock[primary]);
479    close(servSock[policy]);
480    close(servSock[command]);
481
482    /* Free list of sockets */
483    free(servSock); 
484
485    return;
486}
Note: See TracBrowser for help on using the browser.