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