1 | #include <iostream> |
---|
2 | #include <sys/types.h> |
---|
3 | #include <sys/wait.h> |
---|
4 | #include <sys/socket.h> |
---|
5 | #include <netinet/in.h> |
---|
6 | #include <netdb.h> |
---|
7 | #include "tinyxml.h" |
---|
8 | #include "tinystr.h" |
---|
9 | #include <arpa/inet.h> |
---|
10 | #include <sys/mman.h> |
---|
11 | #include <fcntl.h> |
---|
12 | #include <sys/ioctl.h> |
---|
13 | |
---|
14 | //#include "socket/ServerSocket.h" |
---|
15 | //#include "socket/SocketException.h" |
---|
16 | |
---|
17 | using namespace std; |
---|
18 | |
---|
19 | #define CE_SERVER_PORT 30001 |
---|
20 | #define POLICY_SERVER_PORT 30003 |
---|
21 | |
---|
22 | struct CE_Info { |
---|
23 | int numUtilities; |
---|
24 | int numParameters; |
---|
25 | int numObservables; |
---|
26 | int policy_engine; |
---|
27 | int policy_socket; |
---|
28 | int ce_socket; |
---|
29 | }; |
---|
30 | |
---|
31 | struct Utility { |
---|
32 | string name; |
---|
33 | string units; |
---|
34 | string goal; |
---|
35 | float target; |
---|
36 | float value; |
---|
37 | }; |
---|
38 | struct Affect { |
---|
39 | Utility * u; |
---|
40 | string relation; |
---|
41 | }; |
---|
42 | struct Parameter { |
---|
43 | string name; |
---|
44 | string units; |
---|
45 | float min; |
---|
46 | int numAffects; |
---|
47 | Affect affection_list[10]; |
---|
48 | float max; |
---|
49 | float step; |
---|
50 | float value; |
---|
51 | }; |
---|
52 | |
---|
53 | struct Observable { |
---|
54 | string name; |
---|
55 | Affect affection_list[10]; |
---|
56 | int numAffects; |
---|
57 | float value; |
---|
58 | }; |
---|
59 | |
---|
60 | void DieWithError(char *errorMessage) |
---|
61 | { |
---|
62 | perror(errorMessage); |
---|
63 | exit(1); |
---|
64 | } |
---|
65 | |
---|
66 | void print_current_config(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { |
---|
67 | int i = 0; |
---|
68 | int j = 0; |
---|
69 | |
---|
70 | for(i = 0; i < ce_info->numUtilities ; i++) { |
---|
71 | cout << "Utility: " << uList[i]->name << endl; |
---|
72 | cout << " Units: " << uList[i]->units << endl; |
---|
73 | cout << " Goal: " << uList[i]->goal << endl; |
---|
74 | cout << " Target: " << uList[i]->target << endl; |
---|
75 | } |
---|
76 | |
---|
77 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
78 | cout << "Parameter: " << pList[i]->name << endl; |
---|
79 | printf("Cognitive Radio:: Radio Operation Profile has been sucessfully sent.\n"); |
---|
80 | cout << " Units: " << pList[i]->units << endl; |
---|
81 | cout << " Min: " << pList[i]->min << endl; |
---|
82 | cout << " Max: " << pList[i]->max << endl; |
---|
83 | cout << " Step: " << pList[i]->step << endl; |
---|
84 | for(j = 0; j < pList[i]->numAffects; j++) { |
---|
85 | cout << " Affect: " << pList[i]->affection_list[j].u->name << " -> " << pList[i]->affection_list[j].relation << endl; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
90 | cout << "Observable: " << oList[i]->name << endl; |
---|
91 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
92 | cout << " Affect: " << oList[i]->affection_list[j].u->name << " -> " << oList[i]->affection_list[j].relation << endl; |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | int parse_ce_config( TiXmlDocument * doc , Utility * u[], Parameter * p[], Observable * o[], CE_Info * ce_info) { |
---|
99 | |
---|
100 | TiXmlElement* pElem; //!current element |
---|
101 | TiXmlElement* pChild; //!current child of pElem |
---|
102 | TiXmlElement* pChild1; //!current child of pElem |
---|
103 | TiXmlElement* pSecondChild; //!current child of pElem |
---|
104 | TiXmlHandle hDoc(doc); //!handle to xml document |
---|
105 | TiXmlHandle hRoot(0); //! handle to root element |
---|
106 | |
---|
107 | int count = 0; |
---|
108 | int i = 0; |
---|
109 | int j = 0; |
---|
110 | int k = 0; |
---|
111 | int match_found = 0; |
---|
112 | |
---|
113 | pElem = hDoc.FirstChildElement().Element(); |
---|
114 | if(!pElem) { cout << "no valid root! quit-ing function!" << endl; return 0; } |
---|
115 | hRoot = TiXmlHandle(pElem); |
---|
116 | |
---|
117 | // Pull utility information from XML file. |
---|
118 | |
---|
119 | pElem = hRoot.FirstChild("utilities").Element(); |
---|
120 | pChild1 = hRoot.Child("utilities",count).Element(); |
---|
121 | |
---|
122 | |
---|
123 | for(pChild = pChild1->FirstChildElement("utility"); pChild; pChild = pChild->NextSiblingElement()) |
---|
124 | { |
---|
125 | u[i] = new Utility; |
---|
126 | const char *uName = pChild->Attribute("name"); |
---|
127 | if(uName) u[i]->name = uName; |
---|
128 | const char *uUnits = pChild->Attribute("units"); |
---|
129 | if(uUnits) u[i]->units = uUnits; |
---|
130 | const char *uGoal = pChild->Attribute("goal"); |
---|
131 | if(uGoal) u[i]->goal = uGoal; |
---|
132 | if(pChild->QueryFloatAttribute("target",&u[i]->target) != TIXML_SUCCESS) u[i]->target = -1; |
---|
133 | i++; |
---|
134 | } |
---|
135 | ce_info->numUtilities = i; |
---|
136 | cout << "Initialize:: Parsed " << ce_info->numUtilities << " utilities." << endl; |
---|
137 | |
---|
138 | // Pull observable information from XML file. |
---|
139 | i = 0; |
---|
140 | pElem = hRoot.FirstChild("observables").Element(); |
---|
141 | pChild1 = hRoot.Child("observables",count).Element(); |
---|
142 | |
---|
143 | for(pChild = pChild1->FirstChildElement("observable"); pChild; pChild = pChild->NextSiblingElement()) |
---|
144 | { |
---|
145 | |
---|
146 | const char *oName = pChild->Attribute("name"); |
---|
147 | o[i] = new Observable; |
---|
148 | |
---|
149 | if(oName) o[i]->name = oName; |
---|
150 | |
---|
151 | j = 0; |
---|
152 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) |
---|
153 | { |
---|
154 | const char *oUtilName = pSecondChild->Attribute("utility"); |
---|
155 | |
---|
156 | // If a utility affects this parameter find the utility object and assign it |
---|
157 | if(oUtilName) { |
---|
158 | // Search for correct utility |
---|
159 | for( k=0 ; u[k]!=NULL ; k++ ){ |
---|
160 | if(u[k]->name == oUtilName) { |
---|
161 | o[i]->affection_list[j].u = u[k]; |
---|
162 | // Set relationship |
---|
163 | const char *oRelate = pSecondChild->Attribute("relationship"); |
---|
164 | if(oRelate) o[i]->affection_list[j].relation = oRelate; |
---|
165 | j++; |
---|
166 | match_found = 1; |
---|
167 | break; |
---|
168 | } |
---|
169 | } |
---|
170 | } |
---|
171 | if(!match_found) cout << "Error: " << o[i]->name << ": " << oUtilName << " not a valid utility: Affect not added." << endl; |
---|
172 | match_found = 0; |
---|
173 | } |
---|
174 | o[i]->numAffects = j; |
---|
175 | i++; |
---|
176 | } |
---|
177 | ce_info->numObservables = i; |
---|
178 | cout << "Initialize:: Parsed " << ce_info->numObservables << " observables." << endl; |
---|
179 | |
---|
180 | |
---|
181 | // Pull parameter information from XML file. |
---|
182 | pElem = hRoot.FirstChild("parameters").Element(); |
---|
183 | pChild1 = hRoot.Child("parameters",count).Element(); |
---|
184 | |
---|
185 | i = 0; |
---|
186 | for(pChild = pChild1->FirstChildElement("parameter"); pChild; pChild = pChild->NextSiblingElement()) |
---|
187 | { |
---|
188 | p[i] = new Parameter; |
---|
189 | |
---|
190 | const char *pName = pChild->Attribute("name"); |
---|
191 | if(pName) p[i]->name = pName; |
---|
192 | const char *pUnits = pChild->Attribute("units"); |
---|
193 | if(pUnits) p[i]->units = pUnits; |
---|
194 | |
---|
195 | if(pChild->QueryFloatAttribute("min",&p[i]->min) != TIXML_SUCCESS) p[i]->min = -1; |
---|
196 | if(pChild->QueryFloatAttribute("max",&p[i]->max) != TIXML_SUCCESS) p[i]->max = -1; |
---|
197 | if(pChild->QueryFloatAttribute("step",&p[i]->step) != TIXML_SUCCESS) p[i]->step = -1; |
---|
198 | |
---|
199 | j = 0; |
---|
200 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; pSecondChild = pSecondChild->NextSiblingElement()) |
---|
201 | { |
---|
202 | const char *pUtilName = pSecondChild->Attribute("utility"); |
---|
203 | |
---|
204 | // If a utility affects this parameter find the utility object and assign it |
---|
205 | if(pUtilName) { |
---|
206 | // Search for correct utility |
---|
207 | for( k=0 ; u[k]!=NULL ; k++ ){ |
---|
208 | if(u[k]->name == pUtilName) { |
---|
209 | // If match found, assign it to this index |
---|
210 | p[i]->affection_list[j].u = u[k]; |
---|
211 | const char *pRelate = pSecondChild->Attribute("relationship"); |
---|
212 | if(pRelate) { |
---|
213 | p[i]->affection_list[j].relation = pRelate; |
---|
214 | } else { |
---|
215 | cout << "Error: No relation found." << endl; |
---|
216 | } |
---|
217 | match_found = 1; |
---|
218 | j++; |
---|
219 | break; |
---|
220 | } |
---|
221 | } |
---|
222 | } |
---|
223 | if(!match_found) cout << "Error: " << p[i]->name << ": " << pUtilName << " not a valid utility: Affect not added." << endl; |
---|
224 | match_found = 0; |
---|
225 | } |
---|
226 | p[i]->numAffects = j; |
---|
227 | i++; |
---|
228 | |
---|
229 | } |
---|
230 | ce_info->numParameters = i; |
---|
231 | cout << "Initialize:: Parsed " << ce_info->numParameters << " parameters." << endl; |
---|
232 | return 1; |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | void error(char *msg) |
---|
237 | { |
---|
238 | perror(msg); |
---|
239 | exit(1); |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | int ReceiveMessage(int socket,char * buffer) |
---|
244 | { |
---|
245 | int i,n; |
---|
246 | |
---|
247 | n = recv(socket,buffer,256,MSG_PEEK); |
---|
248 | |
---|
249 | for(i=0;i<256;i++){ |
---|
250 | if(strcmp(&buffer[i],"\0") == 0) break; |
---|
251 | } |
---|
252 | n = recv(socket,buffer,i+1,0); |
---|
253 | if (n < 0) |
---|
254 | error("ERROR reading from socket"); |
---|
255 | // printf("ReadMessage:%s %d\n",buffer,n); |
---|
256 | |
---|
257 | return n; |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | int SendMessage(int socketfd, string message) { |
---|
262 | int n; |
---|
263 | |
---|
264 | message.append("\0"); |
---|
265 | // Write message back to client |
---|
266 | n = send(socketfd,message.c_str(),(message.size()+1),0); |
---|
267 | if (n<0) |
---|
268 | error("Error sending to client\n"); |
---|
269 | if(n == 0) |
---|
270 | printf("Client closed the socket.\n"); |
---|
271 | |
---|
272 | //printf("SendMessage:%s %d\n",message.c_str(),n); |
---|
273 | return n; |
---|
274 | } |
---|
275 | |
---|
276 | void GetEnvironment() { |
---|
277 | |
---|
278 | } |
---|
279 | |
---|
280 | void Policy_ValidateSettings() { |
---|
281 | |
---|
282 | } |
---|
283 | |
---|
284 | |
---|
285 | void LoadCEConfiguration(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info){ |
---|
286 | int n,i,j; |
---|
287 | char counter[55]; |
---|
288 | char var[50]; |
---|
289 | //int total_bytes; |
---|
290 | |
---|
291 | printf("Cognitive Radio:: Sending Radio Operating Profile to Cognitive Engine.\n\n"); |
---|
292 | |
---|
293 | // utilities |
---|
294 | // Send number of utilities |
---|
295 | sprintf(counter,"%d",ce_info->numUtilities); |
---|
296 | SendMessage(socketfd,counter); |
---|
297 | // send utility |
---|
298 | for(i = 0; i < ce_info->numUtilities; i++) { |
---|
299 | SendMessage(socketfd,uList[i]->name); |
---|
300 | SendMessage(socketfd,uList[i]->units); |
---|
301 | SendMessage(socketfd,uList[i]->goal); |
---|
302 | sprintf(var,"%f",uList[i]->target); |
---|
303 | SendMessage(socketfd,var); |
---|
304 | } |
---|
305 | |
---|
306 | // parameters |
---|
307 | sprintf(counter,"%i",ce_info->numParameters); |
---|
308 | SendMessage(socketfd,counter); |
---|
309 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
310 | SendMessage(socketfd,pList[i]->name); |
---|
311 | SendMessage(socketfd,pList[i]->units); |
---|
312 | sprintf(var,"%f",pList[i]->min); |
---|
313 | SendMessage(socketfd,var); |
---|
314 | sprintf(var,"%f",pList[i]->max); |
---|
315 | SendMessage(socketfd,var); |
---|
316 | sprintf(var,"%f",pList[i]->step); |
---|
317 | SendMessage(socketfd,var); |
---|
318 | |
---|
319 | sprintf(counter,"%i",pList[i]->numAffects); |
---|
320 | SendMessage(socketfd,counter); |
---|
321 | for(j = 0; j < pList[i]->numAffects; j++) { |
---|
322 | SendMessage(socketfd,pList[i]->affection_list[j].u->name); |
---|
323 | SendMessage(socketfd,pList[i]->affection_list[j].relation); |
---|
324 | } |
---|
325 | } |
---|
326 | |
---|
327 | // observables |
---|
328 | sprintf(counter,"%i",ce_info->numObservables); |
---|
329 | SendMessage(socketfd,counter); |
---|
330 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
331 | SendMessage(socketfd,oList[i]->name); |
---|
332 | |
---|
333 | sprintf(counter,"%i",oList[i]->numAffects); |
---|
334 | SendMessage(socketfd,counter); |
---|
335 | for(j = 0; j < oList[i]->numAffects; j++) { |
---|
336 | SendMessage(socketfd,oList[i]->affection_list[j].u->name); |
---|
337 | SendMessage(socketfd,oList[i]->affection_list[j].relation); |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | // Receive ACK for utils |
---|
342 | char buffer[256]; |
---|
343 | string message; |
---|
344 | n = ReceiveMessage(socketfd, buffer); |
---|
345 | //printf("%s\n", buffer); |
---|
346 | //cout << message << endl; |
---|
347 | //printf("ACK received.\n"); |
---|
348 | |
---|
349 | } |
---|
350 | |
---|
351 | void UpdateCEConfiguration() { |
---|
352 | |
---|
353 | } |
---|
354 | |
---|
355 | void ResetCEConfiguration(){ |
---|
356 | |
---|
357 | } |
---|
358 | |
---|
359 | void UpdateCEExperience(int socketfd, int num_rows, int num_cols, |
---|
360 | float * past_exp[]) |
---|
361 | { |
---|
362 | int i, j; |
---|
363 | char counter[55]; |
---|
364 | char var[50]; |
---|
365 | |
---|
366 | for (i = 0; i < num_rows; i++){ |
---|
367 | for (j = 0; j< num_cols; j++){ |
---|
368 | sprintf(var,"%f",past_exp[i][j]); |
---|
369 | //printf("%f, \n", past_exp[i][j]); |
---|
370 | //printf("%s, \n", var); |
---|
371 | } |
---|
372 | } |
---|
373 | |
---|
374 | // send the number of rows to the ce first |
---|
375 | sprintf(counter,"%d",num_rows); |
---|
376 | SendMessage(socketfd,counter); |
---|
377 | // send the number of columns to the ce |
---|
378 | sprintf(counter,"%d",num_cols); |
---|
379 | SendMessage(socketfd,counter); |
---|
380 | // update ce with experience |
---|
381 | for (i = 0; i < num_rows; i++){ |
---|
382 | for (j = 0; j< num_cols; j++){ |
---|
383 | sprintf(var,"%f",past_exp[i][j]); |
---|
384 | SendMessage(socketfd,var); |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | } |
---|
389 | |
---|
390 | void ResetCEExperience() { |
---|
391 | |
---|
392 | } |
---|
393 | |
---|
394 | // Update operating settings |
---|
395 | // This function will interact with the hardware "drivers" |
---|
396 | void UpdateRadioSettings() |
---|
397 | { |
---|
398 | } |
---|
399 | |
---|
400 | |
---|
401 | int RequestPolicyValidation(Parameter * pList[], CE_Info *ce_info) |
---|
402 | { |
---|
403 | char counter[55]; |
---|
404 | char var[50]; |
---|
405 | int i; |
---|
406 | string control_msg; |
---|
407 | |
---|
408 | int socketfd = ce_info->policy_socket; |
---|
409 | |
---|
410 | // Control message that validation request is coming |
---|
411 | control_msg = "val"; |
---|
412 | SendMessage(socketfd,control_msg); |
---|
413 | |
---|
414 | printf("Cognitive Radio:: Here. %i\n\n", socketfd); |
---|
415 | |
---|
416 | // Send parameter information |
---|
417 | sprintf(counter,"%i",ce_info->numParameters); |
---|
418 | SendMessage(socketfd,counter); |
---|
419 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
420 | SendMessage(socketfd,pList[i]->name); |
---|
421 | SendMessage(socketfd,pList[i]->units); |
---|
422 | sprintf(var,"%f",pList[i]->min); |
---|
423 | SendMessage(socketfd,var); |
---|
424 | sprintf(var,"%f",pList[i]->max); |
---|
425 | SendMessage(socketfd,var); |
---|
426 | sprintf(var,"%f",pList[i]->step); |
---|
427 | SendMessage(socketfd,var); |
---|
428 | sprintf(var,"%f",pList[i]->value); |
---|
429 | SendMessage(socketfd,var); |
---|
430 | |
---|
431 | } |
---|
432 | return 1; |
---|
433 | |
---|
434 | } |
---|
435 | |
---|
436 | |
---|
437 | int RequestCEOptimization(int sockfd, Utility *uList[], |
---|
438 | Parameter *pList[], Observable *oList[], |
---|
439 | CE_Info *ce_info) |
---|
440 | { |
---|
441 | char buffer[256]; |
---|
442 | int i; |
---|
443 | float var; |
---|
444 | |
---|
445 | // Send request optimization message followed by the current environment parameters. |
---|
446 | /* |
---|
447 | SendMessage(sockfd,"request"); |
---|
448 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
449 | SendMessage(sockfd,..); |
---|
450 | } |
---|
451 | */ |
---|
452 | |
---|
453 | // Receive optimized values from the Cognitive Engine |
---|
454 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
455 | bzero(buffer,256); |
---|
456 | ReceiveMessage(sockfd,buffer); |
---|
457 | var = atof(buffer); |
---|
458 | pList[i]->value = var; |
---|
459 | } |
---|
460 | |
---|
461 | |
---|
462 | // If policy engine is connect, validate new values |
---|
463 | if(ce_info->policy_engine == 1) { |
---|
464 | |
---|
465 | printf("Cognitive Radio:: Found Policy Engine!\n"); |
---|
466 | printf("Cognitive Radio:: Validating parameters with Policy Engine\n\n"); |
---|
467 | RequestPolicyValidation(pList,ce_info); |
---|
468 | printf("Cognitive Radio:: Done\n\n"); |
---|
469 | |
---|
470 | } |
---|
471 | |
---|
472 | |
---|
473 | return 1; |
---|
474 | } |
---|
475 | |
---|
476 | void RunSimulator(int socketfd, Utility * uList[], |
---|
477 | Parameter * pList[], Observable * oList[], |
---|
478 | CE_Info * ce_info) { |
---|
479 | |
---|
480 | float **past_exp; |
---|
481 | int num_rows, num_cols; |
---|
482 | |
---|
483 | // Set fake current environment params = current environment |
---|
484 | RequestCEOptimization(socketfd, uList, pList, oList, ce_info); |
---|
485 | |
---|
486 | // Act like we are updating the hardware tranmission settings |
---|
487 | UpdateRadioSettings(); |
---|
488 | |
---|
489 | // Send back fake utility values |
---|
490 | // need to initialize |
---|
491 | //UpdateCEExperience(socketfd, num_rows, num_cols, past_exp); |
---|
492 | } |
---|
493 | |
---|
494 | void InitializePE(int socket, CE_Info * ce_info) |
---|
495 | { |
---|
496 | // Policy Engine is connected |
---|
497 | // Set global policy engine value to 1 |
---|
498 | ce_info->policy_engine = 1; |
---|
499 | ce_info->policy_socket = socket; |
---|
500 | |
---|
501 | return; |
---|
502 | } |
---|
503 | |
---|
504 | void InitializeCE(int socketfd, Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) |
---|
505 | { |
---|
506 | LoadCEConfiguration(socketfd, uList, pList, oList, ce_info); |
---|
507 | |
---|
508 | // cr experience |
---|
509 | float **past_exp; |
---|
510 | int num_cols; |
---|
511 | // get number of columns |
---|
512 | num_cols = ce_info->numUtilities + ce_info->numParameters; |
---|
513 | num_cols = num_cols + ce_info->numObservables; |
---|
514 | num_cols = num_cols + 1; // overall utility |
---|
515 | int num_rows = 2; |
---|
516 | past_exp = (float **)malloc(sizeof(float)*num_rows); |
---|
517 | int i; |
---|
518 | for (i=0; i<num_rows; i++){ |
---|
519 | past_exp[i] = (float*)malloc(sizeof(float)*num_cols); |
---|
520 | } |
---|
521 | // sample experience #1 |
---|
522 | past_exp[0][0] = 1e3f; // throughput |
---|
523 | past_exp[0][1] = 1; // spectral_efficiency |
---|
524 | past_exp[0][2] = -3.5; // log10_ber |
---|
525 | past_exp[0][3] = 1; // mod_scheme |
---|
526 | past_exp[0][4] = -10; // tx_power |
---|
527 | past_exp[0][5] = 10.0; // SNR |
---|
528 | past_exp[0][6] = 0.762; // overall utility*/ |
---|
529 | // sample experience #2 |
---|
530 | past_exp[1][0] = 1e2f; // throughput |
---|
531 | past_exp[1][1] = 1; // spectral_efficiency |
---|
532 | past_exp[1][2] = -3.5; // log10_ber |
---|
533 | past_exp[1][3] = 1; // mod_scheme |
---|
534 | past_exp[1][4] = -14; // tx_power |
---|
535 | past_exp[1][5] = 3.0; // SNR |
---|
536 | past_exp[1][6] = 0.462; // overall utility |
---|
537 | |
---|
538 | // update ce with experience |
---|
539 | printf("Cognitive Radio:: Sending Previous Experience to New Cognitive Engine.\n\n"); |
---|
540 | UpdateCEExperience(socketfd, num_rows, num_cols, past_exp); |
---|
541 | |
---|
542 | RunSimulator(socketfd, uList, pList, oList, ce_info); |
---|
543 | } |
---|
544 | |
---|
545 | int AcceptTCPConnection(int servSock) |
---|
546 | { |
---|
547 | int clntSock; /* Socket descriptor for client */ |
---|
548 | struct sockaddr_in echoClntAddr; |
---|
549 | unsigned int clntLen; |
---|
550 | |
---|
551 | /* Set the size of the in-out parameter */ |
---|
552 | clntLen = sizeof(echoClntAddr); |
---|
553 | |
---|
554 | /* Wait for a client to connect */ |
---|
555 | //if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0) { |
---|
556 | if ((clntSock = accept(servSock, NULL, NULL)) < 0) { |
---|
557 | return -1; |
---|
558 | } |
---|
559 | |
---|
560 | /* clntSock is connected to a client! */ |
---|
561 | |
---|
562 | printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr)); |
---|
563 | |
---|
564 | return clntSock; |
---|
565 | } |
---|
566 | |
---|
567 | int CreateTCPServerSocket(unsigned short port) |
---|
568 | { |
---|
569 | int sock; /* socket to create */ |
---|
570 | struct sockaddr_in echoServAddr; /* Local address */ |
---|
571 | |
---|
572 | /* Create socket for incoming connections */ |
---|
573 | if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) |
---|
574 | DieWithError("socket() failed"); |
---|
575 | |
---|
576 | /* Construct local address structure */ |
---|
577 | memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ |
---|
578 | echoServAddr.sin_family = AF_INET; /* Internet address family */ |
---|
579 | echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ |
---|
580 | echoServAddr.sin_port = htons(port); /* Local port */ |
---|
581 | |
---|
582 | /* Bind to the local address */ |
---|
583 | if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) |
---|
584 | DieWithError("bind() failed"); |
---|
585 | |
---|
586 | /* Mark the socket so it will listen for incoming connections */ |
---|
587 | if (listen(sock, 5) < 0) { |
---|
588 | printf("listen() failed\n"); |
---|
589 | return 0; |
---|
590 | } |
---|
591 | |
---|
592 | return sock; |
---|
593 | } |
---|
594 | |
---|
595 | void HandleTCPClient(int socketfd, Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) |
---|
596 | { |
---|
597 | char buffer[256]; /* Buffer for echo string */ |
---|
598 | |
---|
599 | /* Receive message from client */ |
---|
600 | bzero(buffer,256); |
---|
601 | ReceiveMessage(socketfd,buffer); |
---|
602 | |
---|
603 | printf("Cognitive Radio:: Message Received - %s.\n\n", buffer); |
---|
604 | |
---|
605 | if(strcmp(buffer,"c_register") == 0) |
---|
606 | InitializeCE(socketfd, uList, pList, oList, ce_info); |
---|
607 | |
---|
608 | if(strcmp(buffer,"p_register") == 0) |
---|
609 | InitializePE(socketfd, ce_info); |
---|
610 | |
---|
611 | if(strcmp(buffer,"optimize") == 0) |
---|
612 | RunSimulator(socketfd, uList, pList, oList, ce_info); |
---|
613 | |
---|
614 | //close(socketfd); /* Close client socket */ |
---|
615 | } |
---|
616 | |
---|
617 | int StartServers(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { |
---|
618 | int * servSock; |
---|
619 | int running = 1; |
---|
620 | struct timeval selTimeout; |
---|
621 | int timeout = 10; |
---|
622 | int cognitive_engine = 0; |
---|
623 | int policy_engine = 1; |
---|
624 | int port, rc, on = 1; |
---|
625 | int new_sd; |
---|
626 | int desc_ready = 0; |
---|
627 | fd_set sockSet; |
---|
628 | |
---|
629 | servSock = (int *) malloc(2 * sizeof(int)); |
---|
630 | |
---|
631 | servSock[cognitive_engine] = CreateTCPServerSocket(CE_SERVER_PORT); |
---|
632 | servSock[policy_engine] = CreateTCPServerSocket(POLICY_SERVER_PORT); |
---|
633 | |
---|
634 | |
---|
635 | int maxDescriptor = servSock[cognitive_engine]; |
---|
636 | |
---|
637 | if(servSock[cognitive_engine] < servSock[policy_engine]) |
---|
638 | maxDescriptor = servSock[policy_engine]; |
---|
639 | |
---|
640 | rc = setsockopt(servSock[cognitive_engine], SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)); |
---|
641 | if(rc < 0) |
---|
642 | { |
---|
643 | perror("setsockopt() failed"); |
---|
644 | close(servSock[cognitive_engine]); |
---|
645 | exit(-1); |
---|
646 | } |
---|
647 | |
---|
648 | rc = setsockopt(servSock[policy_engine], SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)); |
---|
649 | if(rc < 0) |
---|
650 | { |
---|
651 | perror("setsockopt() failed"); |
---|
652 | close(servSock[policy_engine]); |
---|
653 | exit(-1); |
---|
654 | } |
---|
655 | |
---|
656 | rc = ioctl(servSock[cognitive_engine], FIONBIO, (char*)&on); |
---|
657 | if(rc < 0) |
---|
658 | { |
---|
659 | perror("ioctl() failed"); |
---|
660 | close(servSock[cognitive_engine]); |
---|
661 | exit(-1); |
---|
662 | } |
---|
663 | |
---|
664 | rc = ioctl(servSock[policy_engine], FIONBIO, (char*)&on); |
---|
665 | if(rc < 0) |
---|
666 | { |
---|
667 | perror("ioctl() failed"); |
---|
668 | close(servSock[policy_engine]); |
---|
669 | exit(-1); |
---|
670 | } |
---|
671 | |
---|
672 | printf("Starting server: Hit return to shutdown\n"); |
---|
673 | while (running) |
---|
674 | { |
---|
675 | /* Zero socket descriptor vector and set for server sockets */ |
---|
676 | /* This must be reset every time select() is called */ |
---|
677 | FD_ZERO(&sockSet); |
---|
678 | /* Add keyboard to descriptor vector */ |
---|
679 | FD_SET(STDIN_FILENO, &sockSet); |
---|
680 | FD_SET(servSock[cognitive_engine], &sockSet); |
---|
681 | FD_SET(servSock[policy_engine], &sockSet); |
---|
682 | |
---|
683 | /* Timeout specification */ |
---|
684 | /* This must be reset every time select() is called */ |
---|
685 | selTimeout.tv_sec = timeout; /* timeout (secs.) */ |
---|
686 | selTimeout.tv_usec = 0; /* 0 microseconds */ |
---|
687 | |
---|
688 | /* Suspend program until descriptor is ready or timeout */ |
---|
689 | rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
690 | if (rc == 0) |
---|
691 | printf("No echo requests for %i secs...Server still alive\n", timeout); |
---|
692 | else |
---|
693 | { |
---|
694 | if (FD_ISSET(0, &sockSet)) /* Check keyboard */ |
---|
695 | { |
---|
696 | printf("Shutting down server\n"); |
---|
697 | getchar(); |
---|
698 | running = 0; |
---|
699 | } |
---|
700 | |
---|
701 | desc_ready = rc; |
---|
702 | |
---|
703 | for (port = 0; port <= maxDescriptor && desc_ready > 0; port++) { |
---|
704 | if (FD_ISSET(port, &sockSet)) |
---|
705 | { |
---|
706 | printf("Request on port %d: ", port); |
---|
707 | desc_ready -= 1; |
---|
708 | |
---|
709 | |
---|
710 | if( (port == servSock[cognitive_engine]) || (port == servSock[policy_engine])) { |
---|
711 | |
---|
712 | do |
---|
713 | { |
---|
714 | new_sd = AcceptTCPConnection(port); |
---|
715 | if(new_sd < 0) |
---|
716 | { |
---|
717 | break; |
---|
718 | } |
---|
719 | |
---|
720 | HandleTCPClient(new_sd, uList, pList, oList, ce_info); |
---|
721 | FD_SET(new_sd,&sockSet); |
---|
722 | if(new_sd > maxDescriptor) |
---|
723 | maxDescriptor = new_sd; |
---|
724 | printf("New incoming connection - %i\n\n",new_sd); |
---|
725 | } while(new_sd != -1); |
---|
726 | } else { |
---|
727 | |
---|
728 | printf("Request on already open descriptor.\n\n"); |
---|
729 | HandleTCPClient(port, uList, pList, oList, ce_info); |
---|
730 | |
---|
731 | } |
---|
732 | |
---|
733 | } |
---|
734 | } |
---|
735 | } |
---|
736 | } |
---|
737 | |
---|
738 | /* Close sockets */ |
---|
739 | for (port = 0; port < 2; port++) |
---|
740 | close(servSock[port]); |
---|
741 | |
---|
742 | /* Free list of sockets */ |
---|
743 | free(servSock); |
---|
744 | |
---|
745 | return 0; |
---|
746 | } |
---|
747 | |
---|
748 | int main(int argc, char* argv[]) { |
---|
749 | |
---|
750 | |
---|
751 | // CognitiveEngine CE; |
---|
752 | // CognitiveEngineShell Shell; |
---|
753 | string pFilename; |
---|
754 | int fd; |
---|
755 | |
---|
756 | Utility * uList[10]; |
---|
757 | Parameter * pList[10]; |
---|
758 | Observable * oList[10]; |
---|
759 | struct CE_Info *ce_info; |
---|
760 | |
---|
761 | if((fd = open("/dev/zero", O_RDWR)) == -1) |
---|
762 | return 1; |
---|
763 | |
---|
764 | ce_info = (struct CE_Info *)mmap(0,sizeof(CE_Info),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); |
---|
765 | |
---|
766 | close(fd); |
---|
767 | |
---|
768 | if(argc < 2) { |
---|
769 | cout << "Warning no XML file specific using default: example.xml" << endl; |
---|
770 | pFilename = "example.xml"; |
---|
771 | } else { |
---|
772 | pFilename = argv[1]; |
---|
773 | } |
---|
774 | |
---|
775 | TiXmlDocument doc( pFilename.c_str() ); |
---|
776 | bool loadOkay = doc.LoadFile(); |
---|
777 | if (!loadOkay) |
---|
778 | { |
---|
779 | cout << "Loading " << pFilename << " failed." << endl; |
---|
780 | return 0; |
---|
781 | } |
---|
782 | |
---|
783 | cout << "\n\nInitialize:: Attemping to parse " << pFilename << "." << endl; |
---|
784 | parse_ce_config( &doc , uList, pList, oList, ce_info); |
---|
785 | cout << "Initialize:: Configuration file parsing completed.\n" << endl; |
---|
786 | |
---|
787 | //print_current_config(uList, pList, oList, &ce_info); |
---|
788 | |
---|
789 | StartServers(uList, pList, oList, ce_info); |
---|
790 | return 1; |
---|
791 | } |
---|