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 a default implementation for a cognitive engine. Note |
---|
18 | * that this implementation is very basic, and is meant to act as a non-abstract |
---|
19 | * parent class. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <cstdlib> |
---|
23 | #include <cstring> |
---|
24 | #include <stdint.h> |
---|
25 | #include <cmath> |
---|
26 | #include <string> |
---|
27 | |
---|
28 | #include "vtcross/cognitive_engine.h" |
---|
29 | #include "vtcross/common.h" |
---|
30 | #include "vtcross/containers.h" |
---|
31 | #include "vtcross/debug.h" |
---|
32 | #include "vtcross/error.h" |
---|
33 | #include "vtcross/socketcomm.h" |
---|
34 | |
---|
35 | using namespace std; |
---|
36 | |
---|
37 | |
---|
38 | CognitiveEngine::CognitiveEngine() |
---|
39 | { |
---|
40 | LOG("Creating Cognitive Engine.\n"); |
---|
41 | SML_present = false; |
---|
42 | commandSocketFD = -1; |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | CognitiveEngine::~CognitiveEngine() |
---|
47 | { |
---|
48 | delete [] pList; |
---|
49 | delete [] oList; |
---|
50 | delete [] uList; |
---|
51 | |
---|
52 | delete [] radioInfo; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | CognitiveEngine::CognitiveEngine(const char *serverName, const char *serverPort, \ |
---|
57 | const int32_t numFields, const bool SML) |
---|
58 | { |
---|
59 | LOG("Creating Cognitive Engine.\n"); |
---|
60 | |
---|
61 | pList = new Parameter[numFields]; |
---|
62 | oList = new Observable[numFields]; |
---|
63 | uList = new Utility[numFields]; |
---|
64 | |
---|
65 | radioInfo = new Radio_Info; |
---|
66 | |
---|
67 | ConnectToRemoteComponent(serverName, serverPort, SML); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | void |
---|
72 | CognitiveEngine::SendComponentType() |
---|
73 | { |
---|
74 | SendMessage(commandSocketFD, "response_engine_cognitive"); |
---|
75 | LOG("Cognitive Engine responded to GetRemoteComponentType query.\n"); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | void |
---|
80 | CognitiveEngine::ConnectToRemoteComponent(const char *serverName, \ |
---|
81 | const char *serverPort, const bool SML) |
---|
82 | { |
---|
83 | commandSocketFD = ClientSocket(serverName, serverPort); |
---|
84 | |
---|
85 | SML_present = SML; |
---|
86 | |
---|
87 | if(SML) { |
---|
88 | LOG("Cognitive Engine connected to SML at %s.\n", serverName); |
---|
89 | |
---|
90 | RegisterComponent(); |
---|
91 | ReceiveRadioConfiguration(); |
---|
92 | ReceiveExperience(); |
---|
93 | RegisterServices(); |
---|
94 | } |
---|
95 | else { |
---|
96 | LOG("Cognitive Engine connected to shell at %s.\n", serverName); |
---|
97 | |
---|
98 | RegisterComponent(); |
---|
99 | ReceiveRadioConfiguration(); |
---|
100 | ReceiveExperience(); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | void |
---|
106 | CognitiveEngine::WaitForSignal() |
---|
107 | { |
---|
108 | char buffer[256]; |
---|
109 | |
---|
110 | while(true) { |
---|
111 | memset(buffer, 0, 256); |
---|
112 | |
---|
113 | ReadMessage(commandSocketFD, buffer); |
---|
114 | |
---|
115 | if(strcmp(buffer, "update_performance") == 0) { |
---|
116 | PerformUpdatePerformance(); |
---|
117 | } |
---|
118 | else if(strcmp(buffer, "request_optimization_service") == 0) { |
---|
119 | PerformRequestOptimizationService(); |
---|
120 | } |
---|
121 | else if(strcmp(buffer, "request_optimization") == 0) { |
---|
122 | PerformRequestOptimization(); |
---|
123 | } |
---|
124 | else if(strcmp(buffer, "query_component_type") == 0) { |
---|
125 | PerformQueryComponentType(); |
---|
126 | } |
---|
127 | else if(strcmp(buffer, "connect_sml") == 0) { |
---|
128 | PerformConnectSML(); |
---|
129 | } |
---|
130 | else if(strcmp(buffer, "disconnect_sml") == 0) { |
---|
131 | PerformDisconnectSML(); |
---|
132 | } |
---|
133 | else if(strcmp(buffer, "reset_engine_cognitive") == 0) { |
---|
134 | PerformResetEngineCognitive(); |
---|
135 | } |
---|
136 | else if(strcmp(buffer, "shutdown_engine_cognitive") == 0) { |
---|
137 | PerformShutdownEngineCognitive(); |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | void |
---|
144 | CognitiveEngine::Shutdown() |
---|
145 | { |
---|
146 | if(SML_present) { |
---|
147 | DeregisterServices(); |
---|
148 | DeregisterComponent(); |
---|
149 | } |
---|
150 | else { |
---|
151 | DeregisterComponent(); |
---|
152 | } |
---|
153 | // TODO Need to actually kill this process... |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | void |
---|
158 | CognitiveEngine::Reset() |
---|
159 | { |
---|
160 | LOG("Resetting Cognitive Engine.\n"); |
---|
161 | |
---|
162 | if(SML_present) { |
---|
163 | DeregisterServices(); |
---|
164 | DeregisterComponent(); |
---|
165 | } |
---|
166 | else { |
---|
167 | DeregisterComponent(); |
---|
168 | } |
---|
169 | |
---|
170 | // TODO This function then needs to re-call "ConnectToRemoteComponent" to |
---|
171 | // finish the reset - otherwise, it is just a shutdown. |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | void |
---|
176 | CognitiveEngine::RegisterComponent() |
---|
177 | { |
---|
178 | char buffer[256]; |
---|
179 | SendMessage(commandSocketFD, "register_engine_cognitive"); |
---|
180 | LOG("Cognitive Engine:: Registration message sent to shell.\n"); |
---|
181 | |
---|
182 | memset(buffer, 0, 256); |
---|
183 | ReadMessage(commandSocketFD, buffer); |
---|
184 | |
---|
185 | // TODO Are we reading an ACK here? If so, why aren't we doing anything with |
---|
186 | // it? |
---|
187 | } |
---|
188 | |
---|
189 | void |
---|
190 | CognitiveEngine::DeregisterComponent() |
---|
191 | { |
---|
192 | SendMessage(commandSocketFD, "deregister_engine_cognitive"); |
---|
193 | LOG("Cognitive Engine:: Deregistration message sent.\n"); |
---|
194 | |
---|
195 | shutdown(commandSocketFD, 2); |
---|
196 | close(commandSocketFD); |
---|
197 | commandSocketFD = -1; |
---|
198 | LOG("Cognitive Engine:: Shell socket closed.\n"); |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | void |
---|
203 | CognitiveEngine::ReceiveRadioConfiguration() |
---|
204 | { |
---|
205 | LOG("Cognitive Engine:: Receiving Radio Configuration.\n"); |
---|
206 | |
---|
207 | char buffer[256]; |
---|
208 | |
---|
209 | /* Receive Set of Utilities */ |
---|
210 | memset(buffer, 0, 256); |
---|
211 | ReadMessage(commandSocketFD, buffer); |
---|
212 | radioInfo->numUtilities = atoi(buffer); |
---|
213 | |
---|
214 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
215 | memset(buffer, 0, 256); |
---|
216 | ReadMessage(commandSocketFD, buffer); |
---|
217 | uList[i].name = std::string(buffer); |
---|
218 | |
---|
219 | memset(buffer, 0, 256); |
---|
220 | ReadMessage(commandSocketFD, buffer); |
---|
221 | uList[i].units = std::string(buffer); |
---|
222 | |
---|
223 | memset(buffer, 0, 256); |
---|
224 | ReadMessage(commandSocketFD, buffer); |
---|
225 | uList[i].goal = std::string(buffer); |
---|
226 | |
---|
227 | memset(buffer, 0, 256); |
---|
228 | ReadMessage(commandSocketFD, buffer); |
---|
229 | uList[i].target = atof(buffer); |
---|
230 | } |
---|
231 | |
---|
232 | /* Receive Set of Parameters */ |
---|
233 | memset(buffer, 0, 256); |
---|
234 | ReadMessage(commandSocketFD, buffer); |
---|
235 | radioInfo->numParameters = atoi(buffer); |
---|
236 | |
---|
237 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
238 | memset(buffer, 0, 256); |
---|
239 | ReadMessage(commandSocketFD, buffer); |
---|
240 | pList[i].name = std::string(buffer); |
---|
241 | |
---|
242 | memset(buffer, 0, 256); |
---|
243 | ReadMessage(commandSocketFD, buffer); |
---|
244 | pList[i].units = std::string(buffer); |
---|
245 | |
---|
246 | memset(buffer, 0, 256); |
---|
247 | ReadMessage(commandSocketFD, buffer); |
---|
248 | pList[i].min = atof(buffer); |
---|
249 | |
---|
250 | memset(buffer, 0, 256); |
---|
251 | ReadMessage(commandSocketFD, buffer); |
---|
252 | pList[i].max = atof(buffer); |
---|
253 | |
---|
254 | memset(buffer, 0, 256); |
---|
255 | ReadMessage(commandSocketFD, buffer); |
---|
256 | pList[i].step = atof(buffer); |
---|
257 | |
---|
258 | memset(buffer, 0, 256); |
---|
259 | ReadMessage(commandSocketFD,buffer); |
---|
260 | pList[i].numAffects = atoi(buffer); |
---|
261 | |
---|
262 | for(size_t j = 0; j < pList[i].numAffects; j++) { |
---|
263 | memset(buffer, 0, 256); |
---|
264 | ReadMessage(commandSocketFD,buffer); |
---|
265 | // TODO for + if{break} = while? |
---|
266 | for(size_t k = 0; k < radioInfo->numUtilities; k++) { |
---|
267 | if(uList[k].name == std::string(buffer)) { |
---|
268 | pList[i].affection_list[j].u = &uList[k]; |
---|
269 | break; |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | memset(buffer, 0, 256); |
---|
274 | ReadMessage(commandSocketFD, buffer); |
---|
275 | pList[i].affection_list[j].relation = std::string(buffer); |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | /* Receive Set of Observables */ |
---|
280 | memset(buffer, 0, 256); |
---|
281 | ReadMessage(commandSocketFD, buffer); |
---|
282 | radioInfo->numObservables = atoi(buffer); |
---|
283 | |
---|
284 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
285 | memset(buffer, 0, 256); |
---|
286 | ReadMessage(commandSocketFD, buffer); |
---|
287 | oList[i].name = std::string(buffer); |
---|
288 | |
---|
289 | memset(buffer, 0, 256); |
---|
290 | ReadMessage(commandSocketFD, buffer); |
---|
291 | oList[i].numAffects = atoi(buffer); |
---|
292 | |
---|
293 | for(size_t j = 0; j < oList[i].numAffects; j++) { |
---|
294 | memset(buffer, 0, 256); |
---|
295 | ReadMessage(commandSocketFD, buffer); |
---|
296 | // TODO for + if{break} = while? |
---|
297 | for(size_t k = 0; k < radioInfo->numUtilities; k++) { |
---|
298 | if(uList[k].name == std::string(buffer)){ |
---|
299 | oList[i].affection_list[j].u = &uList[k]; |
---|
300 | break; |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | memset(buffer, 0, 256); |
---|
305 | ReadMessage(commandSocketFD, buffer); |
---|
306 | oList[i].affection_list[j].relation = std::string(buffer); |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | SendMessage(commandSocketFD, "receive_config_ack"); |
---|
311 | } |
---|
312 | |
---|
313 | |
---|
314 | void |
---|
315 | CognitiveEngine::ReceiveExperience() |
---|
316 | { |
---|
317 | LOG("Cognitive Engine:: Receiving Experience Report.\n"); |
---|
318 | char buffer[256]; |
---|
319 | uint32_t numberExp; |
---|
320 | |
---|
321 | /* Receive number of experience entries */ |
---|
322 | memset(buffer, 0, 256); |
---|
323 | ReadMessage(commandSocketFD, buffer); |
---|
324 | numberExp = atoi(buffer); |
---|
325 | |
---|
326 | LOG("Cognitive Engine:: Waiting for %i number of entries.\n", numberExp); |
---|
327 | |
---|
328 | SendMessage(commandSocketFD, "receive_exp_ack"); |
---|
329 | } |
---|
330 | |
---|
331 | |
---|
332 | /*************************************************************************** |
---|
333 | * |
---|
334 | * Every function below this banner must be overloaded by child classes. |
---|
335 | * |
---|
336 | ***************************************************************************/ |
---|
337 | |
---|
338 | |
---|
339 | Parameter* |
---|
340 | CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters) |
---|
341 | { |
---|
342 | LOG("Cognitive Engine:: Generating solution.\n"); |
---|
343 | |
---|
344 | LOG("CognitiveEngine:: GetSolution not implemented.\n"); |
---|
345 | |
---|
346 | return NULL; |
---|
347 | } |
---|
348 | |
---|
349 | |
---|
350 | Parameter* |
---|
351 | CognitiveEngine::GetSolution(Observable *observables, \ |
---|
352 | Parameter *currentParameters, std::string service) |
---|
353 | { |
---|
354 | LOG("Cognitive Engine:: Generating solution for %s service.\n", service.c_str()); |
---|
355 | |
---|
356 | LOG("CognitiveEngine:: GetSolution with service not implemented.\n"); |
---|
357 | |
---|
358 | return NULL; |
---|
359 | } |
---|
360 | |
---|
361 | |
---|
362 | void |
---|
363 | CognitiveEngine::ReceiveFeedback(Observable *observables,Parameter *parameters) |
---|
364 | { |
---|
365 | LOG("Cognitive Engine:: Receiving feedback.\n"); |
---|
366 | |
---|
367 | LOG("CognitiveEngine:: ReceiveFeedback not implemented.\n"); |
---|
368 | } |
---|
369 | |
---|
370 | |
---|
371 | void |
---|
372 | CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \ |
---|
373 | std::string service) |
---|
374 | { |
---|
375 | LOG("Cognitive Engine:: Receiving feedback.\n"); |
---|
376 | |
---|
377 | LOG("CognitiveEngine:: ReceiveFeedback not implemented.\n"); |
---|
378 | } |
---|
379 | |
---|
380 | |
---|
381 | void |
---|
382 | CognitiveEngine::BuildCognitiveEngine() |
---|
383 | { |
---|
384 | LOG("Cognitive Engine:: BuildCognitiveEngine not implemented.\n"); |
---|
385 | } |
---|
386 | |
---|
387 | |
---|
388 | void |
---|
389 | CognitiveEngine::RegisterServices() |
---|
390 | { |
---|
391 | LOG("Cognitive Engine:: RegisterServices not implemented.\n"); |
---|
392 | } |
---|
393 | |
---|
394 | |
---|
395 | void |
---|
396 | CognitiveEngine::DeregisterServices() |
---|
397 | { |
---|
398 | LOG("Cognitive Engine:: DeregisterServices not implemented.\n"); |
---|
399 | } |
---|
400 | |
---|
401 | |
---|
402 | void |
---|
403 | CognitiveEngine::PerformUpdatePerformance() |
---|
404 | { |
---|
405 | } |
---|
406 | |
---|
407 | |
---|
408 | void |
---|
409 | CognitiveEngine::PerformRequestOptimizationService() |
---|
410 | { |
---|
411 | } |
---|
412 | |
---|
413 | |
---|
414 | void |
---|
415 | CognitiveEngine::PerformRequestOptimization() |
---|
416 | { |
---|
417 | } |
---|
418 | |
---|
419 | |
---|
420 | void |
---|
421 | CognitiveEngine::PerformQueryComponentType() |
---|
422 | { |
---|
423 | } |
---|
424 | |
---|
425 | |
---|
426 | void |
---|
427 | CognitiveEngine::PerformConnectSML() |
---|
428 | { |
---|
429 | } |
---|
430 | |
---|
431 | |
---|
432 | void |
---|
433 | CognitiveEngine::PerformDisconnectSML() |
---|
434 | { |
---|
435 | } |
---|
436 | |
---|
437 | |
---|
438 | void |
---|
439 | CognitiveEngine::PerformResetEngineCognitive() |
---|
440 | { |
---|
441 | } |
---|
442 | |
---|
443 | |
---|
444 | void |
---|
445 | CognitiveEngine::PerformShutdownEngineCognitive() |
---|
446 | { |
---|
447 | } |
---|
448 | |
---|