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