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

Revision 279, 23.4 KB (checked in by wrodgers, 15 years ago)

Updated shell to conform with API

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, "%i", numberExp);
190    SendMessage(socketFD, numberExpString);
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
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
279
280int32_t
281CognitiveRadioShell::LoadRadioConfiguration(const char* radioConfig, \
282        Parameter* &pList, Utility* &uList, Observable* &oList, \
283        Radio_Info* radioInfo)
284{
285    TiXmlElement *pElem;
286    TiXmlElement *pChild;
287    TiXmlElement *pChild1;
288    TiXmlElement *pSecondChild;
289    TiXmlHandle hRoot(0);
290
291    int32_t count = 0;
292    size_t item_count = 0;
293    size_t affect_count = 0;
294    uint32_t attribute_count = 0;
295    bool match_found = false;
296
297    LOG("Cognitive Radio Shell:: Loading radio configuration.\n");
298
299    TiXmlDocument doc( radioConfig );
300    bool loadOkay = doc.LoadFile();
301    if(!loadOkay)
302        ERROR(1,"Loading radio configuration failed: %s\n", radioConfig);
303
304    TiXmlHandle hDoc(&doc);
305   
306    pElem = hDoc.FirstChildElement().Element();
307
308    if(!pElem)
309        ERROR(1, "No valid root!");
310
311    hRoot = TiXmlHandle(pElem);
312
313    pElem = hRoot.FirstChild("utilities").Element();
314    pChild1 = hRoot.Child("utilities", count).Element();
315
316    for(pChild = pChild1->FirstChildElement("utility"); pChild; \
317        pChild = pChild->NextSiblingElement()) {
318
319        const char *uName = pChild->Attribute("name");
320        if(uName)
321            uList[item_count].name = uName;   
322
323        const char *uUnits = pChild->Attribute("units");
324        if(uUnits)
325            uList[item_count].units = uUnits;
326
327        const char *uGoal = pChild->Attribute("goal");
328        if(uGoal)
329            uList[item_count].goal = uGoal;
330
331        if(pChild->QueryFloatAttribute("target", &uList[item_count].target) != TIXML_SUCCESS)
332            uList[item_count].target = -1;
333
334        item_count++;
335    }
336
337    radio_info->numUtilities = item_count;   
338    LOG("Cognitive Radio Shell:: Parsed %d utilities.\n", radioInfo->numUtilities);
339
340    item_count = 0;
341    pElem = hRoot.FirstChild("observables").Element();
342    pChild1 = hRoot.Child("observables", count).Element();
343
344    for(pChild = pChild1->FirstChildElement("observable"); pChild; \
345        pChild = pChild->NextSiblingElement()) {
346
347        const char *oName = pChild->Attribute("name");
348        if(oName)
349            oList[item_count].name = oName;
350       
351        affect_count = 0;
352        for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \
353            pSecondChild = pSecondChild->NextSiblingElement()) {
354
355            const char *oUtilName = pSecondChild->Attribute("utility");
356            if(oUtilName) {
357                for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++ ) {
358                    if(uList[attribute_count].name == oUtilName) {
359
360                        oList[item_count].affection_list[affect_count].u = &uList[attribute_count];
361                        const char *oRelate = pSecondChild->Attribute("relationship");
362                        if(oRelate)
363                            oList[item_count].affection_list[affect_count].relation = oRelate;
364
365                        affect_count++;
366                        match_found = true;
367                        break;
368                    }
369                }
370            }
371
372            if(!match_found) {
373                ERROR(1, "Error: %s: %s is not a valid utility.\n", \
374                    oList[item_count].name.c_str(), oUtilName);
375            }
376            else
377                match_found = false;   
378        }
379        oList[item_count].numAffects = affect_count;
380        item_count++;
381    }
382
383    radioInfo->numObservables = item_count;   
384    LOG("Cognitive Radio Shell:: Parsed %d observables.\n", radioInfo->numObservables);
385
386    pElem = hRoot.FirstChild("parameters").Element();
387    pChild1 = hRoot.Child("parameters", count).Element();
388   
389    item_count = 0;
390    for(pChild = pChild1->FirstChildElement("parameter"); pChild; \
391        pChild = pChild->NextSiblingElement()) {
392
393        const char *pName = pChild->Attribute("name");
394        if(pName)
395            pList[item_count].name = pName;   
396
397        const char *pUnits = pChild->Attribute("units");
398        if(pUnits)
399            pList[item_count].units = pUnits;
400
401        if(pChild->QueryFloatAttribute("min", &pList[item_count].min) != TIXML_SUCCESS)
402            pList[item_count].min = -1;
403
404        if(pChild->QueryFloatAttribute("max", &pList[item_count].max) != TIXML_SUCCESS)
405            pList[item_count].max = -1;
406
407        if(pChild->QueryFloatAttribute("step", &pList[item_count].step) != TIXML_SUCCESS)
408            pList[item_count].step = -1;
409       
410        affect_count = 0;
411        for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \
412
413            pSecondChild = pSecondChild->NextSiblingElement()) {
414            const char *pUtilName = pSecondChild->Attribute("utility");
415            if(pUtilName) {
416                for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++) {
417                    if(uList[attribute_count].name == pUtilName) {
418                        pList[item_count].affection_list[affect_count].u = &uList[attribute_count];   
419
420                        const char *pRelate = pSecondChild->Attribute("relationship");
421                        if(pRelate)
422                            pList[item_count].affection_list[affect_count].relation = pRelate;
423                        else
424                            LOG("Error: No relation found.\n");
425
426                        match_found = true;
427                        affect_count++;
428                        break;
429                    }
430                }
431            }
432
433            if(!match_found) {
434                ERROR(1, "Error: %s: %s is not a valid utility.\n", \
435                    pList[item_count].name.c_str(), pUtilName);
436            }
437
438            match_found = false;   
439        }
440
441        pList[item_count].numAffects = affect_count;
442        item_count++;
443    }
444
445    radioInfo->numParameters = item_count;
446    LOG("Cognitive Radio Shell:: Parsed %d parameters.\n", radioInfo->numParameters);
447
448    return 1;
449}
450
451
452void
453CognitiveRadioShell::GetOptimalParameters(int32_t socketFD)
454{
455    char buffer[256];
456    char counter[55];
457    char var[50];
458
459    /* Receive Set of Observables */
460    LOG("Cognitive Radio Shell:: Got request for optimization.\n");
461    memset(buffer, 0, 256);
462    ReadMessage(commandSocketFD, buffer);
463    uint32_t numObservables = atoi(buffer);
464 
465    LOG("Cognitive Radio Shell:: Attempting to get %i observables.\n", numObservables);
466    Observable *o = new Observable[numObservables];
467 
468    for(size_t i = 0; i < numObservables; i++) {
469        memset(buffer, 0, 256);
470        ReadMessage(commandSocketFD, buffer);
471        o[i].name = std::string(buffer);
472   
473        memset(buffer, 0, 256);
474        ReadMessage(commandSocketFD, buffer);
475        o[i].value = atof(buffer);
476    }
477
478    /* Receive Set of Current Parameters */
479    memset(buffer, 0, 256);
480    ReadMessage(commandSocketFD,buffer);
481    uint32_t numCurrentParameters = atoi(buffer);
482 
483    LOG("Cognitive Radio Shell:: Attempting to get %i parameters.\n",numCurrentParameters);
484    Parameter * cp = new Parameter[numCurrentParameters];
485
486    for (size_t i = 0; i < numCurrentParameters; i++){
487        memset(buffer, 0, 256);
488        ReadMessage(commandSocketFD,buffer);
489        cp[i].name = std::string(buffer);
490        memset(buffer, 0, 256);
491        ReadMessage(commandSocketFD,buffer);
492        cp[i].value = atof(buffer);
493    }
494
495    /* Send to Cognitive Engine
496     * TODO: With multiple CEs we need to make a decision about where
497     * to send this information
498     */
499    if(!SML_present) {
500        LOG("Cognitive Radio Shell:: Passing on observables to Cognitive Engine\n");
501        SendMessage(ceSocketFD,"request_optimization");
502        sprintf(counter,"%i",numObservables);
503        SendMessage(ceSocketFD,counter);
504        for(size_t i = 0; i < numObservables; i++) {
505            SendMessage(ceSocketFD,o[i].name.c_str());
506            sprintf(var,"%f",o[i].value);
507            SendMessage(ceSocketFD,var);
508        }
509       
510        LOG("Cognitive Radio Shell:: Passing on current parameters to Cognitive Engine\n");
511        sprintf(counter,"%i",numCurrentParameters);
512        SendMessage(ceSocketFD,counter);
513        for(size_t i = 0; i < numCurrentParameters; i++) {
514            SendMessage(ceSocketFD,cp[i].name.c_str());
515            sprintf(var,"%f",cp[i].value);
516            SendMessage(ceSocketFD,var);
517        }
518    }
519
520    LOG("Cognitive Radio Shell:: Receiving optimized parameters from Cognitive Engine.\n");
521    /* Receive Set of Parameters */
522    memset(buffer, 0, 256);
523    ReadMessage(ceSocketFD, buffer);
524    uint32_t numParameters = atoi(buffer);
525   
526    Parameter *p = new Parameter[numParameters];
527 
528    for(size_t i = 0; i < numParameters; i++) {
529        memset(buffer, 0, 256);
530        ReadMessage(ceSocketFD, buffer);
531        p[i].name = std::string(buffer);
532   
533        memset(buffer, 0, 256);
534        ReadMessage(ceSocketFD, buffer);
535        p[i].value = atof(buffer);
536    }
537
538    /* Send to Application
539     */
540    LOG("Cognitive Radio Shell:: Sending optimized parameters to Application.\n");
541    memset(counter, 0, 55);
542    sprintf(counter, "%i", numParameters);
543    SendMessage(commandSocketFD, counter);
544    for(size_t i = 0; i < numParameters; i++) {
545        SendMessage(commandSocketFD, p[i].name.c_str());
546        sprintf(var, "%f", p[i].value);
547        SendMessage(commandSocketFD, var);
548    }
549
550    delete [] o;
551    delete [] p;
552}
553
554// TODO point of always returning 1?
555bool
556CognitiveRadioShell::UpdateParameterPerformance(int32_t socketFD)
557{
558    char counter[55];
559    char var[50];
560    char buffer[256];
561
562    /* Receive Set of Parameters */
563    memset(buffer, 0, 256);
564    ReadMessage(commandSocketFD,buffer);
565    uint32_t numParameters = atoi(buffer);
566 
567    Parameter *p = new Parameter[numParameters];
568
569    for (size_t i = 0; i < numParameters; i++){
570        memset(buffer, 0, 256);
571        ReadMessage(commandSocketFD,buffer);
572        p[i].name = std::string(buffer);
573        memset(buffer, 0, 256);
574        ReadMessage(commandSocketFD,buffer);
575        p[i].value = atof(buffer);
576    }
577
578    /* Receive Set of Observables */
579    memset(buffer, 0, 256);
580    ReadMessage(commandSocketFD, buffer);
581    uint32_t numObservables = atoi(buffer);
582 
583    Observable *o = new Observable[numObservables];
584 
585    for(size_t i = 0; i < numObservables; i++) {
586        memset(buffer, 0, 256);
587        ReadMessage(commandSocketFD, buffer);
588        o[i].name = std::string(buffer);
589   
590        memset(buffer, 0, 256);
591        ReadMessage(commandSocketFD, buffer);
592        o[i].value = atof(buffer);
593    }
594
595    SendMessage(ceSocketFD, "update_performance");
596   
597    /* Send Parameters */
598    memset(counter, 0, 55);
599    sprintf(counter, "%i", numParameters);
600    SendMessage(ceSocketFD, counter);
601   
602    for(size_t i = 0; i < numParameters; i++) {
603        SendMessage(ceSocketFD,p[i].name.c_str());
604        sprintf(var,"%f",p[i].value);
605        SendMessage(ceSocketFD,var); 
606    }   
607   
608    /* Send Observables */
609    sprintf(counter, "%i", numObservables);
610    SendMessage(ceSocketFD, counter);
611    for(size_t i = 0; i < numObservables; i++) {
612        SendMessage(ceSocketFD, o[i].name.c_str());
613        sprintf(var, "%f", o[i].value);
614        SendMessage(ceSocketFD, var);
615    }   
616
617    delete [] p;
618    delete [] o;
619   
620    return 1;
621}
622
623
624void
625CognitiveRadioShell::HandleMessage(int32_t socketFD)
626{
627    char buffer[256];
628
629    ReadMessage(socketFD, buffer);
630
631    // TODO trying to read this code lock makes my eyes bleed
632    if(strcmp(buffer, "register_engine_cognitive") == 0) {
633        RegisterCognitiveEngine(socketFD);
634    } else if(strcmp(buffer, "deregister_engine_cognitive") == 0) {
635        DeregisterCognitiveEngine(socketFD);
636    } else if(strcmp(buffer, "register_engine_policy") == 0) {
637        RegisterPolicyEngine(socketFD);
638    } else if(strcmp(buffer, "deregister_engine_policy") == 0) {
639        DeregisterPolicyEngine(socketFD);
640    } else if(strcmp(buffer, "register_sml") == 0) {
641        RegisterSML(socketFD);
642    } else if(strcmp(buffer, "deregister_sml") == 0) {
643        DeregisterSML(socketFD);
644    } else if(strcmp(buffer, "update_performance") == 0) {
645        UpdateParameterPerformance(socketFD);
646    } else if(strcmp(buffer, "get_number_utilities") == 0) {
647        char numUtilities[20];
648        sprintf(numUtilities, "%i", radio_info->numUtilities);
649        SendMessage(commandSocketFD, numUtilities);
650    } else if(strcmp(buffer, "get_number_observables") == 0) {
651        char numObservables[20];
652        sprintf(numObservables, "%i", radio_info->numObservables);
653        SendMessage(commandSocketFD, numObservables);
654    } else if(strcmp(buffer, "get_number_parameters") == 0) {
655        char numParameters[20];
656        sprintf(numParameters, "%i", radio_info->numParameters);
657        SendMessage(commandSocketFD, numParameters);
658    } else if(strcmp(buffer, "request_optimization") == 0) {
659        /* Receive optimization request and current environment */
660        GetOptimalParameters(socketFD); 
661    } else if(strcmp(buffer, "request_optimization_service") == 0) {
662        /* Receive optimization request and current environment */
663        //GetOptimalParametersService(socketFD); 
664    }
665}
666
667
668void
669CognitiveRadioShell::StartShellServer()
670{
671    struct timeval selTimeout;
672    int32_t primary = 0;
673    int32_t policy = 1;
674    int32_t command = 2;
675    int32_t running = 1;
676    int32_t port, rc, new_sd = 1;
677    int32_t desc_ready = 1;
678    int32_t timeout = 10;
679    fd_set sockSet;
680
681    int32_t *servSock = new int32_t[3];
682
683    servSock[primary] = CreateTCPServerSocket(primaryPort);
684    servSock[policy] = CreateTCPServerSocket(policyPort);
685    servSock[command] = CreateTCPServerSocket(commandPort);
686
687    int32_t maxDescriptor;
688
689    if(servSock[primary] > servSock[policy])
690        maxDescriptor = servSock[primary];
691    else
692        maxDescriptor = servSock[policy];
693
694    if(servSock[command] > maxDescriptor)
695        maxDescriptor = servSock[command];
696
697    if(InitializeTCPServerPort(servSock[primary]) == -1)
698        ERROR(1,"Error initializing primary port\n");
699 
700    if(InitializeTCPServerPort(servSock[policy]) == -1)
701        ERROR(1,"Error initializing policy port\n");
702
703    if(InitializeTCPServerPort(servSock[command]) == -1)
704        ERROR(1,"Error initializing command port\n");
705
706    while (running) {
707        /* Zero socket descriptor vector and set for server sockets */
708        /* This must be reset every time select() is called */
709        FD_ZERO(&sockSet);
710        FD_SET(servSock[primary], &sockSet);
711        FD_SET(servSock[policy], &sockSet);
712        FD_SET(servSock[command], &sockSet);
713
714        /* Timeout specification */
715        /* This must be reset every time select() is called */
716        selTimeout.tv_sec = timeout;       /* timeout (secs.) */
717        selTimeout.tv_usec = 0;            /* 0 microseconds */
718
719        /* Suspend program until descriptor is ready or timeout */
720        rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout);
721        if(rc == 0)
722            LOG("No echo requests for %i secs...Server still alive\n", timeout);
723        else {
724            desc_ready = rc;
725
726            for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) {
727                if(FD_ISSET(port, &sockSet)) {
728                    desc_ready -= 1;
729
730                    /* Check if request is new or on an existing open descriptor */
731                    if((port == servSock[primary]) || (port == servSock[policy]) || (port == servSock[command])) {
732                        do {
733                            new_sd = AcceptTCPConnection(port);
734                            if(new_sd < 0)
735                                break;
736                           
737                            if(port == servSock[primary])
738                                ceSocketFD = new_sd;
739                            if(port == servSock[command])
740                                commandSocketFD = new_sd;
741                            if(port == servSock[policy])
742                                policySocketFD = new_sd;
743
744                            HandleMessage(new_sd);
745                            FD_SET(new_sd,&sockSet);
746                            if(new_sd > maxDescriptor)
747                                maxDescriptor = new_sd;
748                            //LOG("New incoming connection - %i\n\n",new_sd);
749                        } while(new_sd != -1);
750                    }
751                    else {
752                        //LOG("Request on already open descriptor.\n\n");
753                        HandleMessage(port);
754                    }
755                }
756            }
757        }
758    }
759
760
761    /* Close sockets */
762    close(servSock[primary]);
763    close(servSock[policy]);
764    close(servSock[command]);
765
766    /* Free list of sockets */
767    delete servSock;
768
769    return;
770}
Note: See TracBrowser for help on using the browser.