Changeset 316
- Timestamp:
- 07/10/09 13:45:38 (15 years ago)
- Location:
- vtcross/trunk/src
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp
r313 r316 74 74 data_DB _data_DB; 75 75 const char *_SML_Config; 76 bool shellFound; 76 77 77 78 //Callback function used internally by some of the SQLite3 commands … … 655 656 656 657 //Send a message directly to the shell 657 //printf("name = %s\n", miss[activeMission].services[sourceID].name.c_str());658 658 if(miss[activeMission].services[sourceID].name.find("shell")!=string::npos) 659 { 660 //If the name on the output doesn't start with "~", search the DB to find the output that should be returned 661 if(miss[activeMission].services[sourceID].output[0].find("~") == string::npos){ 662 // printf("taken1\n"); 659 { 660 int k = 0; 661 shellFound=true; 662 while(k<10 && !miss[activeMission].input[k].empty()){ 663 k++; 664 } 665 sprintf(buffer, "%d", k); 666 SendMessage(shellSocketFD, buffer); 667 for(int t = 0; t < k; t++){ 663 668 memset(buffer, 0 , 256); 664 669 strcpy(_data_DB->command, "select "); … … 667 672 strcat(_data_DB->command, _data_DB->tablename); 668 673 strcat(_data_DB->command, " where Tag=="); 669 sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission]. services[sourceID].output[0].c_str());674 sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].input[t].c_str()); 670 675 sqlite3_stmt * pStatement; 671 676 int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 672 677 if (rc == SQLITE_OK){ 673 678 if (sqlite3_step(pStatement) == SQLITE_ROW) 674 data .append((const char*) sqlite3_column_text(pStatement, 1));679 data=((const char*) sqlite3_column_text(pStatement, 1)); 675 680 else { 676 data.append("1@output1@"); 677 data.append(miss[activeMission].services[sourceID].output[0]); 678 data.append("@"); 679 //printf("data = %s\n", data.c_str()); 680 681 //printf("2222data_DB:: Data not yet in DB.\n"); 681 printf("data_DB:: Data not yet in DB.\n"); 682 682 rc=31337; 683 683 } … … 688 688 sqlite3_finalize(pStatement); 689 689 token = strtok((char *)data.c_str(), "@"); 690 token = strtok(NULL, "@"); 690 691 SendMessage(shellSocketFD, token); 691 692 token = strtok(NULL, "@"); 692 while(token){ 693 SendMessage(shellSocketFD, token); 694 token = strtok(NULL, "@"); 695 } 693 SendMessage(shellSocketFD, token); 696 694 } 697 //printf("done shell\n");698 // LOG("ServiceManagementLayer:: Finished with data transaction.\n");699 695 return; 700 696 } … … 825 821 * -All ordering constraints have been relaxed in this version; all data is stored locally and only sent when requested 826 822 * -If and while support fully implemented - up to three levels (if's can be nested, but dowhiles cannot) 827 * -For dowhiles, assumes loop condition determined on next to last line (last line is a shell statement to keep the application updated)823 * -For dowhiles, assumes loop condition determined on last line 828 824 * -IMPORTANT: DB uses '@' to seperate individual statements; using '@' in the data stream will result in incorrect behavior 829 825 */ … … 834 830 { 835 831 //printf("start PAM\n"); 836 uint16_t i = 0; 832 uint16_t i = 0, t; 833 shellFound = false; 837 834 std::string data_param, data_obsv, data; 838 835 std::string input; … … 845 842 char *errorMsg; 846 843 char* cols[] = {(char *)"Tag", (char *)"Data"}; 844 845 LOG("ServiceManagementLayer:: Received PerformActiveMission command.\n"); 846 847 847 //Get the inputs 848 848 memset(buffer, 0, 256); 849 //Needed to read the zero passed in by GOP as the number of observables 849 850 ReadMessage(shellSocketFD, buffer); 850 LOG("ServiceManagementLayer:: Received PerformActiveMission command.\n"); 851 //TODO this whole section needs to be reworked depending on how relevent data is actually transmitted from the app to the shell to the SML 852 int32_t t = atoi(buffer); 853 /* Receive Set of Observables */ 854 for(int32_t m = 0; m < t; m++) { 855 //printf("data=%s\n", data_obsv.c_str()); 856 memset(buffer1, 0, 256); 857 ReadMessage(shellSocketFD, buffer1); 858 strcpy(_data_DB->command, "insert into "); 859 strcat(_data_DB->command, _data_DB->tablename); 860 strcat(_data_DB->command, " ("); 861 strcat(_data_DB->command, cols[0]); 862 strcat(_data_DB->command, ", "); 863 strcat(_data_DB->command, cols[1]); 864 strcat(_data_DB->command, ") "); 865 memset(buffer, 0, 256); 866 ReadMessage(shellSocketFD, buffer); 867 sprintf(_data_DB->command, "%s values('%s', '1@%s@%s", _data_DB->command, buffer1, buffer1, buffer); 868 strcat(_data_DB->command, "');"); 869 rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg); 870 if( rc!=SQLITE_OK && rc!=101 ) 871 fprintf(stderr, "SQL error: %s\n", errorMsg); 872 } 851 873 852 874 853 /* Receive Set of Parameters */ … … 895 874 } 896 875 897 898 899 900 //Receive any other defined inputs901 /*while(i < 10 && !miss[activeMission].input[i].empty()){902 //New data being added to DB903 //printf("inserting data from shell\n");904 memset(buffer1, 0, 256);905 ReadMessage(shellSocketFD, buffer1);906 t=atoi(buffer1);907 //printf("t=%d\n", t);908 for(int m = 0; m < t; m++) {909 data.append("@");910 memset(buffer, 0, 256);911 ReadMessage(shellSocketFD, buffer);912 data.append(buffer);913 data.append("@");914 memset(buffer, 0, 256);915 ReadMessage(shellSocketFD, buffer);916 data.append(buffer);917 }918 //printf("here %s\n", data.c_str());919 strcpy(_data_DB->command, "insert into ");920 strcat(_data_DB->command, _data_DB->tablename);921 strcat(_data_DB->command, " (");922 strcat(_data_DB->command, cols[0]);923 strcat(_data_DB->command, ", ");924 strcat(_data_DB->command, cols[1]);925 strcat(_data_DB->command, ") ");926 strcat(_data_DB->command, " values('");927 strcat(_data_DB->command, miss[activeMission].input[i].c_str());928 sprintf(_data_DB->command, "%s', '%s%s');", _data_DB->command, buffer1, data.c_str());929 char *errorMsg;930 rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg);931 if( rc!=SQLITE_OK && rc!=101 )932 fprintf(stderr, "SQL error: %s\n", errorMsg);933 //printf("SML: finished adding data from shell on input %d\n", i);934 i++;935 data.clear();936 }*/937 938 939 876 //Useful for spotchecking what's in the database 940 877 /*printf("\n\n\n"); … … 954 891 955 892 956 957 958 // printf("done\n");959 893 i=0; 960 894 int32_t numstatements[3] = {0,0,0}; … … 967 901 check.clear(); 968 902 int t; 969 for(t = 0; t < 5; t++){903 for(t = 0; t < 10; t++){ 970 904 if(!miss[activeMission].services[i].output[t].empty()){ 971 905 //printf("i-numstmts-1 = %d\n", i-numstatements[0]-1); … … 1002 936 bool doit = false; 1003 937 if(miss[activeMission].services[i].output[t].find(">") != string::npos){ 1004 //printf("foundit!\n");1005 938 std::string data2; 1006 939 strcpy(_data_DB->command, "SELECT "); … … 1044 977 input.clear(); 1045 978 check.clear(); 1046 for(t = 0; t < 5; t++){979 for(t = 0; t < 10; t++){ 1047 980 if(!miss[activeMission].services[k].output[t].empty()){ 1048 981 //printf("i-numstmts = %d\n", i-numstatements-1); … … 1076 1009 } 1077 1010 //printf("L1:--- %s %s---\n", miss[activeMission].services[k].output[t].c_str(), token); 1078 //TODO change to strstr1079 1011 bool doit = false; 1080 1012 //printf("here! %s\n", miss[activeMission].services[k].output[t].c_str()); … … 1120 1052 input.clear(); 1121 1053 check.clear(); 1122 for(t = 0; t < 5; t++){1054 for(t = 0; t < 10; t++){ 1123 1055 if(!miss[activeMission].services[j].output[t].empty()){ 1124 1056 //printf("i-numstmts = %d\n", i-numstatements-1); … … 1153 1085 } 1154 1086 //printf("L2:---%s||%s---\n", miss[activeMission].services[j].output[t].c_str(), token); 1155 //TODO change to strstr1156 1087 bool doit = false; 1157 1088 //printf("here! %s\n", miss[activeMission].services[j].output[t].c_str()); … … 1208 1139 } 1209 1140 data.clear(); 1210 //printf("L 0:while detected %d, %d\n", k, miss[activeMission].services[i].num_conds);1141 //printf("L2:while detected %d, %d\n", k, miss[activeMission].services[i].num_conds); 1211 1142 input.clear(); 1212 1143 check.clear(); 1213 1144 int t; 1214 for(t = 0; t < 5; t++){1145 for(t = 0; t < 10; t++){ 1215 1146 if(!miss[activeMission].services[j].output[t].empty()){ 1216 input=miss[activeMission].services[l-2].output[t]; 1217 //printf("input=%s\n", input.c_str()); 1147 input=miss[activeMission].services[l-1].output[t]; 1218 1148 strcpy(_data_DB->command, "SELECT "); 1219 1149 strcat(_data_DB->command, _data_DB->tablename); … … 1276 1206 } 1277 1207 data.clear(); 1278 //printf("L 0:while detected %d, %d\n", k, miss[activeMission].services[i].num_conds);1208 //printf("L1:while detected %d, %d\n", k, miss[activeMission].services[i].num_conds); 1279 1209 input.clear(); 1280 1210 check.clear(); 1281 1211 int t; 1282 for(t = 0; t < 5; t++){1212 for(t = 0; t < 10; t++){ 1283 1213 if(!miss[activeMission].services[k].output[t].empty()){ 1284 input=miss[activeMission].services[j- 2].output[t];1214 input=miss[activeMission].services[j-1].output[t]; 1285 1215 //printf("input=%s\n", input.c_str()); 1286 1216 strcpy(_data_DB->command, "SELECT "); … … 1303 1233 } 1304 1234 sqlite3_finalize(pStatement); 1305 //printf("data=%s\n", data.c_str());1235 //printf("data=%s\n", data.c_str()); 1306 1236 token = strtok((char *)data.c_str(), "@"); 1307 1237 token = strtok(NULL, "@"); … … 1352 1282 check.clear(); 1353 1283 int t; 1354 for(t = 0; t < 5; t++){1284 for(t = 0; t < 10; t++){ 1355 1285 if(!miss[activeMission].services[i].output[t].empty()){ 1356 input=miss[activeMission].services[k- 2].output[t];1286 input=miss[activeMission].services[k-1].output[t]; 1357 1287 //printf("input=%s\n", input.c_str()); 1358 1288 strcpy(_data_DB->command, "SELECT "); … … 1400 1330 i=0; 1401 1331 data.clear(); 1402 //get the ouptuts 1403 while(i < 10 && !miss[activeMission].output[i].empty()){ 1404 //printf("sending output data to shell\n"); 1405 strcpy(_data_DB->command, "select "); 1406 strcat(_data_DB->command, _data_DB->tablename); 1407 strcat(_data_DB->command, ".* from "); 1408 strcat(_data_DB->command, _data_DB->tablename); 1409 strcat(_data_DB->command, " where Tag=="); 1410 sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].output[i].c_str()); 1411 sqlite3_stmt * pStatement; 1412 int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 1413 if (rc == SQLITE_OK){ 1414 if (sqlite3_step(pStatement) == SQLITE_ROW) 1415 data.append((const char*) sqlite3_column_text(pStatement, 1)); 1416 else { 1417 printf("1data_DB:: Data not yet in DB.\n"); 1418 rc=31337; 1419 } 1420 } 1421 else { 1422 printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command); 1423 } 1424 //printf("here %s\n", data.c_str()); 1425 sqlite3_finalize(pStatement); 1426 char *data_ch = (char *) data.c_str(); 1427 char *token = strtok(data_ch, "@"); 1428 SendMessage(shellSocketFD, token); 1429 token = strtok(NULL, "@"); 1430 while(token){ 1431 SendMessage(shellSocketFD, token); 1432 //printf("token1 = %s\n", token); 1433 token = strtok(NULL, "@"); 1434 SendMessage(shellSocketFD, token); 1435 //printf("token2 = %s\n", token); 1436 token = strtok(NULL, "@"); 1332 1333 1334 1335 if(!shellFound) 1336 { 1337 int k = 0; 1338 while(k<10 && !miss[activeMission].input[k].empty()){ 1339 k++; 1437 1340 } 1438 i++; 1439 data.clear(); 1341 sprintf(buffer, "%d", k); 1342 SendMessage(shellSocketFD, buffer); 1343 for(int t = 0; t < k; t++){ 1344 //printf("---%s---\n", miss[activeMission].input[t].c_str()); 1345 SendMessage(shellSocketFD, miss[activeMission].input[t].c_str()); 1346 SendMessage(shellSocketFD, "0"); 1347 } 1440 1348 } 1441 1349 1442 1350 1443 1351 LOG("ServiceManagementLayer:: Done performing active mission.\n"); 1444 /* LOG("ServiceManagementLayer:: Done sending output data to shell from PerformActiveMission.\n");1352 /* 1445 1353 strcpy(_data_DB->command, "select "); 1446 1354 strcat(_data_DB->command, _data_DB->tablename); … … 1467 1375 ServiceManagementLayer::ListServices() 1468 1376 { 1469 // generate commandi1470 1377 strcpy(_services_DB->command, "select "); 1471 1378 strcat(_services_DB->command, _services_DB->tablename); … … 1542 1449 int32_t service_num = 0; 1543 1450 uint16_t cond_array[] = {0, 0, 0}; 1544 //printf("mission_num = %d\n", mission_num);1545 //memset(cond_array, 0, 2);1546 1451 1547 1452 for(pChild1 = (pChild0->FirstChildElement())->FirstChildElement(); pChild1; \ 1548 1453 pChild1 = pChild1->NextSiblingElement()) 1549 1454 { 1550 //printf("here1\n");1551 1455 int32_t conditional_0 = service_num; 1552 1456 for(pChild2 = pChild1->FirstChildElement(); \ … … 1578 1482 if(pChild4->Attribute(buffer2)) 1579 1483 mList[mission_num].services[service_num].output[i-1] = pChild4->Attribute(buffer2); 1580 //printf("i1=%s\n", mList[mission_num].services[conditional_0].input[i].c_str());1581 1484 } 1582 1485 if(pChild4->Attribute("parameter")) … … 1648 1551 mList[mission_num].services[conditional_0].output[i-1] = pChild1->Attribute(buffer2); 1649 1552 } 1650 // printf("i1=%s\n", mList[mission_num].services[conditional_0].input[0].c_str());1651 1553 if(pChild1->Attribute("parameter")) 1652 1554 mList[mission_num].services[conditional_0].parameter = pChild1->Attribute("parameter"); … … 1655 1557 service_num++; 1656 1558 } 1657 //for(int i = 0; i < service_num; i++)1658 //printf("ttt%s\n",mList[mission_num].services[i].input[0].c_str());1659 1559 1660 1560 mList[mission_num].numServices = service_num; … … 1662 1562 mList[mission_num].missionID = atoi(pChild0->Attribute("id")); 1663 1563 for(int i = 1; i <= 10; i++) { 1664 char buffer[9]=" input";1564 char buffer[9]="param"; 1665 1565 sprintf(buffer, "%s%d", buffer, i); 1666 if(pChild0->Attribute(buffer)) 1566 if(pChild0->Attribute(buffer)){ 1667 1567 mList[mission_num].input[i-1] = pChild0->Attribute(buffer); 1668 char buffer2[9]="output"; 1669 sprintf(buffer2, "%s%d", buffer2, i); 1670 if(pChild0->Attribute(buffer2)) 1671 mList[mission_num].output[i-1] = pChild0->Attribute(buffer2); 1568 } 1672 1569 } 1673 //printf("mis, input1=%s, output1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str());1674 //printf("NUMSERVICES = %d\n", mList[mission_num].numServices);1675 1570 mission_num++; 1676 //printf("here\n"); 1677 } 1678 //LOG("ServiceManagementLayer:: Done Loading Configuration"); 1571 } 1572 LOG("ServiceManagementLayer:: Done Loading Configuration\n"); 1679 1573 } 1680 1574 … … 1688 1582 ServiceManagementLayer::RegisterCognitiveEngine(int32_t ID) 1689 1583 { 1690 //LOG("SML::regcogeng");1691 1584 SendMessage(shellSocketFD, "register_engine_cognitive"); 1692 1585 1693 // printf("SSFD = %d\n", shellSocketFD);1694 1586 LOG("ServiceManagementLayer:: CE registration message forwarded to shell.\n"); 1695 1587 char buffer[256]; 1696 1588 memset(buffer, 0, 256); 1697 1589 ReadMessage(shellSocketFD, buffer); 1698 //printf("ServiceManagementLayer::buffer = %s\n", buffer);1699 1590 SendMessage(CE_List[ID].FD, buffer); 1700 1591 1701 1592 TransferRadioConfiguration(ID); 1702 1593 memset(buffer, 0, 256); 1703 //printf("start trans exp\n");1704 1594 TransferExperience(ID); 1705 1595 memset(buffer, 0, 256); 1706 1596 numberOfCognitiveEngines++; 1707 1597 CE_Present = true; 1708 //printf("done registering CE!\n");1709 1598 } 1710 1599 … … 1807 1696 ERROR(1,"Error initializing primary port\n"); 1808 1697 1809 int i = 10000000; //TODO change to "running" if endpoint can be reached1810 1698 while (running) { 1811 i--;1812 1699 /* Zero socket descriptor vector and set for server sockets */ 1813 1700 /* This must be reset every time select() is called */ -
vtcross/trunk/src/shell/CognitiveRadioShell.cpp
r285 r316 123 123 sprintf(counter, "%d", radio_info->numUtilities); 124 124 SendMessage(socketFD, counter); 125 LOG("Cognitive Radio Shell:: Sending radio configuration to Cognitive Engine. %s\n",counter);126 125 for(size_t i = 0; i < radio_info->numUtilities; i++) { 127 126 SendMessage(socketFD, utils[i].name.c_str()); … … 536 535 SendMessage(ceSocketFD,cp[i].name.c_str()); 537 536 sprintf(var,"%f",cp[i].value); 538 SendMessage(ceSocketFD,var); 537 SendMessage(ceSocketFD,var); 539 538 } 540 539 … … 741 740 742 741 /* Suspend program until descriptor is ready or timeout */ 743 rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); 742 //printf("Waiting for signal...\n"); 743 rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, NULL); 744 744 if(rc == 0) 745 745 LOG("No echo requests for %i secs...Server still alive\n", timeout); … … 763 763 if(port == servSock[policy]) 764 764 policySocketFD = new_sd; 765 //printf("%d %d %d :: %d\n", ceSocketFD, commandSocketFD, policySocketFD, new_sd);766 765 767 766 HandleMessage(new_sd);