root/vtcross/branches/trnewman/CE_shell/src/main_cognitive_radio.cpp @ 53

Revision 35, 7.2 KB (checked in by trnewman, 16 years ago)

adding c++

Line 
1#include <iostream>
2#include "tinyxml.h"
3#include "tinystr.h"
4#include "socket/ServerSocket.h"
5#include "socket/SocketException.h"
6
7using namespace std;
8
9#define SERVER_PORT 30000
10
11struct CE_Info {
12        int numUtilities;
13        int numParameters;
14        int numObservables;
15};
16
17
18
19struct Utility {
20        string name;
21        string units;
22        string goal;
23        float target;
24};
25struct Affect {
26        Utility * u;
27        string relation;
28};
29struct Parameter {
30        string name;
31        string units;
32        float min;
33        int numAffects;
34        Affect affection_list[10];
35        float max;
36        float step;
37};
38
39struct Observable {
40        string name;
41        Affect affection_list[10];
42        int numAffects;
43};
44
45void print_current_config(Utility * uList[], Parameter * pList[10], Observable * oList[], CE_Info * ce_info) {
46        int i = 0;
47        int j = 0;
48
49        for(i = 0; i < ce_info->numUtilities ; i++) {
50                cout << "Utility:  " << uList[i]->name << endl;
51                cout << "     Units:  " << uList[i]->units << endl;
52                cout << "     Goal:   " << uList[i]->goal << endl;
53                cout << "     Target: " << uList[i]->target << endl;
54        }
55
56
57        for(i = 0; i < ce_info->numParameters; i++) {
58                cout << "Parameter:  " << pList[i]->name << endl;
59                cout << "       Units:   " << pList[i]->units << endl;
60                cout << "       Min:     " << pList[i]->min << endl;
61                cout << "       Max:     " << pList[i]->max << endl;
62                cout << "       Step:    " << pList[i]->step << endl;
63                for(j = 0; j < pList[i]->numAffects; j++) {
64                        cout << "       Affect: " << pList[i]->affection_list[j].u->name << " -> " << pList[i]->affection_list[j].relation << endl;
65                }
66        }
67        for(i = 0; i < ce_info->numObservables; i++) {
68                cout << "Observable:  " << oList[i]->name << endl;
69                for(j = 0; j < oList[i]->numAffects; j++) {
70                        cout << "       Affect: " << oList[i]->affection_list[j].u->name << " -> " << oList[i]->affection_list[j].relation << endl;
71                }
72        }
73}
74
75
76int parse_ce_config( TiXmlDocument * doc , Utility * u[], Parameter * p[], Observable * o[], CE_Info * ce_info) {
77
78        TiXmlElement* pElem;    //!current element
79        TiXmlElement* pChild;   //!current child of pElem
80        TiXmlElement* pChild1;  //!current child of pElem
81        TiXmlElement* pSecondChild;     //!current child of pElem
82        TiXmlHandle hDoc(doc);  //!handle to xml document
83        TiXmlHandle hRoot(0); //! handle to root element
84
85        int count = 0;
86        int i = 0;
87        int j = 0;
88        int k = 0;
89
90        pElem = hDoc.FirstChildElement().Element();
91        if(!pElem) { cout << "no valid root! quit-ing function!" << endl; return 0; }
92        hRoot = TiXmlHandle(pElem);
93
94        // Pull utility information from XML file.
95
96        pElem = hRoot.FirstChild("utilities").Element();
97        pChild1 = hRoot.Child("utilities",count).Element();
98
99
100        for(pChild = pChild1->FirstChildElement("utility"); pChild; pChild = pChild->NextSiblingElement())
101        {
102                u[i] = new Utility;
103                const char *uName = pChild->Attribute("name");
104                if(uName) u[i]->name = uName;   
105                const char *uUnits = pChild->Attribute("units");
106                if(uUnits) u[i]->units = uUnits;
107                const char *uGoal = pChild->Attribute("goal");
108                if(uGoal) u[i]->goal = uGoal;
109                if(pChild->QueryFloatAttribute("target",&u[i]->target) != TIXML_SUCCESS) u[i]->target = -1;
110                i++;
111        }
112        ce_info->numUtilities = i;     
113        cout << "Parsed " << ce_info->numUtilities << " utilities." << endl;
114
115        // Pull observable information from XML file.
116        i = 0;
117        pElem = hRoot.FirstChild("observables").Element();
118        pChild1 = hRoot.Child("observables",count).Element();
119       
120        for(pChild = pChild1->FirstChildElement("observable"); pChild; pChild = pChild->NextSiblingElement())
121        {
122
123                const char *oName = pChild->Attribute("name");
124                o[i] = new Observable;
125
126                if(oName) o[i]->name = oName;
127               
128                j = 0;
129                for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement())
130                {
131                        const char *oUtilName = pSecondChild->Attribute("utility");
132
133                        // If a utility affects this parameter find the utility object and assign it
134                        if(oUtilName) {
135                                // Search for correct utility
136                                for(k=0;k<10;k++){
137                                        if(u[k]->name == oUtilName) {
138                                                o[i]->affection_list[j].u = u[k];
139                                                break;
140                                        }
141                                }
142                        }
143                       
144                        // Set relationship
145                        const char *oRelate = pSecondChild->Attribute("relationship");
146                        if(oRelate) o[i]->affection_list[j].relation = oRelate;
147                        j++;
148                }
149                o[i]->numAffects = j;
150                i++;
151        }
152        ce_info->numObservables = i;   
153        cout << "Parsed " << ce_info->numObservables << " observables." << endl;
154       
155
156        // Pull parameter information from XML file.
157        pElem = hRoot.FirstChild("parameters").Element();
158        pChild1 = hRoot.Child("parameters",count).Element();
159       
160        i = 0;
161        for(pChild = pChild1->FirstChildElement("parameter"); pChild; pChild = pChild->NextSiblingElement())
162        {
163                p[i] = new Parameter;
164
165                const char *pName = pChild->Attribute("name");
166                if(pName) p[i]->name = pName;   
167                const char *pUnits = pChild->Attribute("units");
168                if(pUnits) p[i]->units = pUnits;
169
170                if(pChild->QueryFloatAttribute("min",&p[i]->min) != TIXML_SUCCESS) p[i]->min = -1;
171                if(pChild->QueryFloatAttribute("max",&p[i]->max) != TIXML_SUCCESS) p[i]->max = -1;
172                if(pChild->QueryFloatAttribute("step",&p[i]->step) != TIXML_SUCCESS) p[i]->step = -1;
173               
174                j = 0;
175                for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement())
176                {
177                        const char *pUtilName = pSecondChild->Attribute("utility");
178                       
179                        // If a utility affects this parameter find the utility object and assign it
180                        if(pUtilName) {
181                                // Search for correct utility
182                                for( k=0 ; u[k]!=NULL ; k++ ){
183                                        if(u[k]->name == pUtilName) {
184                                                p[i]->affection_list[j].u = u[k];       
185                                                break;
186                                        }
187                                }
188                        }
189                       
190                        // Set relationship
191                        const char *pRelate = pSecondChild->Attribute("relationship");
192                        if(pRelate) {
193                                p[i]->affection_list[j].relation = pRelate;
194                        } else {
195                                cout << "Error: No relation found." << endl;
196                        }
197                        j++;
198               
199                }
200                p[i]->numAffects = j;
201                i++;
202
203        }
204        ce_info->numParameters = i;     
205        cout << "Parsed " << ce_info->numParameters << " parameters." << endl;
206        return 1;
207}
208
209
210int StartShell() {
211   // Start socket server
212
213
214   try
215   {
216      //Create the socket
217      ServerSocket server ( SERVER_PORT );
218      ServerSocket server_1 ( 30001 );
219
220      cout << "Waiting for Cognitive Engine to connect: port " << SERVER_PORT << endl;;
221      while ( true )
222      {
223         ServerSocket new_sock;
224         server.accept ( new_sock );
225
226         try
227         {
228            while ( true ) {
229               string data;
230               new_sock >> data;
231               new_sock << data;
232            }
233         }
234         catch ( SocketException& ) {}
235
236      }
237   }
238   catch ( SocketException& e )
239   {
240      cout << "Exception was caught:" << e.description() << "\nExiting.\n";
241   }
242
243   return 0;
244}
245
246int main(int argc, char* argv[]) {
247
248
249        // CognitiveEngine CE;
250        // CognitiveEngineShell Shell;
251        string pFilename;
252
253        Utility * uList[10];
254        Parameter * pList[10];
255        Observable * oList[10];
256        CE_Info ce_info;
257
258        if(argc < 2) {
259                cout << "Warning no XML file specific using default: example.xml" << endl;
260                pFilename = "example.xml";
261        } else { 
262                pFilename = argv[1];
263        }
264
265        TiXmlDocument doc( pFilename.c_str() );
266        bool loadOkay = doc.LoadFile();
267        if (!loadOkay)
268        {
269                cout << "Loading " << pFilename << " failed." << endl;
270                return 0;
271        }
272
273        cout << "Attemping to parse " << pFilename << "." << endl;
274        parse_ce_config( &doc , uList, pList, oList, &ce_info);
275        cout << "Configuration file parsing completed." << endl;
276
277        StartShell();
278
279   return 1;
280}
Note: See TracBrowser for help on using the browser.