root/vtcross/branches/trnewman/CR_shell/src/main_cognitive_radio.cpp @ 71

Revision 71, 10.7 KB (checked in by ahe, 16 years ago)

send xml from server to client through socket

Line 
1#include <iostream>
2#include <sys/types.h>
3#include <sys/wait.h>
4#include "tinyxml.h"
5#include "tinystr.h"
6#include "socket/ServerSocket.h"
7#include "socket/SocketException.h"
8
9
10using namespace std;
11
12#define SERVER_PORT 30000
13
14struct CE_Info {
15        int numUtilities;
16        int numParameters;
17        int numObservables;
18};
19
20struct Utility {
21        string name;
22        string units;
23        string goal;
24        float target;
25};
26struct Affect {
27        Utility * u;
28        string relation;
29};
30struct Parameter {
31        string name;
32        string units;
33        float min;
34        int numAffects;
35        Affect affection_list[10];
36        float max;
37        float step;
38};
39
40struct Observable {
41        string name;
42        Affect affection_list[10];
43        int numAffects;
44};
45
46void print_current_config(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) {
47        int i = 0;
48        int j = 0;
49
50        for(i = 0; i < ce_info->numUtilities ; i++) {
51                cout << "Utility:  " << uList[i]->name << endl;
52                cout << "     Units:  " << uList[i]->units << endl;
53                cout << "     Goal:   " << uList[i]->goal << endl;
54                cout << "     Target: " << uList[i]->target << endl;
55        }
56
57
58        for(i = 0; i < ce_info->numParameters; i++) {
59                cout << "Parameter:  " << pList[i]->name << endl;
60                cout << "       Units:   " << pList[i]->units << endl;
61                cout << "       Min:     " << pList[i]->min << endl;
62                cout << "       Max:     " << pList[i]->max << endl;
63                cout << "       Step:    " << pList[i]->step << endl;
64                for(j = 0; j < pList[i]->numAffects; j++) {
65                        cout << "       Affect: " << pList[i]->affection_list[j].u->name << " -> " << pList[i]->affection_list[j].relation << endl;
66                }
67        }
68        for(i = 0; i < ce_info->numObservables; i++) {
69                cout << "Observable:  " << oList[i]->name << endl;
70                for(j = 0; j < oList[i]->numAffects; j++) {
71                        cout << "       Affect: " << oList[i]->affection_list[j].u->name << " -> " << oList[i]->affection_list[j].relation << endl;
72                }
73        }
74}
75
76
77int parse_ce_config( TiXmlDocument * doc , Utility * u[], Parameter * p[], Observable * o[], CE_Info * ce_info) {
78
79        TiXmlElement* pElem;    //!current element
80        TiXmlElement* pChild;   //!current child of pElem
81        TiXmlElement* pChild1;  //!current child of pElem
82        TiXmlElement* pSecondChild;     //!current child of pElem
83        TiXmlHandle hDoc(doc);  //!handle to xml document
84        TiXmlHandle hRoot(0); //! handle to root element
85
86        int count = 0;
87        int i = 0;
88        int j = 0;
89        int k = 0;
90
91        pElem = hDoc.FirstChildElement().Element();
92        if(!pElem) { cout << "no valid root! quit-ing function!" << endl; return 0; }
93        hRoot = TiXmlHandle(pElem);
94
95        // Pull utility information from XML file.
96
97        pElem = hRoot.FirstChild("utilities").Element();
98        pChild1 = hRoot.Child("utilities",count).Element();
99
100
101        for(pChild = pChild1->FirstChildElement("utility"); pChild; pChild = pChild->NextSiblingElement())
102        {
103                u[i] = new Utility;
104                const char *uName = pChild->Attribute("name");
105                if(uName) u[i]->name = uName;   
106                const char *uUnits = pChild->Attribute("units");
107                if(uUnits) u[i]->units = uUnits;
108                const char *uGoal = pChild->Attribute("goal");
109                if(uGoal) u[i]->goal = uGoal;
110                if(pChild->QueryFloatAttribute("target",&u[i]->target) != TIXML_SUCCESS) u[i]->target = -1;
111                i++;
112        }
113        ce_info->numUtilities = i;     
114        cout << "Parsed " << ce_info->numUtilities << " utilities." << endl;
115
116        // Pull observable information from XML file.
117        i = 0;
118        pElem = hRoot.FirstChild("observables").Element();
119        pChild1 = hRoot.Child("observables",count).Element();
120       
121        for(pChild = pChild1->FirstChildElement("observable"); pChild; pChild = pChild->NextSiblingElement())
122        {
123
124                const char *oName = pChild->Attribute("name");
125                o[i] = new Observable;
126
127                if(oName) o[i]->name = oName;
128               
129                j = 0;
130                for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement())
131                {
132                        const char *oUtilName = pSecondChild->Attribute("utility");
133
134                        // If a utility affects this parameter find the utility object and assign it
135                        if(oUtilName) {
136                                // Search for correct utility
137                                for(k=0;k<10;k++){
138                                        if(u[k]->name == oUtilName) {
139                                                o[i]->affection_list[j].u = u[k];
140                                                break;
141                                        }
142                                }
143                        }
144                       
145                        // Set relationship
146                        const char *oRelate = pSecondChild->Attribute("relationship");
147                        if(oRelate) o[i]->affection_list[j].relation = oRelate;
148                        j++;
149                }
150                o[i]->numAffects = j;
151                i++;
152        }
153        ce_info->numObservables = i;   
154        cout << "Parsed " << ce_info->numObservables << " observables." << endl;
155       
156
157        // Pull parameter information from XML file.
158        pElem = hRoot.FirstChild("parameters").Element();
159        pChild1 = hRoot.Child("parameters",count).Element();
160       
161        i = 0;
162        for(pChild = pChild1->FirstChildElement("parameter"); pChild; pChild = pChild->NextSiblingElement())
163        {
164                p[i] = new Parameter;
165
166                const char *pName = pChild->Attribute("name");
167                if(pName) p[i]->name = pName;   
168                const char *pUnits = pChild->Attribute("units");
169                if(pUnits) p[i]->units = pUnits;
170
171                if(pChild->QueryFloatAttribute("min",&p[i]->min) != TIXML_SUCCESS) p[i]->min = -1;
172                if(pChild->QueryFloatAttribute("max",&p[i]->max) != TIXML_SUCCESS) p[i]->max = -1;
173                if(pChild->QueryFloatAttribute("step",&p[i]->step) != TIXML_SUCCESS) p[i]->step = -1;
174               
175                j = 0;
176                for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement())
177                {
178                        const char *pUtilName = pSecondChild->Attribute("utility");
179                       
180                        // If a utility affects this parameter find the utility object and assign it
181                        if(pUtilName) {
182                                // Search for correct utility
183                                for( k=0 ; u[k]!=NULL ; k++ ){
184                                        if(u[k]->name == pUtilName) {
185                                                p[i]->affection_list[j].u = u[k];       
186                                                break;
187                                        }
188                                }
189                        }
190                       
191                        // Set relationship
192                        const char *pRelate = pSecondChild->Attribute("relationship");
193                        if(pRelate) {
194                                p[i]->affection_list[j].relation = pRelate;
195                        } else {
196                                cout << "Error: No relation found." << endl;
197                        }
198                        j++;
199               
200                }
201                p[i]->numAffects = j;
202                i++;
203
204        }
205        ce_info->numParameters = i;     
206        cout << "Parsed " << ce_info->numParameters << " parameters." << endl;
207        return 1;
208}
209
210
211void error(char *msg)
212{
213    perror(msg);
214    exit(1);
215}
216
217int ReceiveMessage(int socketfd, string message) {
218       
219        char buffer[256];
220        int n;
221
222        // Clear incoming data buffer
223    bzero(buffer,256);
224   
225        // Wait for incoming message
226        n = read(socketfd,buffer,256);
227       
228        // Print message
229    if (n > 0)
230        printf("Here is the received meessage: %s\n",buffer);
231    //printf("n = %d\n",n);
232       
233    return n;
234}
235
236int SendMessage(int socketfd, string message) {
237        int n;
238    int len;
239   
240    char LEN[8];
241    char * temp;
242
243        message.append("\0000");       
244        // Write message back to client
245        n = write(socketfd,message.c_str(),(message.size()+1));
246        if (n<0)
247                error("Error sending to client\n");
248        printf("sent: %s %i\n", message.c_str(),n);
249       
250        return n;
251}
252
253void StartMessaging(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) {
254        int n,i,j;
255        char counter[55];
256        char var[50];
257   
258//    while(1) {
259                // Send number of utilities
260                sprintf(counter,"%d",ce_info->numUtilities);
261                SendMessage(socketfd,counter);
262               
263                // utilities
264        for(i = 0; i < ce_info->numUtilities; i++) {
265        //for(i = 0; i < 1; i++) {
266                        SendMessage(socketfd,uList[i]->name);
267                        SendMessage(socketfd,uList[i]->units);
268                        SendMessage(socketfd,uList[i]->goal);
269                        sprintf(var,"%f",uList[i]->target);
270                        SendMessage(socketfd,var);
271                }
272
273                // parameters
274        sprintf(counter,"%i",ce_info->numParameters);
275                SendMessage(socketfd,counter);
276                for(i = 0; i < ce_info->numParameters; i++) {
277                        SendMessage(socketfd,pList[i]->name);
278                        SendMessage(socketfd,pList[i]->units);
279                        sprintf(var,"%f",pList[i]->min);
280                        SendMessage(socketfd,var);
281                        sprintf(var,"%f",pList[i]->max);
282                        SendMessage(socketfd,var);
283                        sprintf(var,"%f",pList[i]->step);
284                        SendMessage(socketfd,var);
285               
286                        sprintf(counter,"%i",pList[i]->numAffects);
287                        SendMessage(socketfd,counter);
288                        for(j = 0; j < pList[i]->numAffects; j++) {
289                                SendMessage(socketfd,pList[i]->affection_list[j].u->name);
290                                SendMessage(socketfd,pList[i]->affection_list[j].relation);
291                        }
292                }
293
294        // observables
295                sprintf(counter,"%i",ce_info->numObservables);
296                SendMessage(socketfd,counter);
297                for(i = 0; i < ce_info->numObservables; i++) {
298                        SendMessage(socketfd,oList[i]->name);
299                       
300                        sprintf(counter,"%i",oList[i]->numAffects);
301                        SendMessage(socketfd,counter);
302                        for(j = 0; j < oList[i]->numAffects; j++) {
303                                SendMessage(socketfd,oList[i]->affection_list[j].u->name);
304                                SendMessage(socketfd,oList[i]->affection_list[j].relation);
305                        }
306                }
307       
308                // Receive a message
309        string message;
310                n = ReceiveMessage(socketfd, message);
311        cout << message << endl;
312
313
314        //printf("n = %d\n", n);
315                /*if (n < 0) {
316                        printf("ERROR reading from socket\n");
317                        close(socketfd);
318                        return;
319                }
320                if (n == 0) {
321                        printf("Client has closed the connection.\n");
322                        return;
323                }*/
324       
325//    }
326}
327
328int StartShell(int port,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) {
329        // Start socket server
330        int sockfd, newsockfd, clilen;
331        struct sockaddr_in serv_addr, cli_addr;
332
333        // Setup socket connection
334     
335        sockfd = socket(AF_INET, SOCK_STREAM, 0);
336        if (sockfd < 0)
337                error("ERROR opening socket");
338        bzero((char *) &serv_addr, sizeof(serv_addr));
339        serv_addr.sin_family = AF_INET;
340        serv_addr.sin_addr.s_addr = INADDR_ANY;
341        serv_addr.sin_port = htons(port);
342        if (bind(sockfd, (struct sockaddr *) &serv_addr,
343                sizeof(serv_addr)) < 0)
344                error("ERROR on binding");
345        listen(sockfd,5);
346        clilen = sizeof(cli_addr);
347
348        //while(1) {
349                newsockfd = accept(sockfd,
350                        (struct sockaddr *) &cli_addr,
351                        (socklen_t*)&clilen);
352                if (newsockfd < 0)
353                        error("ERROR on accept");
354
355                // Begin parsing the messages
356                StartMessaging(newsockfd,uList, pList, oList, ce_info);
357        //}
358        return 0;
359}
360
361int main(int argc, char* argv[]) {
362
363
364        // CognitiveEngine CE;
365        // CognitiveEngineShell Shell;
366        string pFilename;
367        int pid;
368
369        Utility * uList[10];
370        Parameter * pList[10];
371        Observable * oList[10];
372        CE_Info ce_info;
373
374        if(argc < 2) {
375                cout << "Warning no XML file specific using default: example.xml" << endl;
376                pFilename = "example.xml";
377        } else { 
378                pFilename = argv[1];
379        }
380
381        TiXmlDocument doc( pFilename.c_str() );
382        bool loadOkay = doc.LoadFile();
383        if (!loadOkay)
384        {
385                cout << "Loading " << pFilename << " failed." << endl;
386                return 0;
387        }
388
389        cout << "Attemping to parse " << pFilename << "." << endl;
390        parse_ce_config( &doc , uList, pList, oList, &ce_info);
391        cout << "Configuration file parsing completed." << endl;
392
393    print_current_config(uList, pList, oList, &ce_info);
394       
395    pid = fork();
396        if(pid == 0) {
397                // In child process - open policy engine port.
398        //      StartShell(30000,uList, pList, oList, &ce_info);
399        } else {
400                // In parent process - open cognitive engine port.
401                StartShell(30002,uList, pList, oList, &ce_info);
402        }
403       
404
405   return 1;
406}
Note: See TracBrowser for help on using the browser.