1 | /* Virginia Tech Cognitive Radio Open Source Systems |
---|
2 | * Virginia Tech, 2009 |
---|
3 | * |
---|
4 | * LICENSE INFORMATION GOES HERE |
---|
5 | */ |
---|
6 | |
---|
7 | /* Inter-component communication handled by sockets and FD's. |
---|
8 | * Server support has been completely implemented and tested. |
---|
9 | * |
---|
10 | * Services are stored in a SQLite DB by the ID of the CE that registered them. Service |
---|
11 | * support has been completely implemented and tested. |
---|
12 | * |
---|
13 | * Missions are loaded from an XML file, connected with services provided by components, |
---|
14 | * and run. See the documentation for the "PerformActiveMission" below for important |
---|
15 | * info. Mission support has been completely implemented but mostly untested |
---|
16 | * (except for the XML parsing, which has been fully tested). |
---|
17 | * |
---|
18 | * It is important that in the XML configuration file, inputs and outputs are listed |
---|
19 | * in the order that they appear. See the documentation for the "PerformActiveMission" |
---|
20 | * method for more information. |
---|
21 | * |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include <stdlib.h> |
---|
26 | #include <string.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <cstring> |
---|
29 | #include <stdint.h> |
---|
30 | |
---|
31 | #include "../../trunk/src/include/vtcross/common.h" |
---|
32 | |
---|
33 | #include "../../trunk/src/include/vtcross/components.h" |
---|
34 | #include "../../trunk/src/include/vtcross/containers.h" |
---|
35 | #include "../../trunk/src/include/vtcross/debug.h" |
---|
36 | #include "../../trunk/src/include/vtcross/error.h" |
---|
37 | #include "../../trunk/src/include/vtcross/socketcomm.h" |
---|
38 | |
---|
39 | |
---|
40 | #include <cstdlib> |
---|
41 | #include <cstring> |
---|
42 | #include <stdint.h> |
---|
43 | #include <math.h> |
---|
44 | |
---|
45 | #include <arpa/inet.h> |
---|
46 | #include <iostream> |
---|
47 | #include <netinet/in.h> |
---|
48 | #include <netdb.h> |
---|
49 | #include <fcntl.h> |
---|
50 | #include <sys/ioctl.h> |
---|
51 | #include <sys/mman.h> |
---|
52 | #include <sys/socket.h> |
---|
53 | #include <sys/types.h> |
---|
54 | #include <sys/wait.h> |
---|
55 | |
---|
56 | #include "../../trunk/src/include/tinyxml/tinyxml.h" |
---|
57 | #include "../../trunk/src/include/tinyxml/tinystr.h" |
---|
58 | #include "sqlite3.h" |
---|
59 | //#include "sqlite3ext.h" |
---|
60 | |
---|
61 | typedef struct services_s * services_DB; |
---|
62 | |
---|
63 | struct services_s { |
---|
64 | char filename[64]; |
---|
65 | char tablename[64]; |
---|
66 | char command[2048]; |
---|
67 | sqlite3 *db; |
---|
68 | unsigned int num_columns; |
---|
69 | }; |
---|
70 | |
---|
71 | services_DB _services_DB; |
---|
72 | const char *_SML_Config; |
---|
73 | |
---|
74 | int callback(void *notUsed, int argc, char **argv, char **azColName){ |
---|
75 | int i; |
---|
76 | for(i=0; i<argc; i++){ |
---|
77 | printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); |
---|
78 | } |
---|
79 | printf("\n"); |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | ServiceManagementLayer::ServiceManagementLayer() |
---|
86 | { |
---|
87 | LOG("Creating Service Management Layer.\n"); |
---|
88 | shellSocketFD = -1; |
---|
89 | numberOfCognitiveEngines = 0; |
---|
90 | CE_Present = false; |
---|
91 | cogEngSrv = 1; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | ServiceManagementLayer::~ServiceManagementLayer() |
---|
96 | { |
---|
97 | char *errorMsg; |
---|
98 | strcpy(_services_DB->command, "drop table "); |
---|
99 | strcat(_services_DB->command, _services_DB->tablename); |
---|
100 | int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); |
---|
101 | if( rc!=SQLITE_OK && rc!=101 ) |
---|
102 | fprintf(stderr, "SQL error: %s\n", errorMsg); |
---|
103 | strcpy(_services_DB->command, "vacuum"); |
---|
104 | rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); |
---|
105 | if( rc!=SQLITE_OK && rc!=101 ) |
---|
106 | fprintf(stderr, "SQL error: %s\n", errorMsg); |
---|
107 | free(_services_DB); |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | ServiceManagementLayer::ServiceManagementLayer(const char* SML_Config, \ |
---|
112 | const char* serverName, const char* serverPort) |
---|
113 | { |
---|
114 | LOG("Creating Service Management Layer.\n"); |
---|
115 | _SML_Config = SML_Config; |
---|
116 | |
---|
117 | ConnectToShell(serverName, serverPort); |
---|
118 | CE_List = (CE_Reg *) malloc(10*sizeof(struct CE_Reg)); |
---|
119 | CE_List = new CE_Reg[10]; |
---|
120 | |
---|
121 | miss = new Mission[10]; |
---|
122 | for(int i = 0; i < 10; i++) |
---|
123 | miss[i].services = new Service[20]; |
---|
124 | |
---|
125 | Current_ID = 0; |
---|
126 | |
---|
127 | LoadConfiguration(SML_Config, miss); |
---|
128 | CreateServicesDB(); |
---|
129 | } |
---|
130 | |
---|
131 | //VERIFIED May 26 |
---|
132 | void |
---|
133 | ServiceManagementLayer::CreateServicesDB() |
---|
134 | { |
---|
135 | _services_DB = (services_DB) malloc(sizeof(struct services_s)); |
---|
136 | char *errorMsg; |
---|
137 | |
---|
138 | // create database |
---|
139 | |
---|
140 | // copy filename |
---|
141 | unsigned int i=0; |
---|
142 | strcpy(_services_DB->filename, "Services_Table"); |
---|
143 | |
---|
144 | // execute create database command |
---|
145 | // database handle |
---|
146 | //_services_DB->db = NULL; |
---|
147 | sqlite3_open(_services_DB->filename, &(_services_DB->db)); |
---|
148 | char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"}; |
---|
149 | |
---|
150 | // create table |
---|
151 | |
---|
152 | // copy tablename |
---|
153 | strcpy(_services_DB->tablename, "Services"); |
---|
154 | |
---|
155 | // number of columns in the table |
---|
156 | _services_DB->num_columns = 2; |
---|
157 | |
---|
158 | // generate command |
---|
159 | strcpy(_services_DB->command, "CREATE TABLE "); |
---|
160 | strcat(_services_DB->command, _services_DB->tablename); |
---|
161 | strcat(_services_DB->command, "("); |
---|
162 | //strcat(_services_DB->command, "SRVID INTEGER PRIMARY KEY, "); |
---|
163 | strcat(_services_DB->command, cols[0]); |
---|
164 | strcat(_services_DB->command, " INT, "); |
---|
165 | strcat(_services_DB->command, cols[1]); |
---|
166 | strcat(_services_DB->command, " TEXT"); |
---|
167 | strcat(_services_DB->command, ");"); |
---|
168 | |
---|
169 | // execute create table command |
---|
170 | sqlite3_stmt *ppStmt; /* OUT: Statement handle */ |
---|
171 | const char *pzTail; /* OUT: Pointer to unused portion of zSql */ |
---|
172 | |
---|
173 | int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command, 128, &ppStmt, &pzTail); |
---|
174 | if( rc!=SQLITE_OK && rc!=101 ) |
---|
175 | printf("SQL error(1): %d\n", rc); |
---|
176 | rc = sqlite3_step(ppStmt); |
---|
177 | if( rc!=SQLITE_OK && rc!=101 ) |
---|
178 | printf("SQL error(2): %d\n", rc); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | void |
---|
183 | ServiceManagementLayer::SendComponentType() |
---|
184 | { |
---|
185 | SendMessage(shellSocketFD, "response_sml"); |
---|
186 | LOG("SML responded to GetRemoteComponentType query.\n"); |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | void |
---|
191 | ServiceManagementLayer::ConnectToShell(const char* serverName, \ |
---|
192 | const char* serverPort) |
---|
193 | { |
---|
194 | shellSocketFD = ClientSocket(serverName, serverPort); |
---|
195 | |
---|
196 | //Set the shell socket to non-block mode |
---|
197 | /*int oldflags = fcntl (shellSocketFD, F_GETFL, 0); |
---|
198 | oldflags |= O_NONBLOCK; |
---|
199 | fcntl (shellSocketFD, F_SETFL, oldflags);*/ |
---|
200 | |
---|
201 | RegisterComponent(); |
---|
202 | } |
---|
203 | |
---|
204 | void |
---|
205 | ServiceManagementLayer::MessageHandler(int32_t ID) |
---|
206 | { |
---|
207 | char buffer[256]; |
---|
208 | memset(buffer, 0, 256); |
---|
209 | int32_t _FD; |
---|
210 | //printf("ID= %d\n", ID); |
---|
211 | |
---|
212 | if(ID != -1) |
---|
213 | _FD = CE_List[ID].FD; |
---|
214 | else |
---|
215 | _FD = shellSocketFD; |
---|
216 | |
---|
217 | //printf("FD=%d, shellFD=%d", _FD, shellSocketFD); |
---|
218 | ssize_t msgLength = recv(_FD, buffer, 256, MSG_PEEK); |
---|
219 | if(msgLength == 0){ |
---|
220 | //printf("Error reading from socket.\n"); |
---|
221 | //sleep(5); |
---|
222 | return; |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | size_t i; |
---|
227 | for(i = 0; i < 256; i++) { |
---|
228 | if(strcmp(&buffer[i], "\0") == 0) |
---|
229 | break; |
---|
230 | } |
---|
231 | // Read the message into msgBuffer |
---|
232 | recv(_FD, buffer, i + 1, 0); |
---|
233 | //printf("********* %s **********\n", buffer); |
---|
234 | // TODO |
---|
235 | // If we send integer op codes rather than strings, this process will be |
---|
236 | // MUCH faster since instead of donig string compares we can simply |
---|
237 | // switch on the integer value... |
---|
238 | /*if(strcmp(buffer, "register_service") == 0) { |
---|
239 | if(strcmp(buffer, "policy_geo") == 0) { |
---|
240 | } |
---|
241 | else if(strcmp(buffer, "policy_time") == 0) { |
---|
242 | } |
---|
243 | else if(strcmp(buffer, "policy_spectrum") == 0) { |
---|
244 | } |
---|
245 | else if(strcmp(buffer, "policy_spacial") == 0) { |
---|
246 | } |
---|
247 | } |
---|
248 | else if(strcmp(buffer, "deregister_service") == 0) { |
---|
249 | if(strcmp(buffer, "policy_geo") == 0) { |
---|
250 | } |
---|
251 | else if(strcmp(buffer, "policy_time") == 0) { |
---|
252 | } |
---|
253 | else if(strcmp(buffer, "policy_spectrum") == 0) { |
---|
254 | } |
---|
255 | else if(strcmp(buffer, "policy_spacial") == 0) { |
---|
256 | } |
---|
257 | }*/ |
---|
258 | if(strcmp(buffer, "query_component_type") == 0) { |
---|
259 | SendComponentType(); |
---|
260 | } |
---|
261 | else if(strcmp(buffer, "reset_sml") == 0) { |
---|
262 | Reset(); |
---|
263 | } |
---|
264 | else if(strcmp(buffer, "shutdown_sml") == 0) { |
---|
265 | Shutdown(); |
---|
266 | } |
---|
267 | else if(strcmp(buffer, "register_engine_cognitive") == 0) { |
---|
268 | RegisterCognitiveEngine(ID); |
---|
269 | } |
---|
270 | else if(strcmp(buffer, "register_service") == 0) { |
---|
271 | ReceiveServices(ID); |
---|
272 | } |
---|
273 | else if(strcmp(buffer, "send_component_type") == 0) { |
---|
274 | SendComponentType(); |
---|
275 | } |
---|
276 | else if(strcmp(buffer, "list_services") == 0) { |
---|
277 | ListServices(); |
---|
278 | } |
---|
279 | else if(strcmp(buffer, "set_active_mission") == 0) { |
---|
280 | SetActiveMission(); |
---|
281 | } |
---|
282 | else if(strcmp(buffer, "request_optimization") == 0) { |
---|
283 | PerformActiveMission(); |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | |
---|
288 | |
---|
289 | void |
---|
290 | ServiceManagementLayer::Shutdown() |
---|
291 | { |
---|
292 | DeregisterComponent(); |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | void |
---|
297 | ServiceManagementLayer::Reset() |
---|
298 | { |
---|
299 | DeregisterComponent(); |
---|
300 | LoadConfiguration(_SML_Config, miss); |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | void |
---|
305 | ServiceManagementLayer::RegisterComponent() |
---|
306 | { |
---|
307 | SendMessage(shellSocketFD, "register_sml"); |
---|
308 | LOG("ServiceManagementLayer:: Registration message sent.\n"); |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | void |
---|
313 | ServiceManagementLayer::DeregisterComponent() |
---|
314 | { |
---|
315 | SendMessage(shellSocketFD, "deregister_sml"); |
---|
316 | LOG("ServiceManagementLayer:: Deregistration message sent.\n"); |
---|
317 | |
---|
318 | shutdown(shellSocketFD, 2); |
---|
319 | close(shellSocketFD); |
---|
320 | shellSocketFD = -1; |
---|
321 | LOG("ServiceManagementLayer:: Shell socket closed.\n"); |
---|
322 | } |
---|
323 | |
---|
324 | /* Streams config data directly from the shell to the CE, and checks |
---|
325 | * for an "ack" message from the CE after every sent message |
---|
326 | * to know when to stop communication. |
---|
327 | */ |
---|
328 | void |
---|
329 | ServiceManagementLayer::TransferRadioConfiguration(int32_t ID) |
---|
330 | { |
---|
331 | struct timeval selTimeout; |
---|
332 | fd_set sockSet; |
---|
333 | int32_t rc = 0; |
---|
334 | char buffer[256]; |
---|
335 | //Send data until the CE sends an ACK message back |
---|
336 | while(rc==0){ |
---|
337 | memset(buffer, 0, 256); |
---|
338 | //Receive data from Shell |
---|
339 | ReadMessage(shellSocketFD, buffer); |
---|
340 | //Send data to CE |
---|
341 | SendMessage(CE_List[ID].FD, buffer); |
---|
342 | FD_ZERO(&sockSet); |
---|
343 | FD_SET(CE_List[ID].FD, &sockSet); |
---|
344 | selTimeout.tv_sec = 0; |
---|
345 | selTimeout.tv_usec = 0; |
---|
346 | //Check if there is a message on the CE socket ready to be processed |
---|
347 | rc=select(CE_List[ID].FD + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
348 | } |
---|
349 | memset(buffer, 0, 256); |
---|
350 | ReadMessage(CE_List[ID].FD, buffer); |
---|
351 | SendMessage(shellSocketFD, buffer); |
---|
352 | } |
---|
353 | |
---|
354 | //Simmilar to TransferRadioConfig, just with Experience data |
---|
355 | void |
---|
356 | ServiceManagementLayer::TransferExperience(int32_t ID) |
---|
357 | { |
---|
358 | struct timeval selTimeout; |
---|
359 | fd_set sockSet; |
---|
360 | int32_t rc = 0; |
---|
361 | char buffer[256]; |
---|
362 | //Send data until the CE sends an ACK message back |
---|
363 | while(rc==0){ |
---|
364 | memset(buffer, 0, 256); |
---|
365 | //Receive data from Shell |
---|
366 | ReadMessage(shellSocketFD, buffer); |
---|
367 | //Send data to CE |
---|
368 | SendMessage(CE_List[ID].FD, buffer); |
---|
369 | FD_ZERO(&sockSet); |
---|
370 | FD_SET(CE_List[ID].FD, &sockSet); |
---|
371 | selTimeout.tv_sec = 0; |
---|
372 | selTimeout.tv_usec = 0; |
---|
373 | //Check if there is a message on the CE socket ready to be processed |
---|
374 | rc = select(CE_List[ID].FD + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
375 | } |
---|
376 | memset(buffer, 0, 256); |
---|
377 | ReadMessage(CE_List[ID].FD, buffer); |
---|
378 | SendMessage(shellSocketFD, buffer); |
---|
379 | } |
---|
380 | |
---|
381 | //VERIFIED May 26 |
---|
382 | void |
---|
383 | ServiceManagementLayer::ReceiveServices(int32_t ID) |
---|
384 | { |
---|
385 | char buffer[256]; |
---|
386 | memset(buffer, 0, 256); |
---|
387 | ReadMessage(CE_List[ID].FD, buffer); |
---|
388 | char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"}; |
---|
389 | |
---|
390 | // generate command |
---|
391 | //printf("%s\n", _services_DB->command); |
---|
392 | strcpy(_services_DB->command, "insert into "); |
---|
393 | strcat(_services_DB->command, _services_DB->tablename); |
---|
394 | strcat(_services_DB->command, " ("); |
---|
395 | strcat(_services_DB->command, cols[0]); |
---|
396 | strcat(_services_DB->command, ", "); |
---|
397 | strcat(_services_DB->command, cols[1]); |
---|
398 | strcat(_services_DB->command, ") "); |
---|
399 | strcat(_services_DB->command, " values("); |
---|
400 | sprintf(_services_DB->command, "%s%d", _services_DB->command, ID); |
---|
401 | strcat(_services_DB->command, ", '"); |
---|
402 | strcat(_services_DB->command, buffer); |
---|
403 | strcat(_services_DB->command, "');"); |
---|
404 | |
---|
405 | //printf("search command: %s\n", _services_DB->command); |
---|
406 | // execute add command |
---|
407 | char *errorMsg; |
---|
408 | int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); |
---|
409 | if( rc!=SQLITE_OK && rc!=101 ) |
---|
410 | fprintf(stderr, "SQL error: %s\n", errorMsg); |
---|
411 | printf("SML: Registering service '%s' from component number '%d'\n", buffer, ID); |
---|
412 | |
---|
413 | } |
---|
414 | |
---|
415 | //This method associates the services that components provide with the services that are requested in the mission |
---|
416 | //Each service in the mission is given the ID and FD of a component that has registered to provide that service |
---|
417 | //Deregistration is okay until this method is called without a reload, but if deregistration occurs after this |
---|
418 | // method is called it needs to be called again even if other engines also provide the services |
---|
419 | |
---|
420 | //Verified May 29 |
---|
421 | void |
---|
422 | ServiceManagementLayer::SetActiveMission() |
---|
423 | { |
---|
424 | char buffer[256]; |
---|
425 | memset(buffer, 0, 256); |
---|
426 | ReadMessage(shellSocketFD, buffer); |
---|
427 | int32_t missID = atoi(buffer); |
---|
428 | for(activeMission = 0; activeMission < 10; activeMission++) |
---|
429 | { |
---|
430 | if(miss[activeMission].missionID == missID) |
---|
431 | break; |
---|
432 | } |
---|
433 | for(int i = 0; i < miss[activeMission].numServices; i++) |
---|
434 | { |
---|
435 | strcpy(_services_DB->command, "select "); |
---|
436 | strcat(_services_DB->command, _services_DB->tablename); |
---|
437 | strcat(_services_DB->command, ".* from "); |
---|
438 | strcat(_services_DB->command, _services_DB->tablename); |
---|
439 | strcat(_services_DB->command, " where Service_Name=="); |
---|
440 | sprintf(_services_DB->command, "%s'%s';", _services_DB->command, miss[activeMission].services[i].name.c_str()); |
---|
441 | |
---|
442 | |
---|
443 | sqlite3_stmt * pStatement; |
---|
444 | int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command, -1, &pStatement, NULL); |
---|
445 | if (rc == SQLITE_OK){ |
---|
446 | if (sqlite3_step(pStatement) == SQLITE_ROW) |
---|
447 | miss[activeMission].services[i].componentID = sqlite3_column_int(pStatement, 0); |
---|
448 | else { |
---|
449 | printf("services_DB:: Mission requires service not provided by any connected component.\n"); |
---|
450 | rc=31337; |
---|
451 | } |
---|
452 | } else { |
---|
453 | printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_services_DB->command); |
---|
454 | } |
---|
455 | |
---|
456 | sqlite3_finalize(pStatement); |
---|
457 | miss[activeMission].services[i].socketFD = CE_List[miss[activeMission].services[i].componentID].FD; |
---|
458 | } |
---|
459 | printf("\nhere ---%d, %d---\n", miss[activeMission].services[0].componentID, miss[activeMission].services[1].componentID); |
---|
460 | } |
---|
461 | |
---|
462 | //This is a helper method for the "PerformActiveMission" function |
---|
463 | //It takes the ID's of a source and a destination and does a raw transfer between them |
---|
464 | //It knows when to stop when an "ack" signal returns from the destination component |
---|
465 | //An ID of "-1" coresponds to the shell |
---|
466 | void |
---|
467 | ServiceManagementLayer::TransactData(int32_t sourceID, int32_t destID) |
---|
468 | { |
---|
469 | printf("transact occuring between %d and %d\n", sourceID, destID); |
---|
470 | int32_t FD_src, FD_dest; |
---|
471 | struct timeval selTimeout; |
---|
472 | if(sourceID==-1) |
---|
473 | FD_src=shellSocketFD; |
---|
474 | else |
---|
475 | FD_src=CE_List[sourceID].FD; |
---|
476 | if(destID==-1) |
---|
477 | FD_dest=shellSocketFD; |
---|
478 | else |
---|
479 | FD_dest=CE_List[destID].FD; |
---|
480 | |
---|
481 | int32_t rc = 0; |
---|
482 | fd_set sockSet; |
---|
483 | char buffer[256]; |
---|
484 | //Send data until the CE sends an ACK message back |
---|
485 | while(rc==0){ |
---|
486 | memset(buffer, 0, 256); |
---|
487 | //Receive data from Shell |
---|
488 | printf("reading data from %d\n", FD_src); |
---|
489 | ReadMessage(FD_src, buffer); |
---|
490 | //Send data to CE |
---|
491 | printf("sending data to %d\n", FD_dest); |
---|
492 | SendMessage(FD_dest, buffer); |
---|
493 | FD_ZERO(&sockSet); |
---|
494 | FD_SET(FD_dest, &sockSet); |
---|
495 | selTimeout.tv_sec = 0; |
---|
496 | selTimeout.tv_usec = 50; |
---|
497 | //Check if there is a message on the CE socket ready to be processed |
---|
498 | rc=select(FD_dest + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
499 | } |
---|
500 | memset(buffer, 0, 256); |
---|
501 | ReadMessage(FD_dest, buffer); |
---|
502 | printf("done transact data %s\n", buffer); |
---|
503 | } |
---|
504 | |
---|
505 | // Rules for active missions (currently) |
---|
506 | // -Three inputs/outputs per service and per mission |
---|
507 | // -Inputs simply define a path, so multiple variables can be transmitted over the same input |
---|
508 | // -Each component that takes input is responsible for sending an "ack" signal when transmission is complete |
---|
509 | // -Transmission will continue until the "ack" signal is recieved |
---|
510 | // -Any input from shell to a CE must be the first input |
---|
511 | // -CE-CE inputs must be listed in the order the ouptuts are generated |
---|
512 | // -No ability to store data locally at this time, so all CE's must be ready to receive data as soon as it is sent |
---|
513 | // -Shell should be ready for output in order that it is generated |
---|
514 | // -If/While supported and nesting too (except nested while loops) |
---|
515 | // -Conditions must be boolean flags |
---|
516 | // -Flags are set by putting either the character string "true" or "false" on the buffer |
---|
517 | |
---|
518 | // This function works by first sending the inputs from the shell to the appropriate components |
---|
519 | // The first service should begin immeadiately, as should any others who have all of their input paramaters |
---|
520 | // When they complete, the output path is found and the data is transfered as it becomes available |
---|
521 | // Presumably at this point the second function has all of it's paramaters, so it begins to compute, and the cycle repeats |
---|
522 | // If the generated output is an overall output, it is sent on to the shell |
---|
523 | // "if" and "while" statements are handled by setting up a faux service that has a true input, a false input, and a boolean flag |
---|
524 | // If the true input is non-NULL and the flag is true, the statements execute |
---|
525 | // Likewise, if the false input is non-NULL and the flag is false, the statements execute |
---|
526 | // These flags are set during execution any time one of these faux services' inputs appear in an output statement |
---|
527 | // |
---|
528 | void |
---|
529 | ServiceManagementLayer::PerformActiveMission() |
---|
530 | { |
---|
531 | int i = 0; |
---|
532 | char *buffer; |
---|
533 | if(!miss[activeMission].input[0].empty()){ |
---|
534 | printf("mission input 1 not empty\n"); |
---|
535 | for(int j = 0; j < miss[activeMission].numServices; j++) |
---|
536 | { |
---|
537 | if(miss[activeMission].services[j].input[0].compare(miss[activeMission].input[0]) == 0){ |
---|
538 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
539 | memset(buffer, 0, 256); |
---|
540 | ReadMessage(shellSocketFD, buffer); |
---|
541 | if(strcmp(buffer, "false")==0) |
---|
542 | miss[activeMission].services[j].cond_flag=false; |
---|
543 | else |
---|
544 | miss[activeMission].services[j].cond_flag=true; |
---|
545 | break; |
---|
546 | } |
---|
547 | SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); |
---|
548 | SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); |
---|
549 | TransactData(-1, miss[activeMission].services[j].componentID); |
---|
550 | } |
---|
551 | } |
---|
552 | } |
---|
553 | if(!miss[activeMission].input[1].empty()){ |
---|
554 | for(int j = 0; j < miss[activeMission].numServices; j++) |
---|
555 | { |
---|
556 | if(miss[activeMission].services[j].input[1].compare(miss[activeMission].input[0]) == 0){ |
---|
557 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
558 | memset(buffer, 0, 256); |
---|
559 | ReadMessage(shellSocketFD, buffer); |
---|
560 | if(strcmp(buffer, "false")==0) |
---|
561 | miss[activeMission].services[j].cond_flag=false; |
---|
562 | else |
---|
563 | miss[activeMission].services[j].cond_flag=true; |
---|
564 | break; |
---|
565 | } |
---|
566 | SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); |
---|
567 | SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); |
---|
568 | TransactData(-1, miss[activeMission].services[j].componentID); |
---|
569 | } |
---|
570 | if(miss[activeMission].services[j].input[1].compare(miss[activeMission].input[1]) == 0) |
---|
571 | TransactData(-1, miss[activeMission].services[j].componentID); |
---|
572 | } |
---|
573 | } |
---|
574 | if(!miss[activeMission].input[2].empty()){ |
---|
575 | for(int j = 0; j < miss[activeMission].numServices; j++) |
---|
576 | { |
---|
577 | if(miss[activeMission].services[j].input[2].compare(miss[activeMission].input[0]) == 0){ |
---|
578 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
579 | memset(buffer, 0, 256); |
---|
580 | ReadMessage(shellSocketFD, buffer); |
---|
581 | if(strcmp(buffer, "false")==0) |
---|
582 | miss[activeMission].services[j].cond_flag=false; |
---|
583 | else |
---|
584 | miss[activeMission].services[j].cond_flag=true; |
---|
585 | break; |
---|
586 | } |
---|
587 | SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); |
---|
588 | SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); |
---|
589 | TransactData(-1, miss[activeMission].services[j].componentID); |
---|
590 | } |
---|
591 | if(miss[activeMission].services[j].input[2].compare(miss[activeMission].input[1]) == 0) |
---|
592 | TransactData(-1, miss[activeMission].services[j].componentID); |
---|
593 | if(miss[activeMission].services[j].input[2].compare(miss[activeMission].input[2]) == 0) |
---|
594 | TransactData(-1, miss[activeMission].services[j].componentID); |
---|
595 | } |
---|
596 | } |
---|
597 | while(i != miss[activeMission].numServices) |
---|
598 | { |
---|
599 | if(strcmp(miss[activeMission].services[i].name.c_str(), "while")==0) |
---|
600 | { |
---|
601 | int32_t stmt_i = i; |
---|
602 | i+=miss[activeMission].services[stmt_i].num_conds; |
---|
603 | while((miss[activeMission].services[stmt_i].cond_flag && !miss[activeMission].services[stmt_i].input[0].empty()) ||\ |
---|
604 | (!miss[activeMission].services[stmt_i].cond_flag && !miss[activeMission].services[stmt_i].input[1].empty())){ |
---|
605 | for(int32_t k=stmt_i+1; k <= stmt_i+miss[activeMission].services[stmt_i].num_conds; k++){ |
---|
606 | if(strcmp(miss[activeMission].services[i].name.c_str(), "if")==0){ |
---|
607 | int32_t stmt_k = k; |
---|
608 | if((miss[activeMission].services[stmt_k].cond_flag && !miss[activeMission].services[stmt_k].input[0].empty()) ||\ |
---|
609 | (!miss[activeMission].services[stmt_k].cond_flag && !miss[activeMission].services[stmt_k].input[1].empty())){ |
---|
610 | for(int n = stmt_k+1; n <= stmt_k+miss[activeMission].services[stmt_k].num_conds; n++){ |
---|
611 | for(int m = 0; m < 3; m++){ |
---|
612 | for(int j = 0; j < 3; j++){ |
---|
613 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].output[j])==0){ |
---|
614 | TransactData(miss[activeMission].services[k].componentID, -1); |
---|
615 | break; |
---|
616 | } |
---|
617 | } |
---|
618 | for(int j = 0; j < miss[activeMission].numServices; j++){ |
---|
619 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[0]) == 0){ |
---|
620 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
621 | memset(buffer, 0, 256); |
---|
622 | ReadMessage(miss[activeMission].services[k].socketFD, buffer); |
---|
623 | if(strcmp(buffer, "false")==0) |
---|
624 | miss[activeMission].services[j].cond_flag=false; |
---|
625 | else |
---|
626 | miss[activeMission].services[j].cond_flag=true; |
---|
627 | break; |
---|
628 | } |
---|
629 | SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); |
---|
630 | SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); |
---|
631 | TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); |
---|
632 | break; |
---|
633 | } |
---|
634 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[1]) == 0){ |
---|
635 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
636 | memset(buffer, 0, 256); |
---|
637 | ReadMessage(miss[activeMission].services[k].socketFD, buffer); |
---|
638 | if(strcmp(buffer, "false")==0) |
---|
639 | miss[activeMission].services[j].cond_flag=false; |
---|
640 | else |
---|
641 | miss[activeMission].services[j].cond_flag=true; |
---|
642 | break; |
---|
643 | } |
---|
644 | TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); |
---|
645 | break; |
---|
646 | } |
---|
647 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[2]) == 0){ |
---|
648 | TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); |
---|
649 | break; |
---|
650 | } |
---|
651 | } |
---|
652 | } |
---|
653 | } |
---|
654 | } |
---|
655 | } |
---|
656 | else{ |
---|
657 | for(int m = 0; m < 3; m++) |
---|
658 | { |
---|
659 | for(int j = 0; j < 3; j++){ |
---|
660 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].output[j])==0){ |
---|
661 | TransactData(miss[activeMission].services[k].componentID, -1); |
---|
662 | break; |
---|
663 | } |
---|
664 | } |
---|
665 | for(int j = 0; j < miss[activeMission].numServices; j++){ |
---|
666 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[0]) == 0){ |
---|
667 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
668 | memset(buffer, 0, 256); |
---|
669 | ReadMessage(miss[activeMission].services[k].socketFD, buffer); |
---|
670 | if(strcmp(buffer, "false")==0) |
---|
671 | miss[activeMission].services[j].cond_flag=false; |
---|
672 | else |
---|
673 | miss[activeMission].services[j].cond_flag=true; |
---|
674 | break; |
---|
675 | } |
---|
676 | SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); |
---|
677 | SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); |
---|
678 | TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); |
---|
679 | break; |
---|
680 | } |
---|
681 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[1]) == 0){ |
---|
682 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
683 | memset(buffer, 0, 256); |
---|
684 | ReadMessage(miss[activeMission].services[k].socketFD, buffer); |
---|
685 | if(strcmp(buffer, "false")==0) |
---|
686 | miss[activeMission].services[j].cond_flag=false; |
---|
687 | else |
---|
688 | miss[activeMission].services[j].cond_flag=true; |
---|
689 | break; |
---|
690 | } |
---|
691 | TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); |
---|
692 | break; |
---|
693 | } |
---|
694 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[2]) == 0){ |
---|
695 | TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); |
---|
696 | break; |
---|
697 | } |
---|
698 | } |
---|
699 | } |
---|
700 | } |
---|
701 | } |
---|
702 | } |
---|
703 | } |
---|
704 | else if(strcmp(miss[activeMission].services[i].name.c_str(), "if")==0) |
---|
705 | { |
---|
706 | int32_t stmt_i = i; |
---|
707 | i+=miss[activeMission].services[stmt_i].num_conds; |
---|
708 | if((miss[activeMission].services[stmt_i].cond_flag && !miss[activeMission].services[stmt_i].input[0].empty()) ||\ |
---|
709 | (!miss[activeMission].services[stmt_i].cond_flag && !miss[activeMission].services[stmt_i].input[1].empty())){ |
---|
710 | for(int32_t k=stmt_i+1; k <= stmt_i+miss[activeMission].services[stmt_i].num_conds; k++){ |
---|
711 | if(strcmp(miss[activeMission].services[i].name.c_str(), "if")==0){ |
---|
712 | int32_t stmt_k = k; |
---|
713 | if((miss[activeMission].services[stmt_k].cond_flag && !miss[activeMission].services[stmt_k].input[0].empty()) ||\ |
---|
714 | (!miss[activeMission].services[stmt_k].cond_flag && !miss[activeMission].services[stmt_k].input[1].empty())){ |
---|
715 | for(int n = stmt_k+1; n <= stmt_k+miss[activeMission].services[stmt_k].num_conds; n++){ |
---|
716 | for(int m = 0; m < 3; m++) |
---|
717 | { |
---|
718 | for(int j = 0; j < 3; j++){ |
---|
719 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].output[j])==0){ |
---|
720 | TransactData(miss[activeMission].services[k].componentID, -1); |
---|
721 | break; |
---|
722 | } |
---|
723 | } |
---|
724 | for(int j = 0; j < miss[activeMission].numServices; j++){ |
---|
725 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[0]) == 0){ |
---|
726 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
727 | memset(buffer, 0, 256); |
---|
728 | ReadMessage(miss[activeMission].services[k].socketFD, buffer); |
---|
729 | if(strcmp(buffer, "false")==0) |
---|
730 | miss[activeMission].services[j].cond_flag=false; |
---|
731 | else |
---|
732 | miss[activeMission].services[j].cond_flag=true; |
---|
733 | break; |
---|
734 | } |
---|
735 | SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); |
---|
736 | SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); |
---|
737 | TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); |
---|
738 | break; |
---|
739 | } |
---|
740 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[1]) == 0){ |
---|
741 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
742 | memset(buffer, 0, 256); |
---|
743 | ReadMessage(miss[activeMission].services[k].socketFD, buffer); |
---|
744 | if(strcmp(buffer, "false")==0) |
---|
745 | miss[activeMission].services[j].cond_flag=false; |
---|
746 | else |
---|
747 | miss[activeMission].services[j].cond_flag=true; |
---|
748 | break; |
---|
749 | } |
---|
750 | TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); |
---|
751 | break; |
---|
752 | } |
---|
753 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[2]) == 0){ |
---|
754 | TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); |
---|
755 | break; |
---|
756 | } |
---|
757 | } |
---|
758 | } |
---|
759 | } |
---|
760 | } |
---|
761 | } |
---|
762 | else{ |
---|
763 | for(int m = 0; m < 3; m++) |
---|
764 | { |
---|
765 | for(int j = 0; j < 3; j++){ |
---|
766 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].output[j])==0){ |
---|
767 | TransactData(miss[activeMission].services[k].componentID, -1); |
---|
768 | break; |
---|
769 | } |
---|
770 | } |
---|
771 | for(int j = 0; j < miss[activeMission].numServices; j++){ |
---|
772 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[0]) == 0){ |
---|
773 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
774 | ReadMessage(miss[activeMission].services[k].socketFD, buffer); |
---|
775 | if(strcmp(buffer, "false")==0) |
---|
776 | miss[activeMission].services[j].cond_flag=false; |
---|
777 | else |
---|
778 | miss[activeMission].services[j].cond_flag=true; |
---|
779 | break; |
---|
780 | } |
---|
781 | SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); |
---|
782 | SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); |
---|
783 | TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); |
---|
784 | break; |
---|
785 | } |
---|
786 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[1]) == 0){ |
---|
787 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
788 | ReadMessage(miss[activeMission].services[k].socketFD, buffer); |
---|
789 | if(strcmp(buffer, "false")==0) |
---|
790 | miss[activeMission].services[j].cond_flag=false; |
---|
791 | else |
---|
792 | miss[activeMission].services[j].cond_flag=true; |
---|
793 | break; |
---|
794 | } |
---|
795 | TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); |
---|
796 | break; |
---|
797 | } |
---|
798 | if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[2]) == 0){ |
---|
799 | TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); |
---|
800 | break; |
---|
801 | } |
---|
802 | } |
---|
803 | } |
---|
804 | } |
---|
805 | } |
---|
806 | } |
---|
807 | } |
---|
808 | else{ |
---|
809 | for(int m = 0; m < 3; m++) |
---|
810 | { |
---|
811 | printf("\ninside SAM\n"); |
---|
812 | for(int j = 0; j < 3; j++){ |
---|
813 | if(miss[activeMission].services[i].output[m].compare(miss[activeMission].output[j])==0 && \ |
---|
814 | !miss[activeMission].services[i].output[j].empty()){ |
---|
815 | printf("i=%d, m=%d, j=%d\n", i, m, j); |
---|
816 | TransactData(miss[activeMission].services[i].componentID, -1); |
---|
817 | break; |
---|
818 | } |
---|
819 | } |
---|
820 | for(int j = 0; j < miss[activeMission].numServices; j++){ |
---|
821 | if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[0]) == 0 && \ |
---|
822 | !miss[activeMission].services[i].output[m].empty()){ |
---|
823 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
824 | ReadMessage(miss[activeMission].services[i].socketFD, buffer); |
---|
825 | if(strcmp(buffer, "false")==0) |
---|
826 | miss[activeMission].services[j].cond_flag=false; |
---|
827 | else |
---|
828 | miss[activeMission].services[j].cond_flag=true; |
---|
829 | break; |
---|
830 | } |
---|
831 | SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); |
---|
832 | SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); |
---|
833 | TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID); |
---|
834 | break; |
---|
835 | } |
---|
836 | if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[1]) == 0 && \ |
---|
837 | !miss[activeMission].services[i].output[m].empty()){ |
---|
838 | if(miss[activeMission].services[j].num_conds > 0){ |
---|
839 | ReadMessage(miss[activeMission].services[i].socketFD, buffer); |
---|
840 | if(strcmp(buffer, "false")==0) |
---|
841 | miss[activeMission].services[j].cond_flag=false; |
---|
842 | else |
---|
843 | miss[activeMission].services[j].cond_flag=true; |
---|
844 | break; |
---|
845 | } |
---|
846 | TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID); |
---|
847 | break; |
---|
848 | } |
---|
849 | if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[2]) == 0 && \ |
---|
850 | !miss[activeMission].services[i].output[m].empty()){ |
---|
851 | TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID); |
---|
852 | break; |
---|
853 | } |
---|
854 | } |
---|
855 | } |
---|
856 | } |
---|
857 | i++; |
---|
858 | } |
---|
859 | } |
---|
860 | |
---|
861 | |
---|
862 | //Verified May 26 |
---|
863 | void |
---|
864 | ServiceManagementLayer::ListServices() |
---|
865 | { |
---|
866 | // generate commandi |
---|
867 | strcpy(_services_DB->command, "select "); |
---|
868 | strcat(_services_DB->command, _services_DB->tablename); |
---|
869 | strcat(_services_DB->command, ".* from "); |
---|
870 | strcat(_services_DB->command, _services_DB->tablename); |
---|
871 | strcat(_services_DB->command, ";"); |
---|
872 | |
---|
873 | // execute print (select all) command |
---|
874 | char *errorMsg; |
---|
875 | int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); |
---|
876 | if( rc!=SQLITE_OK && rc!=101 ) |
---|
877 | fprintf(stderr, "SQL error: %s\n", errorMsg); |
---|
878 | printf("database %s, table %s:\n", _services_DB->filename, _services_DB->tablename); |
---|
879 | } |
---|
880 | |
---|
881 | |
---|
882 | void |
---|
883 | ServiceManagementLayer::ReloadConfiguration() |
---|
884 | { |
---|
885 | LOG("ServiceManagementLayer:: Reloading Configuration.\n"); |
---|
886 | free(miss); |
---|
887 | miss = new Mission[10]; |
---|
888 | for(int i = 0; i < 10; i++) |
---|
889 | miss[i].services = new Service[20]; |
---|
890 | LoadConfiguration(_SML_Config, miss); |
---|
891 | } |
---|
892 | |
---|
893 | //IMPORTANT - See formatting instructions for correct parsing of data |
---|
894 | //Can currently handle 3 inputs and 3 outputs per service, but easily expandable |
---|
895 | //Also, can handle one layer of nested conditional statements, but could |
---|
896 | //be expanded to meet additional needs. Only support now is for straight bool flags, |
---|
897 | //but support could be added for more complex conditionals later. |
---|
898 | |
---|
899 | //Components assigned to mission during "set active mission" stage so that |
---|
900 | //components can still continue to register after the configuration is loaded |
---|
901 | |
---|
902 | //Verified May 26 |
---|
903 | void |
---|
904 | ServiceManagementLayer::LoadConfiguration(const char *SML_Config, Mission* &mList) |
---|
905 | { |
---|
906 | TiXmlElement *pMission; |
---|
907 | TiXmlElement *pService; |
---|
908 | TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3; |
---|
909 | TiXmlHandle hRoot(0); |
---|
910 | //printf("ServiceManagementLayer:: Loading Configuration.\n"); |
---|
911 | TiXmlDocument doc("."); |
---|
912 | doc.LoadFile(SML_Config); |
---|
913 | bool loadOkay = doc.LoadFile(); |
---|
914 | if(!loadOkay) |
---|
915 | printf("Loading SML configuration failed: %s\n", SML_Config); |
---|
916 | |
---|
917 | TiXmlHandle hDoc(&doc); |
---|
918 | |
---|
919 | pMission = hDoc.FirstChildElement().Element(); |
---|
920 | |
---|
921 | if(!pMission) |
---|
922 | printf("No valid root!"); |
---|
923 | |
---|
924 | hRoot = TiXmlHandle(pMission); |
---|
925 | pService = pMission->FirstChildElement(); |
---|
926 | int32_t mission_num = 0; |
---|
927 | //Iterate through the missions |
---|
928 | for(pChild0 = pMission->FirstChildElement(); pChild0 ; \ |
---|
929 | pChild0 = pChild0->NextSiblingElement()) |
---|
930 | { |
---|
931 | int32_t service_num = 0; |
---|
932 | uint16_t cond_array[] = {0, 0}; |
---|
933 | //printf("mission_num = %d\n", mission_num); |
---|
934 | memset(cond_array, 0, 2); |
---|
935 | |
---|
936 | for(pChild1 = pChild0->FirstChildElement(); pChild1; \ |
---|
937 | pChild1 = pChild1->NextSiblingElement()) |
---|
938 | { |
---|
939 | int32_t conditional_0 = service_num; |
---|
940 | for(pChild2 = pChild1->FirstChildElement(); \ |
---|
941 | pChild2; pChild2 = pChild2->NextSiblingElement()) |
---|
942 | { |
---|
943 | service_num++; |
---|
944 | int32_t conditional_1 = service_num; |
---|
945 | for(pChild3 = pChild2->FirstChildElement(); \ |
---|
946 | pChild3; pChild3 = pChild3 ->NextSiblingElement()) |
---|
947 | { |
---|
948 | service_num++; |
---|
949 | mList[mission_num].services[service_num].name = pChild3->Attribute("name"); |
---|
950 | if(pChild3->Attribute("input1")) |
---|
951 | mList[mission_num].services[service_num].input[0] = pChild3->Attribute("input1"); |
---|
952 | if(pChild3->Attribute("input2")) |
---|
953 | mList[mission_num].services[service_num].input[1] = pChild3->Attribute("input2"); |
---|
954 | if(pChild3->Attribute("input3")) |
---|
955 | mList[mission_num].services[service_num].input[2] = pChild3->Attribute("input3"); |
---|
956 | if(pChild3->Attribute("output1")) |
---|
957 | mList[mission_num].services[service_num].output[0] = pChild3->Attribute("output1"); |
---|
958 | if(pChild3->Attribute("output2")) |
---|
959 | mList[mission_num].services[service_num].output[1] = pChild3->Attribute("output2"); |
---|
960 | if(pChild3->Attribute("output3")) |
---|
961 | mList[mission_num].services[service_num].output[2] = pChild3->Attribute("output3"); |
---|
962 | cond_array[1]++; |
---|
963 | } |
---|
964 | |
---|
965 | if(conditional_1 != service_num){ |
---|
966 | mList[mission_num].services[conditional_1].name = pChild2->Value(); |
---|
967 | if(pChild2->Attribute("input_t")) |
---|
968 | mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input_t"); |
---|
969 | if(pChild2->Attribute("input_f")) |
---|
970 | mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input_f"); |
---|
971 | } |
---|
972 | else{ |
---|
973 | mList[mission_num].services[conditional_1].name = pChild2->Attribute("name"); |
---|
974 | if(pChild2->Attribute("input1")) |
---|
975 | mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input1"); |
---|
976 | if(pChild2->Attribute("input2")) |
---|
977 | mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input2"); |
---|
978 | if(pChild2->Attribute("input3")) |
---|
979 | mList[mission_num].services[service_num].input[2] = pChild2->Attribute("input3"); |
---|
980 | if(pChild2->Attribute("output1")) |
---|
981 | mList[mission_num].services[service_num].output[0] = pChild2->Attribute("output1"); |
---|
982 | if(pChild2->Attribute("output2")) |
---|
983 | mList[mission_num].services[service_num].output[1] = pChild2->Attribute("output2"); |
---|
984 | if(pChild2->Attribute("output3")) |
---|
985 | mList[mission_num].services[service_num].output[2] = pChild2->Attribute("output3"); |
---|
986 | } |
---|
987 | |
---|
988 | mList[mission_num].services[conditional_1].num_conds = cond_array[1]; |
---|
989 | cond_array[1] = 0; |
---|
990 | cond_array[0]++; |
---|
991 | } |
---|
992 | if(conditional_0 != service_num) |
---|
993 | mList[mission_num].services[conditional_0].name = pChild1->Value(); |
---|
994 | if(pChild1->Attribute("input_t")) |
---|
995 | mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input_t"); |
---|
996 | if(pChild1->Attribute("input_f")) |
---|
997 | mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input_f"); |
---|
998 | else{ |
---|
999 | mList[mission_num].services[conditional_0].name = pChild1->Attribute("name"); |
---|
1000 | if(pChild1->Attribute("input1")) |
---|
1001 | mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input1"); |
---|
1002 | if(pChild1->Attribute("input2")) |
---|
1003 | mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input2"); |
---|
1004 | if(pChild1->Attribute("input3")) |
---|
1005 | mList[mission_num].services[service_num].input[2] = pChild1->Attribute("input3"); |
---|
1006 | if(pChild1->Attribute("output1")) |
---|
1007 | mList[mission_num].services[service_num].output[0] = pChild1->Attribute("output1"); |
---|
1008 | if(pChild1->Attribute("output2")) |
---|
1009 | mList[mission_num].services[service_num].output[1] = pChild1->Attribute("output2"); |
---|
1010 | if(pChild1->Attribute("output3")) |
---|
1011 | mList[mission_num].services[service_num].output[2] = pChild1->Attribute("output3"); |
---|
1012 | } |
---|
1013 | |
---|
1014 | mList[mission_num].services[conditional_0].num_conds = cond_array[0]; |
---|
1015 | //printf("hello\n"); |
---|
1016 | printf("input1=%s, outpu1=%s\n", mList[mission_num].services[service_num].input[0].c_str(), mList[mission_num].services[service_num].output[0].c_str()); |
---|
1017 | cond_array[0] = 0; |
---|
1018 | service_num++; |
---|
1019 | } |
---|
1020 | mList[mission_num].numServices = service_num; |
---|
1021 | mList[mission_num].name = pChild0->Attribute("name"); |
---|
1022 | mList[mission_num].missionID = atoi(pChild0->Attribute("id")); |
---|
1023 | if(pChild0->Attribute("input1")) |
---|
1024 | mList[mission_num].input[0] = pChild0->Attribute("input1"); |
---|
1025 | if(pChild0->Attribute("input2")) |
---|
1026 | mList[mission_num].input[1] = pChild0->Attribute("input2"); |
---|
1027 | if(pChild0->Attribute("input3")) |
---|
1028 | mList[mission_num].input[2] = pChild0->Attribute("input3"); |
---|
1029 | if(pChild0->Attribute("output1")) |
---|
1030 | mList[mission_num].output[0] = pChild0->Attribute("output1"); |
---|
1031 | if(pChild0->Attribute("output2")) |
---|
1032 | mList[mission_num].output[1] = pChild0->Attribute("output2"); |
---|
1033 | if(pChild0->Attribute("output3")) |
---|
1034 | mList[mission_num].output[2] = pChild0->Attribute("output3"); |
---|
1035 | printf("input1=%s, outpu1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str()); |
---|
1036 | mission_num++; |
---|
1037 | } |
---|
1038 | } |
---|
1039 | |
---|
1040 | //Verified w/o Transfers May 26 |
---|
1041 | void |
---|
1042 | ServiceManagementLayer::RegisterCognitiveEngine(int32_t ID) |
---|
1043 | { |
---|
1044 | SendMessage(shellSocketFD, "register_engine_cognitive"); |
---|
1045 | LOG("ServiceManagementLayer:: CE registration message forwarded to shell.\n"); |
---|
1046 | |
---|
1047 | //printf("ServiceManagementLayer:: CE registration message forwarded to shell.\n"); |
---|
1048 | |
---|
1049 | //TransferRadioConfiguration(ID); |
---|
1050 | //TransferExperience(ID); |
---|
1051 | |
---|
1052 | numberOfCognitiveEngines++; |
---|
1053 | CE_Present = true; |
---|
1054 | } |
---|
1055 | |
---|
1056 | void |
---|
1057 | ServiceManagementLayer::DeregisterServices(int32_t ID) |
---|
1058 | { |
---|
1059 | char str_buffer[64]; |
---|
1060 | strcpy(_services_DB->command, "delete "); |
---|
1061 | strcat(_services_DB->command, _services_DB->tablename); |
---|
1062 | strcat(_services_DB->command, ".* from "); |
---|
1063 | strcat(_services_DB->command, _services_DB->tablename); |
---|
1064 | strcat(_services_DB->command, " where "); |
---|
1065 | strcat(_services_DB->command, "ID_Num"); |
---|
1066 | strcat(_services_DB->command, "=="); |
---|
1067 | sprintf(str_buffer, "%d;", ID); |
---|
1068 | strcat(_services_DB->command, str_buffer); |
---|
1069 | sqlite3_exec(_services_DB->db, _services_DB->command, NULL, 0, NULL); |
---|
1070 | } |
---|
1071 | |
---|
1072 | |
---|
1073 | void |
---|
1074 | ServiceManagementLayer::DeregisterCognitiveEngine(int32_t ID) |
---|
1075 | { |
---|
1076 | LOG("ServiceManagementLayer:: CE deregistration message forwarded to shell.\n"); |
---|
1077 | |
---|
1078 | numberOfCognitiveEngines--; |
---|
1079 | if(numberOfCognitiveEngines == 0) |
---|
1080 | CE_Present = false; |
---|
1081 | |
---|
1082 | SendMessage(shellSocketFD, "deregister_engine_cognitive"); |
---|
1083 | char buffer[256]; |
---|
1084 | memset(buffer, 0, 256); |
---|
1085 | ReadMessage(shellSocketFD, buffer); |
---|
1086 | SendMessage(CE_List[ID].FD, buffer); |
---|
1087 | if(strcmp("deregister_ack", buffer) != 0) { |
---|
1088 | ERROR(1, "SML:: Failed to close CE socket\n"); |
---|
1089 | } |
---|
1090 | CE_List[ID].FD = -1; |
---|
1091 | CE_List[ID].ID_num = -1; |
---|
1092 | |
---|
1093 | LOG("Cognitive Radio Shell:: Socket closed.\n"); |
---|
1094 | } |
---|
1095 | |
---|
1096 | |
---|
1097 | //VERIFIED May 21 |
---|
1098 | void |
---|
1099 | ServiceManagementLayer::StartSMLServer() |
---|
1100 | { |
---|
1101 | struct timeval selTimeout; |
---|
1102 | int32_t running = 1; |
---|
1103 | int32_t port, rc, new_sd = 1; |
---|
1104 | int32_t desc_ready = 1; |
---|
1105 | int32_t timeout = 10; |
---|
1106 | //If there is, call the MessageHandler with the Shell_Msg code of -1 |
---|
1107 | fd_set sockSet, shellSet; |
---|
1108 | |
---|
1109 | cogEngSrv = CreateTCPServerSocket(2032); |
---|
1110 | int32_t maxDescriptor = cogEngSrv; |
---|
1111 | |
---|
1112 | if(InitializeTCPServerPort(cogEngSrv) == -1) |
---|
1113 | ERROR(1,"Error initializing primary port\n"); |
---|
1114 | |
---|
1115 | |
---|
1116 | /*int FD = ClientSocket("127.0.0.1", "2013"); |
---|
1117 | SendMessage(FD, "register_engine_cognitive"); |
---|
1118 | SendMessage(FD, "register_service"); |
---|
1119 | SendMessage(FD, "test_srv"); |
---|
1120 | SendMessage(FD, "list_services"); |
---|
1121 | int FD2 = ClientSocket("127.0.0.1", "2013"); |
---|
1122 | SendMessage(FD2, "register_engine_cognitive"); |
---|
1123 | SendMessage(FD2, "register_service"); |
---|
1124 | SendMessage(FD2, "test_srv1"); |
---|
1125 | SendMessage(FD2, "list_services");*/ |
---|
1126 | //SendMessage(shellSocketFD, "list_services");*/ |
---|
1127 | //printf("num = %d, %d, %d\n", one, two, three); |
---|
1128 | |
---|
1129 | |
---|
1130 | int i = 10000000; |
---|
1131 | while (i>0) { |
---|
1132 | i--; |
---|
1133 | /* Zero socket descriptor vector and set for server sockets */ |
---|
1134 | /* This must be reset every time select() is called */ |
---|
1135 | FD_ZERO(&sockSet); |
---|
1136 | FD_SET(cogEngSrv, &sockSet); |
---|
1137 | for(int k = 0; k < Current_ID; k++) |
---|
1138 | FD_SET(CE_List[k].FD, &sockSet); |
---|
1139 | //printf("k=%d, CID=%d\n", k, CE_List[k].FD); |
---|
1140 | |
---|
1141 | /* Timeout specification */ |
---|
1142 | /* This must be reset every time select() is called */ |
---|
1143 | selTimeout.tv_sec = 0; /* timeout (secs.) */ |
---|
1144 | selTimeout.tv_usec = 0; /* 0 microseconds */ |
---|
1145 | //Changed both to zero so that select will check messages from the shell instead of blocking |
---|
1146 | //when there is no command from the CE's to be processed |
---|
1147 | |
---|
1148 | //Check if there is a message on the socket waiting to be read |
---|
1149 | rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); |
---|
1150 | //printf("rc=%d\n", rc); |
---|
1151 | if(rc == 0){ |
---|
1152 | //LOG("No echo requests for %i secs...Server still alive\n", timeout); |
---|
1153 | |
---|
1154 | FD_ZERO(&shellSet); |
---|
1155 | FD_SET(shellSocketFD, &shellSet); |
---|
1156 | selTimeout.tv_sec = 0; |
---|
1157 | selTimeout.tv_usec = 0; |
---|
1158 | //Check if there is a message on the shell socket ready to be processed |
---|
1159 | int rc2 = select(shellSocketFD + 1, &shellSet, NULL, NULL, &selTimeout); |
---|
1160 | //printf("rc2=%d\n", rc2); |
---|
1161 | //If there is, call the MessageHandler with the Shell_Msg code of -1 |
---|
1162 | if(FD_ISSET(shellSocketFD, &shellSet)){ |
---|
1163 | //printf("shell_msg, %d\n", rc2); |
---|
1164 | MessageHandler(-1);} |
---|
1165 | } |
---|
1166 | else { |
---|
1167 | desc_ready = rc; |
---|
1168 | for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) { |
---|
1169 | if(FD_ISSET(port, &sockSet)) { |
---|
1170 | desc_ready -= 1; |
---|
1171 | |
---|
1172 | //Check if request is new or on an existing open descriptor |
---|
1173 | if(port == cogEngSrv) { |
---|
1174 | //printf("here1\n"); |
---|
1175 | // do { |
---|
1176 | new_sd = AcceptTCPConnection(port); |
---|
1177 | |
---|
1178 | //printf("sd = %d\n", new_sd); |
---|
1179 | if(new_sd < 0) |
---|
1180 | break; |
---|
1181 | //Set not to block |
---|
1182 | /*int oldflags = fcntl (new_sd, F_GETFL, 0); |
---|
1183 | oldflags |= O_NONBLOCK; |
---|
1184 | fcntl (new_sd, F_SETFL, oldflags);*/ |
---|
1185 | |
---|
1186 | |
---|
1187 | CE_List[Current_ID].FD = new_sd; |
---|
1188 | CE_List[Current_ID].ID_num = Current_ID; |
---|
1189 | MessageHandler(Current_ID); |
---|
1190 | Current_ID++; |
---|
1191 | |
---|
1192 | FD_SET(new_sd,&sockSet); |
---|
1193 | if(new_sd > maxDescriptor) |
---|
1194 | maxDescriptor = new_sd; |
---|
1195 | //LOG("New incoming connection - %i\n\n",new_sd); |
---|
1196 | // } while(new_sd != -1); |
---|
1197 | } |
---|
1198 | else { |
---|
1199 | //printf("here2\n"); |
---|
1200 | //LOG("Request on already open descriptor.\n\n"); |
---|
1201 | for(int16_t z = 0; z < Current_ID; z++) |
---|
1202 | { |
---|
1203 | if(CE_List[z].FD == port){ |
---|
1204 | //printf("z=%d\n", z); |
---|
1205 | MessageHandler(z);} |
---|
1206 | } |
---|
1207 | } |
---|
1208 | } |
---|
1209 | } |
---|
1210 | } |
---|
1211 | } |
---|
1212 | |
---|
1213 | /* Close sockets */ |
---|
1214 | close(cogEngSrv); |
---|
1215 | |
---|
1216 | //delete &cogEngSrv; |
---|
1217 | return; |
---|
1218 | } |
---|