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

Revision 80, 13.6 KB (checked in by ahe, 16 years ago)

receive results from client

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
9using namespace std;
10
11#define SERVER_PORT 30000
12
13struct CE_Info {
14        int numUtilities;
15        int numParameters;
16        int numObservables;
17};
18
19struct Utility {
20        string name;
21        string units;
22        string goal;
23        float target;
24    float value;
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    float value;
39};
40
41struct Observable {
42        string name;
43        Affect affection_list[10];
44        int numAffects;
45    float value;
46};
47
48void print_current_config(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) {
49        int i = 0;
50        int j = 0;
51
52        for(i = 0; i < ce_info->numUtilities ; i++) {
53                cout << "Utility:  " << uList[i]->name << endl;
54                cout << "     Units:  " << uList[i]->units << endl;
55                cout << "     Goal:   " << uList[i]->goal << endl;
56                cout << "     Target: " << uList[i]->target << endl;
57        }
58
59        for(i = 0; i < ce_info->numParameters; i++) {
60                cout << "Parameter:  " << pList[i]->name << endl;
61                cout << "       Units:   " << pList[i]->units << endl;
62                cout << "       Min:     " << pList[i]->min << endl;
63                cout << "       Max:     " << pList[i]->max << endl;
64                cout << "       Step:    " << pList[i]->step << endl;
65                for(j = 0; j < pList[i]->numAffects; j++) {
66                        cout << "       Affect: " << pList[i]->affection_list[j].u->name << " -> " << pList[i]->affection_list[j].relation << endl;
67                }
68        }
69       
70    for(i = 0; i < ce_info->numObservables; i++) {
71                cout << "Observable:  " << oList[i]->name << endl;
72                for(j = 0; j < oList[i]->numAffects; j++) {
73                        cout << "       Affect: " << oList[i]->affection_list[j].u->name << " -> " << oList[i]->affection_list[j].relation << endl;
74                }
75        }
76}
77
78
79int parse_ce_config( TiXmlDocument * doc , Utility * u[], Parameter * p[], Observable * o[], CE_Info * ce_info) {
80
81        TiXmlElement* pElem;    //!current element
82        TiXmlElement* pChild;   //!current child of pElem
83        TiXmlElement* pChild1;  //!current child of pElem
84        TiXmlElement* pSecondChild;     //!current child of pElem
85        TiXmlHandle hDoc(doc);  //!handle to xml document
86        TiXmlHandle hRoot(0); //! handle to root element
87
88        int count = 0;
89        int i = 0;
90        int j = 0;
91        int k = 0;
92
93        pElem = hDoc.FirstChildElement().Element();
94        if(!pElem) { cout << "no valid root! quit-ing function!" << endl; return 0; }
95        hRoot = TiXmlHandle(pElem);
96
97        // Pull utility information from XML file.
98
99        pElem = hRoot.FirstChild("utilities").Element();
100        pChild1 = hRoot.Child("utilities",count).Element();
101
102
103        for(pChild = pChild1->FirstChildElement("utility"); pChild; pChild = pChild->NextSiblingElement())
104        {
105                u[i] = new Utility;
106                const char *uName = pChild->Attribute("name");
107                if(uName) u[i]->name = uName;   
108                const char *uUnits = pChild->Attribute("units");
109                if(uUnits) u[i]->units = uUnits;
110                const char *uGoal = pChild->Attribute("goal");
111                if(uGoal) u[i]->goal = uGoal;
112                if(pChild->QueryFloatAttribute("target",&u[i]->target) != TIXML_SUCCESS) u[i]->target = -1;
113                i++;
114        }
115        ce_info->numUtilities = i;     
116        cout << "Parsed " << ce_info->numUtilities << " utilities." << endl;
117
118        // Pull observable information from XML file.
119        i = 0;
120        pElem = hRoot.FirstChild("observables").Element();
121        pChild1 = hRoot.Child("observables",count).Element();
122       
123        for(pChild = pChild1->FirstChildElement("observable"); pChild; pChild = pChild->NextSiblingElement())
124        {
125
126                const char *oName = pChild->Attribute("name");
127                o[i] = new Observable;
128
129                if(oName) o[i]->name = oName;
130               
131                j = 0;
132                for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement())
133                {
134                        const char *oUtilName = pSecondChild->Attribute("utility");
135
136                        // If a utility affects this parameter find the utility object and assign it
137                        if(oUtilName) {
138                                // Search for correct utility
139                                for(k=0;k<10;k++){
140                                        if(u[k]->name == oUtilName) {
141                                                o[i]->affection_list[j].u = u[k];
142                                                break;
143                                        }
144                                }
145                        }
146                       
147                        // Set relationship
148                        const char *oRelate = pSecondChild->Attribute("relationship");
149                        if(oRelate) o[i]->affection_list[j].relation = oRelate;
150                        j++;
151                }
152                o[i]->numAffects = j;
153                i++;
154        }
155        ce_info->numObservables = i;   
156        cout << "Parsed " << ce_info->numObservables << " observables." << endl;
157       
158
159        // Pull parameter information from XML file.
160        pElem = hRoot.FirstChild("parameters").Element();
161        pChild1 = hRoot.Child("parameters",count).Element();
162       
163        i = 0;
164        for(pChild = pChild1->FirstChildElement("parameter"); pChild; pChild = pChild->NextSiblingElement())
165        {
166                p[i] = new Parameter;
167
168                const char *pName = pChild->Attribute("name");
169                if(pName) p[i]->name = pName;   
170                const char *pUnits = pChild->Attribute("units");
171                if(pUnits) p[i]->units = pUnits;
172
173                if(pChild->QueryFloatAttribute("min",&p[i]->min) != TIXML_SUCCESS) p[i]->min = -1;
174                if(pChild->QueryFloatAttribute("max",&p[i]->max) != TIXML_SUCCESS) p[i]->max = -1;
175                if(pChild->QueryFloatAttribute("step",&p[i]->step) != TIXML_SUCCESS) p[i]->step = -1;
176               
177                j = 0;
178                for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement())
179                {
180                        const char *pUtilName = pSecondChild->Attribute("utility");
181                       
182                        // If a utility affects this parameter find the utility object and assign it
183                        if(pUtilName) {
184                                // Search for correct utility
185                                for( k=0 ; u[k]!=NULL ; k++ ){
186                                        if(u[k]->name == pUtilName) {
187                                                p[i]->affection_list[j].u = u[k];       
188                                                break;
189                                        }
190                                }
191                        }
192                       
193                        // Set relationship
194                        const char *pRelate = pSecondChild->Attribute("relationship");
195                        if(pRelate) {
196                                p[i]->affection_list[j].relation = pRelate;
197                        } else {
198                                cout << "Error: No relation found." << endl;
199                        }
200                        j++;
201               
202                }
203                p[i]->numAffects = j;
204                i++;
205
206        }
207        ce_info->numParameters = i;     
208        cout << "Parsed " << ce_info->numParameters << " parameters." << endl;
209        return 1;
210}
211
212
213void error(char *msg)
214{
215    perror(msg);
216    exit(1);
217}
218
219int ReceiveMessage(int socketfd, string message) {
220       
221        char buffer[256];
222        int n;
223
224        // Clear incoming data buffer
225        bzero(buffer,256);
226   
227        // Wait for incoming message
228        n = read(socketfd,buffer,256);
229       
230        // Print message
231        if (n > 0)
232                printf("Here is the received meessage: %s\n",buffer);
233        if(n == 0)
234                printf("Client closed the socket.\n");
235       
236        return n;
237}
238
239int SendMessage(int socketfd, string message) {
240        int n;
241
242        message.append("\0000");       
243        // Write message back to client
244        n = write(socketfd,message.c_str(),(message.size()+1));
245        if (n<0)
246                error("Error sending to client\n");
247        if(n == 0)
248                printf("Client closed the socket.\n");
249       
250        return n;
251}
252
253void GetEnvironment() {
254
255}
256
257void Policy_ValidateSettings() {
258
259}
260
261// Update operating settings
262// This function will interact with the hardware "drivers"
263void UpdateRadioSettings() {
264
265}
266
267void LoadCEConfiguration(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info){
268        int n,i,j;
269        char counter[55];
270        char var[50];
271        int total_bytes;   
272
273        printf("Sending configuration to CE.\n");
274 
275        // utilities
276        // Send number of utilities
277        sprintf(counter,"%d",ce_info->numUtilities);
278        SendMessage(socketfd,counter);
279        // send utility
280    for(i = 0; i < ce_info->numUtilities; i++) {
281                SendMessage(socketfd,uList[i]->name);
282                SendMessage(socketfd,uList[i]->units);
283                SendMessage(socketfd,uList[i]->goal);
284                sprintf(var,"%f",uList[i]->target);
285                SendMessage(socketfd,var);
286        }
287
288        // parameters
289    sprintf(counter,"%i",ce_info->numParameters);
290        SendMessage(socketfd,counter);
291        for(i = 0; i < ce_info->numParameters; i++) {
292                SendMessage(socketfd,pList[i]->name);
293                SendMessage(socketfd,pList[i]->units);
294                sprintf(var,"%f",pList[i]->min);
295                SendMessage(socketfd,var);
296                sprintf(var,"%f",pList[i]->max);
297                SendMessage(socketfd,var);
298                sprintf(var,"%f",pList[i]->step);
299                SendMessage(socketfd,var);
300               
301                sprintf(counter,"%i",pList[i]->numAffects);
302                SendMessage(socketfd,counter);
303                for(j = 0; j < pList[i]->numAffects; j++) {
304                        SendMessage(socketfd,pList[i]->affection_list[j].u->name);
305                        SendMessage(socketfd,pList[i]->affection_list[j].relation);
306                }
307        }
308
309    // observables
310        sprintf(counter,"%i",ce_info->numObservables);
311        SendMessage(socketfd,counter);
312        for(i = 0; i < ce_info->numObservables; i++) {
313                SendMessage(socketfd,oList[i]->name);
314               
315                sprintf(counter,"%i",oList[i]->numAffects);
316                SendMessage(socketfd,counter);
317                for(j = 0; j < oList[i]->numAffects; j++) {
318                        SendMessage(socketfd,oList[i]->affection_list[j].u->name);
319                        SendMessage(socketfd,oList[i]->affection_list[j].relation);
320                }
321        }
322       
323        printf("Configuration sent, waiting for ACK...\n");
324        // Receive ACK for utils
325        string message;
326        n = ReceiveMessage(socketfd, message);
327        cout << message << endl;
328        printf("ACK received.\n");
329
330}
331
332void UpdateCEConfiguration() {
333
334}
335
336void ResetCEConfiguration(){
337
338}
339
340void LoadCEExperience(){
341
342}
343
344void UpdateCEExperience() {
345
346}
347
348void ResetCEExperience() {
349
350}
351
352void RequestCEOptimization() {
353
354}
355
356int UpdateRadioSettings(int sockfd, Utility *uList[],
357        Parameter *pList[], Observable *oList[],
358        CE_Info *ce_info)
359{
360    char buffer[256];
361    string message;
362    int i;
363    float var;
364
365    // utility
366    // numUtilities
367    bzero(buffer,256);
368    message.erase();
369    ReceiveMessage(sockfd,message);
370    ce_info->numUtilities = atoi(message.c_str());
371    printf("number of utilities: %d\n", ce_info->numUtilities);
372    for (i = 0; i < ce_info->numUtilities; i++){
373        bzero(buffer,256);
374        message.erase();
375        ReceiveMessage(sockfd,message);
376        var = atof(message.c_str());
377        uList[i]->value = var;
378        printf("utility %s, value: %f\n",
379                uList[i]->name, uList[i]->value);
380    }
381   
382    // paramter
383    // numparameters
384    /*bzero(buffer,256);*/
385    message.erase();
386    ReceiveMessage(sockfd,message);
387    ce_info->numParameters = atoi(message.c_str());
388    printf("number of parameters: %d\n", ce_info->numParameters);
389    for (i = 0; i < ce_info->numParameters; i++){
390        /*bzero(buffer,256);*/
391        message.erase();
392        ReceiveMessage(sockfd,message);
393        var = atof(message.c_str());
394        pList[i]->value = var;
395        printf("parameter %s, value: %f\n",
396                pList[i]->name, pList[i]->value);
397    }
398
399    // observable
400    // numObservables
401    /*bzero(buffer,256);
402    ReceiveMessage(sockfd,buffer);*/
403    message.erase();
404    ReceiveMessage(sockfd,message);
405    ce_info->numObservables = atoi(message.c_str());
406    printf("number of observables: %d\n", ce_info->numObservables);
407    for (i = 0; i < ce_info->numObservables; i++){
408        /*bzero(buffer,256);
409        ReceiveMessage(sockfd,buffer);*/
410        message.erase();
411        ReceiveMessage(sockfd,message);
412        var = atof(message.c_str());
413        oList[i]->value = var;
414        printf("observable %s, value: %f\n",
415                oList[i]->name, oList[i]->value);
416    }
417}
418
419void RunSimulator(int socketfd, Utility * uList[],
420        Parameter * pList[], Observable * oList[],
421        CE_Info * ce_info) {
422
423        // Set fake past environment - 
424        LoadCEExperience();
425       
426        // Set fake current environment params = current environment
427        RequestCEOptimization();
428
429        // Act like we are updating the hardware tranmission settings
430        UpdateRadioSettings(socketfd, uList, pList, oList, ce_info);
431
432        // Send back fake utility values
433        UpdateCEExperience();   
434}
435
436void StartMessaging(int socketfd, Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) {
437
438        LoadCEConfiguration(socketfd, uList, pList, oList, ce_info);
439
440        RunSimulator(socketfd, uList, pList, oList, ce_info);
441}
442
443int StartShell(int port,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) {
444        // Start socket server
445        int sockfd, newsockfd, clilen;
446        struct sockaddr_in serv_addr, cli_addr;
447
448        // Setup server socket connection
449        sockfd = socket(AF_INET, SOCK_STREAM, 0);
450        if (sockfd < 0)
451                error("ERROR opening socket");
452        bzero((char *) &serv_addr, sizeof(serv_addr));
453        serv_addr.sin_family = AF_INET;
454        serv_addr.sin_port = htons(port);
455        serv_addr.sin_addr.s_addr = INADDR_ANY;
456        if (bind(sockfd, (struct sockaddr *) &serv_addr,
457                sizeof(serv_addr)) < 0)
458                error("ERROR on binding");
459        listen(sockfd,5);
460        clilen = sizeof(cli_addr);
461
462        //while(1) {
463                newsockfd = accept(sockfd,
464                        (struct sockaddr *) &cli_addr,
465                        (socklen_t*)&clilen);
466                if (newsockfd < 0)
467                        error("ERROR on accept");
468
469                // Begin parsing the messages
470                StartMessaging(newsockfd, uList, pList, oList, ce_info);
471        //}
472        return 0;
473}
474
475int main(int argc, char* argv[]) {
476
477
478        // CognitiveEngine CE;
479        // CognitiveEngineShell Shell;
480        string pFilename;
481        int pid;
482
483        Utility * uList[10];
484        Parameter * pList[10];
485        Observable * oList[10];
486        CE_Info ce_info;
487
488        if(argc < 2) {
489                cout << "Warning no XML file specific using default: example.xml" << endl;
490                pFilename = "example.xml";
491        } else { 
492                pFilename = argv[1];
493        }
494
495        TiXmlDocument doc( pFilename.c_str() );
496        bool loadOkay = doc.LoadFile();
497        if (!loadOkay)
498        {
499                cout << "Loading " << pFilename << " failed." << endl;
500                return 0;
501        }
502
503        cout << "Attemping to parse " << pFilename << "." << endl;
504        parse_ce_config( &doc , uList, pList, oList, &ce_info);
505        cout << "Configuration file parsing completed." << endl;
506
507    //print_current_config(uList, pList, oList, &ce_info);
508       
509    pid = fork();
510        if(pid == 0) {
511                // In child process - open policy engine port.
512        //      StartShell(30000,uList, pList, oList, &ce_info);
513        } else {
514                // In parent process - open cognitive engine port.
515                StartShell(30002,uList, pList, oList, &ce_info);
516        }
517       
518
519   return 1;
520}
Note: See TracBrowser for help on using the browser.