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