Changeset 277

Show
Ignore:
Timestamp:
06/16/09 11:03:58 (15 years ago)
Author:
wrodgers
Message:

Updating SML functionality

Location:
vtcross/branches
Files:
92 added
5 modified

Legend:

Unmodified
Added
Removed
  • vtcross/branches/wrodgers/src/cognitive_engines/CognitiveEngine.cpp

    r275 r277  
    363363} 
    364364 
    365         RegisterServices(); 
    366365 
    367366void 
     
    404403 
    405404//Combined with deregister component since those two things must happen togeather 
    406 /*void  
     405void  
    407406CognitiveEngine::DeregisterServices() 
    408407{ 
     
    421420    SendMessage(commandSocketFD, "jam_bluetooth"); 
    422421 
    423 }*/ 
     422} 
    424423 
    425424void  
  • vtcross/branches/wrodgers/src/include/vtcross/components.h

    r275 r277  
    9797         */ 
    9898        virtual void RegisterServices() = 0; 
    99         //virtual void DeregisterServices() = 0; 
     99        virtual void DeregisterServices() = 0; 
    100100}; 
    101101 
     
    268268 
    269269        void RegisterServices(); 
    270         //void DeregisterServices(); 
     270        void DeregisterServices(); 
    271271 
    272272    private: 
  • vtcross/branches/wrodgers/src/service_management_layer/Makefile.am

    r200 r277  
    66 
    77sml_demo_SOURCES = ServiceManagementLayer.cpp sml_demo.cpp 
    8 sml_demo_LDADD = ../lib/socketcomm/libsocketcomm.a 
     8sml_demo_LDADD = -ldl -lpthread -lsqlite3  ../lib/tinyxml/libtinyxml.a ../lib/socketcomm/libsocketcomm.a 
    99 
  • vtcross/branches/wrodgers/src/service_management_layer/ServiceManagementLayer.cpp

    r275 r277  
    1414 * and run.  See the documentation for the "PerformActiveMission" below for important  
    1515 * info.   
    16  * 
    17  * NOTE THAT the RegisterServices method works differently than it is described in the documentation 
    18  * this will be fixed in a future release 
    19  */ 
    20  
    21 //TODO Verify while 
     16 */ 
     17 
    2218//TODO Add nested conditional support 
    2319//TODO Verify update functionality 
    2420//TODO Better shutdown 
     21//TODO Verify Deregister services 
     22//TODO printf's 
    2523 
    2624#include <stdlib.h> 
     
    3028#include <stdint.h> 
    3129 
    32 #include "../include/vtcross/common.h" 
    33  
    34 #include "../include/vtcross/components.h" 
    35 #include "../include/vtcross/containers.h" 
    36 #include "../include/vtcross/debug.h" 
    37 #include "../include/vtcross/error.h" 
    38 #include "../include/vtcross/socketcomm.h" 
    39  
    40 /*#include "../../trunk/src/include/vtcross/common.h" 
    41  
    42 #include "../../trunk/src/include/vtcross/components.h" 
    43 #include "../../trunk/src/include/vtcross/containers.h" 
    44 #include "../../trunk/src/include/vtcross/debug.h" 
    45 #include "../../trunk/src/include/vtcross/error.h" 
    46 #include "../../trunk/src/include/vtcross/socketcomm.h"*/ 
    47  
    48  
    49 #include <cstdlib> 
     30#include "vtcross/common.h" 
     31 
     32#include "components.h" 
     33#include "vtcross/containers.h" 
     34#include "vtcross/debug.h" 
     35#include "vtcross/error.h" 
     36#include "vtcross/socketcomm.h" 
    5037#include <cstring> 
    5138#include <stdint.h> 
     
    6350#include <sys/wait.h> 
    6451 
    65 //#include "../../trunk/src/include/tinyxml/tinyxml.h" 
    66 //#include "../../trunk/src/include/tinyxml/tinystr.h" 
    67  
    68 #include "../include/tinyxml/tinyxml.h" 
    69 #include "../include/tinyxml/tinystr.h" 
     52#include "tinyxml/tinyxml.h" 
     53#include "tinyxml/tinystr.h" 
    7054 
    7155#include "sqlite3.h" 
    72 //#include "sqlite3ext.h" 
    7356 
    7457typedef struct services_s *services_DB; 
     
    128111    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
    129112    if( rc!=SQLITE_OK && rc!=101 ) 
    130         fprintf(stderr, "1SQL error: %s\n", errorMsg); 
     113        fprintf(stderr, "ServiceManagementLayer::Destructor services 'drop table' error: %s\n", errorMsg); 
    131114    strcpy(_services_DB->command, "vacuum"); 
    132115    rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
    133116    if( rc!=SQLITE_OK && rc!=101 ) 
    134         fprintf(stderr, "1SQL error: %s\n", errorMsg); 
     117        fprintf(stderr, "ServiceManagementLayer::Destructor services 'vacuum' error: %s\n", errorMsg); 
    135118    free(_services_DB); 
    136119 
     
    139122    rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg); 
    140123    if( rc!=SQLITE_OK && rc!=101 ) 
    141         fprintf(stderr, "2SQL error (1): %s\n", errorMsg); 
     124        fprintf(stderr, "ServiceManagementLayer::Destructor data 'drop table' error: %s\n", errorMsg); 
    142125    strcpy(_data_DB->command, "vacuum"); 
    143126    rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg); 
    144127    if( rc!=SQLITE_OK && rc!=101 ) 
    145         fprintf(stderr, "2SQL error (2): %s\n", errorMsg); 
     128        fprintf(stderr, "ServiceManagementLayer::Destructor data 'vacuum' error: %s\n", errorMsg); 
    146129    free(_data_DB); 
    147130} 
     
    206189    strcat(_services_DB->command, _services_DB->tablename); 
    207190    strcat(_services_DB->command, "("); 
    208     //strcat(_services_DB->command, "SRVID INTEGER PRIMARY KEY, "); 
    209191    strcat(_services_DB->command, cols[0]); 
    210192    strcat(_services_DB->command, " INT, "); 
     
    219201    int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command, 128, &ppStmt, &pzTail); 
    220202    if( rc!=SQLITE_OK && rc!=101 ) 
    221         printf("SQL error(1): %d\n", rc); 
     203        printf("ServiceManagementLayer::CreateServicesDB 'prepare_stmt' error %d\n", rc); 
    222204    rc = sqlite3_step(ppStmt); 
    223205    if( rc!=SQLITE_OK && rc!=101 ) 
    224         printf("SQL error(2): %d\n", rc); 
     206        printf("ServiceManagementLayer::CreateServicesDB 'step' error %d\n"); 
    225207} 
    226208 
     
    275257    int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, 128, &ppStmt, &pzTail); 
    276258    if( rc!=SQLITE_OK && rc!=101 ) 
    277         printf("SQL error(1): %d\n", rc); 
     259        printf("ServiceManagementLayer::CreateDataDB 'prepare_stmt' error %d\n", rc); 
    278260    rc = sqlite3_step(ppStmt); 
    279261    if( rc!=SQLITE_OK && rc!=101 ) 
    280         printf("SQL error(2): %d\n", rc); 
     262        printf("ServiceManagementLayer::CreateDataDB 'step' error %d\n"); 
    281263} 
    282264 
     
    323305    char buffer[256];    
    324306    memset(buffer, 0, 256);   
    325     int32_t _FD;  
    326     //printf("ID= %d\n", ID);  
     307    int32_t _FD;   
    327308     
    328309    if(ID != -1) 
     
    331312        _FD = shellSocketFD; 
    332313    ReadMessage(_FD, buffer); 
    333     /*//printf("FD=%d, shellFD=%d", _FD, shellSocketFD); 
    334     ssize_t msgLength = recv(_FD, buffer, 256, MSG_PEEK); 
    335     if(msgLength == 0){ 
    336         //printf("Error reading from socket.\n"); 
    337         //sleep(5);      
    338         return; 
    339     } 
    340  
    341  
    342     size_t i; 
    343     for(i = 0; i < 256; i++) { 
    344         if(strcmp(&buffer[i], "\0") == 0) 
    345             break; 
    346     } 
    347     // Read the message into msgBuffer 
    348     recv(_FD, buffer, i + 1, 0);*/ 
    349  
     314     
    350315    //--------Policy Engine Stuff - no policy engine support in this version-------// 
    351316 
     
    405370    } 
    406371    else if(strcmp(buffer, "deregister_engine_cognitive") == 0) { 
     372        DeregisterCognitiveEngine(ID); 
     373    } 
     374    else if(strcmp(buffer, "deregister_service") == 0) { 
    407375        DeregisterServices(ID); 
    408         DeregisterCognitiveEngine(ID); 
    409376    } 
    410377} 
     
    434401{ 
    435402    DeregisterComponent(); 
    436     LoadConfiguration(_SML_Config, miss); 
     403    ReloadConfiguration(); 
    437404} 
    438405 
     
    478445 */ 
    479446 
    480 //TODO backwards check instead of forwords check to avoid waiting every data loop? 
     447//Modified to check the incoming message buffer rather than the outgoing message buffer to avoid a portion of the delay 
    481448void  
    482449ServiceManagementLayer::TransferRadioConfiguration(int32_t ID) 
     
    487454    char buffer[256]; 
    488455    //Send data until the CE sends an ACK message back 
    489     while(rc==0){ 
     456    while(rc!=0){ 
    490457        memset(buffer, 0, 256); 
    491458        //Receive data from Shell 
     
    494461        SendMessage(CE_List[ID].FD, buffer); 
    495462        FD_ZERO(&sockSet); 
    496         FD_SET(CE_List[ID].FD, &sockSet); 
     463        FD_SET(shellSocketFD, &sockSet); 
    497464        selTimeout.tv_sec = 0; 
    498465        selTimeout.tv_usec = 50000; 
    499         //Check if there is a message on the CE socket ready to be processed 
    500         rc=select(CE_List[ID].FD + 1, &sockSet, NULL, NULL, &selTimeout); 
     466        //Check if there is a message on the shell ready to be processed 
     467        rc=select(shellSocketFD + 1, &sockSet, NULL, NULL, &selTimeout); 
    501468    } 
    502469    memset(buffer, 0, 256); 
     
    512479 * DESCRIPTION: Simmilar to TransferRadioConfig, just with Experience data 
    513480 */ 
     481 
     482//Modified to check the incoming message buffer rather than the outgoing message buffer to avoid a portion of the delay 
    514483void  
    515484ServiceManagementLayer::TransferExperience(int32_t ID) 
     
    520489    char buffer[256]; 
    521490    //Send data until the CE sends an ACK message back 
    522     while(rc==0){ 
     491    while(rc!=0){ 
    523492        memset(buffer, 0, 256); 
    524493        //Receive data from Shell 
     
    527496        SendMessage(CE_List[ID].FD, buffer); 
    528497        FD_ZERO(&sockSet); 
    529         FD_SET(CE_List[ID].FD, &sockSet); 
     498        FD_SET(shellSocketFD, &sockSet); 
    530499        selTimeout.tv_sec = 0; 
    531500        selTimeout.tv_usec = 50000; 
    532         //Check if there is a message on the CE socket ready to be processed 
    533         rc = select(CE_List[ID].FD + 1, &sockSet, NULL, NULL, &selTimeout); 
     501        //Check if there is a message on the shell ready to be processed 
     502        rc=select(shellSocketFD + 1, &sockSet, NULL, NULL, &selTimeout); 
    534503    } 
    535504    memset(buffer, 0, 256); 
     
    553522     
    554523    // generate command 
    555     //printf("%s\n", _services_DB->command); 
    556524    strcpy(_services_DB->command, "insert into "); 
    557525    strcat(_services_DB->command, _services_DB->tablename);  
     
    572540    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
    573541    if( rc!=SQLITE_OK && rc!=101 ) 
    574         fprintf(stderr, "SQL error: %s\n", errorMsg); 
    575     printf("SML: Registering service '%s' from component number '%d'\n", buffer, ID); 
     542        fprintf(stderr, "ServiceManagementLayer::RecieveServices DB Error %s\n", errorMsg); 
     543    char *outBuffer; 
     544    sprintf(outBuffer, "SML: Registering service '%s' from component number '%d'\n", buffer, ID); 
     545    LOG(outBuffer); 
    576546 
    577547} 
     
    633603 
    634604 
    635     printf("\nhere ---%d, %d---\n", miss[activeMission].services[0].componentID, miss[activeMission].services[1].componentID); 
     605    //printf("\nhere ---%d, %d---\n", miss[activeMission].services[0].componentID, miss[activeMission].services[1].componentID); 
    636606} 
    637607 
     
    649619ServiceManagementLayer::TransactData(int32_t sourceID) 
    650620{ 
    651     printf("transactData\n"); 
     621    printf("TransactData Occuring\n"); 
    652622    char buffer[256]; 
    653623    std::string data; 
     
    661631    //Find and load the input data 
    662632    while(i < 3 && !miss[activeMission].services[sourceID].input[i].empty()){ 
    663         printf("pulling input data out of DB for ID#=%d\n", sourceID); 
     633        //printf("pulling input data out of DB for ID#=%d\n", sourceID); 
    664634        strcpy(_data_DB->command, "select "); 
    665635        strcat(_data_DB->command, _data_DB->tablename); 
     
    690660            token = strtok(NULL, "@"); 
    691661        } 
    692         printf("done pulling input data out of DB for ID#=%d\n", sourceID); 
     662        //printf("done pulling input data out of DB for ID#=%d\n", sourceID); 
    693663        i++; 
    694664    } 
     
    702672    //Use select command to force wait for processing to finish 
    703673    select(miss[activeMission].services[sourceID].socketFD + 1, &sockSet, NULL, NULL, &selTimeout); 
    704     data.clear(); 
    705674    while(j < 3 && !miss[activeMission].services[sourceID].output[j].empty()){ 
    706675        int rc; 
     676        data.clear(); 
    707677        while(true){ 
    708678            //Read the data incrementally and deliminate it with the "@" symbol 
     
    712682                break; 
    713683            data.append(buffer); 
    714             data.append("@"); 
     684            data.append("@");; 
    715685        } 
    716686        printf("SML: putting output data into DB for ID#=%d\n", sourceID); 
     
    732702        if( rc!=SQLITE_OK && rc!=101 ) 
    733703            fprintf(stderr, "SQL error: %s\n", errorMsg); 
    734         printf("SML: done putting ouptut data into DB for ID#='%d'\n", sourceID); 
     704        printf("SML: done putting ouptut data into DB for ID#='%d', data=%s\n", sourceID, data.c_str()); 
    735705        j++; 
    736706    } 
    737707    printf("done transact data!\n"); 
     708 
     709 
     710    printf("\n\n\n"); 
     711    // generate commandi 
     712    strcpy(_data_DB->command, "select "); 
     713    strcat(_data_DB->command, _data_DB->tablename); 
     714    strcat(_data_DB->command, ".* from "); 
     715    strcat(_data_DB->command, _data_DB->tablename); 
     716    strcat(_data_DB->command, ";"); 
     717 
     718    // execute print (select all)  command     
     719    char *errorMsg; 
     720    int rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg); 
     721    if( rc!=SQLITE_OK && rc!=101 ) 
     722        fprintf(stderr, "SQL error: %s\n", errorMsg); 
     723    printf("database %s, table %s:\n", _data_DB->filename, _data_DB->tablename); 
     724    printf("\n\n\n"); 
    738725} 
    739726 
     
    768755ServiceManagementLayer::PerformActiveMission() 
    769756{ 
    770     //TODO verify while 
    771757    int i = 0; 
    772758    std::string data; 
     
    834820                check = "false@"; 
    835821            } 
    836             strcpy(_data_DB->command, "select "); 
    837             strcat(_data_DB->command, _services_DB->tablename); 
    838             strcat(_data_DB->command, ".* from "); 
    839             strcat(_data_DB->command, _services_DB->tablename); 
     822            printf("input=%s\n", input.c_str()); 
     823 
     824            strcpy(_data_DB->command, "SELECT "); 
     825            strcat(_data_DB->command, _data_DB->tablename); 
     826            strcat(_data_DB->command, ".* from "); 
     827            strcat(_data_DB->command, _data_DB->tablename); 
    840828            strcat(_data_DB->command, " where Tag=="); 
    841829            sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str()); 
     
    857845            if(data.compare(check)==0){ 
    858846                printf("if taken\n"); 
    859                 for(int k = i + 1; k < i + miss[activeMission].services[i].num_conds; k++) 
     847                for(int k = i + 1; k <= i + miss[activeMission].services[i].num_conds; k++) 
    860848                    TransactData(k); 
    861849            } 
     
    865853            } 
    866854        } 
    867         //TODO test while 
    868855        else if(miss[activeMission].services[i].name.compare("while")==0) 
    869856        { 
    870857            printf("while detected\n"); 
    871             input.clear(); 
    872             check.clear(); 
    873858            while(true){ 
     859                    data.clear(); 
     860                    input.clear(); 
     861                    check.clear(); 
    874862                    if(!miss[activeMission].services[i].input[0].empty()){ 
    875863                        input=miss[activeMission].services[i].input[0]; 
     
    881869                    } 
    882870                    strcpy(_data_DB->command, "select "); 
    883                     strcat(_data_DB->command, _services_DB->tablename); 
     871                    strcat(_data_DB->command, _data_DB->tablename); 
    884872                    strcat(_data_DB->command, ".* from "); 
    885                     strcat(_data_DB->command, _services_DB->tablename); 
     873                    strcat(_data_DB->command, _data_DB->tablename); 
    886874                    strcat(_data_DB->command, " where Tag=="); 
    887875                    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str()); 
     
    902890                    if(data.compare(check)==0){ 
    903891                        printf("while taken\n"); 
    904                         for(int k = i + 1; k < i + miss[activeMission].services[i].num_conds; k++) 
     892                        for(int k = i + 1; k <= i + miss[activeMission].services[i].num_conds; k++) 
    905893                            TransactData(k); 
    906894                    } 
    907895                    else{ 
    908896                        i+=miss[activeMission].services[i].num_conds; 
    909                         printf("if not taken %d\n", miss[activeMission].services[i].num_conds); 
     897                        printf("while not taken %d\n", miss[activeMission].services[i].num_conds); 
    910898                        break; 
    911899                    } 
     
    994982} 
    995983 
    996 /* CALLED BY: constructor, Reset 
     984/* CALLED BY: constructor 
    997985 * INPUTS: |SML_Config| Address (either relitive or full) of the XML file containing mission data 
    998986 *         |mList| Mission array to be modified  
     
    10941082 
    10951083                mList[mission_num].services[conditional_1].num_conds = cond_array[1]; 
    1096 printf("hello\n"); 
    10971084                cond_array[1] = 0; 
    10981085                cond_array[0]++; 
     
    11041091                    if(pChild1->Attribute("input_f")) 
    11051092                        mList[mission_num].services[conditional_0].input[1] = pChild1->Attribute("input_f"); 
    1106                 printf("---input_t=%s\n", mList[mission_num].services[conditional_0].input[0].c_str()); 
     1093                //printf("---input_t=%s\n", mList[mission_num].services[conditional_0].input[0].c_str()); 
    11071094            } 
    11081095            else{ 
     
    11231110 
    11241111            mList[mission_num].services[conditional_0].num_conds = cond_array[0]; 
    1125             //printf("hello\n"); 
    11261112            cond_array[0] = 0; 
    11271113            service_num++; 
    11281114        } 
    1129         for(int i = 0; i < service_num; i++) 
    1130                 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()); 
     1115        //for(int i = 0; i < service_num; i++) 
     1116         //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()); 
    11311117 
    11321118        mList[mission_num].numServices = service_num; 
     
    11451131        if(pChild0->Attribute("output3")) 
    11461132            mList[mission_num].output[2] = pChild0->Attribute("output3"); 
    1147         printf("mis, input1=%s, output1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str()); 
     1133        //printf("mis, input1=%s, output1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str()); 
    11481134        mission_num++; 
    11491135    } 
     
    11801166 * OUTPUTS: <none> 
    11811167 * 
    1182  * DESCRIPTION: Deleates the services from the DB coresponding to the component ID 
    1183  */ 
    1184 //----NOTE---- 
    1185 //The behavior of this method is different than described in the documentation. 
    1186 //This function deregisteres all of the services when a component deregistes, 
    1187 //whereas the one in the documentation describes a function where an individual 
    1188 //service is deregistered.  This may be changed in the future. 
     1168 * DESCRIPTION: Deletes individual services from the DB 
     1169 * NOTE THAT this function only needs to be called if service deregistration is going 
     1170 * to be done at a different time than component deregistration; it is handled 
     1171 * more efficiently and directly during that deregistration process. 
     1172 */ 
    11891173void  
    11901174ServiceManagementLayer::DeregisterServices(int32_t ID) 
    11911175{ 
     1176    char buffer[256]; 
     1177    memset(buffer, 0, 256); 
     1178    ReadMessage(CE_List[ID].FD, buffer);  
     1179    strcpy(_services_DB->command, "DELETE FROM "); 
     1180    strcat(_services_DB->command, _services_DB->tablename); 
     1181    strcat(_services_DB->command, " WHERE ID_Num IN (SELECT"); 
     1182    sprintf(_services_DB->command, " %s %d",_services_DB->command, ID); 
     1183    strcat(_services_DB->command, " FROM "); 
     1184    strcat(_services_DB->command, _services_DB->tablename); 
     1185    strcat(_services_DB->command, " WHERE Service_Name"); 
     1186    strcat(_services_DB->command, "=="); 
     1187    sprintf(_services_DB->command, "%s'%s');", _services_DB->command, buffer); 
     1188    char *errorMsg; 
     1189    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
     1190    if( rc!=SQLITE_OK && rc!=101 ) 
     1191        fprintf(stderr, "SQL error: %s\n", errorMsg); 
     1192} 
     1193 
     1194/* CALLED BY: MessageHandler 
     1195 * INPUTS: |ID| The ID number of the engine to have it's services deregistered 
     1196 * OUTPUTS: <none> 
     1197 * 
     1198 * DESCRIPTION: Deletes the contact info for the cognitive engine, forwards a deregistration message to the shell 
     1199 * Also, deletes the services from the DB 
     1200 */ 
     1201void  
     1202ServiceManagementLayer::DeregisterCognitiveEngine(int32_t ID) 
     1203{ 
     1204    LOG("ServiceManagementLayer:: CE deregistration message forwarded to shell.\n"); 
     1205 
     1206    numberOfCognitiveEngines--; 
     1207    if(numberOfCognitiveEngines == 0) 
     1208        CE_Present = false; 
     1209 
     1210    SendMessage(shellSocketFD, "deregister_engine_cognitive"); 
     1211    char buffer[256]; 
     1212    memset(buffer, 0, 256); 
     1213    ReadMessage(shellSocketFD, buffer); 
     1214    SendMessage(CE_List[ID].FD, buffer); 
     1215    if(strcmp("deregister_ack", buffer) != 0) { 
     1216        ERROR(1, "SML:: Failed to close CE socket\n"); 
     1217    } 
     1218 
     1219    //Deregister the services 
    11921220    strcpy(_services_DB->command, "DELETE FROM "); 
    11931221    strcat(_services_DB->command, _services_DB->tablename); 
     
    12001228    if( rc!=SQLITE_OK && rc!=101 ) 
    12011229        fprintf(stderr, "SQL error: %s\n", errorMsg); 
    1202 } 
    1203  
    1204 /* CALLED BY: MessageHandler 
    1205  * INPUTS: |ID| The ID number of the engine to have it's services deregistered 
    1206  * OUTPUTS: <none> 
    1207  * 
    1208  * DESCRIPTION: Deletes the contact info for the cognitive engine, forwards a deregistration message to the shell 
    1209  */ 
    1210 void  
    1211 ServiceManagementLayer::DeregisterCognitiveEngine(int32_t ID) 
    1212 { 
    1213     LOG("ServiceManagementLayer:: CE deregistration message forwarded to shell.\n"); 
    1214  
    1215     numberOfCognitiveEngines--; 
    1216     if(numberOfCognitiveEngines == 0) 
    1217         CE_Present = false; 
    1218  
    1219     SendMessage(shellSocketFD, "deregister_engine_cognitive"); 
    1220     char buffer[256]; 
    1221     memset(buffer, 0, 256); 
    1222     ReadMessage(shellSocketFD, buffer); 
    1223     SendMessage(CE_List[ID].FD, buffer); 
    1224     if(strcmp("deregister_ack", buffer) != 0) { 
    1225         ERROR(1, "SML:: Failed to close CE socket\n"); 
    1226     } 
     1230 
     1231 
    12271232    CE_List[ID].FD = -1; 
    12281233    CE_List[ID].ID_num = -1; 
    12291234 
    1230     LOG("Cognitive Radio Shell:: Socket closed.\n"); 
     1235    LOG("Cognitive Radio Shell:: CE Socket closed for engine #%d.\n", ID); 
    12311236} 
    12321237 
     
    12551260        ERROR(1,"Error initializing primary port\n"); 
    12561261 
    1257     int i = 10000000; 
     1262    int i = 10000000;  //TODO change to "running" if endpoint can be reached 
    12581263    while (i>0) { 
    12591264        i--; 
  • vtcross/branches/wrodgers/src/service_management_layer/sml_demo.cpp

    r200 r277  
    1 /* Virginia Tech Cognitive Radio Open Source Systems 
    2  * Virginia Tech, 2009 
    3  * 
    4  * TODO LICENSE INFORMATION GOES HERE 
    5  */ 
    6  
    7 /* TODO DESCRIPTION OF FILE. 
    8  * 
    9  * This file is a temporary demo of a service management layer using some of  
    10  * our socket communication functions. This is *not* an actual implementation  
    11  * of our defined ServiceManagement class. 
    12  */ 
    13  
    14  
    151#include <cstdlib> 
    162#include <cstring> 
    173#include <stdint.h> 
     4#include <string> 
     5#include <stdlib.h>  
     6 
     7#include <arpa/inet.h> 
     8#include <iostream> 
     9#include <netinet/in.h> 
     10#include <netdb.h> 
     11#include <fcntl.h> 
     12#include <sys/ioctl.h> 
     13#include <sys/mman.h> 
     14#include <sys/socket.h> 
     15#include <sys/types.h> 
     16#include <sys/wait.h> 
     17 
     18#include "sqlite3.h" 
     19 
     20 
     21 
     22//#include "tinyxml.h" 
     23//#include "tinyxml.cpp" 
     24//#include "tinystr.h" 
    1825 
    1926#include "vtcross/common.h" 
     
    2532 
    2633 
    27 int32_t  
    28 main(int32_t argc, char *argv[]) 
    29 { 
    30     if(argc < 3) 
    31        ERROR(1, "Usage: %s hostname port\n", argv[0]); 
     34 
     35/*#include <stdio.h> 
     36#include <sys/types.h> 
     37#include <sys/socket.h> 
     38#include <netinet/in.h> 
     39#include <netdb.h> 
     40//#include <iostream> 
     41#include <stdlib.h> 
     42#include <string.h> 
     43#include <sqlite3.h> 
     44#include <sqlite3ext.h>*/ 
     45 
     46//#include "common.h" 
     47 
     48//using namespace std; 
     49 
     50 
     51int main(int argc, char *argv[]) 
     52{    
     53    int shell = CreateTCPServerSocket(1979); 
     54    //sleep(2); 
     55    int temp = AcceptTCPConnection(shell);  
     56   // SendMessage(temp, "list_services"); 
     57    sleep(1); 
     58    int FD = ClientSocket("127.0.0.1", "2036");  
     59    SendMessage(FD, "register_engine_cognitive");    
     60    SendMessage(FD, "register_service"); 
     61    SendMessage(FD, "test_srv");  
     62    SendMessage(FD, "list_services"); 
     63    int FD2 = ClientSocket("127.0.0.1", "2036");  
     64    SendMessage(FD2, "register_engine_cognitive");    
     65    SendMessage(FD2, "register_service"); 
     66    SendMessage(FD2, "test_srv1");  
     67    SendMessage(FD2, "register_service"); 
     68    SendMessage(FD2, "test_srv2"); 
     69    SendMessage(FD2, "list_services"); 
     70    sleep(1); 
     71    printf("preread1\n"); 
     72 
     73    char buffer[256]; 
     74    memset(buffer, 0, 256); 
     75 
     76    SendMessage(temp, "set_active_mission"); 
     77    SendMessage(temp, "0");  
     78    SendMessage(temp, "request_optimization"); 
     79    SendMessage(temp, "params"); 
     80    ReadMessage(FD, buffer); 
     81    printf("preread2\n"); 
     82    printf("-----buffer1= %s-------\n", buffer); 
     83    ReadMessage(FD, buffer); 
     84    printf("-----buffer2= %s-------\n", buffer); 
     85    memset(buffer, 0, 256); 
     86    ReadMessage(FD, buffer); 
     87    printf("-----buffer3= %s-------\n", buffer); 
     88    SendMessage(FD, "ACK"); 
     89    SendMessage(FD, "temp"); 
     90    SendMessage(FD, "raw_data"); 
     91    ReadMessage(FD2, buffer); 
     92    printf("-----buffer4= %s-------\n", buffer); 
     93    ReadMessage(FD2, buffer); 
     94    printf("-----buffer5= %s-------\n", buffer); 
     95    ReadMessage(FD2, buffer); 
     96    printf("-----buffer6= %s-------\n", buffer); 
     97    SendMessage(FD2, "ACK"); 
     98    ReadMessage(FD2, buffer); 
     99    printf("-----buffer7= %s-------\n", buffer); 
     100    ReadMessage(FD2, buffer); 
     101    printf("-----buffer8= %s-------\n", buffer); 
     102    ReadMessage(FD2, buffer); 
     103    printf("-----buffer9= %s-------\n", buffer); 
     104    SendMessage(FD2, "ACK"); 
     105    SendMessage(FD2, "trans"); 
     106    ReadMessage(temp, buffer); 
     107    printf("-----buffer10= %s-------\n", buffer); 
     108    SendMessage(temp, "ACK"); 
     109    /*ReadMessage(FD, buffer); 
     110    printf("-----buffer1= %s-------\n", buffer); 
     111    ReadMessage(FD, buffer); 
     112    printf("-----buffer2= %s-------\n", buffer); 
     113    memset(buffer, 0, 256); 
     114    ReadMessage(FD, buffer); 
     115    printf("-----buffer3= %s-------\n", buffer); 
     116    SendMessage(FD, "ACK"); 
     117    SendMessage(FD, "true"); 
     118    SendMessage(FD, "data"); 
     119    ReadMessage(FD2, buffer); 
     120    printf("-----buffer4= %s-------\n", buffer); 
     121    ReadMessage(FD2, buffer); 
     122    printf("-----buffer5= %s-------\n", buffer); 
     123    memset(buffer, 0, 256); 
     124    ReadMessage(FD2, buffer); 
     125    printf("-----buffer6= %s-------\n", buffer); 
     126    SendMessage(FD2, "ACK"); 
     127    SendMessage(FD2, "trans"); 
     128    ReadMessage(temp, buffer); 
     129    printf("-----buffer7= %s-------\n", buffer); 
     130    SendMessage(temp, "ACK");*/ 
     131    
    32132     
    33     ServiceManagementLayer sml; 
    34133 
    35     LOG("Waiting for signal...\n"); 
    36     while(1) { 
    37 //        policyEngine.WaitForSignal(); 
    38     } 
    39  
    40     return 0; 
     134    //SendMessage(temp, "list_services");   
     135    while(true){    
     136    char buffer[256]; 
     137    memset(buffer, 0, 256); 
     138    ReadMessage(temp, buffer); 
     139    printf("------%s------\n", buffer);} 
     140   /*memset(buffer, 0, 256); 
     141    ReadMessage(temp, buffer); 
     142    printf("------%s------\n", buffer);*/ 
    41143} 
    42