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

Revision 389, 24.1 KB (checked in by trnewman, 15 years ago)

Added DSA CBR reference implementation.
Added simple python example application for DSA CBR.
Added GNUradio python application that uses CROSS and the DSA CBR.

Fixed several bugs in the socket interface.

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