Changeset 287

Show
Ignore:
Timestamp:
06/18/09 08:19:56 (15 years ago)
Author:
wrodgers
Message:

Begun work on nested conditionals, direct communication. This should work for linear ordering of statements, and may work for nonlinear ordering too, although that has yet to be fully tested

Location:
vtcross/trunk/src
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/include/vtcross/containers.h

    r268 r287  
    8787    uint16_t num_conds; 
    8888    bool cond_flag; 
    89     std::string input[3]; 
    90     std::string output[3]; 
     89    std::string input[5]; 
     90    std::string output[5]; 
    9191};  
    9292 
     
    100100    uint32_t numServices; 
    101101    Service *services; 
    102     std::string input[3]; 
    103     std::string output[3]; 
     102    std::string input[5]; 
     103    std::string output[5]; 
    104104}; 
    105105 
  • vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp

    r285 r287  
    2020//TODO Better shutdown 
    2121//TODO Verify Deregister services 
    22 //TODO printf's 
     22//TODO printf's  
    2323 
    2424#include <stdlib.h> 
     
    170170 
    171171    _services_DB = (services_DB) malloc(sizeof(struct services_s)); 
    172     char *errorMsg; 
     172   // char *errorMsg; 
    173173 
    174174    // create database 
    175175 
    176176    // copy filename 
    177     unsigned int i=0; 
    178177    strcpy(_services_DB->filename, "Services_Table"); 
    179178 
     
    195194    rc = sqlite3_step(ppStmt); 
    196195    if( rc!=SQLITE_OK && rc!=101 ) 
    197         printf("ServiceManagementLayer::CreateServicesDB 'step' error %d\n"); 
     196        printf("ServiceManagementLayer::CreateServicesDB 'step' error\n"); 
    198197 
    199198    // number of columns in the table 
     
    217216    rc = sqlite3_step(ppStmt); 
    218217    if( rc!=SQLITE_OK && rc!=101 ) 
    219         printf("ServiceManagementLayer::CreateServicesDB 'step' error %d\n"); 
     218        printf("ServiceManagementLayer::CreateServicesDB 'step' error\n"); 
    220219} 
    221220 
     
    230229{ 
    231230    _data_DB = (data_DB) malloc(sizeof(struct data_s)); 
    232     char *errorMsg; 
     231    //char *errorMsg; 
     232  sqlite3_stmt *ppStmt;  /* OUT: Statement handle */ 
     233  const char *pzTail;     /* OUT: Pointer to unused portion of zSql */ 
    233234 
    234235    // create database 
    235236 
    236237    // copy filename 
    237     unsigned int i=0; 
    238238    strcpy(_data_DB->filename, "Data_Table"); 
    239  
    240239    // execute create database command 
    241240    // database handle 
     
    248247    // copy tablename 
    249248    strcpy(_data_DB->tablename, "Data"); 
     249    sprintf(_data_DB->command, "DROP TABLE IF EXISTS Data;");      
     250 
     251    int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, 128, &ppStmt, &pzTail); 
     252    if( rc!=SQLITE_OK && rc!=101 ) 
     253        printf("ServiceManagementLayer::CreateServicesDB 'prepare_stmt' error %d\n", rc); 
     254    rc = sqlite3_step(ppStmt); 
     255    if( rc!=SQLITE_OK && rc!=101 ) 
     256        printf("ServiceManagementLayer::CreateServicesDB 'step' error\n"); 
     257 
    250258 
    251259    // number of columns in the table 
     
    265273 
    266274    // execute create table command 
    267   sqlite3_stmt *ppStmt;  /* OUT: Statement handle */ 
    268   const char *pzTail;     /* OUT: Pointer to unused portion of zSql */ 
    269  
    270     int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, 128, &ppStmt, &pzTail); 
     275 
     276    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, 128, &ppStmt, &pzTail); 
    271277    if( rc!=SQLITE_OK && rc!=101 ) 
    272278        printf("ServiceManagementLayer::CreateDataDB 'prepare_stmt' error %d\n", rc); 
    273279    rc = sqlite3_step(ppStmt); 
    274280    if( rc!=SQLITE_OK && rc!=101 ) 
    275         printf("ServiceManagementLayer::CreateDataDB 'step' error %d\n"); 
     281        printf("ServiceManagementLayer::CreateDataDB 'step' error\n"); 
    276282} 
    277283 
     
    541547    ReadMessage(CE_List[ID].FD, buffer); 
    542548    char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"}; 
    543     printf("RS_buffer = %s\n", buffer); 
     549    //printf("RS_buffer = %s\n", buffer); 
    544550    // generate command 
    545551    strcpy(_services_DB->command, "insert into "); 
     
    562568    if( rc!=SQLITE_OK && rc!=101 ) 
    563569        fprintf(stderr, "ServiceManagementLayer::RecieveServices DB Error %s\n", errorMsg); 
    564     char *outBuffer; 
    565570    /*sprintf(outBuffer, "SML: Registering service '%s' from component number '%d'", buffer, ID); 
    566571    LOG(outBuffer);*/ 
     
    582587    memset(buffer, 0, 256); 
    583588    ReadMessage(shellSocketFD, buffer); 
    584     int32_t missID = atoi(buffer); 
     589    uint32_t missID = atoi(buffer); 
    585590    for(activeMission = 0; activeMission < 10; activeMission++) 
    586591    { 
     
    590595    } 
    591596 
    592     LOG("ServiceManagementLayer:: Received Set Active Mission command: %i\n",missID); 
     597    LOG("ServiceManagementLayer:: Received Set Active Mission command: %i.\n",missID); 
    593598    //For each service in the mission 
    594     for(int i = 0; i < miss[activeMission].numServices; i++) 
     599    for(uint16_t i = 0; i < miss[activeMission].numServices; i++) 
    595600    {    
    596601        //Check whether the current service is an actual service or a conditional 
    597         if(miss[activeMission].services[i].name.compare("if") && miss[activeMission].services[i].name.compare("while")){ 
     602        if(miss[activeMission].services[i].name.compare("if") && miss[activeMission].services[i].name.compare("while") && \ 
     603           miss[activeMission].services[i].name.compare("shell")){ 
    598604            //If it is a service, search the database of registered services to find the ID of the component that registered it 
    599605            strcpy(_services_DB->command, "select "); 
     
    610616                     miss[activeMission].services[i].componentID =  sqlite3_column_int(pStatement, 0);  
    611617                else { 
    612                     printf("services_DB:: Mission requires service not provided by any connected component.\n"); 
     618                    printf("services_DB:: Mission requires service %s not provided by any connected component.\n",miss[activeMission].services[i].name.c_str()); 
    613619                    rc=31337; 
    614620                } 
     
    616622                printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_services_DB->command); 
    617623            } 
    618  
     624                //printf("s_name=%s\n",miss[activeMission].services[i].name.c_str()); 
    619625            sqlite3_finalize(pStatement); 
    620626            miss[activeMission].services[i].socketFD = CE_List[miss[activeMission].services[i].componentID].FD; 
     
    625631  
    626632    SendMessage(shellSocketFD, "ack"); 
    627  
     633    LOG("ServiceManagementLayer:: Done setting active mission.\n"); 
    628634    //printf("\nhere ---%d, %d---\n", miss[activeMission].services[0].componentID, miss[activeMission].services[1].componentID); 
    629635} 
     
    642648ServiceManagementLayer::TransactData(int32_t sourceID) 
    643649{ 
    644     printf("TransactData Occuring\n"); 
     650    LOG("ServiceManagementLayer:: Data transaction occuring.\n"); 
    645651    char buffer[256]; 
    646652    std::string data; 
     
    648654    int i = 0; 
    649655    fd_set sockSet; 
     656    char *token; 
    650657    struct timeval selTimeout; 
     658 
     659   //Send a message directly to the shell 
     660   //printf("name = %s\n", miss[activeMission].services[sourceID].name.c_str()); 
     661   if(miss[activeMission].services[sourceID].name.find("shell")!=string::npos) 
     662   { 
     663        //printf("caught shell\n"); 
     664        //If the name on the output doesn't start with "~", search the DB to find the output that should be returned 
     665        if(miss[activeMission].services[sourceID].output[0].find("~") == string::npos){ 
     666           // printf("taken1\n"); 
     667            memset(buffer, 0 , 256); 
     668            strcpy(_data_DB->command, "select "); 
     669            strcat(_data_DB->command, _data_DB->tablename); 
     670            strcat(_data_DB->command, ".* from "); 
     671            strcat(_data_DB->command, _data_DB->tablename); 
     672            strcat(_data_DB->command, " where Tag=="); 
     673            sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].services[sourceID].output[0].c_str()); 
     674            sqlite3_stmt * pStatement; 
     675            int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
     676            if (rc == SQLITE_OK){  
     677                if (sqlite3_step(pStatement) == SQLITE_ROW) 
     678                    data.append((const char*) sqlite3_column_text(pStatement, 1));  
     679                else { 
     680                    printf("data_DB:: Data not yet in DB.\n"); 
     681                    rc=31337; 
     682                } 
     683            }  
     684            else { 
     685                printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command); 
     686            } 
     687            sqlite3_finalize(pStatement); 
     688            token = strtok((char *)data.c_str(), "@"); 
     689            SendMessage(shellSocketFD, token); 
     690            token = strtok(NULL, "@"); 
     691            while(token){ 
     692                SendMessage(shellSocketFD, token); 
     693                token = strtok(NULL, "@"); 
     694            } 
     695        } 
     696        //Otherwise, just return the value specified by output TODO (in 1 - <output name> - <output name> format) 
     697        else{ 
     698            printf("taken2\n"); 
     699            memset(buffer, 0, 256); 
     700            SendMessage(shellSocketFD, "1"); 
     701            miss[activeMission].services[sourceID].output[0].erase(0); 
     702            SendMessage(shellSocketFD, miss[activeMission].services[sourceID].output[0].c_str()); 
     703            SendMessage(shellSocketFD, miss[activeMission].services[sourceID].output[0].c_str()); 
     704        } 
     705        //printf("done shell\n"); 
     706        return; 
     707   }  
     708 
     709 
     710 
    651711    //Transmission starting messages 
    652712    SendMessage(miss[activeMission].services[sourceID].socketFD, "request_optimization_service"); 
    653713    SendMessage(miss[activeMission].services[sourceID].socketFD, miss[activeMission].services[sourceID].name.c_str()); 
    654714    //Find and load the input data 
    655     while(i < 3 && !miss[activeMission].services[sourceID].input[i].empty()){ 
     715    while(i < 5 && !miss[activeMission].services[sourceID].input[i].empty()){ 
    656716        //printf("pulling input data out of DB for ID#=%d\n", sourceID); 
    657717        strcpy(_data_DB->command, "select "); 
     
    660720        strcat(_data_DB->command, _data_DB->tablename); 
    661721        strcat(_data_DB->command, " where Tag=="); 
    662         sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].services[sourceID].input[i].c_str()); 
     722        char temp[100]; 
     723        strcpy(temp, miss[activeMission].services[sourceID].input[i].c_str()); 
     724        char *temp2 = strtok(temp, "+"); 
     725        sprintf(_data_DB->command, "%s'%s';", _data_DB->command, temp2); 
    663726        sqlite3_stmt * pStatement; 
    664727        int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
     
    674737            printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command); 
    675738        } 
    676  
    677739        sqlite3_finalize(pStatement); 
    678740        char *data_ch = (char *) data.c_str(); 
     741        int32_t numStatements = 0; 
     742        temp2 = strtok(NULL, "+"); 
     743        while(temp2){ 
     744            numStatements++; 
     745            temp2 = strtok(NULL, "+"); 
     746        } 
     747        //printf("here1%d\n", numStatements); 
     748        char temp4[10]; 
     749        memset(temp4, 0, 10); 
     750        sprintf(temp4, "%d", numStatements); 
    679751        //Tokenize the data and pass it along 
    680         char *token = strtok(data_ch, "@"); 
     752        //printf("here2 %s\n", temp4); 
     753        if(strstr(miss[activeMission].services[sourceID].input[i].c_str(), "+")){ 
     754            SendMessage(miss[activeMission].services[sourceID].socketFD, temp4); 
     755            token = strtok(data_ch, "@"); 
     756        } 
     757        else{ 
     758            token = strtok(data_ch, "@"); 
     759            SendMessage(miss[activeMission].services[sourceID].socketFD, token); 
     760        } 
     761        token = strtok(NULL, "@"); 
     762 
     763 
     764        //Either have to send whole block of memory or just one piece 
    681765        while(token){ 
    682             SendMessage(miss[activeMission].services[sourceID].socketFD, token); 
     766            if(strstr(miss[activeMission].services[sourceID].input[i].c_str(), token)) 
     767            { 
     768                //printf("1tokenizing %s %s!\n", miss[activeMission].services[sourceID].input[i].c_str(), token); 
     769                SendMessage(miss[activeMission].services[sourceID].socketFD, token); 
     770                token = strtok(NULL, "@"); 
     771                SendMessage(miss[activeMission].services[sourceID].socketFD, token); 
     772            } 
     773            else if(!strstr(miss[activeMission].services[sourceID].input[i].c_str(), "+")) 
     774            { 
     775                //printf("t not detected tokenizing %s %s!\n", miss[activeMission].services[sourceID].input[i].c_str(), token); 
     776                SendMessage(miss[activeMission].services[sourceID].socketFD, token); 
     777                token = strtok(NULL, "@"); 
     778                SendMessage(miss[activeMission].services[sourceID].socketFD, token); 
     779            } 
     780            else{ 
     781                //printf("3tokenizing %s %s!\n", miss[activeMission].services[sourceID].input[i].c_str(), token); 
     782                token = strtok(NULL, "@");} 
    683783            token = strtok(NULL, "@"); 
    684784        } 
     785        //printf("done\n"); 
    685786        //printf("done pulling input data out of DB for ID#=%d\n", sourceID); 
    686787        i++; 
    687     } 
    688  
     788        data.clear(); 
     789    } 
    689790    int32_t j = 0; 
    690791    FD_ZERO(&sockSet); 
     
    695796    //Use select command to force wait for processing to finish 
    696797    select(miss[activeMission].services[sourceID].socketFD + 1, &sockSet, NULL, NULL, &selTimeout); 
    697     while(j < 3 && !miss[activeMission].services[sourceID].output[j].empty()){ 
     798        //printf("done\n"); 
     799   //TODO rewrite part of data on output? 
     800   //TODO true false format? 
     801    while(j < 5 && !miss[activeMission].services[sourceID].output[j].empty()){ 
    698802        int rc; 
    699         data.clear(); 
    700         while(true){ 
     803        memset(buffer, 0, 256); 
     804        ReadMessage(miss[activeMission].services[sourceID].socketFD, buffer); 
     805       // printf("waiting\n");       
     806        data.append(buffer); 
     807        data.append("@"); 
     808        int t = atoi(buffer); 
     809        //printf("%d\n", t); 
     810        for(int k = 0; k < t; k++){ 
    701811            //Read the data incrementally and deliminate it with the "@" symbol 
    702812            memset(buffer, 0, 256); 
    703813            ReadMessage(miss[activeMission].services[sourceID].socketFD, buffer); 
    704             if(strcmp(buffer, "output_finished")==0) 
    705                 break; 
    706             data.append(buffer); 
    707             data.append("@");; 
     814            if(!strcmp(buffer, "TF")){ 
     815                ReadMessage(miss[activeMission].services[sourceID].socketFD, buffer); 
     816                data.append(buffer); 
     817                data.append("@"); 
     818            } 
     819            else{ 
     820                data.append(buffer); 
     821                data.append("@"); 
     822                memset(buffer, 0, 256); 
     823                ReadMessage(miss[activeMission].services[sourceID].socketFD, buffer); 
     824                data.append(buffer); 
     825                data.append("@"); 
     826            } 
    708827        } 
    709         printf("SML: putting output data into DB for ID#=%d\n", sourceID); 
     828        //printf("SML: putting output data into DB for ID#=%d\n", sourceID); 
    710829 
    711830        strcpy(_data_DB->command, "insert or replace into "); 
     
    725844        if( rc!=SQLITE_OK && rc!=101 ) 
    726845            fprintf(stderr, "SQL error: %s\n", errorMsg); 
    727         printf("SML: done putting ouptut data into DB for ID#='%d', data=%s\n", sourceID, data.c_str()); 
     846        //printf("S: done putting ouptut data into DB for ID#='%d', data=%s\n", sourceID, data.c_str()); 
    728847        j++; 
    729     } 
    730     printf("done transact data!\n"); 
    731  
    732  
    733     printf("\n\n\n"); 
     848        data.clear(); 
     849    } 
     850    //printf("done transact data!\n"); 
     851    LOG("ServiceManagementLayer:: Finished with data transaction.\n"); 
     852 
     853 
     854    /*printf("\n\n\n"); 
    734855    // generate commandi 
    735856    strcpy(_data_DB->command, "select "); 
     
    745866        fprintf(stderr, "SQL error: %s\n", errorMsg); 
    746867    printf("database %s, table %s:\n", _data_DB->filename, _data_DB->tablename); 
    747     printf("\n\n\n"); 
     868    printf("\n\n\n");*/ 
    748869} 
    749870 
     
    775896 * -IMPORTANT: DB uses '@' to seperate individual statements; using '@' in the data stream will result in incorrect behavior 
    776897 */ 
     898 
     899//IF-IF-IF 
     900//IF-IF-WHILE 
     901//IF-WHILE 
     902//WHILE 
    777903void 
    778904ServiceManagementLayer::PerformActiveMission() 
    779905{ 
    780     int i = 0; 
    781     std::string data; 
     906    uint16_t i = 0; 
     907    std::string data_param, data_obsv, data; 
    782908    std::string input; 
    783909    std::string check; 
    784910    char buffer[256]; 
    785     fd_set sockSet; 
    786911    char* cols[] = {(char *)"Tag", (char *)"Data"}; 
    787     struct timeval selTimeout; 
    788912    //Get the inputs 
    789     while(i < 3 && !miss[activeMission].input[i].empty()){ 
    790             //New data being added to DB 
    791         printf("inserting data from shell\n"); 
    792         int rc; 
    793         while(true){ 
    794             memset(buffer, 0, 256); 
    795             ReadMessage(shellSocketFD, buffer); 
    796             if(strcmp(buffer, "input_finished")==0) 
    797                 break; 
    798             data.append(buffer); 
    799             data.append("@"); 
    800             /*FD_ZERO(&sockSet); 
    801             FD_SET(miss[activeMission].services[sourceID].socketFD, &sockSet); 
    802             selTimeout.tv_sec = 0; 
    803             selTimeout.tv_usec = 50000; 
    804             //Check if there is a message on the CE socket ready to be processed 
    805             rc=select(miss[activeMission].services[sourceID].socketFD + 1, &sockSet, NULL, NULL, &selTimeout);*/ 
    806         }//while(rc!=0);; 
     913    memset(buffer, 0, 256); 
     914    ReadMessage(shellSocketFD, buffer); 
     915    LOG("ServiceManagementLayer:: Received PerformActiveMission command.\n"); 
     916        data_obsv.append(buffer); 
     917        data_obsv.append("@"); 
     918 
     919    int t = atoi(buffer); 
     920   // printf("****%d****\n", t); 
     921    for(int k = 0; k < t; k++) { 
     922        memset(buffer, 0, 256); 
     923        ReadMessage(shellSocketFD, buffer); 
     924        data_obsv.append(buffer); 
     925        data_obsv.append("@"); 
     926    
     927        memset(buffer, 0, 256); 
     928        ReadMessage(shellSocketFD, buffer); 
     929        data_obsv.append(buffer); 
     930        data_obsv.append("@"); 
     931        //printf("data=%s\n", data_obsv.c_str()); 
     932    } 
    807933 
    808934        strcpy(_data_DB->command, "insert into "); 
     
    813939        strcat(_data_DB->command, cols[1]); 
    814940        strcat(_data_DB->command, ") "); 
    815         strcat(_data_DB->command, " values('"); 
    816         strcat(_data_DB->command, miss[activeMission].input[i].c_str()); 
    817         strcat(_data_DB->command, "', '"); 
    818         strcat(_data_DB->command, data.c_str()); 
     941        strcat(_data_DB->command, " values('observables', '"); 
     942        strcat(_data_DB->command, data_obsv.c_str()); 
    819943        strcat(_data_DB->command, "');"); 
    820944        char *errorMsg; 
     945        int rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg); 
     946        if( rc!=SQLITE_OK && rc!=101 ) 
     947            fprintf(stderr, "SQL error: %s\n", errorMsg); 
     948 
     949    /* Receive Set of Parameters */ 
     950    memset(buffer, 0, 256); 
     951    ReadMessage(shellSocketFD, buffer); 
     952        data_param.append(buffer); 
     953        data_param.append("@"); 
     954    t=atoi(buffer); 
     955    for(int k = 0; k < t; k++) { 
     956        memset(buffer, 0, 256); 
     957        ReadMessage(shellSocketFD, buffer); 
     958        data_param.append(buffer); 
     959        data_param.append("@"); 
     960    
     961        memset(buffer, 0, 256); 
     962        ReadMessage(shellSocketFD, buffer); 
     963        data_param.append(buffer); 
     964        data_param.append("@"); 
     965    } 
     966        //printf("data=%s\n", data_param.c_str()); 
     967 
     968        strcpy(_data_DB->command, "insert into "); 
     969        strcat(_data_DB->command, _data_DB->tablename);  
     970        strcat(_data_DB->command, " ("); 
     971        strcat(_data_DB->command, cols[0]); 
     972        strcat(_data_DB->command, ", "); 
     973        strcat(_data_DB->command, cols[1]); 
     974        strcat(_data_DB->command, ") "); 
     975        strcat(_data_DB->command, " values('paramaters', '"); 
     976        strcat(_data_DB->command, data_param.c_str()); 
     977        strcat(_data_DB->command, "');");  
    821978        rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg); 
    822979        if( rc!=SQLITE_OK && rc!=101 ) 
    823980            fprintf(stderr, "SQL error: %s\n", errorMsg); 
    824         printf("SML: finished adding data from shell on input %d\n", i); 
    825         i++; 
    826  
    827     } 
     981 
     982 
     983    /*printf("\n\n\n"); 
     984    // generate commandi 
     985    strcpy(_data_DB->command, "select "); 
     986    strcat(_data_DB->command, _data_DB->tablename); 
     987    strcat(_data_DB->command, ".* from "); 
     988    strcat(_data_DB->command, _data_DB->tablename); 
     989    strcat(_data_DB->command, ";"); 
     990 
     991    // execute print (select all)  command   
     992    rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg); 
     993    if( rc!=SQLITE_OK && rc!=101 ) 
     994        fprintf(stderr, "SQL error: %s\n", errorMsg); 
     995    printf("database %s, table %s:\n", _data_DB->filename, _data_DB->tablename); 
     996    printf("\n\n\n");*/ 
     997 
     998 
     999 
     1000 
     1001   // printf("done\n"); 
    8281002    i=0; 
    8291003    data.clear(); 
     
    8321006        if(miss[activeMission].services[i].name.compare("if")==0) 
    8331007        { 
    834             printf("if detected\n"); 
     1008           // printf("L0:if detected\n"); 
    8351009            input.clear(); 
    8361010            check.clear(); 
    8371011            if(!miss[activeMission].services[i].input[0].empty()){ 
    8381012                input=miss[activeMission].services[i].input[0]; 
    839                 check = "true@"; 
     1013                check = "1@true@"; 
    8401014            } 
    8411015            else{ 
    8421016                input=miss[activeMission].services[i].input[1]; 
    843                 check = "false@"; 
     1017                check = "1@false@"; 
    8441018            } 
    845             printf("input=%s\n", input.c_str()); 
     1019            //printf("L0:input=%s\n", input.c_str()); 
    8461020 
    8471021            strcpy(_data_DB->command, "SELECT "); 
     
    8521026            sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str()); 
    8531027            sqlite3_stmt * pStatement; 
    854             int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
     1028            rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
    8551029            if (rc == SQLITE_OK){  
    8561030                if (sqlite3_step(pStatement) == SQLITE_ROW) 
     
    8651039 
    8661040            sqlite3_finalize(pStatement); 
    867             printf("data=%s, check=%s\n", data.c_str(), check.c_str()); 
     1041           // printf("L0:data=%s, check=%s\n", data.c_str(), check.c_str()); 
     1042           // printf("L0:--- %d  %d---\n", miss[activeMission].services[i].num_conds, i); 
    8681043            if(data.compare(check)==0){ 
    869                 printf("if taken\n"); 
    870                 for(int k = i + 1; k <= i + miss[activeMission].services[i].num_conds; k++) 
    871                     TransactData(k); 
     1044                //printf("L0:if taken\n"); 
     1045                 
     1046                for(int k = i + 1; k <= i + miss[activeMission].services[i].num_conds; k++){ 
     1047                        //printf("k=%d\n", k); 
     1048                        if(miss[activeMission].services[k].name.compare("if")==0) 
     1049                        { 
     1050                            //printf("L1:if detected\n"); 
     1051                            input.clear(); 
     1052                            check.clear(); 
     1053                            if(!miss[activeMission].services[k].input[0].empty()){ 
     1054                                input=miss[activeMission].services[k].input[0]; 
     1055                                check = "1@true@"; 
     1056                            } 
     1057                            else{ 
     1058                                input=miss[activeMission].services[k].input[1]; 
     1059                                check = "1@false@"; 
     1060                            } 
     1061                            //printf("input=%s\n", input.c_str()); 
     1062 
     1063                            strcpy(_data_DB->command, "SELECT "); 
     1064                            strcat(_data_DB->command, _data_DB->tablename); 
     1065                            strcat(_data_DB->command, ".* from "); 
     1066                            strcat(_data_DB->command, _data_DB->tablename); 
     1067                            strcat(_data_DB->command, " where Tag=="); 
     1068                            sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str()); 
     1069                            sqlite3_stmt * pStatement; 
     1070                            rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
     1071                            if (rc == SQLITE_OK){  
     1072                                if (sqlite3_step(pStatement) == SQLITE_ROW) 
     1073                                     data = (const char *) sqlite3_column_text(pStatement, 1);  
     1074                                else { 
     1075                                        printf("1 data_DB:: Data not yet in DB. %d\n", k); 
     1076                                        rc=31337; 
     1077                                } 
     1078                            } else { 
     1079                                printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command); 
     1080                            } 
     1081 
     1082                            sqlite3_finalize(pStatement); 
     1083                            //printf("L1:data=%s, check=%s\n", data.c_str(), check.c_str()); 
     1084                            //printf("L1:--- %d  %d---\n", miss[activeMission].services[i].num_conds, k); 
     1085                            if(data.compare(check)==0){ 
     1086                                //printf("L1:if taken\n"); 
     1087                                for(int j = k + 1; j <= k + miss[activeMission].services[k].num_conds; j++){ 
     1088                                   // printf("j=%d\n", k); 
     1089                                    //printf("name!!!! = %s\n", miss[activeMission].services[j].name.c_str()); 
     1090                                    if(miss[activeMission].services[j].name.compare("if")==0) 
     1091                                    { 
     1092                                           // printf("L2:if detected\n"); 
     1093                                            input.clear(); 
     1094                                            check.clear(); 
     1095                                            if(!miss[activeMission].services[j].input[0].empty()){ 
     1096                                                input=miss[activeMission].services[j].input[0]; 
     1097                                                check = "1@true@"; 
     1098                                            } 
     1099                                            else{ 
     1100                                                input=miss[activeMission].services[j].input[1]; 
     1101                                                check = "1@false@"; 
     1102                                            } 
     1103                                            //printf("L2:input=%s\n", input.c_str()); 
     1104 
     1105                                            strcpy(_data_DB->command, "SELECT "); 
     1106                                            strcat(_data_DB->command, _data_DB->tablename); 
     1107                                            strcat(_data_DB->command, ".* from "); 
     1108                                            strcat(_data_DB->command, _data_DB->tablename); 
     1109                                            strcat(_data_DB->command, " where Tag=="); 
     1110                                            sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str()); 
     1111                                            sqlite3_stmt * pStatement; 
     1112                                            rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
     1113                                            if (rc == SQLITE_OK){  
     1114                                                if (sqlite3_step(pStatement) == SQLITE_ROW) 
     1115                                                     data = (const char *) sqlite3_column_text(pStatement, 1);  
     1116                                                else { 
     1117                                                        printf("1 data_DB:: Data not yet in DB. %d\n", k); 
     1118                                                        rc=31337; 
     1119                                                } 
     1120                                            } else { 
     1121                                                printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command); 
     1122                                            } 
     1123 
     1124                                            sqlite3_finalize(pStatement); 
     1125                                           // printf("L2:data=%s, check=%s\n", data.c_str(), check.c_str()); 
     1126                                           // printf("L2:--- %d  %d---\n", miss[activeMission].services[i].num_conds, k); 
     1127                                            if(data.compare(check)==0){ 
     1128                                                //printf("L2:if taken\n"); 
     1129                                                for(int p = j + 1; p <= j + miss[activeMission].services[j].num_conds; p++) 
     1130                                                    TransactData(p); 
     1131                                            } 
     1132                                            else{ 
     1133                                                //printf("L2:if not taken %d\n", miss[activeMission].services[j].num_conds); 
     1134                                            } 
     1135                                        j+=miss[activeMission].services[j].num_conds; 
     1136                                    } 
     1137 
     1138                                    else if(miss[activeMission].services[j].name.compare("while")==0) 
     1139                                    { 
     1140                                        //printf("while detected\n"); 
     1141                                        while(true){ 
     1142                                            data.clear(); 
     1143                                            input.clear(); 
     1144                                            check.clear(); 
     1145                                            if(!miss[activeMission].services[j].input[0].empty()){ 
     1146                                                input=miss[activeMission].services[j].input[0]; 
     1147                                                check = "1@true@"; 
     1148                                            } 
     1149                                            else{ 
     1150                                                input=miss[activeMission].services[j].input[1]; 
     1151                                                check = "1@false@"; 
     1152                                            } 
     1153                                            strcpy(_data_DB->command, "select "); 
     1154                                            strcat(_data_DB->command, _data_DB->tablename); 
     1155                                            strcat(_data_DB->command, ".* from "); 
     1156                                            strcat(_data_DB->command, _data_DB->tablename); 
     1157                                            strcat(_data_DB->command, " where Tag=="); 
     1158                                            sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str()); 
     1159                                            sqlite3_stmt * pStatement; 
     1160                                            rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
     1161                                            if (rc == SQLITE_OK){  
     1162                                                if (sqlite3_step(pStatement) == SQLITE_ROW) 
     1163                                                    data = (const char *) sqlite3_column_text(pStatement, 1);  
     1164                                                else  
     1165                                                    printf("1 data_DB:: Data not yet in DB. %d\n", i); 
     1166                                            }  
     1167                                            else { 
     1168                                                printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command); 
     1169                                            } 
     1170 
     1171                                            sqlite3_finalize(pStatement); 
     1172                                            printf("data=%s, check=%s\n", data.c_str(), check.c_str()); 
     1173                                            if(data.compare(check)==0){ 
     1174                                                printf("while taken\n"); 
     1175                                                for(int p = j + 1; p <= j + miss[activeMission].services[j].num_conds; p++) 
     1176                                                    TransactData(p); 
     1177                                            } 
     1178                                            else{ 
     1179                                                printf("while not taken %d\n", miss[activeMission].services[k].num_conds); 
     1180                                                 j+=miss[activeMission].services[j].num_conds; 
     1181                                                break; 
     1182                                            } 
     1183                                        } 
     1184                                    } 
     1185                                    else{ 
     1186                                        printf("L2: nocond transact\n"); 
     1187                                        TransactData(j);} 
     1188                                    j++; 
     1189                                } 
     1190                            } 
     1191                            else{ 
     1192                                //printf("if not taken %d\n", miss[activeMission].services[k].num_conds); 
     1193                            } 
     1194                            k+=miss[activeMission].services[k].num_conds; 
     1195                        } 
     1196                        else if(miss[activeMission].services[k].name.compare("while")==0) 
     1197                        { 
     1198                            //printf("while detected\n"); 
     1199                            while(true){ 
     1200                                    data.clear(); 
     1201                                    input.clear(); 
     1202                                    check.clear(); 
     1203                                    if(!miss[activeMission].services[k].input[0].empty()){ 
     1204                                        input=miss[activeMission].services[k].input[0]; 
     1205                                        check = "1@true@"; 
     1206                                    } 
     1207                                    else{ 
     1208                                        input=miss[activeMission].services[k].input[1]; 
     1209                                        check = "1@false@"; 
     1210                                    } 
     1211                                    strcpy(_data_DB->command, "select "); 
     1212                                    strcat(_data_DB->command, _data_DB->tablename); 
     1213                                    strcat(_data_DB->command, ".* from "); 
     1214                                    strcat(_data_DB->command, _data_DB->tablename); 
     1215                                    strcat(_data_DB->command, " where Tag=="); 
     1216                                    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str()); 
     1217                                    sqlite3_stmt * pStatement; 
     1218                                    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
     1219                                    if (rc == SQLITE_OK){  
     1220                                        if (sqlite3_step(pStatement) == SQLITE_ROW) 
     1221                                            data = (const char *) sqlite3_column_text(pStatement, 1);  
     1222                                        else  
     1223                                            printf("1 data_DB:: Data not yet in DB. %d\n", i); 
     1224                                    }  
     1225                                    else { 
     1226                                        printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command); 
     1227                                    } 
     1228 
     1229                                    sqlite3_finalize(pStatement); 
     1230                                    printf("data=%s, check=%s\n", data.c_str(), check.c_str()); 
     1231                                    if(data.compare(check)==0){ 
     1232                                        printf("while taken\n"); 
     1233                                        for(int k = i + 1; k <= i + miss[activeMission].services[i].num_conds; k++) 
     1234                                            TransactData(k); 
     1235                                    } 
     1236                                    else{ 
     1237                                        printf("while not taken %d\n", miss[activeMission].services[k].num_conds); 
     1238                                        k+=miss[activeMission].services[k].num_conds; 
     1239                                        break; 
     1240                                    } 
     1241                            } 
     1242                        } 
     1243                        else{ 
     1244                            //printf("L1: nocond transact\n"); 
     1245                            TransactData(k);} 
     1246                        k++; 
     1247                } 
    8721248            } 
    8731249            else{ 
    874                 i+=miss[activeMission].services[i].num_conds; 
    875                 printf("if not taken %d\n", miss[activeMission].services[i].num_conds); 
     1250                printf("La2 if not taken %d\n", miss[activeMission].services[i].num_conds); 
    8761251            } 
     1252            //printf("i=%d\n", i); 
     1253            i+=miss[activeMission].services[i].num_conds; 
     1254            //printf("i=%d\n", i); 
    8771255        } 
    8781256        else if(miss[activeMission].services[i].name.compare("while")==0) 
    8791257        { 
    880             printf("while detected\n"); 
     1258            //printf("while detected\n"); 
    8811259            while(true){ 
    8821260                    data.clear(); 
     
    8851263                    if(!miss[activeMission].services[i].input[0].empty()){ 
    8861264                        input=miss[activeMission].services[i].input[0]; 
    887                         check = "true@"; 
     1265                        check = "1@true@"; 
    8881266                    } 
    8891267                    else{ 
    8901268                        input=miss[activeMission].services[i].input[1]; 
    891                         check = "false@"; 
     1269                        check = "1@false@"; 
    8921270                    } 
    8931271                    strcpy(_data_DB->command, "select "); 
     
    8981276                    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str()); 
    8991277                    sqlite3_stmt * pStatement; 
    900                     int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
     1278                    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
    9011279                    if (rc == SQLITE_OK){  
    9021280                        if (sqlite3_step(pStatement) == SQLITE_ROW) 
     
    9101288 
    9111289                    sqlite3_finalize(pStatement); 
    912                     printf("data=%s, check=%s\n", data.c_str(), check.c_str()); 
     1290                    //printf("data=%s, check=%s\n", data.c_str(), check.c_str()); 
    9131291                    if(data.compare(check)==0){ 
    9141292                        printf("while taken\n"); 
     
    9171295                    } 
    9181296                    else{ 
     1297                        printf("while not taken %d\n", miss[activeMission].services[i].num_conds); 
    9191298                        i+=miss[activeMission].services[i].num_conds; 
    920                         printf("while not taken %d\n", miss[activeMission].services[i].num_conds); 
    9211299                        break; 
    9221300                    } 
     
    9241302        } 
    9251303        else{ 
     1304           // printf("L0 nocond transact\n"); 
    9261305            TransactData(i);} 
    9271306        i++; 
     1307        //printf("i=%d\n", i); 
    9281308    } 
    9291309    i=0; 
    9301310    data.clear(); 
    9311311    //get the ouptuts 
    932     while(i < 3 && !miss[activeMission].output[i].empty()){ 
    933         printf("sending output data to shell\n"); 
     1312    while(i < 5 && !miss[activeMission].output[i].empty()){ 
     1313        //printf("sending output data to shell\n"); 
    9341314        strcpy(_data_DB->command, "select "); 
    9351315        strcat(_data_DB->command, _data_DB->tablename); 
     
    9411321        int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
    9421322        if (rc == SQLITE_OK){  
    943             if (sqlite3_step(pStatement) == SQLITE_ROW) 
     1323            if (sqlite3_step(pStatement) == SQLITE_ROW)  
    9441324                 data.append((const char*) sqlite3_column_text(pStatement, 1));  
    9451325            else { 
     
    9501330        else { 
    9511331            printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command); 
    952         } 
     1332        }  
     1333        //printf("here %s\n", data.c_str()); 
    9531334        sqlite3_finalize(pStatement); 
    9541335        char *data_ch = (char *) data.c_str(); 
    9551336        char *token = strtok(data_ch, "@"); 
     1337        SendMessage(shellSocketFD, token); 
     1338        token = strtok(NULL, "@"); 
    9561339        while(token){ 
    9571340            SendMessage(shellSocketFD, token); 
     1341            //printf("token1 = %s\n", token); 
     1342            token = strtok(NULL, "@"); 
     1343            SendMessage(shellSocketFD, token); 
     1344            //printf("token2 = %s\n", token); 
    9581345            token = strtok(NULL, "@"); 
    9591346        } 
    9601347        i++; 
    961         printf("done sending output data to shell\n"); 
    962     } 
     1348        data.clear(); 
     1349    } 
     1350    LOG("ServiceManagementLayer:: Done sending output data to shell from PerformActiveMission.\n"); 
    9631351} 
    9641352 
     
    10231411{ 
    10241412    TiXmlElement *pMission; 
     1413    TiXmlElement *pService; 
     1414    TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3, *pChild4; 
     1415    TiXmlHandle hRoot(0); 
     1416    //printf("ServiceManagementLayer:: Loading Configuration.\n"); 
     1417    TiXmlDocument doc("."); 
     1418    doc.LoadFile(SML_Config); 
     1419    bool loadOkay = doc.LoadFile(); 
     1420    if(!loadOkay) 
     1421        printf("Loading SML configuration failed: %s\n", SML_Config); 
     1422 
     1423    TiXmlHandle hDoc(&doc); 
     1424     
     1425    pMission = hDoc.FirstChildElement().Element(); 
     1426 
     1427    if(!pMission)  
     1428        printf("No valid root!"); 
     1429 
     1430    hRoot = TiXmlHandle(pMission); 
     1431    pService = pMission->FirstChildElement(); 
     1432    int32_t mission_num = 0; 
     1433    //Iterate through the missions 
     1434    for(pChild0 = pMission->FirstChildElement(); pChild0 ; \ 
     1435        pChild0 = pChild0->NextSiblingElement())  
     1436    { 
     1437        int32_t service_num = 0; 
     1438        uint16_t cond_array[] = {0, 0, 0}; 
     1439        //printf("mission_num = %d\n", mission_num); 
     1440        //memset(cond_array, 0, 2); 
     1441         
     1442        for(pChild1  = pChild0->FirstChildElement(); pChild1; \ 
     1443            pChild1  = pChild1->NextSiblingElement())  
     1444        { 
     1445            int32_t conditional_0 = service_num; 
     1446            for(pChild2 = pChild1->FirstChildElement(); \ 
     1447                pChild2; pChild2 = pChild2->NextSiblingElement()) 
     1448            { 
     1449                service_num++; 
     1450                int32_t conditional_1 = service_num; 
     1451                for(pChild3 = pChild2->FirstChildElement(); \ 
     1452                    pChild3; pChild3 = pChild3 ->NextSiblingElement()) 
     1453                { 
     1454                    service_num++; 
     1455                    int32_t conditional_2 = service_num; 
     1456                        for(pChild4 = pChild3->FirstChildElement(); \ 
     1457                            pChild4; pChild4 = pChild4 ->NextSiblingElement()) 
     1458                        { 
     1459                            service_num++; 
     1460                            mList[mission_num].services[service_num].name = pChild4->Attribute("name"); 
     1461                            if(pChild4->Attribute("input1")) 
     1462                                mList[mission_num].services[service_num].input[0] = pChild4->Attribute("input1"); 
     1463                            if(pChild4->Attribute("input2")) 
     1464                                mList[mission_num].services[service_num].input[1] = pChild4->Attribute("input2"); 
     1465                            if(pChild4->Attribute("input3")) 
     1466                                mList[mission_num].services[service_num].input[2] = pChild4->Attribute("input3"); 
     1467                            if(pChild4->Attribute("input4")) 
     1468                                mList[mission_num].services[service_num].input[3] = pChild4->Attribute("input4"); 
     1469                            if(pChild4->Attribute("input5")) 
     1470                                mList[mission_num].services[service_num].input[4] = pChild4->Attribute("input5"); 
     1471                            if(pChild4->Attribute("output1")) 
     1472                                mList[mission_num].services[service_num].output[0] = pChild4->Attribute("output1"); 
     1473                            if(pChild4->Attribute("output2")) 
     1474                                mList[mission_num].services[service_num].output[1] = pChild4->Attribute("output2"); 
     1475                            if(pChild4->Attribute("output3")) 
     1476                                mList[mission_num].services[service_num].output[2] = pChild4->Attribute("output3"); 
     1477                            if(pChild4->Attribute("output4")) 
     1478                                mList[mission_num].services[service_num].output[3] = pChild4->Attribute("output4"); 
     1479                            if(pChild4->Attribute("output5")) 
     1480                                mList[mission_num].services[service_num].output[4] = pChild4->Attribute("output5"); 
     1481                            cond_array[2]++; 
     1482                        } 
     1483                        if(!strcmp(pChild3->Value(), "shell")) { 
     1484                            mList[mission_num].services[conditional_2].name = pChild3->Value(); 
     1485                            mList[mission_num].services[conditional_2].output[0] = pChild3->Attribute("output1"); 
     1486                //printf("2shell detected\n"); 
     1487                        } 
     1488                        else if(conditional_2 != service_num){ 
     1489                            mList[mission_num].services[conditional_2].name = pChild3->Value(); 
     1490                            if(pChild3->Attribute("input_t")) 
     1491                                mList[mission_num].services[conditional_2].input[0] = pChild3->Attribute("input_t"); 
     1492                            if(pChild3->Attribute("input_f")) 
     1493                                mList[mission_num].services[conditional_2].input[1] = pChild3->Attribute("input_f"); 
     1494                        } 
     1495                        else{ 
     1496                            mList[mission_num].services[service_num].name = pChild3->Attribute("name"); 
     1497                            if(pChild3->Attribute("input1")) 
     1498                                mList[mission_num].services[service_num].input[0] = pChild3->Attribute("input1"); 
     1499                            if(pChild3->Attribute("input2")) 
     1500                                mList[mission_num].services[service_num].input[1] = pChild3->Attribute("input2"); 
     1501                            if(pChild3->Attribute("input3")) 
     1502                                mList[mission_num].services[service_num].input[2] = pChild3->Attribute("input3"); 
     1503                            if(pChild3->Attribute("input4")) 
     1504                                mList[mission_num].services[service_num].input[3] = pChild3->Attribute("input4"); 
     1505                            if(pChild3->Attribute("input5")) 
     1506                                mList[mission_num].services[service_num].input[4] = pChild3->Attribute("input5"); 
     1507                            if(pChild3->Attribute("output1")) 
     1508                                mList[mission_num].services[service_num].output[0] = pChild3->Attribute("output1"); 
     1509                            if(pChild3->Attribute("output2")) 
     1510                                mList[mission_num].services[service_num].output[1] = pChild3->Attribute("output2"); 
     1511                            if(pChild3->Attribute("output3")) 
     1512                                mList[mission_num].services[service_num].output[2] = pChild3->Attribute("output3"); 
     1513                            if(pChild3->Attribute("output4")) 
     1514                                mList[mission_num].services[service_num].output[3] = pChild3->Attribute("output4"); 
     1515                            if(pChild3->Attribute("output5")) 
     1516                                mList[mission_num].services[service_num].output[4] = pChild3->Attribute("output5"); 
     1517                        } 
     1518                        mList[mission_num].services[conditional_2].num_conds = cond_array[2]; 
     1519                        cond_array[1]+=cond_array[2]+1; 
     1520                        //printf("cond_array[2]%d\n", cond_array[2]); 
     1521                        cond_array[2] = 0; 
     1522 
     1523 
     1524                } 
     1525                //printf("here\n"); 
     1526                if(!strcmp(pChild2->Value(), "shell")) { 
     1527                    mList[mission_num].services[conditional_1].name = pChild2->Value(); 
     1528                    mList[mission_num].services[conditional_1].output[0] = pChild2->Attribute("output1"); 
     1529                //printf("1shell detected\n"); 
     1530                } 
     1531                else if(conditional_1 != service_num){ 
     1532                    mList[mission_num].services[conditional_1].name = pChild2->Value(); 
     1533                    if(pChild2->Attribute("input_t")) 
     1534                        mList[mission_num].services[conditional_1].input[0] = pChild2->Attribute("input_t"); 
     1535                    if(pChild2->Attribute("input_f")) 
     1536                        mList[mission_num].services[conditional_1].input[1] = pChild2->Attribute("input_f"); 
     1537                } 
     1538                else{ 
     1539                    mList[mission_num].services[service_num].name = pChild2->Attribute("name"); 
     1540                    if(pChild2->Attribute("input1")) 
     1541                        mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input1"); 
     1542                    if(pChild2->Attribute("input2")) 
     1543                        mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input2"); 
     1544                    if(pChild2->Attribute("input3")) 
     1545                        mList[mission_num].services[service_num].input[2] = pChild2->Attribute("input3"); 
     1546                    if(pChild2->Attribute("input4")) 
     1547                        mList[mission_num].services[service_num].input[3] = pChild2->Attribute("input4"); 
     1548                    if(pChild2->Attribute("input5")) 
     1549                        mList[mission_num].services[service_num].input[4] = pChild2->Attribute("input5"); 
     1550                    if(pChild2->Attribute("output1")) 
     1551                        mList[mission_num].services[service_num].output[0] = pChild2->Attribute("output1"); 
     1552                    if(pChild2->Attribute("output2")) 
     1553                        mList[mission_num].services[service_num].output[1] = pChild2->Attribute("output2"); 
     1554                    if(pChild2->Attribute("output3")) 
     1555                        mList[mission_num].services[service_num].output[2] = pChild2->Attribute("output3"); 
     1556                    if(pChild2->Attribute("output4")) 
     1557                        mList[mission_num].services[service_num].output[3] = pChild2->Attribute("output4"); 
     1558                    if(pChild2->Attribute("output5")) 
     1559                        mList[mission_num].services[service_num].output[4] = pChild2->Attribute("output5"); 
     1560                } 
     1561 
     1562                mList[mission_num].services[conditional_1].num_conds = cond_array[1]; 
     1563                cond_array[0]+=cond_array[1]+1; 
     1564                //printf("cond_array[1]%d\n", cond_array[1]); 
     1565                cond_array[1] = 0; 
     1566            } 
     1567             
     1568            if(!strcmp(pChild1->Value(), "shell")) { 
     1569                //printf("shell detected\n"); 
     1570                mList[mission_num].services[conditional_0].name = pChild1->Value(); 
     1571                mList[mission_num].services[conditional_0].output[0] = pChild1->Attribute("output1"); 
     1572            } 
     1573            else if(conditional_0 != service_num){ 
     1574                mList[mission_num].services[conditional_0].name = pChild1->Value(); 
     1575                    if(pChild1->Attribute("input_t")) 
     1576                        mList[mission_num].services[conditional_0].input[0] = pChild1->Attribute("input_t"); 
     1577                    if(pChild1->Attribute("input_f")) 
     1578                        mList[mission_num].services[conditional_0].input[1] = pChild1->Attribute("input_f"); 
     1579                //printf("---input_t=%s\n", mList[mission_num].services[conditional_0].input[0].c_str()); 
     1580            } 
     1581            else{ 
     1582                mList[mission_num].services[conditional_0].name = pChild1->Attribute("name"); 
     1583                //printf("name=%s\n", mList[mission_num].services[conditional_0].name.c_str()); 
     1584                if(pChild1->Attribute("input1")) 
     1585                    mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input1"); 
     1586                if(pChild1->Attribute("input2")) 
     1587                    mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input2"); 
     1588                if(pChild1->Attribute("input3")) 
     1589                    mList[mission_num].services[service_num].input[2] = pChild1->Attribute("input3"); 
     1590                if(pChild1->Attribute("input4")) 
     1591                    mList[mission_num].services[service_num].input[3] = pChild1->Attribute("input4"); 
     1592                if(pChild1->Attribute("input5")) 
     1593                    mList[mission_num].services[service_num].input[4] = pChild1->Attribute("input5"); 
     1594                if(pChild1->Attribute("output1")) 
     1595                    mList[mission_num].services[service_num].output[0] = pChild1->Attribute("output1"); 
     1596                if(pChild1->Attribute("output2")) 
     1597                    mList[mission_num].services[service_num].output[1] = pChild1->Attribute("output2"); 
     1598                if(pChild1->Attribute("output3")) 
     1599                    mList[mission_num].services[service_num].output[2] = pChild1->Attribute("output3"); 
     1600                if(pChild1->Attribute("output4")) 
     1601                    mList[mission_num].services[service_num].output[3] = pChild1->Attribute("output4"); 
     1602                if(pChild1->Attribute("output5")) 
     1603                    mList[mission_num].services[service_num].output[4] = pChild1->Attribute("output4"); 
     1604            } 
     1605 
     1606            mList[mission_num].services[conditional_0].num_conds = cond_array[0]; 
     1607            cond_array[0] = 0; 
     1608            service_num++; 
     1609        } 
     1610        //for(int i = 0; i < service_num; i++) 
     1611         //printf("ttt%d\n", mList[mission_num].services[i].num_conds); 
     1612         
     1613        mList[mission_num].numServices = service_num; 
     1614        mList[mission_num].name = pChild0->Attribute("name"); 
     1615        mList[mission_num].missionID = atoi(pChild0->Attribute("id")); 
     1616        if(pChild0->Attribute("input1")) 
     1617            mList[mission_num].input[0] = pChild0->Attribute("input1"); 
     1618        if(pChild0->Attribute("input2")) 
     1619            mList[mission_num].input[1] = pChild0->Attribute("input2"); 
     1620        if(pChild0->Attribute("input3")) 
     1621            mList[mission_num].input[2] = pChild0->Attribute("input3"); 
     1622        if(pChild0->Attribute("input4")) 
     1623            mList[mission_num].input[3] = pChild0->Attribute("input4"); 
     1624        if(pChild0->Attribute("input5")) 
     1625            mList[mission_num].input[4] = pChild0->Attribute("input4"); 
     1626        if(pChild0->Attribute("output1")) 
     1627            mList[mission_num].output[0] = pChild0->Attribute("output1"); 
     1628        if(pChild0->Attribute("output2")) 
     1629            mList[mission_num].output[1] = pChild0->Attribute("output2"); 
     1630        if(pChild0->Attribute("output3")) 
     1631            mList[mission_num].output[2] = pChild0->Attribute("output3"); 
     1632        if(pChild0->Attribute("output4")) 
     1633            mList[mission_num].output[3] = pChild0->Attribute("output4"); 
     1634        if(pChild0->Attribute("output5")) 
     1635            mList[mission_num].output[4] = pChild0->Attribute("output5"); 
     1636        //printf("mis, input1=%s, output1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str()); 
     1637        //printf("NUMSERVICES = %d\n", mList[mission_num].numServices); 
     1638        mission_num++; 
     1639    } 
     1640} 
     1641 
     1642/* CALLED BY: MessageHandler 
     1643 * INPUTS: |ID| The ID number of the engine to be registered 
     1644 * OUTPUTS: <none> 
     1645 * 
     1646 * DESCRIPTION: Sends a registration message onto the shell and sends the ACK back to the component 
     1647 */ 
     1648void 
     1649ServiceManagementLayer::RegisterCognitiveEngine(int32_t ID) 
     1650{ 
     1651    //LOG("SML::regcogeng"); 
     1652    SendMessage(shellSocketFD, "register_engine_cognitive"); 
     1653 
     1654   // printf("SSFD = %d\n", shellSocketFD); 
     1655    LOG("ServiceManagementLayer:: CE registration message forwarded to shell.\n"); 
     1656    char buffer[256]; 
     1657    memset(buffer, 0, 256); 
     1658    ReadMessage(shellSocketFD, buffer); 
     1659    //printf("ServiceManagementLayer::buffer = %s\n", buffer); 
     1660    SendMessage(CE_List[ID].FD, buffer); 
     1661 
     1662    TransferRadioConfiguration(ID); 
     1663    memset(buffer, 0, 256); 
     1664    //printf("start trans exp\n"); 
     1665    TransferExperience(ID); 
     1666    memset(buffer, 0, 256); 
     1667    numberOfCognitiveEngines++; 
     1668    CE_Present = true; 
     1669    //printf("done registering CE!\n"); 
     1670} 
     1671 
     1672/* CALLED BY: MessageHandler 
     1673 * INPUTS: |ID| The ID number of the engine to have it's services deregistered 
     1674 * OUTPUTS: <none> 
     1675 * 
     1676 * DESCRIPTION: Deletes individual services from the DB 
     1677 * NOTE THAT this function only needs to be called if service deregistration is going 
     1678 * to be done at a different time than component deregistration; it is handled 
     1679 * more efficiently and directly during that deregistration process. 
     1680 */ 
     1681void  
     1682ServiceManagementLayer::DeregisterServices(int32_t ID) 
     1683{ 
     1684    char buffer[256]; 
     1685    memset(buffer, 0, 256); 
     1686    ReadMessage(CE_List[ID].FD, buffer);  
     1687    strcpy(_services_DB->command, "DELETE FROM "); 
     1688    strcat(_services_DB->command, _services_DB->tablename); 
     1689    strcat(_services_DB->command, " WHERE ID_Num IN (SELECT"); 
     1690    sprintf(_services_DB->command, " %s %d",_services_DB->command, ID); 
     1691    strcat(_services_DB->command, " FROM "); 
     1692    strcat(_services_DB->command, _services_DB->tablename); 
     1693    strcat(_services_DB->command, " WHERE Service_Name"); 
     1694    strcat(_services_DB->command, "=="); 
     1695    sprintf(_services_DB->command, "%s'%s');", _services_DB->command, buffer); 
     1696    char *errorMsg; 
     1697    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
     1698    if( rc!=SQLITE_OK && rc!=101 ) 
     1699        fprintf(stderr, "SQL error: %s\n", errorMsg); 
     1700} 
     1701 
     1702/* CALLED BY: MessageHandler 
     1703 * INPUTS: |ID| The ID number of the engine to have it's services deregistered 
     1704 * OUTPUTS: <none> 
     1705 * 
     1706 * DESCRIPTION: Deletes the contact info for the cognitive engine, forwards a deregistration message to the shell 
     1707 * Also, deletes the services from the DB 
     1708 */ 
     1709void  
     1710ServiceManagementLayer::DeregisterCognitiveEngine(int32_t ID) 
     1711{ 
     1712    LOG("ServiceManagementLayer:: CE deregistration message forwarded to shell.\n"); 
     1713 
     1714    numberOfCognitiveEngines--; 
     1715    if(numberOfCognitiveEngines == 0) 
     1716        CE_Present = false; 
     1717 
     1718    SendMessage(shellSocketFD, "deregister_engine_cognitive"); 
     1719    char buffer[256]; 
     1720    memset(buffer, 0, 256); 
     1721    ReadMessage(shellSocketFD, buffer); 
     1722    SendMessage(CE_List[ID].FD, buffer); 
     1723    if(strcmp("deregister_ack", buffer) != 0) { 
     1724        ERROR(1, "SML:: Failed to close CE socket\n"); 
     1725    } 
     1726 
     1727    //Deregister the services 
     1728    strcpy(_services_DB->command, "DELETE FROM "); 
     1729    strcat(_services_DB->command, _services_DB->tablename); 
     1730    strcat(_services_DB->command, " WHERE "); 
     1731    strcat(_services_DB->command, "ID_Num"); 
     1732    strcat(_services_DB->command, "=="); 
     1733    sprintf(_services_DB->command, "%s%d;", _services_DB->command, ID); 
     1734    char *errorMsg; 
     1735    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
     1736    if( rc!=SQLITE_OK && rc!=101 ) 
     1737        fprintf(stderr, "SQL error: %s\n", errorMsg); 
     1738 
     1739 
     1740    CE_List[ID].FD = -1; 
     1741    CE_List[ID].ID_num = -1; 
     1742 
     1743    LOG("Cognitive Radio Shell:: CE Socket closed for engine #%d.\n", ID); 
     1744} 
     1745 
     1746 
     1747/* CALLED BY: test class 
     1748 * INPUTS: <none> 
     1749 * OUTPUTS: <none> 
     1750 * 
     1751 * DESCRIPTION: Sets up a server socket and listens for communication on either that or the shell socket 
     1752 */ 
     1753void 
     1754ServiceManagementLayer::StartSMLServer() 
     1755{ 
     1756    //printf("Ready for CE Signal! (registration done)\n"); 
     1757    struct timeval selTimeout; 
     1758    int32_t running = 1; 
     1759    int32_t port, rc, new_sd = 1; 
     1760    int32_t desc_ready = 1; 
     1761                //If there is, call the MessageHandler with the Shell_Msg code of -1 
     1762    fd_set sockSet, shellSet; 
     1763 
     1764    cogEngSrv = CreateTCPServerSocket(SMLport); 
     1765    int32_t maxDescriptor = cogEngSrv; 
     1766 
     1767    if(InitializeTCPServerPort(cogEngSrv) == -1) 
     1768        ERROR(1,"Error initializing primary port\n"); 
     1769 
     1770    int i = 10000000;  //TODO change to "running" if endpoint can be reached 
     1771    while (running) { 
     1772        i--; 
     1773        /* Zero socket descriptor vector and set for server sockets */ 
     1774        /* This must be reset every time select() is called */ 
     1775        FD_ZERO(&sockSet); 
     1776        FD_SET(cogEngSrv, &sockSet); 
     1777        for(uint16_t k = 0; k < Current_ID; k++){ 
     1778            if(CE_List[k].ID_num != -1) 
     1779                FD_SET(CE_List[k].FD, &sockSet); 
     1780        } 
     1781            //printf("k=%d, CID=%d\n", k, CE_List[k].FD); 
     1782 
     1783        /* Timeout specification */ 
     1784        /* This must be reset every time select() is called */ 
     1785        selTimeout.tv_sec = 0;       /* timeout (secs.) */ 
     1786        selTimeout.tv_usec = 0;            /* 0 microseconds */ 
     1787        //Changed both to zero so that select will check messages from the shell instead of blocking 
     1788        //when there is no command from the CE's to be processed 
     1789 
     1790        //Check if there is a message on the socket waiting to be read 
     1791        rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout);  
     1792        //printf("rc=%d\n", rc); 
     1793        if(rc == 0){ 
     1794            //LOG("No echo requests for %i secs...Server still alive\n", timeout); 
     1795         
     1796            FD_ZERO(&shellSet); 
     1797            FD_SET(shellSocketFD, &shellSet); 
     1798            selTimeout.tv_sec = 0; 
     1799            selTimeout.tv_usec = 0; 
     1800            //Check if there is a message on the shell socket ready to be processed 
     1801            select(shellSocketFD + 1, &shellSet, NULL, NULL, &selTimeout); 
     1802            //printf("rc2=%d\n", rc2); 
     1803                //If there is, call the MessageHandler with the Shell_Msg code of -1 
     1804            if(FD_ISSET(shellSocketFD, &shellSet)){ 
     1805                //printf("shell_msg, %d\n", rc2); 
     1806                MessageHandler(-1);} 
     1807        } 
     1808        else {  
     1809            desc_ready = rc; 
     1810            for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) { 
     1811                if(FD_ISSET(port, &sockSet)) { 
     1812                    desc_ready -= 1; 
     1813 
     1814                    //Check if request is new or on an existing open descriptor 
     1815                    if(port == cogEngSrv) {  
     1816                        //If new, assign it a descriptor and give it an ID 
     1817                        new_sd = AcceptTCPConnection(port); 
     1818                          
     1819                        if(new_sd < 0)  
     1820                            break; 
     1821 
     1822                        CE_List[Current_ID].FD = new_sd; 
     1823                        CE_List[Current_ID].ID_num = Current_ID; 
     1824                        MessageHandler(Current_ID); 
     1825                        Current_ID++; 
     1826         
     1827                        FD_SET(new_sd,&sockSet); 
     1828                        if(new_sd > maxDescriptor)  
     1829                           maxDescriptor = new_sd; 
     1830                    }  
     1831                    else { 
     1832                        //If old, figure out which ID it coresponds to and handle it accordingly 
     1833                        for(uint16_t z = 0; z < Current_ID; z++) 
     1834                        { 
     1835                                if(CE_List[z].FD == port){ 
     1836                                        MessageHandler(z);} 
     1837                        } 
     1838                    } 
     1839                } 
     1840            } 
     1841        } 
     1842    }         
     1843 
     1844    /* Close sockets */ 
     1845    close(cogEngSrv); 
     1846 
     1847    //delete &cogEngSrv; 
     1848    return; 
     1849} 
     1850 
     1851 
     1852 
     1853 
     1854    /*TiXmlElement *pMission; 
    10251855    TiXmlElement *pService; 
    10261856    TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3; 
     
    10891919                } 
    10901920                else{ 
    1091                     mList[mission_num].services[conditional_1].name = pChild2->Attribute("name"); 
    1092                     if(pChild2->Attribute("input1")) 
    1093                         mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input1"); 
    1094                     if(pChild2->Attribute("input2")) 
    1095                         mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input2"); 
    1096                     if(pChild2->Attribute("input3")) 
    1097                         mList[mission_num].services[service_num].input[2] = pChild2->Attribute("input3"); 
    1098                     if(pChild2->Attribute("output1")) 
    1099                         mList[mission_num].services[service_num].output[0] = pChild2->Attribute("output1"); 
    1100                     if(pChild2->Attribute("output2")) 
    1101                         mList[mission_num].services[service_num].output[1] = pChild2->Attribute("output2"); 
    1102                     if(pChild2->Attribute("output3")) 
    1103                         mList[mission_num].services[service_num].output[2] = pChild2->Attribute("output3"); 
    1104                 } 
    1105  
    1106                 mList[mission_num].services[conditional_1].num_conds = cond_array[1]; 
    1107                 cond_array[1] = 0; 
    1108                 cond_array[0]++; 
    1109             } 
    1110             if(conditional_0 != service_num){ 
    1111                 mList[mission_num].services[conditional_0].name = pChild1->Value(); 
    1112                     if(pChild1->Attribute("input_t")) 
    1113                         mList[mission_num].services[conditional_0].input[0] = pChild1->Attribute("input_t"); 
    1114                     if(pChild1->Attribute("input_f")) 
    1115                         mList[mission_num].services[conditional_0].input[1] = pChild1->Attribute("input_f"); 
    1116                 //printf("---input_t=%s\n", mList[mission_num].services[conditional_0].input[0].c_str()); 
    1117             } 
    1118             else{ 
    1119                 mList[mission_num].services[conditional_0].name = pChild1->Attribute("name"); 
    1120                 if(pChild1->Attribute("input1")) 
    1121                     mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input1"); 
    1122                 if(pChild1->Attribute("input2")) 
    1123                     mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input2"); 
    1124                 if(pChild1->Attribute("input3")) 
    1125                     mList[mission_num].services[service_num].input[2] = pChild1->Attribute("input3"); 
    1126                 if(pChild1->Attribute("output1")) 
    1127                     mList[mission_num].services[service_num].output[0] = pChild1->Attribute("output1"); 
    1128                 if(pChild1->Attribute("output2")) 
    1129                     mList[mission_num].services[service_num].output[1] = pChild1->Attribute("output2"); 
    1130                 if(pChild1->Attribute("output3")) 
    1131                     mList[mission_num].services[service_num].output[2] = pChild1->Attribute("output3"); 
    1132             } 
    1133  
    1134             mList[mission_num].services[conditional_0].num_conds = cond_array[0]; 
    1135             cond_array[0] = 0; 
    1136             service_num++; 
    1137         } 
    1138         //for(int i = 0; i < service_num; i++) 
    1139          //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()); 
    1140  
    1141         mList[mission_num].numServices = service_num; 
    1142         mList[mission_num].name = pChild0->Attribute("name"); 
    1143         mList[mission_num].missionID = atoi(pChild0->Attribute("id")); 
    1144         if(pChild0->Attribute("input1")) 
    1145             mList[mission_num].input[0] = pChild0->Attribute("input1"); 
    1146         if(pChild0->Attribute("input2")) 
    1147             mList[mission_num].input[1] = pChild0->Attribute("input2"); 
    1148         if(pChild0->Attribute("input3")) 
    1149             mList[mission_num].input[2] = pChild0->Attribute("input3"); 
    1150         if(pChild0->Attribute("output1")) 
    1151             mList[mission_num].output[0] = pChild0->Attribute("output1"); 
    1152         if(pChild0->Attribute("output2")) 
    1153             mList[mission_num].output[1] = pChild0->Attribute("output2"); 
    1154         if(pChild0->Attribute("output3")) 
    1155             mList[mission_num].output[2] = pChild0->Attribute("output3"); 
    1156         //printf("mis, input1=%s, output1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str()); 
    1157         mission_num++; 
    1158     } 
    1159 } 
    1160  
    1161 /* CALLED BY: MessageHandler 
    1162  * INPUTS: |ID| The ID number of the engine to be registered 
    1163  * OUTPUTS: <none> 
    1164  * 
    1165  * DESCRIPTION: Sends a registration message onto the shell and sends the ACK back to the component 
    1166  */ 
    1167 void 
    1168 ServiceManagementLayer::RegisterCognitiveEngine(int32_t ID) 
    1169 { 
    1170     //LOG("SML::regcogeng"); 
    1171     SendMessage(shellSocketFD, "register_engine_cognitive"); 
    1172  
    1173    // printf("SSFD = %d\n", shellSocketFD); 
    1174     LOG("ServiceManagementLayer:: CE registration message forwarded to shell.\n"); 
    1175     char buffer[256]; 
    1176     memset(buffer, 0, 256); 
    1177     ReadMessage(shellSocketFD, buffer); 
    1178     //printf("ServiceManagementLayer::buffer = %s\n", buffer); 
    1179     SendMessage(CE_List[ID].FD, buffer); 
    1180  
    1181     TransferRadioConfiguration(ID); 
    1182     memset(buffer, 0, 256); 
    1183     //printf("start trans exp\n"); 
    1184     TransferExperience(ID); 
    1185     memset(buffer, 0, 256); 
    1186     numberOfCognitiveEngines++; 
    1187     CE_Present = true; 
    1188     //printf("done registering CE!\n"); 
    1189 } 
    1190  
    1191 /* CALLED BY: MessageHandler 
    1192  * INPUTS: |ID| The ID number of the engine to have it's services deregistered 
    1193  * OUTPUTS: <none> 
    1194  * 
    1195  * DESCRIPTION: Deletes individual services from the DB 
    1196  * NOTE THAT this function only needs to be called if service deregistration is going 
    1197  * to be done at a different time than component deregistration; it is handled 
    1198  * more efficiently and directly during that deregistration process. 
    1199  */ 
    1200 void  
    1201 ServiceManagementLayer::DeregisterServices(int32_t ID) 
    1202 { 
    1203     char buffer[256]; 
    1204     memset(buffer, 0, 256); 
    1205     ReadMessage(CE_List[ID].FD, buffer);  
    1206     strcpy(_services_DB->command, "DELETE FROM "); 
    1207     strcat(_services_DB->command, _services_DB->tablename); 
    1208     strcat(_services_DB->command, " WHERE ID_Num IN (SELECT"); 
    1209     sprintf(_services_DB->command, " %s %d",_services_DB->command, ID); 
    1210     strcat(_services_DB->command, " FROM "); 
    1211     strcat(_services_DB->command, _services_DB->tablename); 
    1212     strcat(_services_DB->command, " WHERE Service_Name"); 
    1213     strcat(_services_DB->command, "=="); 
    1214     sprintf(_services_DB->command, "%s'%s');", _services_DB->command, buffer); 
    1215     char *errorMsg; 
    1216     int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
    1217     if( rc!=SQLITE_OK && rc!=101 ) 
    1218         fprintf(stderr, "SQL error: %s\n", errorMsg); 
    1219 } 
    1220  
    1221 /* CALLED BY: MessageHandler 
    1222  * INPUTS: |ID| The ID number of the engine to have it's services deregistered 
    1223  * OUTPUTS: <none> 
    1224  * 
    1225  * DESCRIPTION: Deletes the contact info for the cognitive engine, forwards a deregistration message to the shell 
    1226  * Also, deletes the services from the DB 
    1227  */ 
    1228 void  
    1229 ServiceManagementLayer::DeregisterCognitiveEngine(int32_t ID) 
    1230 { 
    1231     LOG("ServiceManagementLayer:: CE deregistration message forwarded to shell.\n"); 
    1232  
    1233     numberOfCognitiveEngines--; 
    1234     if(numberOfCognitiveEngines == 0) 
    1235         CE_Present = false; 
    1236  
    1237     SendMessage(shellSocketFD, "deregister_engine_cognitive"); 
    1238     char buffer[256]; 
    1239     memset(buffer, 0, 256); 
    1240     ReadMessage(shellSocketFD, buffer); 
    1241     SendMessage(CE_List[ID].FD, buffer); 
    1242     if(strcmp("deregister_ack", buffer) != 0) { 
    1243         ERROR(1, "SML:: Failed to close CE socket\n"); 
    1244     } 
    1245  
    1246     //Deregister the services 
    1247     strcpy(_services_DB->command, "DELETE FROM "); 
    1248     strcat(_services_DB->command, _services_DB->tablename); 
    1249     strcat(_services_DB->command, " WHERE "); 
    1250     strcat(_services_DB->command, "ID_Num"); 
    1251     strcat(_services_DB->command, "=="); 
    1252     sprintf(_services_DB->command, "%s%d;", _services_DB->command, ID); 
    1253     char *errorMsg; 
    1254     int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
    1255     if( rc!=SQLITE_OK && rc!=101 ) 
    1256         fprintf(stderr, "SQL error: %s\n", errorMsg); 
    1257  
    1258  
    1259     CE_List[ID].FD = -1; 
    1260     CE_List[ID].ID_num = -1; 
    1261  
    1262     LOG("Cognitive Radio Shell:: CE Socket closed for engine #%d.\n", ID); 
    1263 } 
    1264  
    1265  
    1266 /* CALLED BY: test class 
    1267  * INPUTS: <none> 
    1268  * OUTPUTS: <none> 
    1269  * 
    1270  * DESCRIPTION: Sets up a server socket and listens for communication on either that or the shell socket 
    1271  */ 
    1272 void 
    1273 ServiceManagementLayer::StartSMLServer() 
    1274 { 
    1275     //printf("Ready for CE Signal! (registration done)\n"); 
    1276     struct timeval selTimeout; 
    1277     int32_t running = 1; 
    1278     int32_t port, rc, new_sd = 1; 
    1279     int32_t desc_ready = 1; 
    1280     int32_t timeout = 10; 
    1281                 //If there is, call the MessageHandler with the Shell_Msg code of -1 
    1282     fd_set sockSet, shellSet; 
    1283  
    1284     cogEngSrv = CreateTCPServerSocket(SMLport); 
    1285     int32_t maxDescriptor = cogEngSrv; 
    1286  
    1287     if(InitializeTCPServerPort(cogEngSrv) == -1) 
    1288         ERROR(1,"Error initializing primary port\n"); 
    1289  
    1290     int i = 10000000;  //TODO change to "running" if endpoint can be reached 
    1291     while (running) { 
    1292         i--; 
    1293         /* Zero socket descriptor vector and set for server sockets */ 
    1294         /* This must be reset every time select() is called */ 
    1295         FD_ZERO(&sockSet); 
    1296         FD_SET(cogEngSrv, &sockSet); 
    1297         for(int k = 0; k < Current_ID; k++){ 
    1298             if(CE_List[k].ID_num != -1) 
    1299                 FD_SET(CE_List[k].FD, &sockSet); 
    1300         } 
    1301             //printf("k=%d, CID=%d\n", k, CE_List[k].FD); 
    1302  
    1303         /* Timeout specification */ 
    1304         /* This must be reset every time select() is called */ 
    1305         selTimeout.tv_sec = 0;       /* timeout (secs.) */ 
    1306         selTimeout.tv_usec = 0;            /* 0 microseconds */ 
    1307         //Changed both to zero so that select will check messages from the shell instead of blocking 
    1308         //when there is no command from the CE's to be processed 
    1309  
    1310         //Check if there is a message on the socket waiting to be read 
    1311         rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout);  
    1312         //printf("rc=%d\n", rc); 
    1313         if(rc == 0){ 
    1314             //LOG("No echo requests for %i secs...Server still alive\n", timeout); 
    1315          
    1316             FD_ZERO(&shellSet); 
    1317             FD_SET(shellSocketFD, &shellSet); 
    1318             selTimeout.tv_sec = 0; 
    1319             selTimeout.tv_usec = 0; 
    1320             //Check if there is a message on the shell socket ready to be processed 
    1321             int rc2 = select(shellSocketFD + 1, &shellSet, NULL, NULL, &selTimeout); 
    1322             //printf("rc2=%d\n", rc2); 
    1323                 //If there is, call the MessageHandler with the Shell_Msg code of -1 
    1324             if(FD_ISSET(shellSocketFD, &shellSet)){ 
    1325                 //printf("shell_msg, %d\n", rc2); 
    1326                 MessageHandler(-1);} 
    1327         } 
    1328         else {  
    1329             desc_ready = rc; 
    1330             for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) { 
    1331                 if(FD_ISSET(port, &sockSet)) { 
    1332                     desc_ready -= 1; 
    1333  
    1334                     //Check if request is new or on an existing open descriptor 
    1335                     if(port == cogEngSrv) {  
    1336                         //If new, assign it a descriptor and give it an ID 
    1337                         new_sd = AcceptTCPConnection(port); 
    1338                           
    1339                         if(new_sd < 0)  
    1340                             break; 
    1341  
    1342                         CE_List[Current_ID].FD = new_sd; 
    1343                         CE_List[Current_ID].ID_num = Current_ID; 
    1344                         MessageHandler(Current_ID); 
    1345                         Current_ID++; 
    1346          
    1347                         FD_SET(new_sd,&sockSet); 
    1348                         if(new_sd > maxDescriptor)  
    1349                            maxDescriptor = new_sd; 
    1350                     }  
    1351                     else { 
    1352                         //If old, figure out which ID it coresponds to and handle it accordingly 
    1353                         for(int16_t z = 0; z < Current_ID; z++) 
    1354                         { 
    1355                                 if(CE_List[z].FD == port){ 
    1356                                         MessageHandler(z);} 
    1357                         } 
    1358                     } 
    1359                 } 
    1360             } 
    1361         } 
    1362     }         
    1363  
    1364     /* Close sockets */ 
    1365     close(cogEngSrv); 
    1366  
    1367     //delete &cogEngSrv; 
    1368     return; 
    1369 } 
    1370  
    1371  
    1372  
    1373  
    1374     /*TiXmlElement *pMission; 
    1375     TiXmlElement *pService; 
    1376     TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3; 
    1377     TiXmlHandle hRoot(0); 
    1378     //printf("ServiceManagementLayer:: Loading Configuration.\n"); 
    1379     TiXmlDocument doc("."); 
    1380     doc.LoadFile(SML_Config); 
    1381     bool loadOkay = doc.LoadFile(); 
    1382     if(!loadOkay) 
    1383         printf("Loading SML configuration failed: %s\n", SML_Config); 
    1384  
    1385     TiXmlHandle hDoc(&doc); 
    1386      
    1387     pMission = hDoc.FirstChildElement().Element(); 
    1388  
    1389     if(!pMission)  
    1390         printf("No valid root!"); 
    1391  
    1392     hRoot = TiXmlHandle(pMission); 
    1393     pService = pMission->FirstChildElement(); 
    1394     int32_t mission_num = 0; 
    1395     //Iterate through the missions 
    1396     for(pChild0 = pMission->FirstChildElement(); pChild0 ; \ 
    1397         pChild0 = pChild0->NextSiblingElement())  
    1398     { 
    1399         int32_t service_num = 0; 
    1400         uint16_t cond_array[] = {0, 0}; 
    1401         //printf("mission_num = %d\n", mission_num); 
    1402         memset(cond_array, 0, 2); 
    1403          
    1404         for(pChild1  = pChild0->FirstChildElement(); pChild1; \ 
    1405             pChild1  = pChild1->NextSiblingElement())  
    1406         { 
    1407             int32_t conditional_0 = service_num; 
    1408             for(pChild2 = pChild1->FirstChildElement(); \ 
    1409                 pChild2; pChild2 = pChild2->NextSiblingElement()) 
    1410             { 
    1411                 service_num++; 
    1412                 int32_t conditional_1 = service_num; 
    1413                 for(pChild3 = pChild2->FirstChildElement(); \ 
    1414                     pChild3; pChild3 = pChild3 ->NextSiblingElement()) 
    1415                 { 
    1416                     service_num++; 
    1417                     mList[mission_num].services[service_num].name = pChild3->Attribute("name"); 
    1418                     if(pChild3->Attribute("input1")) 
    1419                         mList[mission_num].services[service_num].input[0] = pChild3->Attribute("input1"); 
    1420                     if(pChild3->Attribute("input2")) 
    1421                         mList[mission_num].services[service_num].input[1] = pChild3->Attribute("input2"); 
    1422                     if(pChild3->Attribute("input3")) 
    1423                         mList[mission_num].services[service_num].input[2] = pChild3->Attribute("input3"); 
    1424                     if(pChild3->Attribute("output1")) 
    1425                         mList[mission_num].services[service_num].output[0] = pChild3->Attribute("output1"); 
    1426                     if(pChild3->Attribute("output2")) 
    1427                         mList[mission_num].services[service_num].output[1] = pChild3->Attribute("output2"); 
    1428                     if(pChild3->Attribute("output3")) 
    1429                         mList[mission_num].services[service_num].output[2] = pChild3->Attribute("output3"); 
    1430                     cond_array[1]++; 
    1431                 } 
    1432  
    1433                 if(conditional_1 != service_num){ 
    1434                     mList[mission_num].services[conditional_1].name = pChild2->Value(); 
    1435                     if(pChild2->Attribute("input_t")) 
    1436                         mList[mission_num].services[conditional_1].input[0] = pChild2->Attribute("input_t"); 
    1437                     if(pChild2->Attribute("input_f")) 
    1438                         mList[mission_num].services[conditional_1].input[1] = pChild2->Attribute("input_f"); 
    1439                 } 
    1440                 else{ 
    14411921                    mList[mission_num].services[service_num].name = pChild2->Attribute("name"); 
    14421922                    if(pChild2->Attribute("input1"))