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 | #include <iostream> |
---|
28 | |
---|
29 | #include "vtcross/cognitive_engine.h" |
---|
30 | #include "vtcross/common.h" |
---|
31 | #include "vtcross/containers.h" |
---|
32 | #include "vtcross/debug.h" |
---|
33 | #include "vtcross/error.h" |
---|
34 | #include "vtcross/socketcomm.h" |
---|
35 | |
---|
36 | using namespace std; |
---|
37 | |
---|
38 | |
---|
39 | CognitiveEngine::CognitiveEngine() |
---|
40 | { |
---|
41 | LOG("Creating Cognitive Engine.\n"); |
---|
42 | SML_present = false; |
---|
43 | commandSocketFD = -1; |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | CognitiveEngine::~CognitiveEngine() |
---|
48 | { |
---|
49 | delete [] pList; |
---|
50 | delete [] oList; |
---|
51 | delete [] uList; |
---|
52 | |
---|
53 | delete [] radioInfo; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | CognitiveEngine::CognitiveEngine(const char *serverName, const char *serverPort, \ |
---|
58 | const int32_t numFields, const bool SML) |
---|
59 | { |
---|
60 | LOG("Creating Cognitive Engine.\n"); |
---|
61 | |
---|
62 | pList = new Parameter[numFields]; |
---|
63 | oList = new Observable[numFields]; |
---|
64 | uList = new Utility[numFields]; |
---|
65 | |
---|
66 | radioInfo = new Radio_Info; |
---|
67 | |
---|
68 | ConnectToRemoteComponent(serverName, serverPort, SML); |
---|
69 | |
---|
70 | } |
---|
71 | |
---|
72 | void |
---|
73 | CognitiveEngine::SendComponentType() |
---|
74 | { |
---|
75 | SendMessage(commandSocketFD, "response_engine_cognitive"); |
---|
76 | LOG("Cognitive Engine responded to GetRemoteComponentType query.\n"); |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | void |
---|
81 | CognitiveEngine::ConnectToRemoteComponent(const char *serverName, \ |
---|
82 | const char *serverPort, const bool SML) |
---|
83 | { |
---|
84 | commandSocketFD = ClientSocket(serverName, serverPort); |
---|
85 | |
---|
86 | SML_present = SML; |
---|
87 | |
---|
88 | |
---|
89 | // Considering only Shell to work in this project!! |
---|
90 | |
---|
91 | LOG("Cognitive Engine connected to shell at %s.\n", serverName); |
---|
92 | |
---|
93 | RegisterComponent(); |
---|
94 | ReceiveRadioConfiguration(); |
---|
95 | ReceiveExperience(); |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | void |
---|
101 | CognitiveEngine::WaitForSignal() |
---|
102 | { |
---|
103 | char buffer[256]; |
---|
104 | |
---|
105 | std::cout << "IN WAITFORSIGNAL METHOD" << std::endl; |
---|
106 | |
---|
107 | while(true) { |
---|
108 | memset(buffer, 0, 256); |
---|
109 | |
---|
110 | ReadMessage(commandSocketFD, buffer); |
---|
111 | |
---|
112 | if(strcmp(buffer, "update_performance") == 0) { |
---|
113 | PerformUpdatePerformance(); |
---|
114 | } |
---|
115 | else if(strcmp(buffer, "request_optimization") == 0) { |
---|
116 | PerformRequestOptimization(); |
---|
117 | } |
---|
118 | else if(strcmp(buffer, "reset_engine_cognitive") == 0) { |
---|
119 | PerformResetEngineCognitive(); |
---|
120 | } |
---|
121 | else if(strcmp(buffer, "shutdown_engine_cognitive") == 0) { |
---|
122 | PerformShutdownEngineCognitive(); |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | void |
---|
129 | CognitiveEngine::Shutdown() |
---|
130 | { |
---|
131 | DeregisterComponent(); |
---|
132 | // TODO Need to actually kill this process... |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | void |
---|
137 | CognitiveEngine::Reset() |
---|
138 | { |
---|
139 | LOG("Resetting Cognitive Engine.\n"); |
---|
140 | |
---|
141 | DeregisterComponent(); |
---|
142 | |
---|
143 | // TODO This function then needs to re-call "ConnectToRemoteComponent" to |
---|
144 | // finish the reset - otherwise, it is just a shutdown. |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | void |
---|
149 | CognitiveEngine::RegisterComponent() |
---|
150 | { |
---|
151 | char buffer[256]; |
---|
152 | SendMessage(commandSocketFD, "register_engine_cognitive"); |
---|
153 | LOG("Cognitive Engine:: Registration message sent to shell.\n"); |
---|
154 | |
---|
155 | memset(buffer, 0, 256); |
---|
156 | ReadMessage(commandSocketFD, buffer); |
---|
157 | |
---|
158 | // TODO Are we reading an ACK here? If so, why aren't we doing anything with |
---|
159 | // it? |
---|
160 | } |
---|
161 | |
---|
162 | void |
---|
163 | CognitiveEngine::DeregisterComponent() |
---|
164 | { |
---|
165 | SendMessage(commandSocketFD, "deregister_engine_cognitive"); |
---|
166 | LOG("Cognitive Engine:: Deregistration message sent.\n"); |
---|
167 | |
---|
168 | shutdown(commandSocketFD, 2); |
---|
169 | close(commandSocketFD); |
---|
170 | commandSocketFD = -1; |
---|
171 | LOG("Cognitive Engine:: Shell socket closed.\n"); |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | void |
---|
176 | CognitiveEngine::ReceiveRadioConfiguration() |
---|
177 | { |
---|
178 | LOG("Cognitive Engine:: Receiving Radio Configuration.\n"); |
---|
179 | |
---|
180 | char buffer[256]; |
---|
181 | |
---|
182 | /* Receive Set of Utilities */ |
---|
183 | memset(buffer, 0, 256); |
---|
184 | ReadMessage(commandSocketFD, buffer); |
---|
185 | radioInfo->numUtilities = atoi(buffer); |
---|
186 | |
---|
187 | for(size_t i = 0; i < radioInfo->numUtilities; i++) { |
---|
188 | memset(buffer, 0, 256); |
---|
189 | ReadMessage(commandSocketFD, buffer); |
---|
190 | uList[i].name = std::string(buffer); |
---|
191 | |
---|
192 | memset(buffer, 0, 256); |
---|
193 | ReadMessage(commandSocketFD, buffer); |
---|
194 | uList[i].units = std::string(buffer); |
---|
195 | |
---|
196 | memset(buffer, 0, 256); |
---|
197 | ReadMessage(commandSocketFD, buffer); |
---|
198 | uList[i].goal = std::string(buffer); |
---|
199 | |
---|
200 | memset(buffer, 0, 256); |
---|
201 | ReadMessage(commandSocketFD, buffer); |
---|
202 | uList[i].target = atof(buffer); |
---|
203 | } |
---|
204 | |
---|
205 | /* Receive Set of Parameters */ |
---|
206 | memset(buffer, 0, 256); |
---|
207 | ReadMessage(commandSocketFD, buffer); |
---|
208 | radioInfo->numParameters = atoi(buffer); |
---|
209 | |
---|
210 | for(size_t i = 0; i < radioInfo->numParameters; i++) { |
---|
211 | memset(buffer, 0, 256); |
---|
212 | ReadMessage(commandSocketFD, buffer); |
---|
213 | pList[i].name = std::string(buffer); |
---|
214 | |
---|
215 | memset(buffer, 0, 256); |
---|
216 | ReadMessage(commandSocketFD, buffer); |
---|
217 | pList[i].units = std::string(buffer); |
---|
218 | |
---|
219 | memset(buffer, 0, 256); |
---|
220 | ReadMessage(commandSocketFD, buffer); |
---|
221 | pList[i].min = atof(buffer); |
---|
222 | |
---|
223 | memset(buffer, 0, 256); |
---|
224 | ReadMessage(commandSocketFD, buffer); |
---|
225 | pList[i].max = atof(buffer); |
---|
226 | |
---|
227 | memset(buffer, 0, 256); |
---|
228 | ReadMessage(commandSocketFD, buffer); |
---|
229 | pList[i].step = atof(buffer); |
---|
230 | |
---|
231 | memset(buffer, 0, 256); |
---|
232 | ReadMessage(commandSocketFD,buffer); |
---|
233 | pList[i].numAffects = atoi(buffer); |
---|
234 | |
---|
235 | for(size_t j = 0; j < pList[i].numAffects; j++) { |
---|
236 | memset(buffer, 0, 256); |
---|
237 | ReadMessage(commandSocketFD,buffer); |
---|
238 | // TODO for + if{break} = while? |
---|
239 | for(size_t k = 0; k < radioInfo->numUtilities; k++) { |
---|
240 | if(uList[k].name == std::string(buffer)) { |
---|
241 | pList[i].affection_list[j].u = &uList[k]; |
---|
242 | break; |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | memset(buffer, 0, 256); |
---|
247 | ReadMessage(commandSocketFD, buffer); |
---|
248 | pList[i].affection_list[j].relation = std::string(buffer); |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | /* Receive Set of Observables */ |
---|
253 | memset(buffer, 0, 256); |
---|
254 | ReadMessage(commandSocketFD, buffer); |
---|
255 | radioInfo->numObservables = atoi(buffer); |
---|
256 | |
---|
257 | for(size_t i = 0; i < radioInfo->numObservables; i++) { |
---|
258 | memset(buffer, 0, 256); |
---|
259 | ReadMessage(commandSocketFD, buffer); |
---|
260 | oList[i].name = std::string(buffer); |
---|
261 | |
---|
262 | memset(buffer, 0, 256); |
---|
263 | ReadMessage(commandSocketFD, buffer); |
---|
264 | oList[i].numAffects = atoi(buffer); |
---|
265 | |
---|
266 | for(size_t j = 0; j < oList[i].numAffects; j++) { |
---|
267 | memset(buffer, 0, 256); |
---|
268 | ReadMessage(commandSocketFD, buffer); |
---|
269 | // TODO for + if{break} = while? |
---|
270 | for(size_t k = 0; k < radioInfo->numUtilities; k++) { |
---|
271 | if(uList[k].name == std::string(buffer)){ |
---|
272 | oList[i].affection_list[j].u = &uList[k]; |
---|
273 | break; |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | memset(buffer, 0, 256); |
---|
278 | ReadMessage(commandSocketFD, buffer); |
---|
279 | oList[i].affection_list[j].relation = std::string(buffer); |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | SendMessage(commandSocketFD, "receive_config_ack"); |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | void |
---|
288 | CognitiveEngine::ReceiveExperience() |
---|
289 | { |
---|
290 | LOG("Cognitive Engine:: Receiving Experience Report.\n"); |
---|
291 | char buffer[256]; |
---|
292 | uint32_t numberExp; |
---|
293 | |
---|
294 | /* Receive number of experience entries */ |
---|
295 | memset(buffer, 0, 256); |
---|
296 | ReadMessage(commandSocketFD, buffer); |
---|
297 | numberExp = atoi(buffer); |
---|
298 | |
---|
299 | LOG("Cognitive Engine:: Waiting for %i number of entries.\n", numberExp); |
---|
300 | SendMessage(commandSocketFD, "receive_exp_ack"); |
---|
301 | |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | /*************************************************************************** |
---|
306 | * |
---|
307 | * Every function below this banner must be overloaded by child classes. |
---|
308 | * |
---|
309 | ***************************************************************************/ |
---|
310 | |
---|
311 | |
---|
312 | Parameter* |
---|
313 | CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters, Utility *utilities) |
---|
314 | { |
---|
315 | LOG("Cognitive Engine:: Generating solution.\n"); |
---|
316 | |
---|
317 | LOG("CognitiveEngine:: GetSolution not implemented.\n"); |
---|
318 | |
---|
319 | return NULL; |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | void |
---|
324 | CognitiveEngine::ReceiveFeedback(Observable *observables,Parameter *parameters, Utility *utilities) |
---|
325 | { |
---|
326 | LOG("Cognitive Engine:: Receiving feedback.\n"); |
---|
327 | |
---|
328 | LOG("CognitiveEngine:: ReceiveFeedback not implemented.\n"); |
---|
329 | } |
---|
330 | |
---|
331 | |
---|
332 | void |
---|
333 | CognitiveEngine::BuildCognitiveEngine() |
---|
334 | { |
---|
335 | LOG("Cognitive Engine:: BuildCognitiveEngine not implemented.\n"); |
---|
336 | } |
---|
337 | |
---|
338 | |
---|
339 | void |
---|
340 | CognitiveEngine::PerformUpdatePerformance() |
---|
341 | { |
---|
342 | } |
---|
343 | |
---|
344 | |
---|
345 | void |
---|
346 | CognitiveEngine::PerformRequestOptimization() |
---|
347 | { |
---|
348 | } |
---|
349 | |
---|
350 | |
---|
351 | void |
---|
352 | CognitiveEngine::PerformResetEngineCognitive() |
---|
353 | { |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | void |
---|
358 | CognitiveEngine::PerformShutdownEngineCognitive() |
---|
359 | { |
---|
360 | } |
---|
361 | |
---|