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

Revision 281, 23.9 KB (checked in by trnewman, 15 years ago)

Adding set active mission functionality in the shell

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
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
644void
645CognitiveRadioShell::HandleMessage(int32_t socketFD)
646{
647    char buffer[256];
648
649    ReadMessage(socketFD, buffer);
650
651    // TODO trying to read this code lock makes my eyes bleed
652    if(strcmp(buffer, "register_engine_cognitive") == 0) {
653        RegisterCognitiveEngine(socketFD);
654    } else if(strcmp(buffer, "deregister_engine_cognitive") == 0) {
655        DeregisterCognitiveEngine(socketFD);
656    } else if(strcmp(buffer, "register_engine_policy") == 0) {
657        RegisterPolicyEngine(socketFD);
658    } else if(strcmp(buffer, "deregister_engine_policy") == 0) {
659        DeregisterPolicyEngine(socketFD);
660    } else if(strcmp(buffer, "register_sml") == 0) {
661        RegisterSML(socketFD);
662    } else if(strcmp(buffer, "deregister_sml") == 0) {
663        DeregisterSML(socketFD);
664    } else if(strcmp(buffer, "update_performance") == 0) {
665        UpdateParameterPerformance(socketFD);
666    } else if(strcmp(buffer, "get_number_utilities") == 0) {
667        char numUtilities[20];
668        sprintf(numUtilities, "%i", radio_info->numUtilities);
669        SendMessage(commandSocketFD, numUtilities);
670    } else if(strcmp(buffer, "get_number_observables") == 0) {
671        char numObservables[20];
672        sprintf(numObservables, "%i", radio_info->numObservables);
673        SendMessage(commandSocketFD, numObservables);
674    } else if(strcmp(buffer, "get_number_parameters") == 0) {
675        char numParameters[20];
676        sprintf(numParameters, "%i", radio_info->numParameters);
677        SendMessage(commandSocketFD, numParameters);
678    } else if(strcmp(buffer, "request_optimization") == 0) {
679        /* Receive optimization request and current environment */
680        GetOptimalParameters(socketFD); 
681    } else if(strcmp(buffer, "set_active_mission") == 0) {
682        SetActiveMission(socketFD); 
683    } else if(strcmp(buffer, "request_optimization_service") == 0) {
684        /* Receive optimization request and current environment */
685        //GetOptimalParametersService(socketFD); 
686    }
687}
688
689
690void
691CognitiveRadioShell::StartShellServer()
692{
693    struct timeval selTimeout;
694    int32_t primary = 0;
695    int32_t policy = 1;
696    int32_t command = 2;
697    int32_t running = 1;
698    int32_t port, rc, new_sd = 1;
699    int32_t desc_ready = 1;
700    int32_t timeout = 10;
701    fd_set sockSet;
702
703    int32_t *servSock = new int32_t[3];
704
705    servSock[primary] = CreateTCPServerSocket(primaryPort);
706    servSock[policy] = CreateTCPServerSocket(policyPort);
707    servSock[command] = CreateTCPServerSocket(commandPort);
708
709    int32_t maxDescriptor;
710
711    if(servSock[primary] > servSock[policy])
712        maxDescriptor = servSock[primary];
713    else
714        maxDescriptor = servSock[policy];
715
716    if(servSock[command] > maxDescriptor)
717        maxDescriptor = servSock[command];
718
719    if(InitializeTCPServerPort(servSock[primary]) == -1)
720        ERROR(1,"Error initializing primary port\n");
721 
722    if(InitializeTCPServerPort(servSock[policy]) == -1)
723        ERROR(1,"Error initializing policy port\n");
724
725    if(InitializeTCPServerPort(servSock[command]) == -1)
726        ERROR(1,"Error initializing command port\n");
727
728    while (running) {
729        /* Zero socket descriptor vector and set for server sockets */
730        /* This must be reset every time select() is called */
731        FD_ZERO(&sockSet);
732        FD_SET(servSock[primary], &sockSet);
733        FD_SET(servSock[policy], &sockSet);
734        FD_SET(servSock[command], &sockSet);
735
736        /* Timeout specification */
737        /* This must be reset every time select() is called */
738        selTimeout.tv_sec = timeout;       /* timeout (secs.) */
739        selTimeout.tv_usec = 0;            /* 0 microseconds */
740
741        /* Suspend program until descriptor is ready or timeout */
742        rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout);
743        if(rc == 0)
744            LOG("No echo requests for %i secs...Server still alive\n", timeout);
745        else {
746            desc_ready = rc;
747
748            for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) {
749                if(FD_ISSET(port, &sockSet)) {
750                    desc_ready -= 1;
751
752                    /* Check if request is new or on an existing open descriptor */
753                    if((port == servSock[primary]) || (port == servSock[policy]) || (port == servSock[command])) {
754                        do {
755                            new_sd = AcceptTCPConnection(port);
756                            if(new_sd < 0)
757                                break;
758                           
759                            if(port == servSock[primary])
760                                ceSocketFD = new_sd;
761                            if(port == servSock[command])
762                                commandSocketFD = new_sd;
763                            if(port == servSock[policy])
764                                policySocketFD = new_sd;
765
766                            HandleMessage(new_sd);
767                            FD_SET(new_sd,&sockSet);
768                            if(new_sd > maxDescriptor)
769                                maxDescriptor = new_sd;
770                            //LOG("New incoming connection - %i\n\n",new_sd);
771                        } while(new_sd != -1);
772                    }
773                    else {
774                        //LOG("Request on already open descriptor.\n\n");
775                        HandleMessage(port);
776                    }
777                }
778            }
779        }
780    }
781
782
783    /* Close sockets */
784    close(servSock[primary]);
785    close(servSock[policy]);
786    close(servSock[command]);
787
788    /* Free list of sockets */
789    delete servSock;
790
791    return;
792}
Note: See TracBrowser for help on using the browser.