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

Revision 230, 23.0 KB (checked in by trnewman, 15 years ago)

Added a loop in the demo to actually adapt.

Added simple adaptation functionality and proper db querying in the CE.

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
113bool
114CognitiveRadioShell::SendRadioConfiguration(int32_t socketfd)
115{
116    LOG("Cognitive Radio Shell:: Sending radio configuration to Cognitive Engine.\n");
117    uint32_t i,j;
118    char counter[55];
119    char var[50];
120
121    // utilities
122    // Send number of utilities
123    sprintf(counter,"%d",radio_info->numUtilities);
124    SendMessage(socketfd,counter);
125    // send utility     
126    for(i = 0; i < radio_info->numUtilities; i++) {
127        SendMessage(socketfd, utils[i].name.c_str());
128        SendMessage(socketfd, utils[i].units.c_str());
129        SendMessage(socketfd, utils[i].goal.c_str());
130        sprintf(var,"%f", utils[i].target);
131        SendMessage(socketfd, var);
132    }
133
134    // parameters
135    sprintf(counter,"%i",radio_info->numParameters);
136    SendMessage(socketfd,counter);
137    for(i = 0; i < radio_info->numParameters; i++) {
138        SendMessage(socketfd, params[i].name.c_str());
139        SendMessage(socketfd, params[i].units.c_str());
140        sprintf(var, "%f", params[i].min);
141        SendMessage(socketfd,var);
142        sprintf(var, "%f", params[i].max);
143        SendMessage(socketfd, var);
144        sprintf(var, "%f", params[i].step);
145        SendMessage(socketfd, var);
146
147        sprintf(counter, "%i", params[i].numAffects);
148        SendMessage(socketfd, counter);
149        for(j = 0; j < params[i].numAffects; j++) {
150            SendMessage(socketfd, params[i].affection_list[j].u->name.c_str());
151            SendMessage(socketfd, params[i].affection_list[j].relation.c_str());
152        }
153    }
154
155    // observables
156    sprintf(counter,"%i",radio_info->numObservables);
157    SendMessage(socketfd, counter);
158    for(i = 0; i < radio_info->numObservables; i++) {
159        SendMessage(socketfd, observables[i].name.c_str());
160               
161        sprintf(counter, "%i", observables[i].numAffects);
162        SendMessage(socketfd, counter);
163        for(j = 0; j < observables[i].numAffects; j++) {
164            SendMessage(socketfd, observables[i].affection_list[j].u->name.c_str());
165            SendMessage(socketfd, observables[i].affection_list[j].relation.c_str());
166        }
167    }
168       
169    // Receive ACK for utils
170    char buffer[256];
171    memset(buffer, 0, 256);
172    ReadMessage(socketfd, buffer);
173
174    if(strcmp(buffer, "receive_config_ack") != 0) {
175        LOG("Cognitive Radio Shell:: Unexpected response: %s\n", buffer);
176        return 0;
177    }
178
179    return 1;
180
181}
182
183bool
184CognitiveRadioShell::SendRadioExperience(int32_t socketFD)
185{
186
187    LOG("Cognitive Radio Shell:: Sending radio experience to Cognitive Engine.\n");
188    int32_t numberExp = 4;
189        char numberExpString[50];
190
191    sprintf(numberExpString,"%i",numberExp);
192    SendMessage(socketFD, numberExpString);
193
194    char buffer[256];
195    memset(buffer, 0, 256);
196    ReadMessage(socketFD, buffer);
197    if(strcmp(buffer, "receive_exp_ack") != 0) {
198        LOG("Cognitive Radio Shell:: Unexpected response: %s\n",buffer);
199        return 0;
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    SendRadioConfiguration(socketFD);
211    SendRadioExperience(socketFD);
212
213    numberOfCognitiveEngines++;
214    CE_present = true;
215}
216
217
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)
225        CE_present = false;
226
227    SendMessage(socketFD, "deregister_ack");
228    shutdown(socketFD, 2);
229    close(socketFD);
230    LOG("Cognitive Radio Shell:: Socket closed.\n");
231}
232
233
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
241
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
255
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
264
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
278
279int32_t
280CognitiveRadioShell::LoadRadioConfiguration(const char* radioConfig, \
281        Parameter* &pList, Utility* &uList, Observable* &oList, \
282        Radio_Info* radioInfo)
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;
293    uint32_t attribute_count = 0;
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();
300    if(!loadOkay)
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; \
316        pChild = pChild->NextSiblingElement()) {
317
318        const char *uName = pChild->Attribute("name");
319        if(uName)
320            uList[item_count].name = uName;   
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
336    radio_info->numUtilities = item_count;   
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; \
344        pChild = pChild->NextSiblingElement()) {
345
346        const char *oName = pChild->Attribute("name");
347        if(oName)
348            oList[item_count].name = oName;
349       
350        affect_count = 0;
351        for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \
352            pSecondChild = pSecondChild->NextSiblingElement()) {
353
354            const char *oUtilName = pSecondChild->Attribute("utility");
355            if(oUtilName) {
356                for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++ ) {
357                    if(uList[attribute_count].name == oUtilName) {
358
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;
363
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
376                match_found = false;   
377        }
378        oList[item_count].numAffects = affect_count;
379        item_count++;
380    }
381
382    radioInfo->numObservables = item_count;   
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();
387   
388    item_count = 0;
389    for(pChild = pChild1->FirstChildElement("parameter"); pChild; \
390        pChild = pChild->NextSiblingElement()) {
391
392        const char *pName = pChild->Attribute("name");
393        if(pName)
394            pList[item_count].name = pName;   
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;
408       
409        affect_count = 0;
410        for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \
411
412            pSecondChild = pSecondChild->NextSiblingElement()) {
413            const char *pUtilName = pSecondChild->Attribute("utility");
414            if(pUtilName) {
415                for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++) {
416                    if(uList[attribute_count].name == pUtilName) {
417                        pList[item_count].affection_list[affect_count].u = &uList[attribute_count];   
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
432            if(!match_found) {
433                ERROR(1, "Error: %s: %s is not a valid utility.\n", \
434                    pList[item_count].name.c_str(), pUtilName);
435            }
436
437            match_found = false;   
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}
450
451
452void
453CognitiveRadioShell::GetOptimalParameters(int32_t socketFD)
454{
455    char buffer[256];
456    uint32_t numObservables,numParameters,numCurrentParameters;
457    char counter[55];
458    char var[50];
459
460    /* Receive Set of Observables */
461    LOG("Cognitive Radio Shell:: Got request for optimization.\n");
462    memset(buffer, 0, 256);
463    ReadMessage(commandSocketFD, buffer);
464    numObservables = atoi(buffer);
465 
466        LOG("Cognitive Radio Shell:: Attempting to get %i observables.\n", numObservables);
467    Observable *o = new Observable[numObservables];
468 
469    for(size_t i = 0; i < numObservables; i++) {
470        memset(buffer, 0, 256);
471        ReadMessage(commandSocketFD, buffer);
472        o[i].name = std::string(buffer);
473   
474        memset(buffer, 0, 256);
475        ReadMessage(commandSocketFD, buffer);
476        o[i].value = atof(buffer);
477    }
478        /* Receive Set of Current Parameters */
479    memset(buffer, 0, 256);
480    ReadMessage(commandSocketFD,buffer);
481    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    /* Send to Cognitive Engine
495         * TODO: With multiple CEs we need to make a decision about where
496         * to send this information
497         */
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    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
554bool
555CognitiveRadioShell::UpdateParameterPerformance(int32_t socketFD)
556{
557    char counter[55];
558    uint32_t numObservables,numParameters;
559    char var[50];
560    char buffer[256];
561
562        /* Receive Set of Parameters */
563    memset(buffer, 0, 256);
564    ReadMessage(commandSocketFD,buffer);
565    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    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
596    SendMessage(ceSocketFD, "update_performance");
597   
598    // Send Parameters
599    memset(counter, 0, 55);
600    sprintf(counter, "%i", numParameters);
601    SendMessage(ceSocketFD, counter);
602   
603    for(size_t i = 0; i < numParameters; i++) {
604        SendMessage(ceSocketFD,p[i].name.c_str());
605        sprintf(var,"%f",p[i].value);
606        SendMessage(ceSocketFD,var); 
607    }   
608   
609    // Send Observables
610    sprintf(counter, "%i", numObservables);
611    SendMessage(ceSocketFD, counter);
612    for(size_t i = 0; i < numObservables; i++) {
613        SendMessage(ceSocketFD, o[i].name.c_str());
614        sprintf(var, "%f", o[i].value);
615        SendMessage(ceSocketFD, var);
616    }   
617   
618    return 1;
619}
620void
621CognitiveRadioShell::HandleMessage(int32_t socketFD)
622{
623    char buffer[256];
624
625    ReadMessage(socketFD, buffer);
626
627    if(strcmp(buffer, "register_engine_cognitive") == 0) {
628        RegisterCognitiveEngine(socketFD);
629    } else if(strcmp(buffer, "deregister_engine_cognitive") == 0) {
630        DeregisterCognitiveEngine(socketFD);
631    } else if(strcmp(buffer, "register_engine_policy") == 0) {
632        RegisterPolicyEngine(socketFD);
633    } else if(strcmp(buffer, "deregister_engine_policy") == 0) {
634        DeregisterPolicyEngine(socketFD);
635    } else if(strcmp(buffer, "register_sml") == 0) {
636        RegisterSML(socketFD);
637    } else if(strcmp(buffer, "deregister_sml") == 0) {
638        DeregisterSML(socketFD);
639    } else if(strcmp(buffer, "update_performance") == 0) {
640        UpdateParameterPerformance(socketFD);
641    } else if(strcmp(buffer,"get_number_utilities") == 0) {
642                char numUtilities[20];
643                sprintf(numUtilities,"%i",radio_info->numUtilities);
644        SendMessage(commandSocketFD,numUtilities);
645    } else if(strcmp(buffer,"get_number_observables") == 0) {
646                char numObservables[20];
647                sprintf(numObservables,"%i",radio_info->numObservables);
648        SendMessage(commandSocketFD,numObservables);
649    } else if(strcmp(buffer,"get_number_parameters") == 0) {
650                char numParameters[20];
651                sprintf(numParameters,"%i",radio_info->numParameters);
652        SendMessage(commandSocketFD,numParameters);
653    } else if(strcmp(buffer,"request_optimization") == 0) {
654        /* Receive optimization request and current environment */
655        GetOptimalParameters(socketFD); 
656    } else if(strcmp(buffer,"request_optimization_service") == 0) {
657        /* Receive optimization request and current environment */
658        //GetOptimalParametersService(socketFD); 
659    }
660}
661
662
663void
664CognitiveRadioShell::StartShellServer()
665{
666    struct timeval selTimeout;
667    int32_t primary = 0;
668    int32_t policy = 1;
669    int32_t command = 2;
670    int32_t running = 1;
671    int32_t port, rc, new_sd = 1;
672    int32_t desc_ready = 1;
673    int32_t timeout = 10;
674    fd_set sockSet;
675
676    int32_t *servSock = new int32_t[3];
677
678    servSock[primary] = CreateTCPServerSocket(primaryPort);
679    servSock[policy] = CreateTCPServerSocket(policyPort);
680    servSock[command] = CreateTCPServerSocket(commandPort);
681
682    int32_t maxDescriptor;
683
684        if(servSock[primary] > servSock[policy])
685                maxDescriptor = servSock[primary];
686    else
687                maxDescriptor = servSock[policy];
688
689        if(servSock[command] > maxDescriptor)
690                maxDescriptor = servSock[command];
691
692    if(InitializeTCPServerPort(servSock[primary]) == -1)
693        ERROR(1,"Error initializing primary port\n");
694 
695    if(InitializeTCPServerPort(servSock[policy]) == -1)
696        ERROR(1,"Error initializing policy port\n");
697
698    if(InitializeTCPServerPort(servSock[command]) == -1)
699        ERROR(1,"Error initializing command port\n");
700
701    while (running) {
702        /* Zero socket descriptor vector and set for server sockets */
703        /* This must be reset every time select() is called */
704        FD_ZERO(&sockSet);
705        FD_SET(servSock[primary], &sockSet);
706        FD_SET(servSock[policy], &sockSet);
707        FD_SET(servSock[command], &sockSet);
708
709        /* Timeout specification */
710        /* This must be reset every time select() is called */
711        selTimeout.tv_sec = timeout;       /* timeout (secs.) */
712        selTimeout.tv_usec = 0;            /* 0 microseconds */
713
714        /* Suspend program until descriptor is ready or timeout */
715        rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout);
716        if(rc == 0)
717            LOG("No echo requests for %i secs...Server still alive\n", timeout);
718        else {
719            desc_ready = rc;
720
721            for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) {
722                if(FD_ISSET(port, &sockSet)) {
723                    desc_ready -= 1;
724
725                    /* Check if request is new or on an existing open descriptor */
726                    if((port == servSock[primary]) || (port == servSock[policy]) || (port == servSock[command])) {
727                        do {
728                            new_sd = AcceptTCPConnection(port);
729                            if(new_sd < 0)
730                                break;
731                           
732                            if(port == servSock[primary])
733                                ceSocketFD = new_sd;
734                            if(port == servSock[command])
735                                commandSocketFD = new_sd;
736                            if(port == servSock[policy])
737                                policySocketFD = new_sd;
738
739                            HandleMessage(new_sd);
740                            FD_SET(new_sd,&sockSet);
741                            if(new_sd > maxDescriptor)
742                                maxDescriptor = new_sd;
743                            //LOG("New incoming connection - %i\n\n",new_sd);
744                        } while(new_sd != -1);
745                    }
746                    else {
747                        //LOG("Request on already open descriptor.\n\n");
748                        HandleMessage(port);
749                    }
750                }
751            }
752        }
753    }
754
755
756    /* Close sockets */
757    close(servSock[primary]);
758    close(servSock[policy]);
759    close(servSock[command]);
760
761    /* Free list of sockets */
762    delete servSock;
763
764    return;
765}
Note: See TracBrowser for help on using the browser.