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

Revision 65, 10.2 KB (checked in by trnewman, 16 years ago)

new code

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) {
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 = recv(socketfd,buffer,255,0);
227       
228        // Print message
229        if (n > 0) printf("Here is the message: %s\n",buffer);
230
231        return n;
232}
233
234int SendMessage(int socketfd, string message) {
235        int n;
236
237        // Write message back to client
238        n = send(socketfd,message.c_str(),(message.size()+1),MSG_NOSIGNAL);
239        return n;
240
241}
242
243void StartMessaging(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) {
244        int n,i,j;
245        char counter[55];
246        char var[50];
247        int k;
248
249
250//      while(1) {
251                // Send parameters from struct
252                sprintf(counter,"%d",ce_info->numUtilities);
253                SendMessage(socketfd,counter);
254               
255                for(i = 0; i < ce_info->numUtilities ; i++) {
256                        SendMessage(socketfd,uList[i]->name);
257                        SendMessage(socketfd,uList[i]->units);
258                        SendMessage(socketfd,uList[i]->goal);
259                        sprintf(var,"%f",uList[i]->target);
260                        SendMessage(socketfd,var);
261                }
262                sprintf(counter,"%i",ce_info->numParameters);
263                SendMessage(socketfd,counter);
264                for(i = 0; i < ce_info->numParameters; i++) {
265                        SendMessage(socketfd,pList[i]->name);
266                        SendMessage(socketfd,pList[i]->units);
267                        sprintf(var,"%f",pList[i]->min);
268                        SendMessage(socketfd,var);
269                        sprintf(var,"%f",pList[i]->max);
270                        SendMessage(socketfd,var);
271                        sprintf(var,"%f",pList[i]->step);
272                        SendMessage(socketfd,var);
273               
274                        sprintf(counter,"%i",pList[i]->numAffects);
275                        SendMessage(socketfd,counter);
276                        for(j = 0; j < pList[i]->numAffects; j++) {
277                                SendMessage(socketfd,pList[i]->affection_list[j].u->name);
278                                SendMessage(socketfd,pList[i]->affection_list[j].relation);
279                        }
280                }
281                sprintf(counter,"%i",ce_info->numObservables);
282                SendMessage(socketfd,counter);
283                for(i = 0; i < ce_info->numObservables; i++) {
284                        SendMessage(socketfd,oList[i]->name);
285                       
286                        sprintf(counter,"%i",oList[i]->numAffects);
287                        SendMessage(socketfd,counter);
288                        for(j = 0; j < oList[i]->numAffects; j++) {
289                                SendMessage(socketfd,oList[i]->affection_list[j].u->name);
290                                SendMessage(socketfd,oList[i]->affection_list[j].relation);
291                        }
292                }
293/*             
294
295                // Receive a message
296                n = ReceiveMessage(socketfd);   
297                if (n < 0) {
298                        printf("ERROR reading from socket");
299                        close(socketfd);
300                        return;
301                }
302                if (n == 0) {
303                        printf("Client has closed the connection.\n");
304                        return;
305                }
306*/     
307//      }
308}
309
310int StartShell(int port,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) {
311        // Start socket server
312        int sockfd, newsockfd, clilen;
313        struct sockaddr_in serv_addr, cli_addr;
314
315        // Setup socket connection
316     
317        sockfd = socket(AF_INET, SOCK_STREAM, 0);
318        if (sockfd < 0)
319                error("ERROR opening socket");
320        bzero((char *) &serv_addr, sizeof(serv_addr));
321        serv_addr.sin_family = AF_INET;
322        serv_addr.sin_addr.s_addr = INADDR_ANY;
323        serv_addr.sin_port = htons(port);
324        if (bind(sockfd, (struct sockaddr *) &serv_addr,
325                sizeof(serv_addr)) < 0)
326                error("ERROR on binding");
327        listen(sockfd,5);
328        clilen = sizeof(cli_addr);
329
330        while(1) {
331                newsockfd = accept(sockfd,
332                        (struct sockaddr *) &cli_addr,
333                        (socklen_t*)&clilen);
334                if (newsockfd < 0)
335                        error("ERROR on accept");
336
337                // Begin parsing the messages
338                StartMessaging(newsockfd,uList, pList, oList, ce_info);
339        }
340        return 0;
341}
342
343int main(int argc, char* argv[]) {
344
345
346        // CognitiveEngine CE;
347        // CognitiveEngineShell Shell;
348        string pFilename;
349        int pid;
350
351        Utility * uList[10];
352        Parameter * pList[10];
353        Observable * oList[10];
354        CE_Info ce_info;
355
356        if(argc < 2) {
357                cout << "Warning no XML file specific using default: example.xml" << endl;
358                pFilename = "example.xml";
359        } else { 
360                pFilename = argv[1];
361        }
362
363        TiXmlDocument doc( pFilename.c_str() );
364        bool loadOkay = doc.LoadFile();
365        if (!loadOkay)
366        {
367                cout << "Loading " << pFilename << " failed." << endl;
368                return 0;
369        }
370
371        cout << "Attemping to parse " << pFilename << "." << endl;
372        parse_ce_config( &doc , uList, pList, oList, &ce_info);
373        cout << "Configuration file parsing completed." << endl;
374
375        pid = fork();
376        if(pid == 0) {
377                // In child process - open policy engine port.
378        //      StartShell(30003,uList, pList, oList, &ce_info);
379        } else {
380                // In parent process - open cognitive engine port.
381                StartShell(30000,uList, pList, oList, &ce_info);
382        }
383       
384
385   return 1;
386}
Note: See TracBrowser for help on using the browser.