Changeset 316

Show
Ignore:
Timestamp:
07/10/09 13:45:38 (15 years ago)
Author:
wrodgers
Message:

Updated SML and Shell for new I/O model, minor bug fixes

Location:
vtcross/trunk/src
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp

    r313 r316  
    7474data_DB _data_DB; 
    7575const char *_SML_Config; 
     76bool shellFound; 
    7677 
    7778//Callback function used internally by some of the SQLite3 commands 
     
    655656 
    656657   //Send a message directly to the shell 
    657    //printf("name = %s\n", miss[activeMission].services[sourceID].name.c_str()); 
    658658   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++){ 
    663668            memset(buffer, 0 , 256); 
    664669            strcpy(_data_DB->command, "select "); 
     
    667672            strcat(_data_DB->command, _data_DB->tablename); 
    668673            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()); 
    670675            sqlite3_stmt * pStatement; 
    671676            int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
    672677            if (rc == SQLITE_OK){  
    673678                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));  
    675680                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"); 
    682682                    rc=31337; 
    683683                } 
     
    688688            sqlite3_finalize(pStatement); 
    689689            token = strtok((char *)data.c_str(), "@"); 
     690            token = strtok(NULL, "@"); 
    690691            SendMessage(shellSocketFD, token); 
    691692            token = strtok(NULL, "@"); 
    692             while(token){ 
    693                 SendMessage(shellSocketFD, token); 
    694                 token = strtok(NULL, "@"); 
    695             } 
     693            SendMessage(shellSocketFD, token); 
    696694        } 
    697         //printf("done shell\n"); 
    698        // LOG("ServiceManagementLayer:: Finished with data transaction.\n"); 
    699695        return; 
    700696   }  
     
    825821 * -All ordering constraints have been relaxed in this version; all data is stored locally and only sent when requested 
    826822 * -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 
    828824 * -IMPORTANT: DB uses '@' to seperate individual statements; using '@' in the data stream will result in incorrect behavior 
    829825 */ 
     
    834830{ 
    835831    //printf("start PAM\n"); 
    836     uint16_t i = 0; 
     832    uint16_t i = 0, t; 
     833    shellFound = false; 
    837834    std::string data_param, data_obsv, data; 
    838835    std::string input; 
     
    845842    char *errorMsg; 
    846843    char* cols[] = {(char *)"Tag", (char *)"Data"}; 
     844 
     845    LOG("ServiceManagementLayer:: Received PerformActiveMission command.\n"); 
     846 
    847847    //Get the inputs 
    848848    memset(buffer, 0, 256); 
     849    //Needed to read the zero passed in by GOP as the number of observables 
    849850    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 
    873852 
    874853    /* Receive Set of Parameters */ 
     
    895874    } 
    896875 
    897  
    898  
    899  
    900     //Receive any other defined inputs 
    901     /*while(i < 10 && !miss[activeMission].input[i].empty()){ 
    902             //New data being added to DB 
    903         //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  
    939876    //Useful for spotchecking what's in the database 
    940877    /*printf("\n\n\n"); 
     
    954891 
    955892 
    956  
    957  
    958    // printf("done\n"); 
    959893    i=0;  
    960894    int32_t numstatements[3] = {0,0,0}; 
     
    967901            check.clear(); 
    968902            int t; 
    969             for(t = 0; t < 5; t++){ 
     903            for(t = 0; t < 10; t++){ 
    970904                if(!miss[activeMission].services[i].output[t].empty()){ 
    971905                    //printf("i-numstmts-1 = %d\n", i-numstatements[0]-1); 
     
    1002936            bool doit = false; 
    1003937            if(miss[activeMission].services[i].output[t].find(">") != string::npos){ 
    1004                 //printf("foundit!\n"); 
    1005938                    std::string data2; 
    1006939                    strcpy(_data_DB->command, "SELECT "); 
     
    1044977                            input.clear(); 
    1045978                            check.clear(); 
    1046                             for(t = 0; t < 5; t++){ 
     979                            for(t = 0; t < 10; t++){ 
    1047980                                if(!miss[activeMission].services[k].output[t].empty()){ 
    1048981                                    //printf("i-numstmts = %d\n", i-numstatements-1); 
     
    10761009                            } 
    10771010                            //printf("L1:--- %s  %s---\n", miss[activeMission].services[k].output[t].c_str(), token); 
    1078                             //TODO change to strstr 
    10791011                            bool doit = false; 
    10801012                                //printf("here! %s\n", miss[activeMission].services[k].output[t].c_str()); 
     
    11201052                                            input.clear(); 
    11211053                                            check.clear(); 
    1122                                             for(t = 0; t < 5; t++){ 
     1054                                            for(t = 0; t < 10; t++){ 
    11231055                                                if(!miss[activeMission].services[j].output[t].empty()){ 
    11241056                                                    //printf("i-numstmts = %d\n", i-numstatements-1); 
     
    11531085                                            } 
    11541086                                            //printf("L2:---%s||%s---\n", miss[activeMission].services[j].output[t].c_str(), token); 
    1155                                             //TODO change to strstr 
    11561087                                            bool doit = false; 
    11571088                                                //printf("here! %s\n", miss[activeMission].services[j].output[t].c_str()); 
     
    12081139                                                    } 
    12091140                                                    data.clear(); 
    1210                                                     //printf("L0: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); 
    12111142                                                    input.clear(); 
    12121143                                                    check.clear(); 
    12131144                                                    int t; 
    1214                                                     for(t = 0; t < 5; t++){ 
     1145                                                    for(t = 0; t < 10; t++){ 
    12151146                                                        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]; 
    12181148                                                            strcpy(_data_DB->command, "SELECT "); 
    12191149                                                            strcat(_data_DB->command, _data_DB->tablename); 
     
    12761206                                    } 
    12771207                                    data.clear(); 
    1278                                     //printf("L0: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); 
    12791209                                    input.clear(); 
    12801210                                    check.clear(); 
    12811211                                    int t; 
    1282                                     for(t = 0; t < 5; t++){ 
     1212                                    for(t = 0; t < 10; t++){ 
    12831213                                        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]; 
    12851215                                            //printf("input=%s\n", input.c_str()); 
    12861216                                            strcpy(_data_DB->command, "SELECT "); 
     
    13031233                                            } 
    13041234                                            sqlite3_finalize(pStatement); 
    1305                                            // printf("data=%s\n", data.c_str()); 
     1235                                            //printf("data=%s\n", data.c_str()); 
    13061236                                            token = strtok((char *)data.c_str(), "@"); 
    13071237                                            token = strtok(NULL, "@"); 
     
    13521282                    check.clear(); 
    13531283                    int t; 
    1354                     for(t = 0; t < 5; t++){ 
     1284                    for(t = 0; t < 10; t++){ 
    13551285                        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]; 
    13571287                            //printf("input=%s\n", input.c_str()); 
    13581288                            strcpy(_data_DB->command, "SELECT "); 
     
    14001330    i=0; 
    14011331    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++; 
    14371340        } 
    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        } 
    14401348    } 
    14411349 
    14421350 
    14431351    LOG("ServiceManagementLayer:: Done performing active mission.\n"); 
    1444     /*LOG("ServiceManagementLayer:: Done sending output data to shell from PerformActiveMission.\n"); 
     1352    /* 
    14451353    strcpy(_data_DB->command, "select "); 
    14461354    strcat(_data_DB->command, _data_DB->tablename); 
     
    14671375ServiceManagementLayer::ListServices() 
    14681376{ 
    1469     // generate commandi 
    14701377    strcpy(_services_DB->command, "select "); 
    14711378    strcat(_services_DB->command, _services_DB->tablename); 
     
    15421449        int32_t service_num = 0; 
    15431450        uint16_t cond_array[] = {0, 0, 0}; 
    1544         //printf("mission_num = %d\n", mission_num); 
    1545         //memset(cond_array, 0, 2); 
    15461451         
    15471452        for(pChild1  = (pChild0->FirstChildElement())->FirstChildElement(); pChild1; \ 
    15481453            pChild1  = pChild1->NextSiblingElement())  
    15491454        { 
    1550             //printf("here1\n"); 
    15511455            int32_t conditional_0 = service_num; 
    15521456            for(pChild2 = pChild1->FirstChildElement(); \ 
     
    15781482                                if(pChild4->Attribute(buffer2)) 
    15791483                                    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()); 
    15811484                            } 
    15821485                            if(pChild4->Attribute("parameter")) 
     
    16481551                        mList[mission_num].services[conditional_0].output[i-1] = pChild1->Attribute(buffer2); 
    16491552                } 
    1650                    // printf("i1=%s\n", mList[mission_num].services[conditional_0].input[0].c_str()); 
    16511553                if(pChild1->Attribute("parameter")) 
    16521554                    mList[mission_num].services[conditional_0].parameter = pChild1->Attribute("parameter"); 
     
    16551557            service_num++; 
    16561558        } 
    1657         //for(int i = 0; i < service_num; i++) 
    1658          //printf("ttt%s\n",mList[mission_num].services[i].input[0].c_str()); 
    16591559         
    16601560        mList[mission_num].numServices = service_num; 
     
    16621562        mList[mission_num].missionID = atoi(pChild0->Attribute("id")); 
    16631563        for(int i = 1; i <= 10; i++) { 
    1664             char buffer[9]="input"; 
     1564            char buffer[9]="param"; 
    16651565            sprintf(buffer, "%s%d", buffer, i);  
    1666             if(pChild0->Attribute(buffer)) 
     1566            if(pChild0->Attribute(buffer)){ 
    16671567                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            } 
    16721569        } 
    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); 
    16751570        mission_num++; 
    1676         //printf("here\n"); 
    1677     } 
    1678     //LOG("ServiceManagementLayer:: Done Loading Configuration"); 
     1571    } 
     1572    LOG("ServiceManagementLayer:: Done Loading Configuration\n"); 
    16791573} 
    16801574 
     
    16881582ServiceManagementLayer::RegisterCognitiveEngine(int32_t ID) 
    16891583{ 
    1690     //LOG("SML::regcogeng"); 
    16911584    SendMessage(shellSocketFD, "register_engine_cognitive"); 
    16921585 
    1693    // printf("SSFD = %d\n", shellSocketFD); 
    16941586    LOG("ServiceManagementLayer:: CE registration message forwarded to shell.\n"); 
    16951587    char buffer[256]; 
    16961588    memset(buffer, 0, 256); 
    16971589    ReadMessage(shellSocketFD, buffer); 
    1698     //printf("ServiceManagementLayer::buffer = %s\n", buffer); 
    16991590    SendMessage(CE_List[ID].FD, buffer); 
    17001591 
    17011592    TransferRadioConfiguration(ID); 
    17021593    memset(buffer, 0, 256); 
    1703     //printf("start trans exp\n"); 
    17041594    TransferExperience(ID); 
    17051595    memset(buffer, 0, 256); 
    17061596    numberOfCognitiveEngines++; 
    17071597    CE_Present = true; 
    1708     //printf("done registering CE!\n"); 
    17091598} 
    17101599 
     
    18071696        ERROR(1,"Error initializing primary port\n"); 
    18081697 
    1809     int i = 10000000;  //TODO change to "running" if endpoint can be reached 
    18101698    while (running) { 
    1811         i--; 
    18121699        /* Zero socket descriptor vector and set for server sockets */ 
    18131700        /* This must be reset every time select() is called */ 
  • vtcross/trunk/src/shell/CognitiveRadioShell.cpp

    r285 r316  
    123123    sprintf(counter, "%d", radio_info->numUtilities); 
    124124    SendMessage(socketFD, counter); 
    125     LOG("Cognitive Radio Shell:: Sending radio configuration to Cognitive Engine. %s\n",counter); 
    126125    for(size_t i = 0; i < radio_info->numUtilities; i++) { 
    127126        SendMessage(socketFD, utils[i].name.c_str()); 
     
    536535        SendMessage(ceSocketFD,cp[i].name.c_str()); 
    537536        sprintf(var,"%f",cp[i].value); 
    538         SendMessage(ceSocketFD,var); 
     537        SendMessage(ceSocketFD,var);  
    539538    } 
    540539 
     
    741740 
    742741        /* 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);  
    744744        if(rc == 0) 
    745745            LOG("No echo requests for %i secs...Server still alive\n", timeout); 
     
    763763                            if(port == servSock[policy]) 
    764764                                policySocketFD = new_sd; 
    765                             //printf("%d %d %d :: %d\n", ceSocketFD, commandSocketFD, policySocketFD, new_sd); 
    766765 
    767766                            HandleMessage(new_sd);