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

Revision 209, 13.5 KB (checked in by bhilburn, 15 years ago)

Simply whitespace fixes.

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