1 | /* Virginia Tech Cognitive Radio Open Source Systems |
---|
2 | * Virginia Tech, 2009 |
---|
3 | * |
---|
4 | * LICENSE INFORMATION GOES HERE |
---|
5 | */ |
---|
6 | |
---|
7 | /* DESCRIPTION OF FILE. |
---|
8 | */ |
---|
9 | |
---|
10 | |
---|
11 | #include <cstdlib> |
---|
12 | #include <cstring> |
---|
13 | #include <stdint.h> |
---|
14 | #include <string> |
---|
15 | |
---|
16 | #include <arpa/inet.h> |
---|
17 | #include <iostream> |
---|
18 | #include <netinet/in.h> |
---|
19 | #include <netdb.h> |
---|
20 | #include <fcntl.h> |
---|
21 | #include <sys/ioctl.h> |
---|
22 | #include <sys/mman.h> |
---|
23 | #include <sys/socket.h> |
---|
24 | #include <sys/types.h> |
---|
25 | #include <sys/wait.h> |
---|
26 | |
---|
27 | #include "tinyxml/tinyxml.h" |
---|
28 | #include "tinyxml/tinystr.h" |
---|
29 | |
---|
30 | #include "vtcross/common.h" |
---|
31 | #include "vtcross/components.h" |
---|
32 | #include "vtcross/containers.h" |
---|
33 | #include "vtcross/debug.h" |
---|
34 | #include "vtcross/error.h" |
---|
35 | #include "vtcross/socketcomm.h" |
---|
36 | |
---|
37 | |
---|
38 | CognitiveRadioShell::CognitiveRadioShell() |
---|
39 | { |
---|
40 | LOG("Creating Cognitive Radio Shell.\n"); |
---|
41 | SML_present = false; |
---|
42 | PE_present = false; |
---|
43 | CE_present = false; |
---|
44 | |
---|
45 | // TODO =BUG= The params, observables, and utils arrays are not being |
---|
46 | // allocated here. If an shell object is constructed, and then immediately |
---|
47 | // destructed, there will be no allocated memory, and the delete operater |
---|
48 | // will receive a null pointer, which will segfault. |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | CognitiveRadioShell::~CognitiveRadioShell() |
---|
53 | { |
---|
54 | delete [] params; |
---|
55 | delete [] observables; |
---|
56 | delete [] utils; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | CognitiveRadioShell::CognitiveRadioShell(const char* radioConfig, int16_t p1, \ |
---|
61 | int16_t p2, int16_t p3) |
---|
62 | { |
---|
63 | LOG("Creating Cognitive Radio Shell.\n"); |
---|
64 | |
---|
65 | params = new Parameter[10]; |
---|
66 | observables = new Observable[10]; |
---|
67 | utils = new Utility[10]; |
---|
68 | radio_info = new Radio_Info; |
---|
69 | |
---|
70 | LoadRadioConfiguration(radioConfig, params, utils, observables, radio_info); |
---|
71 | |
---|
72 | primaryPort = p1; |
---|
73 | policyPort = p2; |
---|
74 | commandPort = p3; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | void |
---|
79 | CognitiveRadioShell::SendComponentType(int32_t socketFD) |
---|
80 | { |
---|
81 | SendMessage(socketFD, "response_shell"); |
---|
82 | LOG("Cognitive Radio Shell responded to GetRemoteComponentType query.\n"); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | std::string |
---|
87 | CognitiveRadioShell::GetRemoteComponentType(int32_t socketFD) |
---|
88 | { |
---|
89 | SendMessage(socketFD, "request_component_type"); |
---|
90 | |
---|
91 | char buffer[256]; |
---|
92 | memset(buffer, 0, 256); |
---|
93 | ReadMessage(socketFD, buffer); |
---|
94 | |
---|
95 | return std::string(buffer); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | void |
---|
100 | CognitiveRadioShell::Shutdown() |
---|
101 | { |
---|
102 | // TODO should something else be happening here? |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | void |
---|
107 | CognitiveRadioShell::Reset() |
---|
108 | { |
---|
109 | LOG("Resetting Cognitive Radio Shell.\n"); |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | void |
---|
114 | CognitiveRadioShell::SendRadioConfiguration(int32_t socketFD) |
---|
115 | { |
---|
116 | LOG("Cognitive Radio Shell:: Sending radio configuration to Cognitive Engine.\n"); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | void |
---|
121 | CognitiveRadioShell::SendRadioExperience(int32_t socketFD) |
---|
122 | { |
---|
123 | |
---|
124 | LOG("Cognitive Radio Shell:: Sending radio experience to Cognitive Engine.\n"); |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | void |
---|
129 | CognitiveRadioShell::RegisterCognitiveEngine(int32_t socketFD) |
---|
130 | { |
---|
131 | LOG("Cognitive Radio Shell:: Received registration message from Cognitive Engine.\n"); |
---|
132 | |
---|
133 | SendRadioConfiguration(socketFD); |
---|
134 | SendRadioExperience(socketFD); |
---|
135 | |
---|
136 | numberOfCognitiveEngines++; |
---|
137 | CE_present = true; |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | void |
---|
142 | CognitiveRadioShell::DeregisterCognitiveEngine(int32_t socketFD) |
---|
143 | { |
---|
144 | LOG("Cognitive Radio Shell:: Received deregistration message from Cognitive Engine.\n"); |
---|
145 | |
---|
146 | numberOfCognitiveEngines--; |
---|
147 | if(numberOfCognitiveEngines == 0) |
---|
148 | CE_present = false; |
---|
149 | |
---|
150 | SendMessage(socketFD, "deregister_ack"); |
---|
151 | shutdown(socketFD, 2); |
---|
152 | close(socketFD); |
---|
153 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | void |
---|
158 | CognitiveRadioShell::RegisterPolicyEngine(int32_t socketFD) |
---|
159 | { |
---|
160 | LOG("Cognitive Radio Shell:: Received registration message from Policy Engine.\n"); |
---|
161 | PE_present = true; |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | void |
---|
166 | CognitiveRadioShell::DeregisterPolicyEngine(int32_t socketFD) |
---|
167 | { |
---|
168 | LOG("Cognitive Radio Shell:: Received deregistration message from Policy Engine.\n"); |
---|
169 | |
---|
170 | PE_present = false; |
---|
171 | |
---|
172 | SendMessage(socketFD, "deregister_ack"); |
---|
173 | shutdown(socketFD, 2); |
---|
174 | close(socketFD); |
---|
175 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | void |
---|
180 | CognitiveRadioShell::RegisterSML(int32_t socketFD) |
---|
181 | { |
---|
182 | LOG("Cognitive Radio Shell:: Received registration message from SML.\n"); |
---|
183 | |
---|
184 | SML_present = true; |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | void |
---|
189 | CognitiveRadioShell::DeregisterSML(int32_t socketFD) |
---|
190 | { |
---|
191 | LOG("Cognitive Radio Shell:: Received deregistration message from SML.\n"); |
---|
192 | |
---|
193 | SML_present = false; |
---|
194 | |
---|
195 | SendMessage(socketFD, "deregister_ack"); |
---|
196 | shutdown(socketFD, 2); |
---|
197 | close(socketFD); |
---|
198 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | int32_t |
---|
203 | CognitiveRadioShell::LoadRadioConfiguration(const char* radioConfig, \ |
---|
204 | Parameter* &pList, Utility* &uList, Observable* &oList, \ |
---|
205 | Radio_Info* radioInfo) |
---|
206 | { |
---|
207 | TiXmlElement *pElem; |
---|
208 | TiXmlElement *pChild; |
---|
209 | TiXmlElement *pChild1; |
---|
210 | TiXmlElement *pSecondChild; |
---|
211 | TiXmlHandle hRoot(0); |
---|
212 | |
---|
213 | int32_t count = 0; |
---|
214 | size_t item_count = 0; |
---|
215 | size_t affect_count = 0; |
---|
216 | int32_t attribute_count = 0; |
---|
217 | bool match_found = false; |
---|
218 | |
---|
219 | LOG("Cognitive Radio Shell:: Loading radio configuration.\n"); |
---|
220 | |
---|
221 | TiXmlDocument doc( radioConfig ); |
---|
222 | bool loadOkay = doc.LoadFile(); |
---|
223 | if(!loadOkay) |
---|
224 | ERROR(1,"Loading radio configuration failed: %s\n", radioConfig); |
---|
225 | |
---|
226 | TiXmlHandle hDoc(&doc); |
---|
227 | |
---|
228 | pElem = hDoc.FirstChildElement().Element(); |
---|
229 | |
---|
230 | if(!pElem) |
---|
231 | ERROR(1, "No valid root!"); |
---|
232 | |
---|
233 | hRoot = TiXmlHandle(pElem); |
---|
234 | |
---|
235 | pElem = hRoot.FirstChild("utilities").Element(); |
---|
236 | pChild1 = hRoot.Child("utilities", count).Element(); |
---|
237 | |
---|
238 | for(pChild = pChild1->FirstChildElement("utility"); pChild; \ |
---|
239 | pChild = pChild->NextSiblingElement()) { |
---|
240 | |
---|
241 | const char *uName = pChild->Attribute("name"); |
---|
242 | if(uName) |
---|
243 | uList[item_count].name = uName; |
---|
244 | |
---|
245 | const char *uUnits = pChild->Attribute("units"); |
---|
246 | if(uUnits) |
---|
247 | uList[item_count].units = uUnits; |
---|
248 | |
---|
249 | const char *uGoal = pChild->Attribute("goal"); |
---|
250 | if(uGoal) |
---|
251 | uList[item_count].goal = uGoal; |
---|
252 | |
---|
253 | if(pChild->QueryFloatAttribute("target", &uList[item_count].target) != TIXML_SUCCESS) |
---|
254 | uList[item_count].target = -1; |
---|
255 | |
---|
256 | item_count++; |
---|
257 | } |
---|
258 | |
---|
259 | radio_info->numUtilities = item_count; |
---|
260 | LOG("Cognitive Radio Shell:: Parsed %d utilities.\n", radioInfo->numUtilities); |
---|
261 | |
---|
262 | item_count = 0; |
---|
263 | pElem = hRoot.FirstChild("observables").Element(); |
---|
264 | pChild1 = hRoot.Child("observables", count).Element(); |
---|
265 | |
---|
266 | for(pChild = pChild1->FirstChildElement("observable"); pChild; \ |
---|
267 | pChild = pChild->NextSiblingElement()) { |
---|
268 | |
---|
269 | const char *oName = pChild->Attribute("name"); |
---|
270 | if(oName) |
---|
271 | oList[item_count].name = oName; |
---|
272 | |
---|
273 | affect_count = 0; |
---|
274 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ |
---|
275 | pSecondChild = pSecondChild->NextSiblingElement()) { |
---|
276 | |
---|
277 | const char *oUtilName = pSecondChild->Attribute("utility"); |
---|
278 | if(oUtilName) { |
---|
279 | for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++ ) { |
---|
280 | if(uList[attribute_count].name == oUtilName) { |
---|
281 | |
---|
282 | oList[item_count].affection_list[affect_count].u = &uList[attribute_count]; |
---|
283 | const char *oRelate = pSecondChild->Attribute("relationship"); |
---|
284 | if(oRelate) |
---|
285 | oList[item_count].affection_list[affect_count].relation = oRelate; |
---|
286 | |
---|
287 | affect_count++; |
---|
288 | match_found = true; |
---|
289 | break; |
---|
290 | } |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | if(!match_found) { |
---|
295 | ERROR(1, "Error: %s: %s is not a valid utility.\n", \ |
---|
296 | oList[item_count].name.c_str(), oUtilName); |
---|
297 | } |
---|
298 | else |
---|
299 | match_found = false; |
---|
300 | } |
---|
301 | oList[item_count].numAffects = affect_count; |
---|
302 | item_count++; |
---|
303 | } |
---|
304 | |
---|
305 | radioInfo->numObservables = item_count; |
---|
306 | LOG("Cognitive Radio Shell:: Parsed %d observables.\n", radioInfo->numObservables); |
---|
307 | |
---|
308 | pElem = hRoot.FirstChild("parameters").Element(); |
---|
309 | pChild1 = hRoot.Child("parameters", count).Element(); |
---|
310 | |
---|
311 | item_count = 0; |
---|
312 | for(pChild = pChild1->FirstChildElement("parameter"); pChild; \ |
---|
313 | pChild = pChild->NextSiblingElement()) { |
---|
314 | |
---|
315 | const char *pName = pChild->Attribute("name"); |
---|
316 | if(pName) |
---|
317 | pList[item_count].name = pName; |
---|
318 | |
---|
319 | const char *pUnits = pChild->Attribute("units"); |
---|
320 | if(pUnits) |
---|
321 | pList[item_count].units = pUnits; |
---|
322 | |
---|
323 | if(pChild->QueryFloatAttribute("min", &pList[item_count].min) != TIXML_SUCCESS) |
---|
324 | pList[item_count].min = -1; |
---|
325 | |
---|
326 | if(pChild->QueryFloatAttribute("max", &pList[item_count].max) != TIXML_SUCCESS) |
---|
327 | pList[item_count].max = -1; |
---|
328 | |
---|
329 | if(pChild->QueryFloatAttribute("step", &pList[item_count].step) != TIXML_SUCCESS) |
---|
330 | pList[item_count].step = -1; |
---|
331 | |
---|
332 | affect_count = 0; |
---|
333 | for(pSecondChild = pChild->FirstChildElement("affect"); pSecondChild; \ |
---|
334 | |
---|
335 | pSecondChild = pSecondChild->NextSiblingElement()) { |
---|
336 | const char *pUtilName = pSecondChild->Attribute("utility"); |
---|
337 | if(pUtilName) { |
---|
338 | for(attribute_count = 0; attribute_count < radio_info->numUtilities; attribute_count++) { |
---|
339 | if(uList[attribute_count].name == pUtilName) { |
---|
340 | pList[item_count].affection_list[affect_count].u = &uList[attribute_count]; |
---|
341 | |
---|
342 | const char *pRelate = pSecondChild->Attribute("relationship"); |
---|
343 | if(pRelate) |
---|
344 | pList[item_count].affection_list[affect_count].relation = pRelate; |
---|
345 | else |
---|
346 | LOG("Error: No relation found.\n"); |
---|
347 | |
---|
348 | match_found = true; |
---|
349 | affect_count++; |
---|
350 | break; |
---|
351 | } |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | if(!match_found) { |
---|
356 | ERROR(1, "Error: %s: %s is not a valid utility.\n", \ |
---|
357 | pList[item_count].name.c_str(), pUtilName); |
---|
358 | } |
---|
359 | |
---|
360 | match_found = false; |
---|
361 | } |
---|
362 | |
---|
363 | pList[item_count].numAffects = affect_count; |
---|
364 | item_count++; |
---|
365 | } |
---|
366 | |
---|
367 | radioInfo->numParameters = item_count; |
---|
368 | LOG("Cognitive Radio Shell:: Parsed %d parameters.\n", radioInfo->numParameters); |
---|
369 | |
---|
370 | return 1; |
---|
371 | |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | void |
---|
376 | CognitiveRadioShell::GetOptimalParameters(int32_t socketFD) |
---|
377 | { |
---|
378 | } |
---|
379 | |
---|
380 | |
---|
381 | void |
---|
382 | CognitiveRadioShell::HandleMessage(int32_t socketFD) |
---|
383 | { |
---|
384 | char buffer[256]; |
---|
385 | |
---|
386 | ReadMessage(socketFD, buffer); |
---|
387 | |
---|
388 | if(strcmp(buffer,"register_engine_cognitive") == 0) { |
---|
389 | RegisterCognitiveEngine(socketFD); |
---|
390 | } else if(strcmp(buffer,"deregister_engine_cognitive") == 0) { |
---|
391 | DeregisterCognitiveEngine(socketFD); |
---|
392 | } else if(strcmp(buffer,"register_engine_policy") == 0) { |
---|
393 | RegisterPolicyEngine(socketFD); |
---|
394 | } else if(strcmp(buffer,"deregister_engine_policy") == 0) { |
---|
395 | DeregisterPolicyEngine(socketFD); |
---|
396 | } else if(strcmp(buffer,"register_sml") == 0) { |
---|
397 | RegisterSML(socketFD); |
---|
398 | } else if(strcmp(buffer,"deregister_sml") == 0) { |
---|
399 | DeregisterSML(socketFD); |
---|
400 | } else if(strcmp(buffer,"optimize") == 0) { |
---|
401 | /* Receive optimization request and current environment */ |
---|
402 | GetOptimalParameters(socketFD); |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | |
---|
407 | void |
---|
408 | CognitiveRadioShell::StartShellServer() |
---|
409 | { |
---|
410 | struct timeval selTimeout; |
---|
411 | int32_t primary = 0; |
---|
412 | int32_t policy = 1; |
---|
413 | int32_t command = 2; |
---|
414 | int32_t running = 1; |
---|
415 | int32_t port, rc, new_sd = 1; |
---|
416 | int32_t desc_ready = 1; |
---|
417 | int32_t timeout = 10; |
---|
418 | fd_set sockSet; |
---|
419 | |
---|
420 | int32_t *servSock = new int32_t[3]; |
---|
421 | |
---|
422 | servSock[primary] = CreateTCPServerSocket(primaryPort); |
---|
423 | servSock[policy] = CreateTCPServerSocket(policyPort); |
---|
424 | servSock[command] = CreateTCPServerSocket(commandPort); |
---|
425 | |
---|
426 | int32_t maxDescriptor = servSock[command]; |
---|
427 | |
---|
428 | if(InitializeTCPServerPort(servSock[primary]) == -1) |
---|
429 | ERROR(1,"Error initializing primary port\n"); |
---|
430 | |
---|
431 | if(InitializeTCPServerPort(servSock[policy]) == -1) |
---|
432 | ERROR(1,"Error initializing policy port\n"); |
---|
433 | |
---|
434 | if(InitializeTCPServerPort(servSock[command]) == -1) |
---|
435 | ERROR(1,"Error initializing command port\n"); |
---|
436 | |
---|
437 | while (running) { |
---|
438 | /* Zero socket descriptor vector and set for server sockets */ |
---|
439 | /* This must be reset every time select() is called */ |
---|
440 | FD_ZERO(&sockSet); |
---|
441 | FD_SET(servSock[primary], &sockSet); |
---|
442 | FD_SET(servSock[policy], &sockSet); |
---|
443 | FD_SET(servSock[command], &sockSet); |
---|
444 | |
---|
445 | /* Timeout specification */ |
---|
446 | /* This must be reset every time select() is called */ |
---|
447 | selTimeout.tv_sec = timeout; /* timeout (secs.) */ |
---|
448 | selTimeout.tv_usec = 0; /* 0 microseconds */ |
---|
449 | |
---|
450 | /* Suspend program until descriptor is ready or timeout */ |
---|
451 | rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
452 | if(rc == 0) |
---|
453 | LOG("No echo requests for %i secs...Server still alive\n", timeout); |
---|
454 | else { |
---|
455 | desc_ready = rc; |
---|
456 | |
---|
457 | for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) { |
---|
458 | if(FD_ISSET(port, &sockSet)) { |
---|
459 | desc_ready -= 1; |
---|
460 | |
---|
461 | /* Check if request is new or on an existing open descriptor */ |
---|
462 | if((port == servSock[primary]) || (port == servSock[policy]) || (port == servSock[command])) { |
---|
463 | do { |
---|
464 | new_sd = AcceptTCPConnection(port); |
---|
465 | if(new_sd < 0) |
---|
466 | break; |
---|
467 | |
---|
468 | HandleMessage(new_sd); |
---|
469 | FD_SET(new_sd,&sockSet); |
---|
470 | if(new_sd > maxDescriptor) |
---|
471 | maxDescriptor = new_sd; |
---|
472 | //LOG("New incoming connection - %i\n\n",new_sd); |
---|
473 | } while(new_sd != -1); |
---|
474 | } |
---|
475 | else { |
---|
476 | //LOG("Request on already open descriptor.\n\n"); |
---|
477 | HandleMessage(port); |
---|
478 | } |
---|
479 | } |
---|
480 | } |
---|
481 | } |
---|
482 | } |
---|
483 | |
---|
484 | |
---|
485 | /* Close sockets */ |
---|
486 | close(servSock[primary]); |
---|
487 | close(servSock[policy]); |
---|
488 | close(servSock[command]); |
---|
489 | |
---|
490 | /* Free list of sockets */ |
---|
491 | delete servSock; |
---|
492 | |
---|
493 | return; |
---|
494 | } |
---|