1 | /* Virginia Tech Cognitive Radio Open Source Systems |
---|
2 | * Virginia Tech, 2009 |
---|
3 | * |
---|
4 | * LICENSE INFORMATION GOES HERE |
---|
5 | */ |
---|
6 | |
---|
7 | /* DESCRIPTION OF FILE. |
---|
8 | */ |
---|
9 | |
---|
10 | |
---|
11 | #include <cstdlib> |
---|
12 | #include <cstring> |
---|
13 | #include <stdint.h> |
---|
14 | #include <string> |
---|
15 | |
---|
16 | #include <arpa/inet.h> |
---|
17 | #include <iostream> |
---|
18 | #include <netinet/in.h> |
---|
19 | #include <netdb.h> |
---|
20 | #include <fcntl.h> |
---|
21 | #include <sys/ioctl.h> |
---|
22 | #include <sys/mman.h> |
---|
23 | #include <sys/socket.h> |
---|
24 | #include <sys/types.h> |
---|
25 | #include <sys/wait.h> |
---|
26 | |
---|
27 | #include "tinyxml/tinyxml.h" |
---|
28 | #include "tinyxml/tinystr.h" |
---|
29 | |
---|
30 | #include "vtcross/common.h" |
---|
31 | #include "vtcross/components.h" |
---|
32 | #include "vtcross/containers.h" |
---|
33 | #include "vtcross/debug.h" |
---|
34 | #include "vtcross/error.h" |
---|
35 | #include "vtcross/socketcomm.h" |
---|
36 | |
---|
37 | |
---|
38 | CognitiveRadioShell::CognitiveRadioShell() |
---|
39 | { |
---|
40 | LOG("Creating Cognitive Radio Shell.\n"); |
---|
41 | SML_present = false; |
---|
42 | PE_present = false; |
---|
43 | CE_present = false; |
---|
44 | |
---|
45 | // TODO =BUG= The params, observables, and utils arrays are not being |
---|
46 | // allocated here. If an shell object is constructed, and then immediately |
---|
47 | // destructed, there will be no allocated memory, and the delete operater |
---|
48 | // will receive a null pointer, which will segfault. |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | CognitiveRadioShell::~CognitiveRadioShell() |
---|
53 | { |
---|
54 | delete [] params; |
---|
55 | delete [] observables; |
---|
56 | delete [] utils; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | CognitiveRadioShell::CognitiveRadioShell(const char* radioConfig, int16_t p1, \ |
---|
61 | int16_t p2, int16_t p3) |
---|
62 | { |
---|
63 | LOG("Creating Cognitive Radio Shell.\n"); |
---|
64 | |
---|
65 | params = new Parameter[10]; |
---|
66 | observables = new Observable[10]; |
---|
67 | utils = new Utility[10]; |
---|
68 | radio_info = new Radio_Info; |
---|
69 | |
---|
70 | LoadRadioConfiguration(radioConfig, params, utils, observables, radio_info); |
---|
71 | |
---|
72 | primaryPort = p1; |
---|
73 | policyPort = p2; |
---|
74 | commandPort = p3; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | void |
---|
79 | CognitiveRadioShell::SendComponentType(int32_t socketFD) |
---|
80 | { |
---|
81 | SendMessage(socketFD, "response_shell"); |
---|
82 | LOG("Cognitive Radio Shell responded to GetRemoteComponentType query.\n"); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | std::string |
---|
87 | CognitiveRadioShell::GetRemoteComponentType(int32_t socketFD) |
---|
88 | { |
---|
89 | SendMessage(socketFD, "request_component_type"); |
---|
90 | |
---|
91 | char buffer[256]; |
---|
92 | memset(buffer, 0, 256); |
---|
93 | ReadMessage(socketFD, buffer); |
---|
94 | |
---|
95 | return std::string(buffer); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | void |
---|
100 | CognitiveRadioShell::Shutdown() |
---|
101 | { |
---|
102 | // TODO should something else be happening here? |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | void |
---|
107 | CognitiveRadioShell::Reset() |
---|
108 | { |
---|
109 | LOG("Resetting Cognitive Radio Shell.\n"); |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | // TODO what is the point of always returning a 1? |
---|
114 | bool |
---|
115 | CognitiveRadioShell::SendRadioConfiguration(int32_t socketFD) |
---|
116 | { |
---|
117 | LOG("Cognitive Radio Shell:: Sending radio configuration to Cognitive Engine.\n"); |
---|
118 | |
---|
119 | char counter[55]; |
---|
120 | char var[50]; |
---|
121 | |
---|
122 | /* Send utilities */ |
---|
123 | sprintf(counter, "%d", radio_info->numUtilities); |
---|
124 | SendMessage(socketFD, counter); |
---|
125 | LOG("Cognitive Radio Shell:: Sending radio configuration to Cognitive Engine. %s\n",counter); |
---|
126 | for(size_t i = 0; i < radio_info->numUtilities; i++) { |
---|
127 | SendMessage(socketFD, utils[i].name.c_str()); |
---|
128 | SendMessage(socketFD, utils[i].units.c_str()); |
---|
129 | SendMessage(socketFD, utils[i].goal.c_str()); |
---|
130 | sprintf(var,"%f", utils[i].target); |
---|
131 | SendMessage(socketFD, var); |
---|
132 | } |
---|
133 | |
---|
134 | /* Send parameters */ |
---|
135 | sprintf(counter,"%i",radio_info->numParameters); |
---|
136 | SendMessage(socketFD,counter); |
---|
137 | for(size_t i = 0; i < radio_info->numParameters; i++) { |
---|
138 | SendMessage(socketFD, params[i].name.c_str()); |
---|
139 | SendMessage(socketFD, params[i].units.c_str()); |
---|
140 | sprintf(var, "%f", params[i].min); |
---|
141 | SendMessage(socketFD,var); |
---|
142 | sprintf(var, "%f", params[i].max); |
---|
143 | SendMessage(socketFD, var); |
---|
144 | sprintf(var, "%f", params[i].step); |
---|
145 | SendMessage(socketFD, var); |
---|
146 | |
---|
147 | sprintf(counter, "%i", params[i].numAffects); |
---|
148 | SendMessage(socketFD, counter); |
---|
149 | for(size_t j = 0; j < params[i].numAffects; j++) { |
---|
150 | SendMessage(socketFD, params[i].affection_list[j].u->name.c_str()); |
---|
151 | SendMessage(socketFD, params[i].affection_list[j].relation.c_str()); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | /* Send observables */ |
---|
156 | sprintf(counter,"%i",radio_info->numObservables); |
---|
157 | SendMessage(socketFD, counter); |
---|
158 | for(size_t i = 0; i < radio_info->numObservables; i++) { |
---|
159 | SendMessage(socketFD, observables[i].name.c_str()); |
---|
160 | |
---|
161 | sprintf(counter, "%i", observables[i].numAffects); |
---|
162 | SendMessage(socketFD, counter); |
---|
163 | for(size_t j = 0; j < observables[i].numAffects; j++) { |
---|
164 | SendMessage(socketFD, observables[i].affection_list[j].u->name.c_str()); |
---|
165 | SendMessage(socketFD, observables[i].affection_list[j].relation.c_str()); |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | /* Receive ACK for radio configuration */ |
---|
170 | char buffer[256]; |
---|
171 | memset(buffer, 0, 256); |
---|
172 | ReadMessage(socketFD, buffer); |
---|
173 | |
---|
174 | if(strcmp(buffer, "receive_config_ack") != 0) { |
---|
175 | LOG("Cognitive Radio Shell:: Unexpected response: %s\n", buffer); |
---|
176 | return 0; |
---|
177 | } |
---|
178 | |
---|
179 | return 1; |
---|
180 | } |
---|
181 | |
---|
182 | bool |
---|
183 | CognitiveRadioShell::SendRadioExperience(int32_t socketFD) |
---|
184 | { |
---|
185 | |
---|
186 | LOG("Cognitive Radio Shell:: Sending radio experience to Cognitive Engine.\n"); |
---|
187 | int32_t numberExp = 4; |
---|
188 | char numberExpString[50]; |
---|
189 | |
---|
190 | sprintf(numberExpString, "%d", numberExp); |
---|
191 | SendMessage(socketFD, "test"); //TODO modified |
---|
192 | |
---|
193 | char buffer[256]; |
---|
194 | memset(buffer, 0, 256); |
---|
195 | ReadMessage(socketFD, buffer); |
---|
196 | if(strcmp(buffer, "receive_exp_ack") != 0) { |
---|
197 | // TODO perhaps this should be a WARNING instead of a LOG? |
---|
198 | LOG("Cognitive Radio Shell:: Unexpected response: %s\n", buffer); |
---|
199 | return 0; |
---|
200 | } |
---|
201 | //printf("done sending exp \n"); |
---|
202 | return 1; |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | void |
---|
207 | CognitiveRadioShell::RegisterCognitiveEngine(int32_t socketFD) |
---|
208 | { |
---|
209 | LOG("Cognitive Radio Shell:: Received registration message from Cognitive Engine.\n"); |
---|
210 | |
---|
211 | SendMessage(socketFD, "register_ack"); |
---|
212 | SendRadioConfiguration(socketFD); |
---|
213 | SendRadioExperience(socketFD); |
---|
214 | |
---|
215 | numberOfCognitiveEngines++; |
---|
216 | CE_present = true; |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | void |
---|
221 | CognitiveRadioShell::DeregisterCognitiveEngine(int32_t socketFD) |
---|
222 | { |
---|
223 | LOG("Cognitive Radio Shell:: Received deregistration message from Cognitive Engine.\n"); |
---|
224 | |
---|
225 | numberOfCognitiveEngines--; |
---|
226 | if(numberOfCognitiveEngines == 0) |
---|
227 | CE_present = false; |
---|
228 | |
---|
229 | SendMessage(socketFD, "deregister_ack"); |
---|
230 | shutdown(socketFD, 2); |
---|
231 | close(socketFD); |
---|
232 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | void |
---|
237 | CognitiveRadioShell::RegisterPolicyEngine(int32_t socketFD) |
---|
238 | { |
---|
239 | LOG("Cognitive Radio Shell:: Received registration message from Policy Engine.\n"); |
---|
240 | PE_present = true; |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | void |
---|
245 | CognitiveRadioShell::DeregisterPolicyEngine(int32_t socketFD) |
---|
246 | { |
---|
247 | LOG("Cognitive Radio Shell:: Received deregistration message from Policy Engine.\n"); |
---|
248 | |
---|
249 | PE_present = false; |
---|
250 | |
---|
251 | SendMessage(socketFD, "deregister_ack"); |
---|
252 | shutdown(socketFD, 2); |
---|
253 | close(socketFD); |
---|
254 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
255 | } |
---|
256 | |
---|
257 | |
---|
258 | void |
---|
259 | CognitiveRadioShell::RegisterSML(int32_t socketFD) |
---|
260 | { |
---|
261 | LOG("Cognitive Radio Shell:: Received registration message from SML.\n"); |
---|
262 | |
---|
263 | SML_present = true; |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | void |
---|
268 | CognitiveRadioShell::DeregisterSML(int32_t socketFD) |
---|
269 | { |
---|
270 | LOG("Cognitive Radio Shell:: Received deregistration message from SML.\n"); |
---|
271 | |
---|
272 | SML_present = false; |
---|
273 | |
---|
274 | SendMessage(socketFD, "deregister_ack"); |
---|
275 | shutdown(socketFD, 2); |
---|
276 | close(socketFD); |
---|
277 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
278 | } |
---|
279 | |
---|
280 | void |
---|
281 | CognitiveRadioShell::SetActiveMission(int32_t socketFD) |
---|
282 | { |
---|
283 | char buffer[256]; |
---|
284 | |
---|
285 | LOG("Cognitive Radio Shell:: Received Set Active Mission command from host.\n"); |
---|
286 | |
---|
287 | // Read the name of the active mission to be set from the host. |
---|
288 | memset(buffer, 0, 256); |
---|
289 | ReadMessage(commandSocketFD, buffer); |
---|
290 | |
---|
291 | // Send command to SML |
---|
292 | SendMessage(ceSocketFD, "set_active_mission"); |
---|
293 | SendMessage(ceSocketFD, buffer); |
---|
294 | |
---|
295 | // Get ack from SML saying the mission was set properly |
---|
296 | memset(buffer, 0, 256); |
---|
297 | ReadMessage(ceSocketFD, buffer); |
---|
298 | |
---|
299 | // Forward ack to host |
---|
300 | SendMessage(commandSocketFD, buffer); |
---|
301 | } |
---|
302 | |
---|
303 | int32_t |
---|
304 | CognitiveRadioShell::LoadRadioConfiguration(const char* radioConfig, \ |
---|
305 | Parameter* &pList, Utility* &uList, Observable* &oList, \ |
---|
306 | Radio_Info* radioInfo) |
---|
307 | { |
---|
308 | TiXmlElement *pElem; |
---|
309 | TiXmlElement *pChild; |
---|
310 | TiXmlElement *pChild1; |
---|
311 | TiXmlElement *pSecondChild; |
---|
312 | TiXmlHandle hRoot(0); |
---|
313 | |
---|
314 | int32_t count = 0; |
---|
315 | size_t item_count = 0; |
---|
316 | size_t affect_count = 0; |
---|
317 | uint32_t attribute_count = 0; |
---|
318 | bool match_found = false; |
---|
319 | |
---|
320 | LOG("Cognitive Radio Shell:: Loading radio configuration.\n"); |
---|
321 | |
---|
322 | TiXmlDocument doc( radioConfig ); |
---|
323 | bool loadOkay = doc.LoadFile(); |
---|
324 | if(!loadOkay) |
---|
325 | ERROR(1,"Loading radio configuration failed: %s\n", radioConfig); |
---|
326 | |
---|
327 | TiXmlHandle hDoc(&doc); |
---|
328 | |
---|
329 | pElem = hDoc.FirstChildElement().Element(); |
---|
330 | |
---|
331 | if(!pElem) |
---|
332 | ERROR(1, "No valid root!"); |
---|
333 | |
---|
334 | hRoot = TiXmlHandle(pElem); |
---|
335 | |
---|
336 | pElem = hRoot.FirstChild("utilities").Element(); |
---|
337 | pChild1 = hRoot.Child("utilities", count).Element(); |
---|
338 | |
---|
339 | for(pChild = pChild1->FirstChildElement("utility"); pChild; \ |
---|
340 | pChild = pChild->NextSiblingElement()) { |
---|
341 | |
---|
342 | const char *uName = pChild->Attribute("name"); |
---|
343 | if(uName) |
---|
344 | uList[item_count].name = uName; |
---|
345 | |
---|
346 | const char *uUnits = pChild->Attribute("units"); |
---|
347 | if(uUnits) |
---|
348 | uList[item_count].units = uUnits; |
---|
349 | |
---|
350 | const char *uGoal = pChild->Attribute("goal"); |
---|
351 | if(uGoal) |
---|
352 | uList[item_count].goal = uGoal; |
---|
353 | |
---|
354 | if(pChild->QueryFloatAttribute("target", &uList[item_count].target) != TIXML_SUCCESS) |
---|
355 | uList[item_count].target = -1; |
---|
356 | |
---|
357 | item_count++; |
---|
358 | } |
---|
359 | |
---|
360 | radio_info->numUtilities = item_count; |
---|
361 | LOG("Cognitive Radio Shell:: Parsed %d utilities.\n", radioInfo->numUtilities); |
---|
362 | |
---|
363 | item_count = 0; |
---|
364 | pElem = hRoot.FirstChild("observables").Element(); |
---|
365 | pChild1 = hRoot.Child("observables", count).Element(); |
---|
366 | |
---|
367 | for(pChild = pChild1->FirstChildElement("observable"); pChild; \ |
---|
368 | pChild = pChild->NextSiblingElement()) { |
---|
369 | |
---|
370 | const char *oName = pChild->Attribute("name"); |
---|
371 | if(oName) |
---|
372 | oList[item_count].name = oName; |
---|
373 | |
---|
374 | affect_count = 0; |
---|
375 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ |
---|
376 | pSecondChild = pSecondChild->NextSiblingElement()) { |
---|
377 | |
---|
378 | const char *oUtilName = pSecondChild->Attribute("utility"); |
---|
379 | if(oUtilName) { |
---|
380 | for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++ ) { |
---|
381 | if(uList[attribute_count].name == oUtilName) { |
---|
382 | |
---|
383 | oList[item_count].affection_list[affect_count].u = &uList[attribute_count]; |
---|
384 | const char *oRelate = pSecondChild->Attribute("relationship"); |
---|
385 | if(oRelate) |
---|
386 | oList[item_count].affection_list[affect_count].relation = oRelate; |
---|
387 | |
---|
388 | affect_count++; |
---|
389 | match_found = true; |
---|
390 | break; |
---|
391 | } |
---|
392 | } |
---|
393 | } |
---|
394 | |
---|
395 | if(!match_found) { |
---|
396 | ERROR(1, "Error: %s: %s is not a valid utility.\n", \ |
---|
397 | oList[item_count].name.c_str(), oUtilName); |
---|
398 | } |
---|
399 | else |
---|
400 | match_found = false; |
---|
401 | } |
---|
402 | oList[item_count].numAffects = affect_count; |
---|
403 | item_count++; |
---|
404 | } |
---|
405 | |
---|
406 | radioInfo->numObservables = item_count; |
---|
407 | LOG("Cognitive Radio Shell:: Parsed %d observables.\n", radioInfo->numObservables); |
---|
408 | |
---|
409 | pElem = hRoot.FirstChild("parameters").Element(); |
---|
410 | pChild1 = hRoot.Child("parameters", count).Element(); |
---|
411 | |
---|
412 | item_count = 0; |
---|
413 | for(pChild = pChild1->FirstChildElement("parameter"); pChild; \ |
---|
414 | pChild = pChild->NextSiblingElement()) { |
---|
415 | |
---|
416 | const char *pName = pChild->Attribute("name"); |
---|
417 | if(pName) |
---|
418 | pList[item_count].name = pName; |
---|
419 | |
---|
420 | const char *pUnits = pChild->Attribute("units"); |
---|
421 | if(pUnits) |
---|
422 | pList[item_count].units = pUnits; |
---|
423 | |
---|
424 | if(pChild->QueryFloatAttribute("min", &pList[item_count].min) != TIXML_SUCCESS) |
---|
425 | pList[item_count].min = -1; |
---|
426 | |
---|
427 | if(pChild->QueryFloatAttribute("max", &pList[item_count].max) != TIXML_SUCCESS) |
---|
428 | pList[item_count].max = -1; |
---|
429 | |
---|
430 | if(pChild->QueryFloatAttribute("step", &pList[item_count].step) != TIXML_SUCCESS) |
---|
431 | pList[item_count].step = -1; |
---|
432 | |
---|
433 | affect_count = 0; |
---|
434 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ |
---|
435 | |
---|
436 | pSecondChild = pSecondChild->NextSiblingElement()) { |
---|
437 | const char *pUtilName = pSecondChild->Attribute("utility"); |
---|
438 | if(pUtilName) { |
---|
439 | for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++) { |
---|
440 | if(uList[attribute_count].name == pUtilName) { |
---|
441 | pList[item_count].affection_list[affect_count].u = &uList[attribute_count]; |
---|
442 | |
---|
443 | const char *pRelate = pSecondChild->Attribute("relationship"); |
---|
444 | if(pRelate) |
---|
445 | pList[item_count].affection_list[affect_count].relation = pRelate; |
---|
446 | else |
---|
447 | LOG("Error: No relation found.\n"); |
---|
448 | |
---|
449 | match_found = true; |
---|
450 | affect_count++; |
---|
451 | break; |
---|
452 | } |
---|
453 | } |
---|
454 | } |
---|
455 | |
---|
456 | if(!match_found) { |
---|
457 | ERROR(1, "Error: %s: %s is not a valid utility.\n", \ |
---|
458 | pList[item_count].name.c_str(), pUtilName); |
---|
459 | } |
---|
460 | |
---|
461 | match_found = false; |
---|
462 | } |
---|
463 | |
---|
464 | pList[item_count].numAffects = affect_count; |
---|
465 | item_count++; |
---|
466 | } |
---|
467 | |
---|
468 | radioInfo->numParameters = item_count; |
---|
469 | LOG("Cognitive Radio Shell:: Parsed %d parameters.\n", radioInfo->numParameters); |
---|
470 | |
---|
471 | return 1; |
---|
472 | } |
---|
473 | |
---|
474 | |
---|
475 | void |
---|
476 | CognitiveRadioShell::GetOptimalParameters(int32_t socketFD) |
---|
477 | { |
---|
478 | char buffer[256]; |
---|
479 | char counter[55]; |
---|
480 | char var[50]; |
---|
481 | |
---|
482 | /* Receive Set of Observables */ |
---|
483 | LOG("Cognitive Radio Shell:: Got request for optimization.\n"); |
---|
484 | memset(buffer, 0, 256); |
---|
485 | ReadMessage(commandSocketFD, buffer); |
---|
486 | uint32_t numObservables = atoi(buffer); |
---|
487 | |
---|
488 | LOG("Cognitive Radio Shell:: Attempting to get %i observables.\n", numObservables); |
---|
489 | Observable *o = new Observable[numObservables]; |
---|
490 | |
---|
491 | for(size_t i = 0; i < numObservables; i++) { |
---|
492 | memset(buffer, 0, 256); |
---|
493 | ReadMessage(commandSocketFD, buffer); |
---|
494 | o[i].name = std::string(buffer); |
---|
495 | |
---|
496 | memset(buffer, 0, 256); |
---|
497 | ReadMessage(commandSocketFD, buffer); |
---|
498 | o[i].value = atof(buffer); |
---|
499 | } |
---|
500 | |
---|
501 | /* Receive Set of Current Parameters */ |
---|
502 | memset(buffer, 0, 256); |
---|
503 | ReadMessage(commandSocketFD,buffer); |
---|
504 | uint32_t numCurrentParameters = atoi(buffer); |
---|
505 | |
---|
506 | LOG("Cognitive Radio Shell:: Attempting to get %i parameters.\n",numCurrentParameters); |
---|
507 | Parameter * cp = new Parameter[numCurrentParameters]; |
---|
508 | |
---|
509 | for (size_t i = 0; i < numCurrentParameters; i++){ |
---|
510 | memset(buffer, 0, 256); |
---|
511 | ReadMessage(commandSocketFD,buffer); |
---|
512 | cp[i].name = std::string(buffer); |
---|
513 | memset(buffer, 0, 256); |
---|
514 | ReadMessage(commandSocketFD,buffer); |
---|
515 | cp[i].value = atof(buffer); |
---|
516 | } |
---|
517 | |
---|
518 | /* Send to Cognitive Engine |
---|
519 | * TODO: With multiple CEs we need to make a decision about where |
---|
520 | * to send this information |
---|
521 | */ |
---|
522 | LOG("Cognitive Radio Shell:: Passing on observables.\n"); |
---|
523 | SendMessage(ceSocketFD,"request_optimization"); |
---|
524 | sprintf(counter,"%i",numObservables); |
---|
525 | SendMessage(ceSocketFD,counter); |
---|
526 | for(size_t i = 0; i < numObservables; i++) { |
---|
527 | SendMessage(ceSocketFD,o[i].name.c_str()); |
---|
528 | sprintf(var,"%f",o[i].value); |
---|
529 | SendMessage(ceSocketFD,var); |
---|
530 | } |
---|
531 | |
---|
532 | LOG("Cognitive Radio Shell:: Passing on current parameters.\n"); |
---|
533 | sprintf(counter,"%i",numCurrentParameters); |
---|
534 | SendMessage(ceSocketFD,counter); |
---|
535 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
536 | SendMessage(ceSocketFD,cp[i].name.c_str()); |
---|
537 | sprintf(var,"%f",cp[i].value); |
---|
538 | SendMessage(ceSocketFD,var); |
---|
539 | } |
---|
540 | |
---|
541 | LOG("Cognitive Radio Shell:: Receiving optimized parameters.\n"); |
---|
542 | /* Receive Set of Parameters */ |
---|
543 | memset(buffer, 0, 256); |
---|
544 | ReadMessage(ceSocketFD, buffer); |
---|
545 | uint32_t numParameters = atoi(buffer); |
---|
546 | |
---|
547 | Parameter *p = new Parameter[numParameters]; |
---|
548 | |
---|
549 | for(size_t i = 0; i < numParameters; i++) { |
---|
550 | memset(buffer, 0, 256); |
---|
551 | ReadMessage(ceSocketFD, buffer); |
---|
552 | p[i].name = std::string(buffer); |
---|
553 | |
---|
554 | memset(buffer, 0, 256); |
---|
555 | ReadMessage(ceSocketFD, buffer); |
---|
556 | p[i].value = atof(buffer); |
---|
557 | } |
---|
558 | |
---|
559 | /* Send to Application |
---|
560 | */ |
---|
561 | LOG("Cognitive Radio Shell:: Sending optimized parameters to Application.\n"); |
---|
562 | memset(counter, 0, 55); |
---|
563 | sprintf(counter, "%i", numParameters); |
---|
564 | SendMessage(commandSocketFD, counter); |
---|
565 | for(size_t i = 0; i < numParameters; i++) { |
---|
566 | SendMessage(commandSocketFD, p[i].name.c_str()); |
---|
567 | sprintf(var, "%f", p[i].value); |
---|
568 | SendMessage(commandSocketFD, var); |
---|
569 | } |
---|
570 | |
---|
571 | delete [] o; |
---|
572 | delete [] p; |
---|
573 | } |
---|
574 | |
---|
575 | // TODO point of always returning 1? |
---|
576 | bool |
---|
577 | CognitiveRadioShell::UpdateParameterPerformance(int32_t socketFD) |
---|
578 | { |
---|
579 | char counter[55]; |
---|
580 | char var[50]; |
---|
581 | char buffer[256]; |
---|
582 | |
---|
583 | /* Receive Set of Parameters */ |
---|
584 | memset(buffer, 0, 256); |
---|
585 | ReadMessage(commandSocketFD,buffer); |
---|
586 | uint32_t numParameters = atoi(buffer); |
---|
587 | |
---|
588 | Parameter *p = new Parameter[numParameters]; |
---|
589 | |
---|
590 | for (size_t i = 0; i < numParameters; i++){ |
---|
591 | memset(buffer, 0, 256); |
---|
592 | ReadMessage(commandSocketFD,buffer); |
---|
593 | p[i].name = std::string(buffer); |
---|
594 | memset(buffer, 0, 256); |
---|
595 | ReadMessage(commandSocketFD,buffer); |
---|
596 | p[i].value = atof(buffer); |
---|
597 | } |
---|
598 | |
---|
599 | /* Receive Set of Observables */ |
---|
600 | memset(buffer, 0, 256); |
---|
601 | ReadMessage(commandSocketFD, buffer); |
---|
602 | uint32_t numObservables = atoi(buffer); |
---|
603 | |
---|
604 | Observable *o = new Observable[numObservables]; |
---|
605 | |
---|
606 | for(size_t i = 0; i < numObservables; i++) { |
---|
607 | memset(buffer, 0, 256); |
---|
608 | ReadMessage(commandSocketFD, buffer); |
---|
609 | o[i].name = std::string(buffer); |
---|
610 | |
---|
611 | memset(buffer, 0, 256); |
---|
612 | ReadMessage(commandSocketFD, buffer); |
---|
613 | o[i].value = atof(buffer); |
---|
614 | } |
---|
615 | |
---|
616 | SendMessage(ceSocketFD, "update_performance"); |
---|
617 | |
---|
618 | /* Send Parameters */ |
---|
619 | memset(counter, 0, 55); |
---|
620 | sprintf(counter, "%i", numParameters); |
---|
621 | SendMessage(ceSocketFD, counter); |
---|
622 | |
---|
623 | for(size_t i = 0; i < numParameters; i++) { |
---|
624 | SendMessage(ceSocketFD,p[i].name.c_str()); |
---|
625 | sprintf(var,"%f",p[i].value); |
---|
626 | SendMessage(ceSocketFD,var); |
---|
627 | } |
---|
628 | |
---|
629 | /* Send Observables */ |
---|
630 | sprintf(counter, "%i", numObservables); |
---|
631 | SendMessage(ceSocketFD, counter); |
---|
632 | for(size_t i = 0; i < numObservables; i++) { |
---|
633 | SendMessage(ceSocketFD, o[i].name.c_str()); |
---|
634 | sprintf(var, "%f", o[i].value); |
---|
635 | SendMessage(ceSocketFD, var); |
---|
636 | } |
---|
637 | |
---|
638 | delete [] p; |
---|
639 | delete [] o; |
---|
640 | |
---|
641 | return 1; |
---|
642 | } |
---|
643 | |
---|
644 | |
---|
645 | void |
---|
646 | CognitiveRadioShell::HandleMessage(int32_t socketFD) |
---|
647 | { |
---|
648 | char buffer[256]; |
---|
649 | //printf("here\n"); |
---|
650 | ReadMessage(socketFD, buffer); |
---|
651 | |
---|
652 | // TODO trying to read this code lock makes my eyes bleed |
---|
653 | if(strcmp(buffer, "register_engine_cognitive") == 0) { |
---|
654 | RegisterCognitiveEngine(socketFD); |
---|
655 | } else if(strcmp(buffer, "deregister_engine_cognitive") == 0) { |
---|
656 | DeregisterCognitiveEngine(socketFD); |
---|
657 | } else if(strcmp(buffer, "register_engine_policy") == 0) { |
---|
658 | RegisterPolicyEngine(socketFD); |
---|
659 | } else if(strcmp(buffer, "deregister_engine_policy") == 0) { |
---|
660 | DeregisterPolicyEngine(socketFD); |
---|
661 | } else if(strcmp(buffer, "register_sml") == 0) { |
---|
662 | RegisterSML(socketFD); |
---|
663 | } else if(strcmp(buffer, "deregister_sml") == 0) { |
---|
664 | DeregisterSML(socketFD); |
---|
665 | } else if(strcmp(buffer, "update_performance") == 0) { |
---|
666 | UpdateParameterPerformance(socketFD); |
---|
667 | } else if(strcmp(buffer, "get_number_utilities") == 0) { |
---|
668 | char numUtilities[20]; |
---|
669 | sprintf(numUtilities, "%i", radio_info->numUtilities); |
---|
670 | SendMessage(commandSocketFD, numUtilities); |
---|
671 | } else if(strcmp(buffer, "get_number_observables") == 0) { |
---|
672 | char numObservables[20]; |
---|
673 | sprintf(numObservables, "%i", radio_info->numObservables); |
---|
674 | SendMessage(commandSocketFD, numObservables); |
---|
675 | } else if(strcmp(buffer, "get_number_parameters") == 0) { |
---|
676 | char numParameters[20]; |
---|
677 | sprintf(numParameters, "%i", radio_info->numParameters); |
---|
678 | SendMessage(commandSocketFD, numParameters); |
---|
679 | } else if(strcmp(buffer, "request_optimization") == 0) { |
---|
680 | /* Receive optimization request and current environment */ |
---|
681 | GetOptimalParameters(socketFD); |
---|
682 | } else if(strcmp(buffer, "set_active_mission") == 0) { |
---|
683 | SetActiveMission(socketFD); |
---|
684 | } else if(strcmp(buffer, "request_optimization_service") == 0) { |
---|
685 | /* Receive optimization request and current environment */ |
---|
686 | //GetOptimalParametersService(socketFD); |
---|
687 | } |
---|
688 | } |
---|
689 | |
---|
690 | |
---|
691 | void |
---|
692 | CognitiveRadioShell::StartShellServer() |
---|
693 | { |
---|
694 | struct timeval selTimeout; |
---|
695 | int32_t primary = 0; |
---|
696 | int32_t policy = 1; |
---|
697 | int32_t command = 2; |
---|
698 | int32_t running = 1; |
---|
699 | int32_t port, rc, new_sd = 1; |
---|
700 | int32_t desc_ready = 1; |
---|
701 | int32_t timeout = 10; |
---|
702 | fd_set sockSet; |
---|
703 | |
---|
704 | int32_t *servSock = new int32_t[3]; |
---|
705 | |
---|
706 | servSock[primary] = CreateTCPServerSocket(primaryPort); |
---|
707 | servSock[policy] = CreateTCPServerSocket(policyPort); |
---|
708 | servSock[command] = CreateTCPServerSocket(commandPort); |
---|
709 | |
---|
710 | int32_t maxDescriptor; |
---|
711 | |
---|
712 | if(servSock[primary] > servSock[policy]) |
---|
713 | maxDescriptor = servSock[primary]; |
---|
714 | else |
---|
715 | maxDescriptor = servSock[policy]; |
---|
716 | |
---|
717 | if(servSock[command] > maxDescriptor) |
---|
718 | maxDescriptor = servSock[command]; |
---|
719 | |
---|
720 | if(InitializeTCPServerPort(servSock[primary]) == -1) |
---|
721 | ERROR(1,"Error initializing primary port\n"); |
---|
722 | |
---|
723 | if(InitializeTCPServerPort(servSock[policy]) == -1) |
---|
724 | ERROR(1,"Error initializing policy port\n"); |
---|
725 | |
---|
726 | if(InitializeTCPServerPort(servSock[command]) == -1) |
---|
727 | ERROR(1,"Error initializing command port\n"); |
---|
728 | |
---|
729 | while (running) { |
---|
730 | /* Zero socket descriptor vector and set for server sockets */ |
---|
731 | /* This must be reset every time select() is called */ |
---|
732 | //FD_ZERO(&sockSet); |
---|
733 | FD_SET(servSock[primary], &sockSet); |
---|
734 | FD_SET(servSock[policy], &sockSet); |
---|
735 | FD_SET(servSock[command], &sockSet); |
---|
736 | |
---|
737 | /* Timeout specification */ |
---|
738 | /* This must be reset every time select() is called */ |
---|
739 | selTimeout.tv_sec = timeout; /* timeout (secs.) */ |
---|
740 | selTimeout.tv_usec = 0; /* 0 microseconds */ |
---|
741 | |
---|
742 | /* Suspend program until descriptor is ready or timeout */ |
---|
743 | rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
744 | if(rc == 0) |
---|
745 | LOG("No echo requests for %i secs...Server still alive\n", timeout); |
---|
746 | else { |
---|
747 | desc_ready = rc; |
---|
748 | |
---|
749 | for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) { |
---|
750 | if(FD_ISSET(port, &sockSet)) { |
---|
751 | desc_ready -= 1; |
---|
752 | /* Check if request is new or on an existing open descriptor */ |
---|
753 | if((port == servSock[primary]) || (port == servSock[policy]) || (port == servSock[command])) { |
---|
754 | do { |
---|
755 | new_sd = AcceptTCPConnection(port); |
---|
756 | if(new_sd < 0) |
---|
757 | break; |
---|
758 | |
---|
759 | if(port == servSock[primary]) |
---|
760 | ceSocketFD = new_sd; |
---|
761 | if(port == servSock[command]) |
---|
762 | commandSocketFD = new_sd; |
---|
763 | if(port == servSock[policy]) |
---|
764 | policySocketFD = new_sd; |
---|
765 | //printf("%d %d %d :: %d\n", ceSocketFD, commandSocketFD, policySocketFD, new_sd); |
---|
766 | |
---|
767 | HandleMessage(new_sd); |
---|
768 | FD_SET(new_sd,&sockSet); |
---|
769 | if(new_sd > maxDescriptor) |
---|
770 | maxDescriptor = new_sd; |
---|
771 | //LOG("New incoming connection - %i\n\n",new_sd); |
---|
772 | } while(new_sd != -1); |
---|
773 | } |
---|
774 | else { |
---|
775 | //LOG("Request on already open descriptor.\n\n"); |
---|
776 | HandleMessage(port); |
---|
777 | } |
---|
778 | } |
---|
779 | } |
---|
780 | } |
---|
781 | } |
---|
782 | |
---|
783 | |
---|
784 | /* Close sockets */ |
---|
785 | close(servSock[primary]); |
---|
786 | close(servSock[policy]); |
---|
787 | close(servSock[command]); |
---|
788 | |
---|
789 | /* Free list of sockets */ |
---|
790 | delete servSock; |
---|
791 | |
---|
792 | return; |
---|
793 | } |
---|