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

Revision 175, 21.6 KB (checked in by bhilburn, 15 years ago)

Got the shell over to using the proper SendMessage?.

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