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