1 | /* Virginia Tech Cognitive Radio Open Source Systems |
---|
2 | * Virginia Tech, 2009 |
---|
3 | * |
---|
4 | * LICENSE INFORMATION GOES HERE |
---|
5 | */ |
---|
6 | |
---|
7 | /*! This CE was written to extend the functionality of a basic CBR-based CE for |
---|
8 | * use with the OSSIE demonstration. |
---|
9 | */ |
---|
10 | |
---|
11 | |
---|
12 | #include <cmath> |
---|
13 | #include <cstdlib> |
---|
14 | #include <cstring> |
---|
15 | #include <stdint.h> |
---|
16 | #include <string> |
---|
17 | |
---|
18 | #include "vtcross/cbr.h" |
---|
19 | #include "vtcross/cognitive_engine.h" |
---|
20 | #include "vtcross/common.h" |
---|
21 | #include "vtcross/containers.h" |
---|
22 | #include "vtcross/debug.h" |
---|
23 | #include "vtcross/error.h" |
---|
24 | #include "vtcross/socketcomm.h" |
---|
25 | |
---|
26 | |
---|
27 | #define TXPOWER 1 |
---|
28 | #define BER_ENV 1 |
---|
29 | #define BER_OBJ 1 |
---|
30 | |
---|
31 | #define DECREMENTSCALE 5 |
---|
32 | #define INCREMENTSCALE 5 |
---|
33 | |
---|
34 | #define EVF 20 |
---|
35 | |
---|
36 | |
---|
37 | /* ossieCBR Class |
---|
38 | * |
---|
39 | * Derives from the default VTCROSS CBR class to add the following functionality |
---|
40 | * necessary for the OSSIE demo: |
---|
41 | * |
---|
42 | * TODO |
---|
43 | */ |
---|
44 | class ossieCBR : public CBR |
---|
45 | { |
---|
46 | public: |
---|
47 | ossieCBR(); |
---|
48 | |
---|
49 | ossieCBR(string _filename, string _tablename, string _cols[], uint32_t _len); |
---|
50 | |
---|
51 | ~ossieCBR(){}; |
---|
52 | |
---|
53 | int32_t Update(string _where[], string _set[], float *_wherevals, float *_setvals, \ |
---|
54 | unsigned int _wherelen, unsigned int _setlen); |
---|
55 | |
---|
56 | int32_t Search(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \ |
---|
57 | float *_retvals, int32_t evf); |
---|
58 | }; |
---|
59 | |
---|
60 | |
---|
61 | ossieCBR::ossieCBR(string _filename, string _tablename, string _cols[], uint32_t _len) |
---|
62 | { |
---|
63 | /* Store database properties. */ |
---|
64 | filename = _filename; |
---|
65 | tablename = _tablename; |
---|
66 | numColumns = _len; |
---|
67 | |
---|
68 | /* Create the database (or open it if it already exists). */ |
---|
69 | OpenDatabase(); |
---|
70 | |
---|
71 | /* Generate the command that will create the initial table within the |
---|
72 | * VTCROSS database. */ |
---|
73 | command = "CREATE TABLE " + tablename + "("; |
---|
74 | for(size_t i = 0; i < numColumns; i++) { |
---|
75 | command += _cols[i] + " FLOAT"; |
---|
76 | |
---|
77 | /* If this column is not the last entry, add a comma to the command in |
---|
78 | * preperation for the next entry. */ |
---|
79 | if(i != numColumns - 1) |
---|
80 | command += ", "; |
---|
81 | } |
---|
82 | command += ", timestamp DATE, PRIMARY KEY(tx_power));"; |
---|
83 | |
---|
84 | /* Execute the generated command. At this point, the database is ready for |
---|
85 | * use. */ |
---|
86 | ExecuteCommand(); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | int32_t |
---|
91 | ossieCBR::Search(string _names[], int32_t *_ops, float *_vals, uint32_t _n, \ |
---|
92 | float *_retvals, int32_t evf) |
---|
93 | { |
---|
94 | float lower_limit; |
---|
95 | float upper_limit; |
---|
96 | char str_buffer[64]; |
---|
97 | |
---|
98 | command = "select " + tablename + ".* from " + tablename + " where "; |
---|
99 | |
---|
100 | for(size_t i = 0; i < _n; i++) { |
---|
101 | /* Make sure that the passed ops value is valid. */ |
---|
102 | if((_ops[i] < 0) || (_ops[i] > 5)) { |
---|
103 | ERROR(1, "Error: cbr_search(), invalid ops id : %d\n", _ops[i]); |
---|
104 | } |
---|
105 | |
---|
106 | command += _names[i] + " between "; |
---|
107 | |
---|
108 | lower_limit = _vals[i] * (1 - ((float) evf / 100)); |
---|
109 | upper_limit = _vals[i] * (1 + ((float) evf / 100)); |
---|
110 | |
---|
111 | LOG("%f %f %f\n", lower_limit, upper_limit, _vals[i]); |
---|
112 | |
---|
113 | sprintf(str_buffer, "%f", lower_limit); |
---|
114 | command += string(str_buffer) + " and "; |
---|
115 | sprintf(str_buffer, "%f", upper_limit); |
---|
116 | command += string(str_buffer); |
---|
117 | |
---|
118 | if(i < _n - 1) |
---|
119 | command += " AND "; |
---|
120 | else |
---|
121 | command += " order by abs(utility) asc, timestamp desc;"; |
---|
122 | } |
---|
123 | |
---|
124 | LOG("Search command: %s\n", command.c_str()); |
---|
125 | |
---|
126 | return ExecuteSearchCommand(_retvals); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | int32_t |
---|
131 | ossieCBR::Update(string _where[], string _set[], float *_wherevals, float *_setvals, |
---|
132 | uint32_t _wherelen, uint32_t _setlen) |
---|
133 | { |
---|
134 | char str_buffer[64]; |
---|
135 | |
---|
136 | command = "UPDATE " + tablename + " SET "; |
---|
137 | |
---|
138 | for(size_t i = 0; i < _setlen; i++) { |
---|
139 | command += _set[i] + " = "; |
---|
140 | |
---|
141 | sprintf(str_buffer, "%f", _setvals[i]); |
---|
142 | command += string(str_buffer) + " "; |
---|
143 | |
---|
144 | if(i != _setlen - 1) |
---|
145 | command += ", "; |
---|
146 | } |
---|
147 | |
---|
148 | command += ", timestamp = DATETIME('NOW') WHERE "; |
---|
149 | |
---|
150 | for(size_t j = 0; j < _wherelen; j++) { |
---|
151 | command += _where[j] + " = "; |
---|
152 | |
---|
153 | sprintf(str_buffer, "%f", _wherevals[j]); |
---|
154 | command += string(str_buffer) + " "; |
---|
155 | |
---|
156 | if(j != _wherelen - 1) |
---|
157 | command += "AND "; |
---|
158 | } |
---|
159 | command += ";"; |
---|
160 | |
---|
161 | return ExecuteCommand(); |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | /******************** END DEFINITION OF OSSIE CBR CLASS ****************/ |
---|
166 | |
---|
167 | |
---|
168 | class OSSIE_CE : public CognitiveEngine |
---|
169 | { |
---|
170 | public: |
---|
171 | /*! Default constructor. */ |
---|
172 | OSSIE_CE() : CognitiveEngine(){}; |
---|
173 | |
---|
174 | /*! Default destructor. */ |
---|
175 | ~OSSIE_CE(); |
---|
176 | |
---|
177 | /*! \brief Preferred constructor. |
---|
178 | * |
---|
179 | * Overloaded constructor that creates a cognitive engine object and |
---|
180 | * connects it to either the shell or an SML, depening on the SML bool. |
---|
181 | * |
---|
182 | * The 'numFields' parameter sets how large the parameter, observable, |
---|
183 | * and utility arrays should be upon instantiation. |
---|
184 | */ |
---|
185 | OSSIE_CE(const char* serverName, const char* serverPort, \ |
---|
186 | const int32_t numFields, const bool SML) \ |
---|
187 | : CognitiveEngine(serverName, serverPort, numFields, SML){} |
---|
188 | |
---|
189 | |
---|
190 | void RegisterServices(); |
---|
191 | void DeregisterServices(); |
---|
192 | |
---|
193 | /*! \brief Request that the CE optimize a set of parameters. |
---|
194 | * |
---|
195 | * Find the most optimal set of transmission parameters given certain |
---|
196 | * observables and possibly a service if the SML component is present |
---|
197 | * and active. */ |
---|
198 | Parameter *GetSolution(Observable *observables, \ |
---|
199 | Parameter *currentParameters); |
---|
200 | Parameter *GetSolution(Observable *observables, \ |
---|
201 | Parameter *currentParameters, std::string service); |
---|
202 | |
---|
203 | /*! \brief Receive feedback from the radio |
---|
204 | * |
---|
205 | * Receive a feedback from the radio regarding the performance of a |
---|
206 | * certain set of parameters, possibly associated with a service. |
---|
207 | * |
---|
208 | * Feedback is a single set of performance statistics that is achieved |
---|
209 | * corresponding to a specific set of transmission parameters. Feedback |
---|
210 | * helps a Cognitive Engine make better future decisions based upon |
---|
211 | * more accurate performance statistics. |
---|
212 | */ |
---|
213 | void ReceiveFeedback(Observable *observables,Parameter *parameters); |
---|
214 | void ReceiveFeedback(Observable *observables, Parameter *parameters, \ |
---|
215 | std::string service); |
---|
216 | |
---|
217 | |
---|
218 | /*! \brief Initialize the CE and prepare it for operation. |
---|
219 | * |
---|
220 | * BuildCognitiveEngine performs the CE implementation specific work |
---|
221 | * that defines the internals of a CE. For example, a CBR CE engine |
---|
222 | * would build the case-base reasoner or create the database, a neural |
---|
223 | * network based CE may perform the initial training, a GA based CE |
---|
224 | * may build the chromosome structure. |
---|
225 | */ |
---|
226 | void BuildCognitiveEngine(); |
---|
227 | |
---|
228 | /*! \brief Each of these functions responds to a specific command. |
---|
229 | * |
---|
230 | * These functions are left principally un-implemented. It is the duty |
---|
231 | * of child classes to implement these functions, as they define the |
---|
232 | * cognitive engine's functionality. |
---|
233 | */ |
---|
234 | void PerformUpdatePerformance(); |
---|
235 | void PerformRequestOptimizationService(); |
---|
236 | void PerformRequestOptimization(); |
---|
237 | void PerformQueryComponentType(); |
---|
238 | void PerformConnectSML(); |
---|
239 | void PerformDisconnectSML(); |
---|
240 | void PerformResetEngineCognitive(); |
---|
241 | void PerformShutdownEngineCognitive(); |
---|
242 | |
---|
243 | ossieCBR *myCBR; |
---|
244 | }; |
---|
245 | |
---|
246 | |
---|
247 | OSSIE_CE::~OSSIE_CE() |
---|
248 | { |
---|
249 | delete myCBR; |
---|
250 | |
---|
251 | delete [] pList; |
---|
252 | delete [] oList; |
---|
253 | delete [] uList; |
---|
254 | delete [] radioInfo; |
---|
255 | } |
---|
256 | |
---|
257 | // This function needs serious help and is very confusing |
---|
258 | void |
---|
259 | OSSIE_CE::ReceiveFeedback(Observable *observables, Parameter *parameters) |
---|
260 | { |
---|
261 | LOG("Cognitive Engine:: Receiving feedback.\n"); |
---|
262 | |
---|
263 | uint32_t numberColumns = radioInfo->numParameters; |
---|
264 | |
---|
265 | uint32_t utilColumns = radioInfo->numUtilities + 1; |
---|
266 | |
---|
267 | float valList[numberColumns]; |
---|
268 | float newUtilityVals[numberColumns]; |
---|
269 | string nameList[numberColumns]; |
---|
270 | string utilList[utilColumns]; |
---|
271 | |
---|
272 | size_t columnUtilIndex = 0; |
---|
273 | for (size_t i = 0; i < radioInfo->numUtilities; i++){ |
---|
274 | utilList[columnUtilIndex] = uList[i].name; |
---|
275 | columnUtilIndex++; |
---|
276 | } |
---|
277 | utilList[columnUtilIndex] = "utility"; |
---|
278 | |
---|
279 | size_t columnIndex = 0; |
---|
280 | for (size_t i = 0; i < radioInfo->numParameters; i++){ |
---|
281 | nameList[columnIndex] = parameters[i].name; |
---|
282 | columnIndex++; |
---|
283 | } |
---|
284 | |
---|
285 | size_t newUtilityValueIndex = 0; |
---|
286 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
287 | |
---|
288 | /* This is a special case because the observable is also the utility. */ |
---|
289 | newUtilityVals[newUtilityValueIndex] = observables[i].value; |
---|
290 | newUtilityValueIndex++; |
---|
291 | } |
---|
292 | |
---|
293 | /* Calculate Utility */ |
---|
294 | float newUtilityValue = 0; |
---|
295 | |
---|
296 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
297 | newUtilityValue = newUtilityValue + (uList[i].target - observables[i].value); |
---|
298 | } |
---|
299 | newUtilityVals[newUtilityValueIndex] = newUtilityValue; |
---|
300 | |
---|
301 | size_t returnValueIndex = 0; |
---|
302 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
303 | valList[returnValueIndex] = parameters[i].value; |
---|
304 | returnValueIndex++; |
---|
305 | } |
---|
306 | |
---|
307 | myCBR->Update(nameList, utilList, valList, newUtilityVals, |
---|
308 | numberColumns, utilColumns); |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | void |
---|
313 | OSSIE_CE::ReceiveFeedback(Observable *observables, Parameter *parameters, \ |
---|
314 | std::string service) |
---|
315 | { |
---|
316 | LOG("Cognitive Engine:: Receiving feedback.\n"); |
---|
317 | } |
---|
318 | |
---|
319 | |
---|
320 | void |
---|
321 | OSSIE_CE::RegisterServices() |
---|
322 | { |
---|
323 | LOG("Cognitive Engine:: Registering services.\n"); |
---|
324 | |
---|
325 | SendMessage(commandSocketFD, "register_service"); |
---|
326 | SendMessage(commandSocketFD, "test_srv"); |
---|
327 | |
---|
328 | SendMessage(commandSocketFD, "register_service"); |
---|
329 | SendMessage(commandSocketFD, "test_srv1"); |
---|
330 | |
---|
331 | SendMessage(commandSocketFD, "register_service"); |
---|
332 | SendMessage(commandSocketFD, "test_srv2"); |
---|
333 | |
---|
334 | SendMessage(commandSocketFD, "register_service"); |
---|
335 | SendMessage(commandSocketFD, "test_srv3"); |
---|
336 | |
---|
337 | } |
---|
338 | |
---|
339 | |
---|
340 | void |
---|
341 | OSSIE_CE::DeregisterServices() |
---|
342 | { |
---|
343 | LOG("Cognitive Engine:: Deregistering services.\n"); |
---|
344 | |
---|
345 | SendMessage(commandSocketFD, "deregister_service"); |
---|
346 | SendMessage(commandSocketFD, "test_srv"); |
---|
347 | |
---|
348 | SendMessage(commandSocketFD, "deregister_service"); |
---|
349 | SendMessage(commandSocketFD, "test_srv1"); |
---|
350 | |
---|
351 | SendMessage(commandSocketFD, "deregister_service"); |
---|
352 | SendMessage(commandSocketFD, "test_srv2"); |
---|
353 | |
---|
354 | SendMessage(commandSocketFD, "deregister_service"); |
---|
355 | SendMessage(commandSocketFD, "test_srv3"); |
---|
356 | |
---|
357 | } |
---|
358 | |
---|
359 | |
---|
360 | Parameter* |
---|
361 | OSSIE_CE::GetSolution(Observable *observables, Parameter *currentParameters) |
---|
362 | { |
---|
363 | LOG("Cognitive Engine:: Generating solution.\n"); |
---|
364 | |
---|
365 | string searchNames[radioInfo->numObservables]; |
---|
366 | |
---|
367 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
368 | searchNames[i] = observables[i].name; |
---|
369 | } |
---|
370 | |
---|
371 | float searchVals[radioInfo->numObservables]; |
---|
372 | |
---|
373 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
374 | searchVals[i] = observables[i].value; |
---|
375 | } |
---|
376 | |
---|
377 | uint32_t numberColumns = radioInfo->numUtilities + radioInfo->numParameters + \ |
---|
378 | radioInfo->numObservables + 1; |
---|
379 | |
---|
380 | float returnValues[numberColumns]; |
---|
381 | |
---|
382 | int searchOps[radioInfo->numUtilities]; |
---|
383 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
384 | |
---|
385 | /* If the goal is to maximum, set the search operation to |
---|
386 | * return values greater than the target. |
---|
387 | * |
---|
388 | * If the goal is to minimize, set the search operation to |
---|
389 | * return values less than the target. |
---|
390 | */ |
---|
391 | if(strcmp(uList[i].goal.c_str(), "max") == 0) { |
---|
392 | searchOps[i] = 2; |
---|
393 | } else if(strcmp(uList[i].goal.c_str(), "min") == 0) { |
---|
394 | searchOps[i] = 4; |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | myCBR->Print(); |
---|
399 | |
---|
400 | /* CBR specific call */ |
---|
401 | uint32_t rc = myCBR->Search(searchNames, searchOps, searchVals, |
---|
402 | radioInfo->numUtilities, returnValues, EVF); |
---|
403 | |
---|
404 | if(rc == 0){ |
---|
405 | /* Adapt the returned parameters to meet the objective */ |
---|
406 | LOG("Cognitive Engine:: Found\n"); |
---|
407 | |
---|
408 | /* Should do a random adaptation.. */ |
---|
409 | if(returnValues[numberColumns-1] > (uList[0].target * 0.2)) { |
---|
410 | returnValues[1] = returnValues[1] - DECREMENTSCALE*fabs(returnValues[numberColumns-1]); |
---|
411 | LOG("RREEEALLLY CLOSE1\n %f", fabs(returnValues[numberColumns-1])); |
---|
412 | } else if(returnValues[numberColumns-1] < -(uList[0].target * 0.2)) { |
---|
413 | LOG("RREEEALLLY CLOSE2\n %f", fabs(returnValues[numberColumns-1])); |
---|
414 | returnValues[1] = returnValues[1] + INCREMENTSCALE*fabs(returnValues[numberColumns-1]); |
---|
415 | } else { |
---|
416 | LOG("RREEEALLLY CLOSE\n"); |
---|
417 | } |
---|
418 | } else if(rc == 31337) { |
---|
419 | LOG("Cognitive Engine:: Not Found.\n"); |
---|
420 | /* No rows in the CBR, pick default parameters */ |
---|
421 | /* Currently this is hard coded and implementation specific! */ |
---|
422 | returnValues[1] = currentParameters[0].value; |
---|
423 | |
---|
424 | } else { |
---|
425 | LOG("Cognitive Engine:: Search return an invalid value.\n"); |
---|
426 | } |
---|
427 | |
---|
428 | size_t returnValueIndex = 0; |
---|
429 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
430 | uList[i].value = returnValues[returnValueIndex]; |
---|
431 | returnValueIndex++; |
---|
432 | } |
---|
433 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
434 | pList[i].value = returnValues[returnValueIndex]; |
---|
435 | returnValueIndex++; |
---|
436 | } |
---|
437 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
438 | oList[i].value = returnValues[returnValueIndex]; |
---|
439 | returnValueIndex++; |
---|
440 | } |
---|
441 | returnValues[returnValueIndex] = 0; |
---|
442 | |
---|
443 | string allNames[numberColumns]; |
---|
444 | size_t allNameIndex = 0; |
---|
445 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
446 | allNames[allNameIndex] = uList[i].name; |
---|
447 | returnValues[allNameIndex] = uList[i].target; |
---|
448 | allNameIndex++; |
---|
449 | } |
---|
450 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
451 | allNames[allNameIndex] = pList[i].name; |
---|
452 | allNameIndex++; |
---|
453 | } |
---|
454 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
455 | allNames[allNameIndex] = oList[i].name; |
---|
456 | returnValues[allNameIndex] = observables[i].value; |
---|
457 | allNameIndex++; |
---|
458 | } |
---|
459 | allNames[allNameIndex] = "utility"; |
---|
460 | |
---|
461 | // Add row to CBR. |
---|
462 | myCBR->AddRow(allNames, returnValues, returnValueIndex+1); |
---|
463 | |
---|
464 | return pList; |
---|
465 | } |
---|
466 | |
---|
467 | |
---|
468 | Parameter* |
---|
469 | OSSIE_CE::GetSolution(Observable *observables, \ |
---|
470 | Parameter *currentParameters, std::string service) |
---|
471 | { |
---|
472 | LOG("Cognitive Engine:: Generating solution for %s service.\n", service.c_str()); |
---|
473 | |
---|
474 | return pList; |
---|
475 | } |
---|
476 | |
---|
477 | |
---|
478 | void |
---|
479 | OSSIE_CE::BuildCognitiveEngine() |
---|
480 | { |
---|
481 | string filename = "ex1"; |
---|
482 | string tablename = "data"; |
---|
483 | |
---|
484 | uint32_t numberColumns = radioInfo->numUtilities + radioInfo->numParameters + \ |
---|
485 | radioInfo->numObservables + 1; |
---|
486 | |
---|
487 | string cols[numberColumns]; |
---|
488 | |
---|
489 | size_t columnIndex = 0; |
---|
490 | for (size_t i = 0; i < radioInfo->numUtilities; i++){ |
---|
491 | cols[columnIndex] = uList[i].name; |
---|
492 | columnIndex++; |
---|
493 | } |
---|
494 | for (size_t i = 0; i < radioInfo->numParameters; i++){ |
---|
495 | cols[columnIndex] = pList[i].name; |
---|
496 | columnIndex++; |
---|
497 | } |
---|
498 | for (size_t i = 0; i < radioInfo->numObservables; i++){ |
---|
499 | cols[columnIndex] = oList[i].name; |
---|
500 | columnIndex++; |
---|
501 | } |
---|
502 | cols[columnIndex] = "utility"; |
---|
503 | |
---|
504 | myCBR = new ossieCBR(filename, tablename, cols, numberColumns); |
---|
505 | } |
---|
506 | |
---|
507 | void |
---|
508 | OSSIE_CE::PerformUpdatePerformance() |
---|
509 | { |
---|
510 | /* Receive Set of current Parameters */ |
---|
511 | char buffer[256]; |
---|
512 | memset(buffer, 0, 256); |
---|
513 | ReadMessage(commandSocketFD, buffer); |
---|
514 | uint32_t numParameters = atoi(buffer); |
---|
515 | |
---|
516 | Parameter *p = new Parameter[numParameters]; |
---|
517 | |
---|
518 | for(size_t i = 0; i < numParameters; i++) { |
---|
519 | memset(buffer, 0, 256); |
---|
520 | ReadMessage(commandSocketFD, buffer); |
---|
521 | p[i].name = std::string(buffer); |
---|
522 | |
---|
523 | memset(buffer, 0, 256); |
---|
524 | ReadMessage(commandSocketFD, buffer); |
---|
525 | p[i].value = atof(buffer); |
---|
526 | } |
---|
527 | |
---|
528 | /* Receive Set of Observables */ |
---|
529 | memset(buffer, 0, 256); |
---|
530 | ReadMessage(commandSocketFD, buffer); |
---|
531 | uint32_t numObservables = atoi(buffer); |
---|
532 | |
---|
533 | Observable *o = new Observable[numObservables]; |
---|
534 | |
---|
535 | for(size_t i = 0; i < numObservables; i++) { |
---|
536 | memset(buffer, 0, 256); |
---|
537 | ReadMessage(commandSocketFD, buffer); |
---|
538 | o[i].name = std::string(buffer); |
---|
539 | |
---|
540 | memset(buffer, 0, 256); |
---|
541 | ReadMessage(commandSocketFD, buffer); |
---|
542 | o[i].value = atof(buffer); |
---|
543 | } |
---|
544 | |
---|
545 | ReceiveFeedback(o,p); |
---|
546 | |
---|
547 | delete [] o; |
---|
548 | delete [] p; |
---|
549 | } |
---|
550 | |
---|
551 | |
---|
552 | void |
---|
553 | OSSIE_CE::PerformRequestOptimizationService() |
---|
554 | { |
---|
555 | /* Receive Set of Observables */ |
---|
556 | LOG("\nCognitive Engine:: Receiving service name\n"); |
---|
557 | |
---|
558 | char buffer[256]; |
---|
559 | memset(buffer, 0, 256); |
---|
560 | ReadMessage(commandSocketFD,buffer); |
---|
561 | LOG("\nCognitive Engine:: Got service name, %s\n", buffer); |
---|
562 | |
---|
563 | /* Receive Set of Observables */ |
---|
564 | LOG("\nCognitive Engine:: Receiving Observable Parameters\n"); |
---|
565 | |
---|
566 | memset(buffer, 0, 256); |
---|
567 | ReadMessage(commandSocketFD,buffer); |
---|
568 | uint32_t numObservables = atoi(buffer); |
---|
569 | |
---|
570 | Observable *o = new Observable[numObservables]; |
---|
571 | |
---|
572 | for(size_t i = 0; i < numObservables; i++) { |
---|
573 | memset(buffer, 0, 256); |
---|
574 | ReadMessage(commandSocketFD, buffer); |
---|
575 | o[i].name = std::string(buffer); |
---|
576 | |
---|
577 | memset(buffer, 0, 256); |
---|
578 | ReadMessage(commandSocketFD, buffer); |
---|
579 | o[i].value = atof(buffer); |
---|
580 | } |
---|
581 | |
---|
582 | /* Receive Set of current Parameters */ |
---|
583 | LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n"); |
---|
584 | |
---|
585 | memset(buffer, 0, 256); |
---|
586 | ReadMessage(commandSocketFD, buffer); |
---|
587 | uint32_t numCurrentParameters = atoi(buffer); |
---|
588 | |
---|
589 | Parameter *cp = new Parameter[numCurrentParameters]; |
---|
590 | |
---|
591 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
592 | memset(buffer, 0, 256); |
---|
593 | ReadMessage(commandSocketFD, buffer); |
---|
594 | cp[i].name = std::string(buffer); |
---|
595 | |
---|
596 | memset(buffer, 0, 256); |
---|
597 | ReadMessage(commandSocketFD, buffer); |
---|
598 | cp[i].value = atof(buffer); |
---|
599 | } |
---|
600 | LOG("Cognitive Engine:: Processing parameters....\n"); |
---|
601 | |
---|
602 | //Parameter *solutionSet; |
---|
603 | |
---|
604 | //solutionSet = GetSolution(o,cp); |
---|
605 | |
---|
606 | // TODO need to actually do something with the observables here |
---|
607 | |
---|
608 | LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n"); |
---|
609 | char numParametersChar[10]; |
---|
610 | //char solutionValue[50]; |
---|
611 | sprintf(numParametersChar, "%i", radioInfo->numParameters); |
---|
612 | SendMessage(commandSocketFD, numParametersChar); |
---|
613 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
614 | //SendMessage(commandSocketFD, solutionSet[i].name.c_str()); |
---|
615 | SendMessage(commandSocketFD, "test"); |
---|
616 | //memset(solutionValue, 0, 50); |
---|
617 | //sprintf(solutionValue, "%f", solutionSet[i].value); |
---|
618 | //SendMessage(commandSocketFD, solutionValue); |
---|
619 | SendMessage(commandSocketFD, "00"); |
---|
620 | } |
---|
621 | |
---|
622 | delete [] o; |
---|
623 | delete [] cp; |
---|
624 | } |
---|
625 | |
---|
626 | |
---|
627 | void |
---|
628 | OSSIE_CE::PerformRequestOptimization() |
---|
629 | { |
---|
630 | /* Receive Set of Observables */ |
---|
631 | LOG("\nCognitive Engine:: Receiving Observable Parameters\n"); |
---|
632 | |
---|
633 | char buffer[256]; |
---|
634 | memset(buffer, 0, 256); |
---|
635 | ReadMessage(commandSocketFD,buffer); |
---|
636 | uint32_t numObservables = atoi(buffer); |
---|
637 | |
---|
638 | Observable *o = new Observable[numObservables]; |
---|
639 | |
---|
640 | for(size_t i = 0; i < numObservables; i++) { |
---|
641 | memset(buffer, 0, 256); |
---|
642 | ReadMessage(commandSocketFD, buffer); |
---|
643 | o[i].name = std::string(buffer); |
---|
644 | |
---|
645 | memset(buffer, 0, 256); |
---|
646 | ReadMessage(commandSocketFD, buffer); |
---|
647 | o[i].value = atof(buffer); |
---|
648 | } |
---|
649 | |
---|
650 | /* Receive Set of current Parameters */ |
---|
651 | LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n"); |
---|
652 | |
---|
653 | memset(buffer, 0, 256); |
---|
654 | ReadMessage(commandSocketFD, buffer); |
---|
655 | uint32_t numCurrentParameters = atoi(buffer); |
---|
656 | |
---|
657 | Parameter *cp = new Parameter[numCurrentParameters]; |
---|
658 | |
---|
659 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
660 | memset(buffer, 0, 256); |
---|
661 | ReadMessage(commandSocketFD, buffer); |
---|
662 | cp[i].name = std::string(buffer); |
---|
663 | |
---|
664 | memset(buffer, 0, 256); |
---|
665 | ReadMessage(commandSocketFD, buffer); |
---|
666 | cp[i].value = atof(buffer); |
---|
667 | } |
---|
668 | LOG("Cognitive Engine:: Processing parameters....\n"); |
---|
669 | |
---|
670 | Parameter *solutionSet; |
---|
671 | |
---|
672 | solutionSet = GetSolution(o,cp); |
---|
673 | |
---|
674 | // TODO need to actually do something with the observables here |
---|
675 | |
---|
676 | LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n"); |
---|
677 | char numParametersChar[10]; |
---|
678 | char solutionValue[50]; |
---|
679 | sprintf(numParametersChar, "%i", radioInfo->numParameters); |
---|
680 | SendMessage(commandSocketFD, numParametersChar); |
---|
681 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
682 | SendMessage(commandSocketFD, solutionSet[i].name.c_str()); |
---|
683 | memset(solutionValue, 0, 50); |
---|
684 | sprintf(solutionValue, "%f", solutionSet[i].value); |
---|
685 | SendMessage(commandSocketFD, solutionValue); |
---|
686 | } |
---|
687 | |
---|
688 | delete [] o; |
---|
689 | delete [] cp; |
---|
690 | } |
---|
691 | |
---|
692 | |
---|
693 | void |
---|
694 | OSSIE_CE::PerformQueryComponentType() |
---|
695 | { |
---|
696 | SendComponentType(); |
---|
697 | } |
---|
698 | |
---|
699 | |
---|
700 | void |
---|
701 | OSSIE_CE::PerformConnectSML() |
---|
702 | { |
---|
703 | /* This command implies that we are disconnecting from the shell and |
---|
704 | * connecting to a SML component. */ |
---|
705 | char serverName[256]; |
---|
706 | char serverPort[256]; |
---|
707 | // TODO is this going to end up being too slow? |
---|
708 | memset(serverName, 0, 256); |
---|
709 | memset(serverPort, 0, 256); |
---|
710 | |
---|
711 | ReadMessage(commandSocketFD, serverName); |
---|
712 | ReadMessage(commandSocketFD, serverPort); |
---|
713 | |
---|
714 | /* Only continue if we are currently connected to a shell. */ |
---|
715 | if(!SML_present) { |
---|
716 | DeregisterComponent(); |
---|
717 | |
---|
718 | shutdown(commandSocketFD, 2); |
---|
719 | close(commandSocketFD); |
---|
720 | |
---|
721 | ConnectToRemoteComponent(serverName, serverPort, true); |
---|
722 | } |
---|
723 | } |
---|
724 | |
---|
725 | |
---|
726 | void |
---|
727 | OSSIE_CE::PerformDisconnectSML() |
---|
728 | { |
---|
729 | /* This command implies that we are disconnecting from the SML and |
---|
730 | * connecting to a shell component. */ |
---|
731 | char serverName[256]; |
---|
732 | char serverPort[256]; |
---|
733 | // TODO is this going to end up being too slow? |
---|
734 | memset(serverName, 0, 256); |
---|
735 | memset(serverPort, 0, 256); |
---|
736 | |
---|
737 | ReadMessage(commandSocketFD, serverName); |
---|
738 | ReadMessage(commandSocketFD, serverPort); |
---|
739 | |
---|
740 | /* We only want to do this if we are actually connected to an SML |
---|
741 | * currently. */ |
---|
742 | if(SML_present) { |
---|
743 | DeregisterServices(); |
---|
744 | |
---|
745 | shutdown(commandSocketFD, 2); |
---|
746 | close(commandSocketFD); |
---|
747 | |
---|
748 | ConnectToRemoteComponent(serverName, serverPort, false); |
---|
749 | } |
---|
750 | } |
---|
751 | |
---|
752 | |
---|
753 | void |
---|
754 | OSSIE_CE::PerformResetEngineCognitive() |
---|
755 | { |
---|
756 | Reset(); |
---|
757 | } |
---|
758 | |
---|
759 | |
---|
760 | void |
---|
761 | OSSIE_CE::PerformShutdownEngineCognitive() |
---|
762 | { |
---|
763 | Shutdown(); |
---|
764 | } |
---|
765 | |
---|