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 | using namespace std; |
---|
10 | |
---|
11 | #define SERVER_PORT 30000 |
---|
12 | |
---|
13 | struct CE_Info { |
---|
14 | int numUtilities; |
---|
15 | int numParameters; |
---|
16 | int numObservables; |
---|
17 | }; |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | struct Utility { |
---|
22 | string name; |
---|
23 | string units; |
---|
24 | string goal; |
---|
25 | float target; |
---|
26 | }; |
---|
27 | struct Affect { |
---|
28 | Utility * u; |
---|
29 | string relation; |
---|
30 | }; |
---|
31 | struct Parameter { |
---|
32 | string name; |
---|
33 | string units; |
---|
34 | float min; |
---|
35 | int numAffects; |
---|
36 | Affect affection_list[10]; |
---|
37 | float max; |
---|
38 | float step; |
---|
39 | }; |
---|
40 | |
---|
41 | struct Observable { |
---|
42 | string name; |
---|
43 | Affect affection_list[10]; |
---|
44 | int numAffects; |
---|
45 | }; |
---|
46 | |
---|
47 | void print_current_config(Utility * uList[], Parameter * pList[10], Observable * oList[], CE_Info * ce_info) { |
---|
48 | int i = 0; |
---|
49 | int j = 0; |
---|
50 | |
---|
51 | for(i = 0; i < ce_info->numUtilities ; i++) { |
---|
52 | cout << "Utility: " << uList[i]->name << endl; |
---|
53 | cout << " Units: " << uList[i]->units << endl; |
---|
54 | cout << " Goal: " << uList[i]->goal << endl; |
---|
55 | cout << " Target: " << uList[i]->target << endl; |
---|
56 | } |
---|
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 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
70 | cout << "Observable: " << oList[i]->name << endl; |
---|
71 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
72 | cout << " Affect: " << oList[i]->affection_list[j].u->name << " -> " << oList[i]->affection_list[j].relation << endl; |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | int parse_ce_config( TiXmlDocument * doc , Utility * u[], Parameter * p[], Observable * o[], CE_Info * ce_info) { |
---|
79 | |
---|
80 | TiXmlElement* pElem; //!current element |
---|
81 | TiXmlElement* pChild; //!current child of pElem |
---|
82 | TiXmlElement* pChild1; //!current child of pElem |
---|
83 | TiXmlElement* pSecondChild; //!current child of pElem |
---|
84 | TiXmlHandle hDoc(doc); //!handle to xml document |
---|
85 | TiXmlHandle hRoot(0); //! handle to root element |
---|
86 | |
---|
87 | int count = 0; |
---|
88 | int i = 0; |
---|
89 | int j = 0; |
---|
90 | int k = 0; |
---|
91 | |
---|
92 | pElem = hDoc.FirstChildElement().Element(); |
---|
93 | if(!pElem) { cout << "no valid root! quit-ing function!" << endl; return 0; } |
---|
94 | hRoot = TiXmlHandle(pElem); |
---|
95 | |
---|
96 | // Pull utility information from XML file. |
---|
97 | |
---|
98 | pElem = hRoot.FirstChild("utilities").Element(); |
---|
99 | pChild1 = hRoot.Child("utilities",count).Element(); |
---|
100 | |
---|
101 | |
---|
102 | for(pChild = pChild1->FirstChildElement("utility"); pChild; pChild = pChild->NextSiblingElement()) |
---|
103 | { |
---|
104 | u[i] = new Utility; |
---|
105 | const char *uName = pChild->Attribute("name"); |
---|
106 | if(uName) u[i]->name = uName; |
---|
107 | const char *uUnits = pChild->Attribute("units"); |
---|
108 | if(uUnits) u[i]->units = uUnits; |
---|
109 | const char *uGoal = pChild->Attribute("goal"); |
---|
110 | if(uGoal) u[i]->goal = uGoal; |
---|
111 | if(pChild->QueryFloatAttribute("target",&u[i]->target) != TIXML_SUCCESS) u[i]->target = -1; |
---|
112 | i++; |
---|
113 | } |
---|
114 | ce_info->numUtilities = i; |
---|
115 | cout << "Parsed " << ce_info->numUtilities << " utilities." << endl; |
---|
116 | |
---|
117 | // Pull observable information from XML file. |
---|
118 | i = 0; |
---|
119 | pElem = hRoot.FirstChild("observables").Element(); |
---|
120 | pChild1 = hRoot.Child("observables",count).Element(); |
---|
121 | |
---|
122 | for(pChild = pChild1->FirstChildElement("observable"); pChild; pChild = pChild->NextSiblingElement()) |
---|
123 | { |
---|
124 | |
---|
125 | const char *oName = pChild->Attribute("name"); |
---|
126 | o[i] = new Observable; |
---|
127 | |
---|
128 | if(oName) o[i]->name = oName; |
---|
129 | |
---|
130 | j = 0; |
---|
131 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) |
---|
132 | { |
---|
133 | const char *oUtilName = pSecondChild->Attribute("utility"); |
---|
134 | |
---|
135 | // If a utility affects this parameter find the utility object and assign it |
---|
136 | if(oUtilName) { |
---|
137 | // Search for correct utility |
---|
138 | for(k=0;k<10;k++){ |
---|
139 | if(u[k]->name == oUtilName) { |
---|
140 | o[i]->affection_list[j].u = u[k]; |
---|
141 | break; |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | // Set relationship |
---|
147 | const char *oRelate = pSecondChild->Attribute("relationship"); |
---|
148 | if(oRelate) o[i]->affection_list[j].relation = oRelate; |
---|
149 | j++; |
---|
150 | } |
---|
151 | o[i]->numAffects = j; |
---|
152 | i++; |
---|
153 | } |
---|
154 | ce_info->numObservables = i; |
---|
155 | cout << "Parsed " << ce_info->numObservables << " observables." << endl; |
---|
156 | |
---|
157 | |
---|
158 | // Pull parameter information from XML file. |
---|
159 | pElem = hRoot.FirstChild("parameters").Element(); |
---|
160 | pChild1 = hRoot.Child("parameters",count).Element(); |
---|
161 | |
---|
162 | i = 0; |
---|
163 | for(pChild = pChild1->FirstChildElement("parameter"); pChild; pChild = pChild->NextSiblingElement()) |
---|
164 | { |
---|
165 | p[i] = new Parameter; |
---|
166 | |
---|
167 | const char *pName = pChild->Attribute("name"); |
---|
168 | if(pName) p[i]->name = pName; |
---|
169 | const char *pUnits = pChild->Attribute("units"); |
---|
170 | if(pUnits) p[i]->units = pUnits; |
---|
171 | |
---|
172 | if(pChild->QueryFloatAttribute("min",&p[i]->min) != TIXML_SUCCESS) p[i]->min = -1; |
---|
173 | if(pChild->QueryFloatAttribute("max",&p[i]->max) != TIXML_SUCCESS) p[i]->max = -1; |
---|
174 | if(pChild->QueryFloatAttribute("step",&p[i]->step) != TIXML_SUCCESS) p[i]->step = -1; |
---|
175 | |
---|
176 | j = 0; |
---|
177 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) |
---|
178 | { |
---|
179 | const char *pUtilName = pSecondChild->Attribute("utility"); |
---|
180 | |
---|
181 | // If a utility affects this parameter find the utility object and assign it |
---|
182 | if(pUtilName) { |
---|
183 | // Search for correct utility |
---|
184 | for( k=0 ; u[k]!=NULL ; k++ ){ |
---|
185 | if(u[k]->name == pUtilName) { |
---|
186 | p[i]->affection_list[j].u = u[k]; |
---|
187 | break; |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | // Set relationship |
---|
193 | const char *pRelate = pSecondChild->Attribute("relationship"); |
---|
194 | if(pRelate) { |
---|
195 | p[i]->affection_list[j].relation = pRelate; |
---|
196 | } else { |
---|
197 | cout << "Error: No relation found." << endl; |
---|
198 | } |
---|
199 | j++; |
---|
200 | |
---|
201 | } |
---|
202 | p[i]->numAffects = j; |
---|
203 | i++; |
---|
204 | |
---|
205 | } |
---|
206 | ce_info->numParameters = i; |
---|
207 | cout << "Parsed " << ce_info->numParameters << " parameters." << endl; |
---|
208 | return 1; |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | void error(char *msg) |
---|
213 | { |
---|
214 | perror(msg); |
---|
215 | exit(1); |
---|
216 | } |
---|
217 | |
---|
218 | void StartMessageParser( int socketfd) { |
---|
219 | char buffer[256]; |
---|
220 | int n; |
---|
221 | |
---|
222 | while(1) { |
---|
223 | // Clear incoming data buffer |
---|
224 | bzero(buffer,256); |
---|
225 | |
---|
226 | // Wait for incoming message |
---|
227 | n = recv(socketfd,buffer,255,0); |
---|
228 | if (n < 0) error("ERROR reading from socket"); |
---|
229 | if (n == 0) { |
---|
230 | printf("Client has closed the connection.\n"); |
---|
231 | return; |
---|
232 | } |
---|
233 | |
---|
234 | // Print message |
---|
235 | printf("Here is the message: %s\n",buffer); |
---|
236 | |
---|
237 | // Write message back to client |
---|
238 | n = write(socketfd,"I got your message",18); |
---|
239 | if (n < 0) error("ERROR writing to socket"); |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | int StartShell(int port) { |
---|
244 | // Start socket server |
---|
245 | int sockfd, newsockfd, clilen; |
---|
246 | struct sockaddr_in serv_addr, cli_addr; |
---|
247 | |
---|
248 | // Setup socket connection |
---|
249 | |
---|
250 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
251 | if (sockfd < 0) |
---|
252 | error("ERROR opening socket"); |
---|
253 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
---|
254 | serv_addr.sin_family = AF_INET; |
---|
255 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
---|
256 | serv_addr.sin_port = htons(port); |
---|
257 | if (bind(sockfd, (struct sockaddr *) &serv_addr, |
---|
258 | sizeof(serv_addr)) < 0) |
---|
259 | error("ERROR on binding"); |
---|
260 | listen(sockfd,5); |
---|
261 | clilen = sizeof(cli_addr); |
---|
262 | |
---|
263 | while(1) { |
---|
264 | newsockfd = accept(sockfd, |
---|
265 | (struct sockaddr *) &cli_addr, |
---|
266 | (socklen_t*)&clilen); |
---|
267 | if (newsockfd < 0) |
---|
268 | error("ERROR on accept"); |
---|
269 | |
---|
270 | // Begin parsing the messages |
---|
271 | StartMessageParser(newsockfd); |
---|
272 | } |
---|
273 | return 0; |
---|
274 | } |
---|
275 | |
---|
276 | int main(int argc, char* argv[]) { |
---|
277 | |
---|
278 | |
---|
279 | // CognitiveEngine CE; |
---|
280 | // CognitiveEngineShell Shell; |
---|
281 | string pFilename; |
---|
282 | int pid; |
---|
283 | |
---|
284 | Utility * uList[10]; |
---|
285 | Parameter * pList[10]; |
---|
286 | Observable * oList[10]; |
---|
287 | CE_Info ce_info; |
---|
288 | |
---|
289 | if(argc < 2) { |
---|
290 | cout << "Warning no XML file specific using default: example.xml" << endl; |
---|
291 | pFilename = "example.xml"; |
---|
292 | } else { |
---|
293 | pFilename = argv[1]; |
---|
294 | } |
---|
295 | |
---|
296 | TiXmlDocument doc( pFilename.c_str() ); |
---|
297 | bool loadOkay = doc.LoadFile(); |
---|
298 | if (!loadOkay) |
---|
299 | { |
---|
300 | cout << "Loading " << pFilename << " failed." << endl; |
---|
301 | return 0; |
---|
302 | } |
---|
303 | |
---|
304 | cout << "Attemping to parse " << pFilename << "." << endl; |
---|
305 | parse_ce_config( &doc , uList, pList, oList, &ce_info); |
---|
306 | cout << "Configuration file parsing completed." << endl; |
---|
307 | |
---|
308 | pid = fork(); |
---|
309 | if(pid == 0) { |
---|
310 | // In child process - open policy engine port. |
---|
311 | StartShell(30001); |
---|
312 | } else { |
---|
313 | // In parent process - open cognitive engine port. |
---|
314 | StartShell(30000); |
---|
315 | } |
---|
316 | |
---|
317 | |
---|
318 | return 1; |
---|
319 | } |
---|