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

Revision 239, 23.3 KB (checked in by bhilburn, 15 years ago)

Typo and missing header commit merged over to trunk.

RevLine 
[207]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>
[212]14#include <string>
[207]15
16#include <arpa/inet.h>
17#include <iostream>
18#include <netinet/in.h>
19#include <netdb.h>
20#include <fcntl.h>
21#include <sys/ioctl.h>
22#include <sys/mman.h>
23#include <sys/socket.h>
24#include <sys/types.h>
25#include <sys/wait.h>
26
27#include "tinyxml/tinyxml.h"
28#include "tinyxml/tinystr.h"
29
30#include "vtcross/common.h"
31#include "vtcross/components.h"
32#include "vtcross/containers.h"
33#include "vtcross/debug.h"
34#include "vtcross/error.h"
35#include "vtcross/socketcomm.h"
36
37
38CognitiveRadioShell::CognitiveRadioShell()
39{
40    LOG("Creating Cognitive Radio Shell.\n");
41    SML_present = false;
42    PE_present = false;
43    CE_present = false;
[213]44
45    // TODO =BUG= The params, observables, and utils arrays are not being
46    // allocated here. If an shell object is constructed, and then immediately
47    // destructed, there will be no allocated memory, and the delete operater
48    // will receive a null pointer, which will segfault.
[207]49}
50
51
52CognitiveRadioShell::~CognitiveRadioShell()
53{
[213]54    delete [] params;
55    delete [] observables;
56    delete [] utils;
[207]57}
58
59
60CognitiveRadioShell::CognitiveRadioShell(const char* radioConfig, int16_t p1, \
61        int16_t p2, int16_t p3)
62{
63    LOG("Creating Cognitive Radio Shell.\n");
64
[213]65    params = new Parameter[10];
66    observables = new Observable[10];
67    utils = new Utility[10];
[207]68    radio_info = new Radio_Info;
69
[213]70    LoadRadioConfiguration(radioConfig, params, utils, observables, radio_info);
[207]71
72    primaryPort = p1;
73    policyPort = p2;
74    commandPort = p3;
75}
76
77
78void
79CognitiveRadioShell::SendComponentType(int32_t socketFD)
80{
81    SendMessage(socketFD, "response_shell");
82    LOG("Cognitive Radio Shell responded to GetRemoteComponentType query.\n");
83}
84
[209]85
[212]86std::string
87CognitiveRadioShell::GetRemoteComponentType(int32_t socketFD)
88{
89    SendMessage(socketFD, "request_component_type");
90
91    char buffer[256];
92    memset(buffer, 0, 256);
93    ReadMessage(socketFD, buffer);
94
95    return std::string(buffer);
96}
97
98
[207]99void
100CognitiveRadioShell::Shutdown()
101{
102    // TODO should something else be happening here?
103}
104
105
106void
107CognitiveRadioShell::Reset()
108{
109    LOG("Resetting Cognitive Radio Shell.\n");
110}
111
112
[231]113// TODO what is the point of always returning a 1?
[218]114bool
[231]115CognitiveRadioShell::SendRadioConfiguration(int32_t socketFD)
[207]116{
117    LOG("Cognitive Radio Shell:: Sending radio configuration to Cognitive Engine.\n");
[231]118
[218]119    char counter[55];
120    char var[50];
121
[231]122    /* Send utilities */
123    sprintf(counter, "%d", radio_info->numUtilities);
124    SendMessage(socketFD, counter);
125    for(size_t i = 0; i < radio_info->numUtilities; i++) {
126        SendMessage(socketFD, utils[i].name.c_str());
127        SendMessage(socketFD, utils[i].units.c_str());
128        SendMessage(socketFD, utils[i].goal.c_str());
[222]129        sprintf(var,"%f", utils[i].target);
[231]130        SendMessage(socketFD, var);
[218]131    }
132
[231]133    /* Send parameters */
[218]134    sprintf(counter,"%i",radio_info->numParameters);
[231]135    SendMessage(socketFD,counter);
136    for(size_t i = 0; i < radio_info->numParameters; i++) {
137        SendMessage(socketFD, params[i].name.c_str());
138        SendMessage(socketFD, params[i].units.c_str());
[227]139        sprintf(var, "%f", params[i].min);
[231]140        SendMessage(socketFD,var);
[227]141        sprintf(var, "%f", params[i].max);
[231]142        SendMessage(socketFD, var);
[227]143        sprintf(var, "%f", params[i].step);
[231]144        SendMessage(socketFD, var);
[218]145
[227]146        sprintf(counter, "%i", params[i].numAffects);
[231]147        SendMessage(socketFD, counter);
148        for(size_t j = 0; j < params[i].numAffects; j++) {
149            SendMessage(socketFD, params[i].affection_list[j].u->name.c_str());
150            SendMessage(socketFD, params[i].affection_list[j].relation.c_str());
[218]151        }
152    }
153
[231]154    /* Send observables */
[218]155    sprintf(counter,"%i",radio_info->numObservables);
[231]156    SendMessage(socketFD, counter);
157    for(size_t i = 0; i < radio_info->numObservables; i++) {
158        SendMessage(socketFD, observables[i].name.c_str());
[232]159       
[227]160        sprintf(counter, "%i", observables[i].numAffects);
[231]161        SendMessage(socketFD, counter);
162        for(size_t j = 0; j < observables[i].numAffects; j++) {
163            SendMessage(socketFD, observables[i].affection_list[j].u->name.c_str());
164            SendMessage(socketFD, observables[i].affection_list[j].relation.c_str());
[218]165        }
166    }
[232]167   
[231]168    /* Receive ACK for radio configuration */
[218]169    char buffer[256];
170    memset(buffer, 0, 256);
[231]171    ReadMessage(socketFD, buffer);
[218]172
173    if(strcmp(buffer, "receive_config_ack") != 0) {
[227]174        LOG("Cognitive Radio Shell:: Unexpected response: %s\n", buffer);
[218]175        return 0;
176    }
[227]177
[218]178    return 1;
[207]179}
180
[218]181bool
[207]182CognitiveRadioShell::SendRadioExperience(int32_t socketFD)
183{
184
185    LOG("Cognitive Radio Shell:: Sending radio experience to Cognitive Engine.\n");
[218]186    int32_t numberExp = 4;
[232]187    char numberExpString[50];
[218]188
[231]189    sprintf(numberExpString, "%i", numberExp);
[222]190    SendMessage(socketFD, numberExpString);
191
[218]192    char buffer[256];
193    memset(buffer, 0, 256);
194    ReadMessage(socketFD, buffer);
195    if(strcmp(buffer, "receive_exp_ack") != 0) {
[231]196        // TODO perhaps this should be a WARNING instead of a LOG?
197        LOG("Cognitive Radio Shell:: Unexpected response: %s\n", buffer);
[218]198        return 0;
199    }
[231]200
[218]201    return 1;
[207]202}
203
[209]204
[207]205void
206CognitiveRadioShell::RegisterCognitiveEngine(int32_t socketFD)
207{
208    LOG("Cognitive Radio Shell:: Received registration message from Cognitive Engine.\n");
209   
210    SendRadioConfiguration(socketFD);
211    SendRadioExperience(socketFD);
212
213    numberOfCognitiveEngines++;
214    CE_present = true;
215}
216
[209]217
[207]218void
219CognitiveRadioShell::DeregisterCognitiveEngine(int32_t socketFD)
220{
221    LOG("Cognitive Radio Shell:: Received deregistration message from Cognitive Engine.\n");
222
223    numberOfCognitiveEngines--;
224    if(numberOfCognitiveEngines == 0)
[209]225        CE_present = false;
[207]226
227    SendMessage(socketFD, "deregister_ack");
228    shutdown(socketFD, 2);
229    close(socketFD);
230    LOG("Cognitive Radio Shell:: Socket closed.\n");
231}
232
[209]233
[207]234void
235CognitiveRadioShell::RegisterPolicyEngine(int32_t socketFD)
236{
237    LOG("Cognitive Radio Shell:: Received registration message from Policy Engine.\n");
238    PE_present = true;
239}
240
[209]241
[207]242void
243CognitiveRadioShell::DeregisterPolicyEngine(int32_t socketFD)
244{
245    LOG("Cognitive Radio Shell:: Received deregistration message from Policy Engine.\n");
246
247    PE_present = false;
248   
249    SendMessage(socketFD, "deregister_ack");
250    shutdown(socketFD, 2);
251    close(socketFD);
252    LOG("Cognitive Radio Shell:: Socket closed.\n");
253}
254
[209]255
[207]256void
257CognitiveRadioShell::RegisterSML(int32_t socketFD)
258{
259    LOG("Cognitive Radio Shell:: Received registration message from SML.\n");
260
261    SML_present = true;
262}
263
[209]264
[207]265void
266CognitiveRadioShell::DeregisterSML(int32_t socketFD)
267{
268    LOG("Cognitive Radio Shell:: Received deregistration message from SML.\n");
269
270    SML_present = false;
271
272    SendMessage(socketFD, "deregister_ack");
273    shutdown(socketFD, 2);
274    close(socketFD);
275    LOG("Cognitive Radio Shell:: Socket closed.\n");
276}
277
[209]278
[207]279int32_t
[209]280CognitiveRadioShell::LoadRadioConfiguration(const char* radioConfig, \
281        Parameter* &pList, Utility* &uList, Observable* &oList, \
282        Radio_Info* radioInfo)
[207]283{
284    TiXmlElement *pElem;
285    TiXmlElement *pChild;
286    TiXmlElement *pChild1;
287    TiXmlElement *pSecondChild;
288    TiXmlHandle hRoot(0);
289
290    int32_t count = 0;
291    size_t item_count = 0;
292    size_t affect_count = 0;
[218]293    uint32_t attribute_count = 0;
[207]294    bool match_found = false;
295
296    LOG("Cognitive Radio Shell:: Loading radio configuration.\n");
297
298    TiXmlDocument doc( radioConfig );
299    bool loadOkay = doc.LoadFile();
[215]300    if(!loadOkay)
[207]301        ERROR(1,"Loading radio configuration failed: %s\n", radioConfig);
302
303    TiXmlHandle hDoc(&doc);
304   
305    pElem = hDoc.FirstChildElement().Element();
306
307    if(!pElem)
308        ERROR(1, "No valid root!");
309
310    hRoot = TiXmlHandle(pElem);
311
312    pElem = hRoot.FirstChild("utilities").Element();
313    pChild1 = hRoot.Child("utilities", count).Element();
314
315    for(pChild = pChild1->FirstChildElement("utility"); pChild; \
[209]316        pChild = pChild->NextSiblingElement()) {
317
[207]318        const char *uName = pChild->Attribute("name");
319        if(uName)
[208]320            uList[item_count].name = uName;   
[207]321
322        const char *uUnits = pChild->Attribute("units");
323        if(uUnits)
324            uList[item_count].units = uUnits;
325
326        const char *uGoal = pChild->Attribute("goal");
327        if(uGoal)
328            uList[item_count].goal = uGoal;
329
330        if(pChild->QueryFloatAttribute("target", &uList[item_count].target) != TIXML_SUCCESS)
331            uList[item_count].target = -1;
332
333        item_count++;
334    }
335
[208]336    radio_info->numUtilities = item_count;   
[207]337    LOG("Cognitive Radio Shell:: Parsed %d utilities.\n", radioInfo->numUtilities);
338
339    item_count = 0;
340    pElem = hRoot.FirstChild("observables").Element();
341    pChild1 = hRoot.Child("observables", count).Element();
342
343    for(pChild = pChild1->FirstChildElement("observable"); pChild; \
[209]344        pChild = pChild->NextSiblingElement()) {
345
[207]346        const char *oName = pChild->Attribute("name");
347        if(oName)
348            oList[item_count].name = oName;
[208]349       
[207]350        affect_count = 0;
351        for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \
[209]352            pSecondChild = pSecondChild->NextSiblingElement()) {
353
[207]354            const char *oUtilName = pSecondChild->Attribute("utility");
[209]355            if(oUtilName) {
356                for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++ ) {
357                    if(uList[attribute_count].name == oUtilName) {
358
[207]359                        oList[item_count].affection_list[affect_count].u = &uList[attribute_count];
360                        const char *oRelate = pSecondChild->Attribute("relationship");
361                        if(oRelate)
362                            oList[item_count].affection_list[affect_count].relation = oRelate;
[209]363
[207]364                        affect_count++;
365                        match_found = true;
366                        break;
367                    }
368                }
369            }
370
371            if(!match_found) {
372                ERROR(1, "Error: %s: %s is not a valid utility.\n", \
373                    oList[item_count].name.c_str(), oUtilName);
374            }
375            else
[208]376                match_found = false;   
[207]377        }
378        oList[item_count].numAffects = affect_count;
379        item_count++;
380    }
381
[208]382    radioInfo->numObservables = item_count;   
[207]383    LOG("Cognitive Radio Shell:: Parsed %d observables.\n", radioInfo->numObservables);
384
385    pElem = hRoot.FirstChild("parameters").Element();
386    pChild1 = hRoot.Child("parameters", count).Element();
[208]387   
[207]388    item_count = 0;
389    for(pChild = pChild1->FirstChildElement("parameter"); pChild; \
[209]390        pChild = pChild->NextSiblingElement()) {
391
[207]392        const char *pName = pChild->Attribute("name");
393        if(pName)
[208]394            pList[item_count].name = pName;   
[207]395
396        const char *pUnits = pChild->Attribute("units");
397        if(pUnits)
398            pList[item_count].units = pUnits;
399
400        if(pChild->QueryFloatAttribute("min", &pList[item_count].min) != TIXML_SUCCESS)
401            pList[item_count].min = -1;
402
403        if(pChild->QueryFloatAttribute("max", &pList[item_count].max) != TIXML_SUCCESS)
404            pList[item_count].max = -1;
405
406        if(pChild->QueryFloatAttribute("step", &pList[item_count].step) != TIXML_SUCCESS)
407            pList[item_count].step = -1;
[208]408       
[207]409        affect_count = 0;
410        for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \
[209]411
412            pSecondChild = pSecondChild->NextSiblingElement()) {
[207]413            const char *pUtilName = pSecondChild->Attribute("utility");
[209]414            if(pUtilName) {
415                for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++) {
416                    if(uList[attribute_count].name == pUtilName) {
[208]417                        pList[item_count].affection_list[affect_count].u = &uList[attribute_count];   
[207]418
419                        const char *pRelate = pSecondChild->Attribute("relationship");
420                        if(pRelate)
421                            pList[item_count].affection_list[affect_count].relation = pRelate;
422                        else
423                            LOG("Error: No relation found.\n");
424
425                        match_found = true;
426                        affect_count++;
427                        break;
428                    }
429                }
430            }
431
[209]432            if(!match_found) {
[207]433                ERROR(1, "Error: %s: %s is not a valid utility.\n", \
434                    pList[item_count].name.c_str(), pUtilName);
435            }
436
[208]437            match_found = false;   
[207]438        }
439
440        pList[item_count].numAffects = affect_count;
441        item_count++;
442    }
443
444    radioInfo->numParameters = item_count;
445    LOG("Cognitive Radio Shell:: Parsed %d parameters.\n", radioInfo->numParameters);
446
447    return 1;
448}
449
[209]450
[207]451void
452CognitiveRadioShell::GetOptimalParameters(int32_t socketFD)
453{
[222]454    char buffer[256];
455    char counter[55];
456    char var[50];
457
458    /* Receive Set of Observables */
459    LOG("Cognitive Radio Shell:: Got request for optimization.\n");
460    memset(buffer, 0, 256);
[227]461    ReadMessage(commandSocketFD, buffer);
[231]462    uint32_t numObservables = atoi(buffer);
[222]463 
[232]464    LOG("Cognitive Radio Shell:: Attempting to get %i observables.\n", numObservables);
[227]465    Observable *o = new Observable[numObservables];
[222]466 
[227]467    for(size_t i = 0; i < numObservables; i++) {
[222]468        memset(buffer, 0, 256);
[227]469        ReadMessage(commandSocketFD, buffer);
[222]470        o[i].name = std::string(buffer);
[229]471   
[222]472        memset(buffer, 0, 256);
[227]473        ReadMessage(commandSocketFD, buffer);
[222]474        o[i].value = atof(buffer);
475    }
[231]476
[232]477    /* Receive Set of Current Parameters */
[228]478    memset(buffer, 0, 256);
479    ReadMessage(commandSocketFD,buffer);
[231]480    uint32_t numCurrentParameters = atoi(buffer);
[228]481 
[232]482    LOG("Cognitive Radio Shell:: Attempting to get %i parameters.\n",numCurrentParameters);
[228]483    Parameter * cp = new Parameter[numCurrentParameters];
[229]484
485    for (size_t i = 0; i < numCurrentParameters; i++){
[228]486        memset(buffer, 0, 256);
487        ReadMessage(commandSocketFD,buffer);
488        cp[i].name = std::string(buffer);
489        memset(buffer, 0, 256);
490        ReadMessage(commandSocketFD,buffer);
491        cp[i].value = atof(buffer);
492    }
[231]493
[222]494    /* Send to Cognitive Engine
[232]495     * TODO: With multiple CEs we need to make a decision about where
496     * to send this information
497     */
498    if(!SML_present) {
499        LOG("Cognitive Radio Shell:: Passing on observables to Cognitive Engine\n");
[228]500        SendMessage(ceSocketFD,"request_optimization");
501        sprintf(counter,"%i",numObservables);
502        SendMessage(ceSocketFD,counter);
[229]503        for(size_t i = 0; i < numObservables; i++) {
[228]504            SendMessage(ceSocketFD,o[i].name.c_str());
505            sprintf(var,"%f",o[i].value);
[232]506            SendMessage(ceSocketFD,var);
[228]507        }
[232]508       
509        LOG("Cognitive Radio Shell:: Passing on current parameters to Cognitive Engine\n");
[228]510        sprintf(counter,"%i",numCurrentParameters);
511        SendMessage(ceSocketFD,counter);
[229]512        for(size_t i = 0; i < numCurrentParameters; i++) {
[228]513            SendMessage(ceSocketFD,cp[i].name.c_str());
514            sprintf(var,"%f",cp[i].value);
[232]515            SendMessage(ceSocketFD,var);
[228]516        }
[232]517    }
[222]518
[232]519    LOG("Cognitive Radio Shell:: Receiving optimized parameters from Cognitive Engine.\n");
[222]520    /* Receive Set of Parameters */
521    memset(buffer, 0, 256);
[227]522    ReadMessage(ceSocketFD, buffer);
[231]523    uint32_t numParameters = atoi(buffer);
[222]524   
[227]525    Parameter *p = new Parameter[numParameters];
[222]526 
[227]527    for(size_t i = 0; i < numParameters; i++) {
[222]528        memset(buffer, 0, 256);
[227]529        ReadMessage(ceSocketFD, buffer);
[222]530        p[i].name = std::string(buffer);
531   
532        memset(buffer, 0, 256);
[227]533        ReadMessage(ceSocketFD, buffer);
[222]534        p[i].value = atof(buffer);
535    }
536
537    /* Send to Application
[232]538     */
539    LOG("Cognitive Radio Shell:: Sending optimized parameters to Application.\n");
[222]540    memset(counter, 0, 55);
[227]541    sprintf(counter, "%i", numParameters);
542    SendMessage(commandSocketFD, counter);
543    for(size_t i = 0; i < numParameters; i++) {
544        SendMessage(commandSocketFD, p[i].name.c_str());
545        sprintf(var, "%f", p[i].value);
[232]546        SendMessage(commandSocketFD, var);
[222]547    }
[227]548
549    delete [] o;
550    delete [] p;
[207]551}
552
[231]553// TODO point of always returning 1?
[230]554bool
555CognitiveRadioShell::UpdateParameterPerformance(int32_t socketFD)
556{
557    char counter[55];
558    char var[50];
559    char buffer[256];
[209]560
[232]561    /* Receive Set of Parameters */
[230]562    memset(buffer, 0, 256);
563    ReadMessage(commandSocketFD,buffer);
[231]564    uint32_t numParameters = atoi(buffer);
[230]565 
566    Parameter *p = new Parameter[numParameters];
567
568    for (size_t i = 0; i < numParameters; i++){
569        memset(buffer, 0, 256);
570        ReadMessage(commandSocketFD,buffer);
571        p[i].name = std::string(buffer);
572        memset(buffer, 0, 256);
573        ReadMessage(commandSocketFD,buffer);
574        p[i].value = atof(buffer);
575    }
576
577    /* Receive Set of Observables */
578    memset(buffer, 0, 256);
579    ReadMessage(commandSocketFD, buffer);
[231]580    uint32_t numObservables = atoi(buffer);
[230]581 
582    Observable *o = new Observable[numObservables];
583 
584    for(size_t i = 0; i < numObservables; i++) {
585        memset(buffer, 0, 256);
586        ReadMessage(commandSocketFD, buffer);
587        o[i].name = std::string(buffer);
588   
589        memset(buffer, 0, 256);
590        ReadMessage(commandSocketFD, buffer);
591        o[i].value = atof(buffer);
592    }
593
594    SendMessage(ceSocketFD, "update_performance");
595   
[231]596    /* Send Parameters */
[230]597    memset(counter, 0, 55);
598    sprintf(counter, "%i", numParameters);
599    SendMessage(ceSocketFD, counter);
600   
601    for(size_t i = 0; i < numParameters; i++) {
602        SendMessage(ceSocketFD,p[i].name.c_str());
603        sprintf(var,"%f",p[i].value);
604        SendMessage(ceSocketFD,var); 
605    }   
606   
[231]607    /* Send Observables */
[230]608    sprintf(counter, "%i", numObservables);
609    SendMessage(ceSocketFD, counter);
610    for(size_t i = 0; i < numObservables; i++) {
611        SendMessage(ceSocketFD, o[i].name.c_str());
612        sprintf(var, "%f", o[i].value);
613        SendMessage(ceSocketFD, var);
614    }   
[231]615
616    delete [] p;
[239]617    delete [] o;
[230]618   
619    return 1;
620}
[231]621
622
[207]623void
624CognitiveRadioShell::HandleMessage(int32_t socketFD)
625{
626    char buffer[256];
627
628    ReadMessage(socketFD, buffer);
629
[231]630    // TODO trying to read this code lock makes my eyes bleed
[227]631    if(strcmp(buffer, "register_engine_cognitive") == 0) {
[208]632        RegisterCognitiveEngine(socketFD);
[227]633    } else if(strcmp(buffer, "deregister_engine_cognitive") == 0) {
[208]634        DeregisterCognitiveEngine(socketFD);
[227]635    } else if(strcmp(buffer, "register_engine_policy") == 0) {
[208]636        RegisterPolicyEngine(socketFD);
[227]637    } else if(strcmp(buffer, "deregister_engine_policy") == 0) {
[208]638        DeregisterPolicyEngine(socketFD);
[227]639    } else if(strcmp(buffer, "register_sml") == 0) {
[208]640        RegisterSML(socketFD);
[227]641    } else if(strcmp(buffer, "deregister_sml") == 0) {
[208]642        DeregisterSML(socketFD);
[230]643    } else if(strcmp(buffer, "update_performance") == 0) {
644        UpdateParameterPerformance(socketFD);
[231]645    } else if(strcmp(buffer, "get_number_utilities") == 0) {
[232]646        char numUtilities[20];
647        sprintf(numUtilities, "%i", radio_info->numUtilities);
[231]648        SendMessage(commandSocketFD, numUtilities);
649    } else if(strcmp(buffer, "get_number_observables") == 0) {
[232]650        char numObservables[20];
651        sprintf(numObservables, "%i", radio_info->numObservables);
[231]652        SendMessage(commandSocketFD, numObservables);
653    } else if(strcmp(buffer, "get_number_parameters") == 0) {
[232]654        char numParameters[20];
655        sprintf(numParameters, "%i", radio_info->numParameters);
[231]656        SendMessage(commandSocketFD, numParameters);
657    } else if(strcmp(buffer, "request_optimization") == 0) {
[208]658        /* Receive optimization request and current environment */
659        GetOptimalParameters(socketFD); 
[231]660    } else if(strcmp(buffer, "request_optimization_service") == 0) {
[228]661        /* Receive optimization request and current environment */
662        //GetOptimalParametersService(socketFD); 
[207]663    }
664}
665
[209]666
[207]667void
668CognitiveRadioShell::StartShellServer()
669{
670    struct timeval selTimeout;
671    int32_t primary = 0;
672    int32_t policy = 1;
673    int32_t command = 2;
674    int32_t running = 1;
675    int32_t port, rc, new_sd = 1;
676    int32_t desc_ready = 1;
677    int32_t timeout = 10;
678    fd_set sockSet;
679
[210]680    int32_t *servSock = new int32_t[3];
[207]681
682    servSock[primary] = CreateTCPServerSocket(primaryPort);
683    servSock[policy] = CreateTCPServerSocket(policyPort);
684    servSock[command] = CreateTCPServerSocket(commandPort);
685
[222]686    int32_t maxDescriptor;
[207]687
[232]688    if(servSock[primary] > servSock[policy])
689        maxDescriptor = servSock[primary];
[222]690    else
[232]691        maxDescriptor = servSock[policy];
[222]692
[232]693    if(servSock[command] > maxDescriptor)
694        maxDescriptor = servSock[command];
[222]695
[215]696    if(InitializeTCPServerPort(servSock[primary]) == -1)
697        ERROR(1,"Error initializing primary port\n");
[207]698 
[215]699    if(InitializeTCPServerPort(servSock[policy]) == -1)
700        ERROR(1,"Error initializing policy port\n");
[207]701
[215]702    if(InitializeTCPServerPort(servSock[command]) == -1)
703        ERROR(1,"Error initializing command port\n");
[207]704
[209]705    while (running) {
[207]706        /* Zero socket descriptor vector and set for server sockets */
707        /* This must be reset every time select() is called */
708        FD_ZERO(&sockSet);
709        FD_SET(servSock[primary], &sockSet);
710        FD_SET(servSock[policy], &sockSet);
711        FD_SET(servSock[command], &sockSet);
712
713        /* Timeout specification */
714        /* This must be reset every time select() is called */
715        selTimeout.tv_sec = timeout;       /* timeout (secs.) */
716        selTimeout.tv_usec = 0;            /* 0 microseconds */
717
718        /* Suspend program until descriptor is ready or timeout */
719        rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout);
[209]720        if(rc == 0)
[207]721            LOG("No echo requests for %i secs...Server still alive\n", timeout);
[209]722        else {
[207]723            desc_ready = rc;
724
[209]725            for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) {
726                if(FD_ISSET(port, &sockSet)) {
727                    desc_ready -= 1;
[207]728
729                    /* Check if request is new or on an existing open descriptor */
[209]730                    if((port == servSock[primary]) || (port == servSock[policy]) || (port == servSock[command])) {
731                        do {
[207]732                            new_sd = AcceptTCPConnection(port);
733                            if(new_sd < 0)
734                                break;
[222]735                           
736                            if(port == servSock[primary])
737                                ceSocketFD = new_sd;
738                            if(port == servSock[command])
739                                commandSocketFD = new_sd;
740                            if(port == servSock[policy])
741                                policySocketFD = new_sd;
742
[207]743                            HandleMessage(new_sd);
744                            FD_SET(new_sd,&sockSet);
745                            if(new_sd > maxDescriptor)
746                                maxDescriptor = new_sd;
747                            //LOG("New incoming connection - %i\n\n",new_sd);
748                        } while(new_sd != -1);
[209]749                    }
750                    else {
[207]751                        //LOG("Request on already open descriptor.\n\n");
752                        HandleMessage(port);
753                    }
754                }
755            }
756        }
757    }
758
759
760    /* Close sockets */
761    close(servSock[primary]);
762    close(servSock[policy]);
763    close(servSock[command]);
764
765    /* Free list of sockets */
[210]766    delete servSock;
[207]767
768    return;
769}
Note: See TracBrowser for help on using the browser.