root/vtcross/trunk/src/shell/cr_shell.cpp @ 169

Revision 169, 21.8 KB (checked in by bhilburn, 15 years ago)

First pass at refactoring the parse_ce_config function. Still tons of work to do
in this function alone.

Line 
1/* Virginia Tech Cognitive Radio Open Source Systems
2 * Virginia Tech, 2009
3 *
4 * TODO LICENSE INFORMATION GOES HERE
5 */
6
7/* TODO DESCRIPTION OF FILE.
8 */
9
10
11#include <arpa/inet.h>
12#include <iostream>
13#include <netinet/in.h>
14#include <netdb.h>
15#include <fcntl.h>
16#include <sys/ioctl.h>
17#include <sys/mman.h>
18#include <sys/socket.h>
19#include <sys/types.h>
20#include <sys/wait.h>
21
22#include "tinyxml/tinyxml.h"
23#include "tinyxml/tinystr.h"
24
25#include "vtcross/common.h"
26#include "vtcross/components.h"
27#include "vtcross/containers.h"
28#include "vtcross/debug.h"
29#include "vtcross/error.h"
30#include "vtcross/socketcomm.h"
31
32
33using namespace std;
34
35
36#define CE_SERVER_PORT 30001
37#define PE_SERVER_PORT 30003
38
39
40void
41print_current_config(Utility* uList[], Parameter* pList[], \
42        Observable* oList[], CE_Info* ce_info)
43{
44        for(size_t i = 0; i < ce_info->numUtilities ; i++) {
45        LOG("Shell:: Utility: %s\n\tUnits: %s\n\tGoal: %s\n\tTarget: %s\n", \
46                uList[i]->name, uList[i]->units, uList[i]->goal, \
47                uList[i]->target);
48        }
49
50        for(size_t i = 0; i < ce_info->numParameters; i++) {
51            LOG("Shell:: Radio Operation Profile has been sucessfully sent.\n");
52        LOG("Shell:: Parameter: %s\n\tUnits: %s\n\tMin: %s\n\t", \
53                pList[i]->name, pList[i]->units, pList[i]->min);
54        LOG("\tMax: %s\n\tStep: %s\n", pList[i]->max, pList[i]->step);
55
56                for(size_t j = 0; j < pList[i]->numAffects; j++) {
57                        LOG("\t\tAffect %s -> %s\n", pList[i]->affection_list[j].u->name, \
58                    pList[i]->affection_list[j].relation);
59                }
60        }
61       
62    for(size_t i = 0; i < ce_info->numObservables; i++) {
63                LOG("Observable: %s\n", oList[i]->name);
64
65                for(size_t j = 0; j < oList[i]->numAffects; j++) {
66                        LOG("\t\tAffect %s -> %s ", oList[i]->affection_list[j].u->name, \
67                    oList[i]->affection_list[j].relation);
68                }
69        }
70}
71
72
73int32_t
74parse_ce_config(TiXmlDocument* doc, Utility* u[], Parameter* p[], \
75        Observable* o[], CE_Info* ce_info)
76{
77
78        TiXmlElement *pElem;
79        TiXmlElement *pChild;
80        TiXmlElement *pChild1;
81        TiXmlElement *pSecondChild;
82        TiXmlHandle hDoc(doc);
83        TiXmlHandle hRoot(0);
84
85        int32_t count = 0;
86        size_t item_count = 0;
87    size_t affect_count = 0;
88        int32_t attribute_count = 0;
89    bool match_found = false;
90
91        pElem = hDoc.FirstChildElement().Element();
92
93        if(!pElem)
94        ERROR(1, "No valid root!");
95
96        hRoot = TiXmlHandle(pElem);
97
98        pElem = hRoot.FirstChild("utilities").Element();
99        pChild1 = hRoot.Child("utilities", count).Element();
100
101        for(pChild = pChild1->FirstChildElement("utility"); pChild; \
102            pChild = pChild->NextSiblingElement()) {
103
104                u[item_count] = new Utility;
105
106                const char *uName = pChild->Attribute("name");
107                if(uName)
108            u[item_count]->name = uName;       
109
110                const char *uUnits = pChild->Attribute("units");
111                if(uUnits)
112            u[item_count]->units = uUnits;
113
114                const char *uGoal = pChild->Attribute("goal");
115                if(uGoal)
116            u[item_count]->goal = uGoal;
117
118                if(pChild->QueryFloatAttribute("target", &u[i]->target) != TIXML_SUCCESS)
119            u[i]->target = -1;
120
121                item_count++;
122        }
123
124        ce_info->numUtilities = item_count;     
125        LOG("Initialize:: Parsed %d utilities.\n", ce_info->numUtilities);
126
127        item_count = 0;
128        pElem = hRoot.FirstChild("observables").Element();
129        pChild1 = hRoot.Child("observables", count).Element();
130
131        for(pChild = pChild1->FirstChildElement("observable"); pChild; \
132            pChild = pChild->NextSiblingElement()) {
133
134                const char *oName = pChild->Attribute("name");
135                o[item_count] = new Observable;
136                if(oName)
137            o[item_count]->name = oName;
138               
139                affect_count = 0;
140                for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \
141                pSecondChild = pSecondChild->NextSiblingElement()) {
142
143                        const char *oUtilName = pSecondChild->Attribute("utility");
144                        if(oUtilName) {
145                                for(attribute_count = 0; u[attribute_count]! = NULL; attribute_count++ ) {
146                                        if(u[attribute_count]->name == oUtilName) {
147                                                o[i]->affection_list[affect_count].u = u[attribute_count];
148
149                                                const char *oRelate = pSecondChild->Attribute("relationship");
150                                                if(oRelate)
151                            o[item_count]->affection_list[affect_count].relation = oRelate;
152
153                                                affect_count++;
154                                                match_found = true;
155                                                break;
156                                        }
157                                }
158                        }
159
160                        if(!match_found)
161                ERROR(1, "Error: %s: %s is not a valid utility.\n", \
162                        o[i]->name, oUtilName);
163            else
164                match_found = false;   
165                }
166
167                o[item_count]->numAffects = affect_count;
168                item_count++;
169        }
170
171        ce_info->numObservables = item_count;   
172        LOG("Initialize:: Parsed %d observables.\n", ce_info->numObservables);
173
174        pElem = hRoot.FirstChild("parameters").Element();
175        pChild1 = hRoot.Child("parameters", count).Element();
176       
177        item_count = 0;
178        for(pChild = pChild1->FirstChildElement("parameter"); pChild; \
179            pChild = pChild->NextSiblingElement()) {
180
181                p[item_count] = new Parameter;
182
183                const char *pName = pChild->Attribute("name");
184                if(pName)
185            p[item_count]->name = pName;       
186
187                const char *pUnits = pChild->Attribute("units");
188                if(pUnits)
189            p[item_count]->units = pUnits;
190
191                if(pChild->QueryFloatAttribute("min", &p[i]->min) != TIXML_SUCCESS)
192            p[item_count]->min = -1;
193
194                if(pChild->QueryFloatAttribute("max", &p[i]->max) != TIXML_SUCCESS)
195            p[item_count]->max = -1;
196
197                if(pChild->QueryFloatAttribute("step", &p[i]->step) != TIXML_SUCCESS)
198            p[item_count]->step = -1;
199               
200                affect_count = 0;
201                for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \
202                pSecondChild = pSecondChild->NextSiblingElement()) {
203
204                        const char *pUtilName = pSecondChild->Attribute("utility");
205                        if(pUtilName) {
206                                for(attribute_count = 0; u[attribute_count] != NULL; attribute_count++) {
207                                        if(u[attribute_count]->name == pUtilName) {
208                                                p[item_count]->affection_list[affect_count].u = u[attribute_count];     
209
210                                                const char *pRelate = pSecondChild->Attribute("relationship");
211                                                if(pRelate)
212                                                        p[item_count]->affection_list[affect_count].relation = pRelate;
213                                                else
214                                                        LOG("Error: No relation found.\n");
215
216                                                match_found = true;
217                                                affect_count++;
218                                                break;
219                                        }
220                                }
221                        }
222
223                        if(!match_found) {
224                ERROR(1, "Error: %s: %s is not a valid utility.\n", \
225                        p[item_count]->name, pUtilName);
226            }
227
228                        match_found = false;   
229                }
230
231                p[item_count]->numAffects = affect_count;
232                item_count++;
233        }
234
235        ce_info->numParameters = item_count;
236        LOG("Initialize:: Parsed %d parameters.\n", ce_info->numParameters);
237
238        return 1;
239}
240
241
242int32_t
243ReceiveMessage(int32_t socket, char* buffer)
244{
245    int32_t i,n;
246   
247    n = recv(socket,buffer,256,MSG_PEEK);
248   
249    for(i=0;i<256;i++){
250        if(strcmp(&buffer[i],"\0") == 0) break;
251    }
252    n = recv(socket,buffer,i+1,0);
253    if (n < 0)
254        ERROR(1, "Error reading from socket!\n");
255    //    printf("ReadMessage:%s %d\n",buffer,n);
256
257    return n;
258}
259
260
261int32_t
262SendMessage(int32_t socketfd, string message)
263{
264        int32_t n;
265
266        message.append("\0");   
267        // Write message back to client
268    n = send(socketfd,message.c_str(),(message.size()+1),0);
269    if (n<0)
270        ERROR(1, "Error sending to client\n");
271    if(n == 0)
272        printf("Client closed the socket.\n");
273
274        //printf("SendMessage:%s %d\n",message.c_str(),n);     
275    return n;
276}
277
278
279void
280LoadCEConfiguration(int32_t socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info){
281        int32_t n,i,j;
282        char counter[55];
283        char var[50];
284        //int32_t total_bytes;   
285
286        printf("Cognitive Radio:: Sending Radio Operating Profile to Cognitive Engine.\n\n");
287 
288        // utilities
289        // Send number of utilities
290        sprintf(counter,"%d",ce_info->numUtilities);
291        SendMessage(socketfd,counter);
292        // send utility
293    for(i = 0; i < ce_info->numUtilities; i++) {
294                SendMessage(socketfd,uList[i]->name);
295                SendMessage(socketfd,uList[i]->units);
296                SendMessage(socketfd,uList[i]->goal);
297                sprintf(var,"%f",uList[i]->target);
298                SendMessage(socketfd,var);
299        }
300
301        // parameters
302    sprintf(counter,"%i",ce_info->numParameters);
303        SendMessage(socketfd,counter);
304        for(i = 0; i < ce_info->numParameters; i++) {
305                SendMessage(socketfd,pList[i]->name);
306                SendMessage(socketfd,pList[i]->units);
307                sprintf(var,"%f",pList[i]->min);
308                SendMessage(socketfd,var);
309                sprintf(var,"%f",pList[i]->max);
310                SendMessage(socketfd,var);
311                sprintf(var,"%f",pList[i]->step);
312                SendMessage(socketfd,var);
313               
314                sprintf(counter,"%i",pList[i]->numAffects);
315                SendMessage(socketfd,counter);
316                for(j = 0; j < pList[i]->numAffects; j++) {
317                        SendMessage(socketfd,pList[i]->affection_list[j].u->name);
318                        SendMessage(socketfd,pList[i]->affection_list[j].relation);
319                }
320        }
321
322    // observables
323        sprintf(counter,"%i",ce_info->numObservables);
324        SendMessage(socketfd,counter);
325        for(i = 0; i < ce_info->numObservables; i++) {
326                SendMessage(socketfd,oList[i]->name);
327               
328                sprintf(counter,"%i",oList[i]->numAffects);
329                SendMessage(socketfd,counter);
330                for(j = 0; j < oList[i]->numAffects; j++) {
331                        SendMessage(socketfd,oList[i]->affection_list[j].u->name);
332                        SendMessage(socketfd,oList[i]->affection_list[j].relation);
333                }
334        }
335       
336        // Receive ACK for utils
337    char buffer[256];
338        string message;
339        n = ReceiveMessage(socketfd, buffer);
340    //printf("%s\n", buffer);
341        //cout << message << endl;
342        //printf("ACK received.\n");
343
344}
345
346void UpdateCEConfiguration() {
347
348}
349
350void ResetCEConfiguration(){
351
352}
353
354void UpdateCEExperience(int32_t socketfd, int32_t num_rows, int32_t num_cols,
355        float * past_exp[])
356{
357    int32_t i, j;
358        char counter[55];
359        char var[50];
360
361    for (i = 0; i < num_rows; i++){
362        for (j = 0; j< num_cols; j++){
363                sprintf(var,"%f",past_exp[i][j]);
364        //printf("%f, \n", past_exp[i][j]);
365        //printf("%s, \n", var);
366        }
367    }
368   
369    // send the number of rows to the ce first
370        sprintf(counter,"%d",num_rows);
371        SendMessage(socketfd,counter);
372    // send the number of columns to the ce
373        sprintf(counter,"%d",num_cols);
374        SendMessage(socketfd,counter);
375    // update ce with experience
376    for (i = 0; i < num_rows; i++){
377        for (j = 0; j< num_cols; j++){
378                sprintf(var,"%f",past_exp[i][j]);
379                SendMessage(socketfd,var);
380        }
381    }
382
383}
384
385void ResetCEExperience() {
386
387}
388
389// Update operating settings
390// This function will int32_teract with the hardware "drivers"
391void UpdateRadioSettings()
392{
393}
394
395
396int32_t RequestPolicyValidation(Parameter * pList[], CE_Info *ce_info)
397{
398        char counter[55];
399        char var[50];
400    int32_t i;
401    string control_msg;
402   
403    int32_t socketfd = ce_info->policy_socket;
404
405    // Control message that validation request is coming
406    control_msg = "val";
407        SendMessage(socketfd,control_msg);
408
409    printf("Cognitive Radio:: Here. %i\n\n", socketfd);
410
411        // Send parameter information
412    sprintf(counter,"%i",ce_info->numParameters);
413        SendMessage(socketfd,counter);
414        for(i = 0; i < ce_info->numParameters; i++) {
415                SendMessage(socketfd,pList[i]->name);
416                SendMessage(socketfd,pList[i]->units);
417                sprintf(var,"%f",pList[i]->min);
418                SendMessage(socketfd,var);
419                sprintf(var,"%f",pList[i]->max);
420                SendMessage(socketfd,var);
421                sprintf(var,"%f",pList[i]->step);
422                SendMessage(socketfd,var);
423                sprintf(var,"%f",pList[i]->value);
424                SendMessage(socketfd,var);
425               
426        }
427    return 1;
428
429}
430
431
432int32_t RequestCEOptimization(int32_t sockfd, Utility *uList[],
433        Parameter *pList[], Observable *oList[],
434        CE_Info *ce_info)
435{
436    char buffer[256];
437    int32_t i;
438    float var;
439
440    // Send request optimization message followed by the current environment parameters.
441    /*
442    SendMessage(sockfd,"request");
443    for (i = 0; i < ce_info->numObservables; i++){
444        SendMessage(sockfd,..);
445    }
446    */
447
448    // Receive optimized values from the Cognitive Engine
449    for (i = 0; i < ce_info->numParameters; i++){
450        bzero(buffer,256);
451        ReceiveMessage(sockfd,buffer);
452        var = atof(buffer);
453        pList[i]->value = var;
454    }
455
456
457    // If policy engine is connect, validate new values
458    if(ce_info->policy_engine == 1) {
459
460        printf("Cognitive Radio:: Found Policy Engine!\n");
461        printf("Cognitive Radio:: Validating parameters with Policy Engine\n\n");
462        RequestPolicyValidation(pList,ce_info);
463        printf("Cognitive Radio:: Done\n\n");
464
465    }
466
467
468    return 1;
469}
470
471void RunSimulator(int32_t socketfd, Utility * uList[],
472        Parameter * pList[], Observable * oList[],
473        CE_Info * ce_info) {
474       
475        float **past_exp;
476    int32_t num_rows, num_cols;
477
478        // Set fake current environment params = current environment
479        RequestCEOptimization(socketfd, uList, pList, oList, ce_info);
480
481        // Act like we are updating the hardware tranmission settings
482        UpdateRadioSettings();
483
484        // Send back fake utility values
485    // need to initialize
486        //UpdateCEExperience(socketfd, num_rows, num_cols, past_exp);   
487}
488
489void InitializePE(int32_t socket, CE_Info * ce_info)
490{
491    // Policy Engine is connected
492    // Set global policy engine value to 1
493    ce_info->policy_engine = 1;
494    ce_info->policy_socket = socket;
495
496    return;
497}
498
499void InitializeCE(int32_t socketfd, Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info)
500{
501        LoadCEConfiguration(socketfd, uList, pList, oList, ce_info);
502       
503    // cr experience
504    float **past_exp;
505        int32_t num_cols;
506    // get number of columns
507    num_cols = ce_info->numUtilities + ce_info->numParameters;
508    num_cols = num_cols + ce_info->numObservables;
509    num_cols = num_cols + 1;    // overall utility
510    int32_t num_rows = 2;
511    past_exp = (float **)malloc(sizeof(float)*num_rows);
512    int32_t i;
513    for (i=0; i<num_rows; i++){
514        past_exp[i] = (float*)malloc(sizeof(float)*num_cols);
515    }
516    // sample experience #1
517    past_exp[0][0] = 1e3f;  // throughput
518    past_exp[0][1] = 1;     // spectral_efficiency
519    past_exp[0][2] = -3.5;  // log10_ber
520    past_exp[0][3] = 1;     // mod_scheme
521    past_exp[0][4] = -10;   // tx_power
522    past_exp[0][5] = 10.0;  // SNR
523    past_exp[0][6] = 0.762; // overall utility*/
524    // sample experience #2
525    past_exp[1][0] = 1e2f;  // throughput
526    past_exp[1][1] = 1;     // spectral_efficiency
527    past_exp[1][2] = -3.5;  // log10_ber
528    past_exp[1][3] = 1;     // mod_scheme
529    past_exp[1][4] = -14;   // tx_power
530    past_exp[1][5] = 3.0;   // SNR
531    past_exp[1][6] = 0.462; // overall utility
532
533        // update ce with experience
534    printf("Cognitive Radio:: Sending Previous Experience to New Cognitive Engine.\n\n");
535    UpdateCEExperience(socketfd, num_rows, num_cols, past_exp);
536
537        RunSimulator(socketfd, uList, pList, oList, ce_info);
538}
539
540int32_t AcceptTCPConnection(int32_t servSock)
541{
542    int32_t clntSock;                    /* Socket descriptor for client */
543    struct sockaddr_in echoClntAddr;
544    uint32_t clntLen;
545
546    /* Set the size of the in-out parameter */
547    clntLen = sizeof(echoClntAddr);
548
549    /* Wait for a client to connect */
550    //if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0) {
551    if ((clntSock = accept(servSock, NULL, NULL)) < 0) {
552        return -1;
553    }
554   
555    /* clntSock is connected to a client! */
556   
557    printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
558
559    return clntSock;
560}
561
562int32_t CreateTCPServerSocket(unsigned short port)
563{
564    int32_t sock;                        /* socket to create */
565    struct sockaddr_in echoServAddr; /* Local address */
566
567    /* Create socket for incoming connections */
568    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
569        ERROR(1, "socket() failed\n");
570     
571    /* Construct local address structure */
572    memset(&echoServAddr, 0, sizeof(echoServAddr));   /* Zero out structure */
573    echoServAddr.sin_family = AF_INET;                /* int32_ternet address family */
574    echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming int32_terface */
575    echoServAddr.sin_port = htons(port);              /* Local port */
576
577    /* Bind to the local address */
578    if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
579        ERROR(1, "bind() failed\n");
580
581    /* Mark the socket so it will listen for incoming connections */
582    if (listen(sock, 5) < 0) {
583        printf("listen() failed\n");
584        return 0;
585    }
586
587    return sock;
588}
589
590void HandleTCPClient(int32_t socketfd, Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info)
591{
592    char buffer[256];        /* Buffer for echo string */
593
594    /* Receive message from client */
595    bzero(buffer,256);
596    ReceiveMessage(socketfd,buffer);
597
598    printf("Cognitive Radio:: Message Received - %s.\n\n", buffer);
599
600    if(strcmp(buffer,"c_register") == 0)
601            InitializeCE(socketfd, uList, pList, oList, ce_info);
602
603    if(strcmp(buffer,"p_register") == 0)
604            InitializePE(socketfd, ce_info);
605
606    if(strcmp(buffer,"optimize") == 0)
607            RunSimulator(socketfd, uList, pList, oList, ce_info);
608       
609    //close(socketfd);    /* Close client socket */
610}
611
612int32_t StartServers(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) {
613    int32_t * servSock;
614    int32_t running = 1;
615    struct timeval selTimeout;
616    int32_t timeout = 10;
617    int32_t cognitive_engine = 0;
618    int32_t policy_engine = 1;
619    int32_t port, rc, on = 1;
620    int32_t new_sd;
621    int32_t desc_ready = 0;
622    fd_set sockSet;
623   
624    servSock = (int32_t *) malloc(2 * sizeof(int32_t));
625
626    servSock[cognitive_engine] = CreateTCPServerSocket(CE_SERVER_PORT);
627    servSock[policy_engine] = CreateTCPServerSocket(PE_SERVER_PORT);
628
629
630    int32_t maxDescriptor = servSock[cognitive_engine];
631
632    if(servSock[cognitive_engine] < servSock[policy_engine])
633        maxDescriptor = servSock[policy_engine];
634
635    rc = setsockopt(servSock[cognitive_engine], SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
636    if(rc < 0)
637    {
638        perror("setsockopt() failed");
639        close(servSock[cognitive_engine]);
640        exit(-1);
641    }
642   
643    rc = setsockopt(servSock[policy_engine], SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
644    if(rc < 0)
645    {
646        perror("setsockopt() failed");
647        close(servSock[policy_engine]);
648        exit(-1);
649    }
650   
651    rc = ioctl(servSock[cognitive_engine], FIONBIO, (char*)&on);
652    if(rc < 0)
653    {
654        perror("ioctl() failed");
655        close(servSock[cognitive_engine]);
656        exit(-1);
657    }
658   
659    rc = ioctl(servSock[policy_engine], FIONBIO, (char*)&on);
660    if(rc < 0)
661    {
662        perror("ioctl() failed");
663        close(servSock[policy_engine]);
664        exit(-1);
665    }
666   
667    printf("Starting server:  Hit return to shutdown\n");
668    while (running)
669    {
670        /* Zero socket descriptor vector and set for server sockets */
671        /* This must be reset every time select() is called */
672        FD_ZERO(&sockSet);
673        /* Add keyboard to descriptor vector */
674        FD_SET(STDIN_FILENO, &sockSet);
675        FD_SET(servSock[cognitive_engine], &sockSet);
676        FD_SET(servSock[policy_engine], &sockSet);
677
678        /* Timeout specification */
679        /* This must be reset every time select() is called */
680        selTimeout.tv_sec = timeout;       /* timeout (secs.) */
681        selTimeout.tv_usec = 0;            /* 0 microseconds */
682
683        /* Suspend program until descriptor is ready or timeout */
684        rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout);
685        if (rc == 0)
686            printf("No echo requests for %i secs...Server still alive\n", timeout);
687        else
688        {
689            if (FD_ISSET(0, &sockSet)) /* Check keyboard */
690            {
691                printf("Shutting down server\n");
692                getchar();
693                running = 0;
694            }
695
696            desc_ready = rc;
697
698            for (port = 0; port <= maxDescriptor && desc_ready > 0; port++) {
699                if (FD_ISSET(port, &sockSet))
700                {
701                    printf("Request on port %d:  ", port);
702                        desc_ready -= 1;
703
704
705                    if( (port == servSock[cognitive_engine]) || (port == servSock[policy_engine])) {
706                       
707                        do
708                        {
709                            new_sd = AcceptTCPConnection(port);
710                            if(new_sd < 0)
711                            {
712                                break;
713                            }
714                           
715                            HandleTCPClient(new_sd, uList, pList, oList, ce_info);
716                            FD_SET(new_sd,&sockSet);
717                            if(new_sd > maxDescriptor)
718                                maxDescriptor = new_sd;
719                            printf("New incoming connection - %i\n\n",new_sd);
720                        } while(new_sd != -1);
721                    } else {
722                       
723                        printf("Request on already open descriptor.\n\n");
724                        HandleTCPClient(port, uList, pList, oList, ce_info);
725
726                    }
727
728                }
729            }
730        }
731    }
732
733    /* Close sockets */
734    for (port = 0; port < 2; port++)
735        close(servSock[port]);
736
737    /* Free list of sockets */
738    free(servSock);       
739       
740        return 0;
741}
742
743int32_t
744main(int32_t argc, char* argv[])
745{
746        // CognitiveEngine CE;
747        // CognitiveEngineShell Shell;
748        string pFilename;
749    int32_t fd;
750
751        Utility * uList[10];
752        Parameter * pList[10];
753        Observable * oList[10];
754        struct CE_Info *ce_info;
755
756    if((fd = open("/dev/zero", O_RDWR)) == -1)
757            return 1;
758
759    ce_info = (struct CE_Info *)mmap(0,sizeof(CE_Info),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
760
761    close(fd);
762
763        if(argc < 2) {
764                cout << "Warning no XML file specific using default: example.xml" << endl;
765                pFilename = "example.xml";
766        } else { 
767                pFilename = argv[1];
768        }
769
770        TiXmlDocument doc( pFilename.c_str() );
771        bool loadOkay = doc.LoadFile();
772        if (!loadOkay)
773        {
774                cout << "Loading " << pFilename << " failed." << endl;
775                return 0;
776        }
777
778        cout << "\n\nInitialize:: Attemping to parse " << pFilename << "." << endl;
779        parse_ce_config( &doc , uList, pList, oList, ce_info);
780        cout << "Initialize:: Configuration file parsing completed.\n" << endl;
781
782    //print_current_config(uList, pList, oList, &ce_info);
783       
784   StartServers(uList, pList, oList, ce_info);
785   return 1;
786}
Note: See TracBrowser for help on using the browser.