Changeset 278

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

Updated functionality of methods to include ReceiveServices?, other minor changes

Location:
vtcross/trunk/src
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp

    r268 r278  
    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/trunk/src/include/vtcross/components.h

    r268 r278  
    9797         */ 
    9898        virtual void RegisterServices() = 0; 
    99         //virtual void DeregisterServices() = 0; 
     99        virtual void DeregisterServices() = 0; 
    100100}; 
    101101 
     
    169169         */ 
    170170        void CreateServicesDB(); 
     171        void CreateDataDB(); 
    171172 
    172173        void PerformActiveMission(); 
    173         void TransactData(int32_t sourceID, int32_t destID); 
     174        void TransactData(int32_t sourceID); 
    174175 
    175176 
     
    267268 
    268269        void RegisterServices(); 
    269         //void DeregisterServices(); 
     270        void DeregisterServices(); 
    270271 
    271272    private: 
  • vtcross/trunk/src/service_management_layer/Makefile.am

    r200 r278  
    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 
     10 
  • vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp

    r206 r278  
    55 */ 
    66 
    7 /* DESCRIPTION OF FILE. 
    8  */ 
    9  
    10  
    11 #include <cstdlib> 
     7/* Inter-component communication handled by sockets and FD's.   
     8 * Server support has been completely implemented and tested. 
     9 * 
     10 * Services are stored in a SQLite DB by the ID of the CE that registered them.  Service 
     11 * support has been completely implemented and tested. 
     12 * 
     13 * Missions are loaded from an XML file, connected with services provided by components, 
     14 * and run.  See the documentation for the "PerformActiveMission" below for important  
     15 * info.   
     16 */ 
     17 
     18//TODO Add nested conditional support 
     19//TODO Verify update functionality 
     20//TODO Better shutdown 
     21//TODO Verify Deregister services 
     22//TODO printf's 
     23 
     24#include <stdlib.h> 
     25#include <string.h> 
     26#include <stdio.h> 
    1227#include <cstring> 
    1328#include <stdint.h> 
    1429 
    1530#include "vtcross/common.h" 
    16 #include "vtcross/components.h" 
     31 
     32#include "components.h" 
    1733#include "vtcross/containers.h" 
    1834#include "vtcross/debug.h" 
    1935#include "vtcross/error.h" 
    2036#include "vtcross/socketcomm.h" 
     37#include <cstring> 
     38#include <stdint.h> 
     39#include <math.h> 
     40 
     41#include <arpa/inet.h> 
     42#include <iostream> 
     43#include <netinet/in.h> 
     44#include <netdb.h> 
     45#include <fcntl.h> 
     46#include <sys/ioctl.h> 
     47#include <sys/mman.h> 
     48#include <sys/socket.h> 
     49#include <sys/types.h> 
     50#include <sys/wait.h> 
     51 
     52#include "tinyxml/tinyxml.h" 
     53#include "tinyxml/tinystr.h" 
     54 
     55#include "sqlite3.h" 
     56 
     57typedef struct services_s *services_DB; 
     58typedef struct data_s *data_DB; 
     59 
     60using namespace std; 
     61 
     62struct services_s { 
     63    char filename[64]; 
     64    char tablename[64]; 
     65    char command[2048]; 
     66    sqlite3 *db;  
     67    unsigned int num_columns; 
     68}; 
     69 
     70struct data_s { 
     71    char filename[64]; 
     72    char tablename[64]; 
     73    char command[2048]; 
     74    sqlite3 *db;  
     75    unsigned int num_columns; 
     76}; 
     77 
     78services_DB _services_DB; 
     79data_DB _data_DB; 
     80const char *_SML_Config; 
     81 
     82//Callback function used internally by some of the SQLite3 commands 
     83int callback(void *notUsed, int argc, char **argv, char **azColName){ 
     84    int i; 
     85    for(i=0; i<argc; i++){ 
     86        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); 
     87    } 
     88    printf("\n"); 
     89    return 0; 
     90} 
     91 
    2192 
    2293 
     
    2596    LOG("Creating Service Management Layer.\n"); 
    2697    shellSocketFD = -1; 
    27     LoadConfiguration(); 
    28 } 
    29  
    30  
     98    numberOfCognitiveEngines = 0; 
     99    CE_Present = false; 
     100    cogEngSrv = 1; 
     101} 
     102 
     103//Free and clear the DB's associated with this SML in the destructor 
     104//Note that exiting with an error condition will cause SML to not be destructed, 
     105// resulting in the DB's staying in memory until the destructor is encountered in future executions 
    31106ServiceManagementLayer::~ServiceManagementLayer() 
    32107{ 
    33 } 
    34  
    35  
    36 ServiceManagementLayer::ServiceManagementLayer(const char* serverName, \ 
    37         const char* serverPort) 
     108    char *errorMsg; 
     109    strcpy(_services_DB->command, "drop table "); 
     110    strcat(_services_DB->command, _services_DB->tablename); 
     111    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
     112    if( rc!=SQLITE_OK && rc!=101 ) 
     113        fprintf(stderr, "ServiceManagementLayer::Destructor services 'drop table' error: %s\n", errorMsg); 
     114    strcpy(_services_DB->command, "vacuum"); 
     115    rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
     116    if( rc!=SQLITE_OK && rc!=101 ) 
     117        fprintf(stderr, "ServiceManagementLayer::Destructor services 'vacuum' error: %s\n", errorMsg); 
     118    free(_services_DB); 
     119 
     120    strcpy(_data_DB->command, "drop table "); 
     121    strcat(_data_DB->command, _data_DB->tablename); 
     122    rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg); 
     123    if( rc!=SQLITE_OK && rc!=101 ) 
     124        fprintf(stderr, "ServiceManagementLayer::Destructor data 'drop table' error: %s\n", errorMsg); 
     125    strcpy(_data_DB->command, "vacuum"); 
     126    rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg); 
     127    if( rc!=SQLITE_OK && rc!=101 ) 
     128        fprintf(stderr, "ServiceManagementLayer::Destructor data 'vacuum' error: %s\n", errorMsg); 
     129    free(_data_DB); 
     130} 
     131 
     132//Note that sizes of CE_List, miss, and service are hardcoded for now. 
     133//Also, their sizes are hardcoded into the code in various places; a fix for a future version. 
     134ServiceManagementLayer::ServiceManagementLayer(const char* SML_Config, \ 
     135        const char* serverName, const char* serverPort) 
    38136{ 
    39137    LOG("Creating Service Management Layer.\n"); 
     138    _SML_Config = SML_Config; 
    40139 
    41140    ConnectToShell(serverName, serverPort); 
    42  
    43     LoadConfiguration(); 
    44 } 
    45  
    46  
     141    CE_List = (CE_Reg *) malloc(10*sizeof(struct CE_Reg)); 
     142    CE_List = new CE_Reg[10]; 
     143 
     144    miss = new Mission[10]; 
     145    for(int i = 0; i < 10; i++) 
     146        miss[i].services = new Service[20]; 
     147 
     148    Current_ID = 0; 
     149 
     150    LoadConfiguration(SML_Config, miss); 
     151    CreateServicesDB(); 
     152    CreateDataDB(); 
     153} 
     154 
     155/* CALLED BY: constructor 
     156 * INPUTS: <none> 
     157 * OUTPUTS: <none> 
     158 * 
     159 * DESCRIPTION: Create and initialize a DB to hold the services registered by components 
     160 */ 
     161void 
     162ServiceManagementLayer::CreateServicesDB() 
     163{ 
     164    _services_DB = (services_DB) malloc(sizeof(struct services_s)); 
     165    char *errorMsg; 
     166 
     167    // create database 
     168 
     169    // copy filename 
     170    unsigned int i=0; 
     171    strcpy(_services_DB->filename, "Services_Table"); 
     172 
     173    // execute create database command 
     174    // database handle 
     175    //_services_DB->db = NULL; 
     176    sqlite3_open(_services_DB->filename, &(_services_DB->db)); 
     177    char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"}; 
     178 
     179    // create table 
     180 
     181    // copy tablename 
     182    strcpy(_services_DB->tablename, "Services"); 
     183 
     184    // number of columns in the table 
     185    _services_DB->num_columns = 2; 
     186 
     187    // generate command 
     188    strcpy(_services_DB->command, "CREATE TABLE "); 
     189    strcat(_services_DB->command, _services_DB->tablename); 
     190    strcat(_services_DB->command, "("); 
     191    strcat(_services_DB->command, cols[0]); 
     192    strcat(_services_DB->command, " INT, "); 
     193    strcat(_services_DB->command, cols[1]); 
     194    strcat(_services_DB->command, " TEXT"); 
     195    strcat(_services_DB->command, ");"); 
     196 
     197    // execute create table command 
     198  sqlite3_stmt *ppStmt;  /* OUT: Statement handle */ 
     199  const char *pzTail;     /* OUT: Pointer to unused portion of zSql */ 
     200 
     201    int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command, 128, &ppStmt, &pzTail); 
     202    if( rc!=SQLITE_OK && rc!=101 ) 
     203        printf("ServiceManagementLayer::CreateServicesDB 'prepare_stmt' error %d\n", rc); 
     204    rc = sqlite3_step(ppStmt); 
     205    if( rc!=SQLITE_OK && rc!=101 ) 
     206        printf("ServiceManagementLayer::CreateServicesDB 'step' error %d\n"); 
     207} 
     208 
     209/* CALLED BY: constructor 
     210 * INPUTS: <none> 
     211 * OUTPUTS: <none> 
     212 * 
     213 * DESCRIPTION: Create and initialize a DB to hold the data sent by components 
     214 */ 
     215void 
     216ServiceManagementLayer::CreateDataDB() 
     217{ 
     218    _data_DB = (data_DB) malloc(sizeof(struct data_s)); 
     219    char *errorMsg; 
     220 
     221    // create database 
     222 
     223    // copy filename 
     224    unsigned int i=0; 
     225    strcpy(_data_DB->filename, "Data_Table"); 
     226 
     227    // execute create database command 
     228    // database handle 
     229    //_services_DB->db = NULL; 
     230    sqlite3_open(_data_DB->filename, &(_data_DB->db)); 
     231    char* cols[] = {(char *)"Tag", (char *)"Data"}; 
     232 
     233    // create table 
     234 
     235    // copy tablename 
     236    strcpy(_data_DB->tablename, "Data"); 
     237 
     238    // number of columns in the table 
     239    _data_DB->num_columns = 2; 
     240 
     241    // generate command 
     242    strcpy(_data_DB->command, "CREATE TABLE "); 
     243    strcat(_data_DB->command, _data_DB->tablename); 
     244    strcat(_data_DB->command, "("); 
     245    strcat(_data_DB->command, cols[0]); 
     246    //First column is the name of the data (coresponding to the name of the output/input pair) 
     247    //It is the primary key so any subsequent data with the same name will replace the row 
     248    strcat(_data_DB->command, " TEXT PRIMARY KEY ON CONFLICT REPLACE, "); 
     249    strcat(_data_DB->command, cols[1]); 
     250    strcat(_data_DB->command, " TEXT"); 
     251    strcat(_data_DB->command, ");"); 
     252 
     253    // execute create table command 
     254  sqlite3_stmt *ppStmt;  /* OUT: Statement handle */ 
     255  const char *pzTail;     /* OUT: Pointer to unused portion of zSql */ 
     256 
     257    int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, 128, &ppStmt, &pzTail); 
     258    if( rc!=SQLITE_OK && rc!=101 ) 
     259        printf("ServiceManagementLayer::CreateDataDB 'prepare_stmt' error %d\n", rc); 
     260    rc = sqlite3_step(ppStmt); 
     261    if( rc!=SQLITE_OK && rc!=101 ) 
     262        printf("ServiceManagementLayer::CreateDataDB 'step' error %d\n"); 
     263} 
     264 
     265/* CALLED BY: MessageHandler 
     266 * INPUTS: <none> 
     267 * OUTPUTS: <none> 
     268 * 
     269 * DESCRIPTION: Sends a message identifying this component as an SML to the Shell 
     270 */ 
    47271void  
    48272ServiceManagementLayer::SendComponentType() 
     
    52276} 
    53277 
    54  
     278/* CALLED BY: constructor 
     279 * INPUTS: |serverName| the IPv4 name of the server (127.0.0.1 for localhost) 
     280 *         |serverPort| the port on the server to connect to 
     281 * OUTPUTS: <none> 
     282 * 
     283 * DESCRIPTION: Connecting to the shell takes 2 steps 
     284 * 1) Establish a client socket for communication 
     285 * 2) Run the initial Registration/handshake routine 
     286 */ 
    55287void  
    56288ServiceManagementLayer::ConnectToShell(const char* serverName, \ 
     
    58290{ 
    59291    shellSocketFD = ClientSocket(serverName, serverPort); 
    60  
    61292    RegisterComponent(); 
    62293} 
    63294 
    64  
    65 void  
    66 ServiceManagementLayer::WaitForSignal() 
    67 { 
    68     char buffer[256]; 
    69  
    70     while(true) { 
    71         memset(buffer, 0, 256); 
    72         
    73         ReadMessage(shellSocketFD, buffer); 
    74  
    75         // TODO 
    76         // If we send integer op codes rather than strings, this process will be 
    77         // MUCH faster since instead of donig string compares we can simply 
    78         // switch on the integer value... 
    79         if(strcmp(buffer, "register_service") == 0) { 
    80             if(strcmp(buffer, "policy_geo") == 0) { 
    81             } 
    82             else if(strcmp(buffer, "policy_time") == 0) { 
    83             } 
    84             else if(strcmp(buffer, "policy_spectrum") == 0) { 
    85             } 
    86             else if(strcmp(buffer, "policy_spacial") == 0) { 
    87             } 
     295/* CALLED BY: StartSMLServer 
     296 * INPUTS: |ID| The ID number of the CE that has a message wating 
     297 * OUTPUTS: <none> 
     298 * 
     299 * DESCRIPTION: Called whenever a socket is identified as being ready for communication 
     300 *              This funciton reads the message and calls the appropriate helper 
     301 */  
     302void 
     303ServiceManagementLayer::MessageHandler(int32_t ID) 
     304{ 
     305    char buffer[256];    
     306    memset(buffer, 0, 256);   
     307    int32_t _FD;   
     308     
     309    if(ID != -1) 
     310        _FD = CE_List[ID].FD; 
     311    else  
     312        _FD = shellSocketFD; 
     313    ReadMessage(_FD, buffer); 
     314     
     315    //--------Policy Engine Stuff - no policy engine support in this version-------// 
     316 
     317    //printf("********* %s **********\n", buffer); 
     318    // TODO 
     319    // If we send integer op codes rather than strings, this process will be 
     320    // MUCH faster since instead of donig string compares we can simply 
     321    // switch on the integer value... 
     322    /*if(strcmp(buffer, "register_service") == 0) { 
     323        if(strcmp(buffer, "policy_geo") == 0) { 
    88324        } 
    89         else if(strcmp(buffer, "deregister_service") == 0) { 
    90             if(strcmp(buffer, "policy_geo") == 0) { 
    91             } 
    92             else if(strcmp(buffer, "policy_time") == 0) { 
    93             } 
    94             else if(strcmp(buffer, "policy_spectrum") == 0) { 
    95             } 
    96             else if(strcmp(buffer, "policy_spacial") == 0) { 
    97             } 
     325        else if(strcmp(buffer, "policy_time") == 0) { 
    98326        } 
    99         else if(strcmp(buffer, "query_component_type") == 0) { 
    100             SendComponentType(); 
     327        else if(strcmp(buffer, "policy_spectrum") == 0) { 
    101328        } 
    102         else if(strcmp(buffer, "reset_sml") == 0) { 
    103             Reset(); 
     329        else if(strcmp(buffer, "policy_spacial") == 0) { 
    104330        } 
    105         else if(strcmp(buffer, "shutdown_sml") == 0) { 
    106             Shutdown(); 
     331    } 
     332    else if(strcmp(buffer, "deregister_service") == 0) { 
     333        if(strcmp(buffer, "policy_geo") == 0) { 
    107334        } 
    108     } 
    109 } 
    110  
    111  
     335        else if(strcmp(buffer, "policy_time") == 0) { 
     336        } 
     337        else if(strcmp(buffer, "policy_spectrum") == 0) { 
     338        } 
     339        else if(strcmp(buffer, "policy_spacial") == 0) { 
     340        } 
     341    }*/ 
     342 
     343    //Go down the list to call the appropriate function 
     344    if(strcmp(buffer, "query_component_type") == 0) { 
     345        SendComponentType(); 
     346    } 
     347    else if(strcmp(buffer, "reset_sml") == 0) { 
     348        Reset(); 
     349    } 
     350    else if(strcmp(buffer, "shutdown_sml") == 0) { 
     351        Shutdown(); 
     352    } 
     353    else if(strcmp(buffer, "register_engine_cognitive") == 0) { 
     354        RegisterCognitiveEngine(ID); 
     355    } 
     356    else if(strcmp(buffer, "register_service") == 0) { 
     357        ReceiveServices(ID); 
     358    } 
     359    else if(strcmp(buffer, "send_component_type") == 0) { 
     360        SendComponentType(); 
     361    } 
     362    else if(strcmp(buffer, "list_services") == 0) { 
     363        ListServices(); 
     364    } 
     365    else if(strcmp(buffer, "set_active_mission") == 0) { 
     366        SetActiveMission(); 
     367    } 
     368    else if(strcmp(buffer, "request_optimization") == 0) { 
     369        PerformActiveMission(); 
     370    } 
     371    else if(strcmp(buffer, "deregister_engine_cognitive") == 0) { 
     372        DeregisterCognitiveEngine(ID); 
     373    } 
     374    else if(strcmp(buffer, "deregister_service") == 0) { 
     375        DeregisterServices(ID); 
     376    } 
     377} 
     378 
     379//TODO Finish 
     380/* CALLED BY: MessageHandler 
     381 * INPUTS: <none> 
     382 * OUTPUTS: <none> 
     383 * 
     384 * DESCRIPTION: Deregisters the component from the Shell. 
     385 */  
    112386void  
    113387ServiceManagementLayer::Shutdown() 
     
    116390} 
    117391 
    118  
     392//TODO Finish 
     393/* CALLED BY: MessageHandler 
     394 * INPUTS: <none> 
     395 * OUTPUTS: <none> 
     396 * 
     397 * DESCRIPTION: Deregisters the component from the Shell 
     398 */ 
    119399void  
    120400ServiceManagementLayer::Reset() 
    121401{ 
    122402    DeregisterComponent(); 
    123     LoadConfiguration(); 
    124 } 
    125  
    126  
     403    ReloadConfiguration(); 
     404} 
     405 
     406/* CALLED BY: ConnectToShell 
     407 * INPUTS: <none> 
     408 * OUTPUTS: <none> 
     409 * 
     410 * DESCRIPTION: Sends the registration message to the Shell 
     411 */ 
    127412void  
    128413ServiceManagementLayer::RegisterComponent() 
     
    132417} 
    133418 
    134  
     419/* CALLED BY: Shutdown 
     420 * INPUTS: <none> 
     421 * OUTPUTS: <none> 
     422 * 
     423 * DESCRIPTION: Closes the client socket with the shell, sends a deregstration message 
     424 */ 
    135425void  
    136426ServiceManagementLayer::DeregisterComponent() 
     
    146436 
    147437 
     438/* CALLED BY: RegisterCognitiveEngine 
     439 * INPUTS: |ID| The ID number of the component where the data is to be transfered to 
     440 * OUTPUTS: <none> 
     441 * 
     442 * DESCRIPTION: Streams config data directly from the shell to the CE, and checks 
     443 * for an "ack" message from the CE after every sent message  
     444 * to know when to stop communication. 
     445 */ 
     446 
     447//Modified to check the incoming message buffer rather than the outgoing message buffer to avoid a portion of the delay 
    148448void  
    149 ServiceManagementLayer::TransferRadioConfiguration() 
    150 { 
    151 } 
    152  
    153  
     449ServiceManagementLayer::TransferRadioConfiguration(int32_t ID) 
     450{ 
     451    struct timeval selTimeout; 
     452    fd_set sockSet; 
     453    int32_t rc = 0; 
     454    char buffer[256]; 
     455    //Send data until the CE sends an ACK message back 
     456    while(rc!=0){ 
     457        memset(buffer, 0, 256); 
     458        //Receive data from Shell 
     459        ReadMessage(shellSocketFD, buffer); 
     460        //Send data to CE 
     461        SendMessage(CE_List[ID].FD, buffer); 
     462        FD_ZERO(&sockSet); 
     463        FD_SET(shellSocketFD, &sockSet); 
     464        selTimeout.tv_sec = 0; 
     465        selTimeout.tv_usec = 50000; 
     466        //Check if there is a message on the shell ready to be processed 
     467        rc=select(shellSocketFD + 1, &sockSet, NULL, NULL, &selTimeout); 
     468    } 
     469    memset(buffer, 0, 256); 
     470    ReadMessage(CE_List[ID].FD, buffer); 
     471    SendMessage(shellSocketFD, buffer); 
     472} 
     473 
     474 
     475/* CALLED BY: RegisterCognitiveEngine 
     476 * INPUTS: |ID| The ID number of the component where the data is to be transfered to 
     477 * OUTPUTS: <none> 
     478 * 
     479 * DESCRIPTION: Simmilar to TransferRadioConfig, just with Experience data 
     480 */ 
     481 
     482//Modified to check the incoming message buffer rather than the outgoing message buffer to avoid a portion of the delay 
    154483void  
    155 ServiceManagementLayer::TransferExperience() 
    156 { 
    157 } 
    158  
    159  
     484ServiceManagementLayer::TransferExperience(int32_t ID) 
     485{ 
     486    struct timeval selTimeout; 
     487    fd_set sockSet; 
     488    int32_t rc = 0; 
     489    char buffer[256]; 
     490    //Send data until the CE sends an ACK message back 
     491    while(rc!=0){ 
     492        memset(buffer, 0, 256); 
     493        //Receive data from Shell 
     494        ReadMessage(shellSocketFD, buffer); 
     495        //Send data to CE 
     496        SendMessage(CE_List[ID].FD, buffer); 
     497        FD_ZERO(&sockSet); 
     498        FD_SET(shellSocketFD, &sockSet); 
     499        selTimeout.tv_sec = 0; 
     500        selTimeout.tv_usec = 50000; 
     501        //Check if there is a message on the shell ready to be processed 
     502        rc=select(shellSocketFD + 1, &sockSet, NULL, NULL, &selTimeout); 
     503    } 
     504    memset(buffer, 0, 256); 
     505    ReadMessage(CE_List[ID].FD, buffer); 
     506    SendMessage(shellSocketFD, buffer); 
     507} 
     508 
     509/* CALLED BY: MessageHandler 
     510 * INPUTS: |ID| The ID number of the component where service is located 
     511 * OUTPUTS: <none> 
     512 * 
     513 * DESCRIPTION: Inserts a service into the DB with the ID of the component where it exists 
     514 */ 
    160515void  
    161 ServiceManagementLayer::ReceiveServices() 
    162 { 
    163 } 
    164  
    165  
     516ServiceManagementLayer::ReceiveServices(int32_t ID) 
     517{ 
     518    char buffer[256]; 
     519    memset(buffer, 0, 256); 
     520    ReadMessage(CE_List[ID].FD, buffer); 
     521    char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"}; 
     522     
     523    // generate command 
     524    strcpy(_services_DB->command, "insert into "); 
     525    strcat(_services_DB->command, _services_DB->tablename);  
     526    strcat(_services_DB->command, " ("); 
     527    strcat(_services_DB->command, cols[0]); 
     528    strcat(_services_DB->command, ", "); 
     529    strcat(_services_DB->command, cols[1]); 
     530    strcat(_services_DB->command, ") "); 
     531    strcat(_services_DB->command, " values("); 
     532    sprintf(_services_DB->command, "%s%d", _services_DB->command, ID); 
     533    strcat(_services_DB->command, ", '"); 
     534    strcat(_services_DB->command, buffer); 
     535    strcat(_services_DB->command, "');"); 
     536     
     537    //printf("search command: %s\n", _services_DB->command); 
     538    // execute add command 
     539    char *errorMsg; 
     540    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
     541    if( rc!=SQLITE_OK && rc!=101 ) 
     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); 
     546 
     547} 
     548 
     549/* CALLED BY: MessageHandler 
     550 * INPUTS: <none> 
     551 * OUTPUTS: <none> 
     552 * 
     553 * DESCRIPTION: This method associates the services that components provide with the services that are requested in the mission 
     554 * Each service in the mission is given the ID and FD of a component that has registered to provide that service  
     555 * Deregistration is okay until this method is called without a reload, but if deregistration occurs after this 
     556 * method is called it needs to be called again even if other engines also provide the services 
     557 */ 
    166558void  
    167559ServiceManagementLayer::SetActiveMission() 
    168560{ 
    169 } 
    170  
    171  
     561    char buffer[256]; 
     562    memset(buffer, 0, 256); 
     563    ReadMessage(shellSocketFD, buffer); 
     564    int32_t missID = atoi(buffer); 
     565    for(activeMission = 0; activeMission < 10; activeMission++) 
     566    { 
     567        //Find the active mission by comparing mission ID's 
     568        if(miss[activeMission].missionID == missID) 
     569            break; 
     570    } 
     571    //For each service in the mission 
     572    for(int i = 0; i < miss[activeMission].numServices; i++) 
     573    {    
     574        //Check whether the current service is an actual service or a conditional 
     575        if(miss[activeMission].services[i].name.compare("if") && miss[activeMission].services[i].name.compare("while")){ 
     576            //If it is a service, search the database of registered services to find the ID of the component that registered it 
     577            strcpy(_services_DB->command, "select "); 
     578            strcat(_services_DB->command, _services_DB->tablename); 
     579            strcat(_services_DB->command, ".* from "); 
     580            strcat(_services_DB->command, _services_DB->tablename); 
     581            strcat(_services_DB->command, " where Service_Name=="); 
     582            sprintf(_services_DB->command, "%s'%s';", _services_DB->command, miss[activeMission].services[i].name.c_str()); 
     583         
     584            sqlite3_stmt * pStatement; 
     585            int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command, -1, &pStatement, NULL); 
     586            if (rc == SQLITE_OK){  
     587                if (sqlite3_step(pStatement) == SQLITE_ROW) 
     588                     miss[activeMission].services[i].componentID =  sqlite3_column_int(pStatement, 0);  
     589                else { 
     590                    printf("services_DB:: Mission requires service not provided by any connected component.\n"); 
     591                    rc=31337; 
     592                } 
     593             } else { 
     594                printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_services_DB->command); 
     595            } 
     596 
     597            sqlite3_finalize(pStatement); 
     598            miss[activeMission].services[i].socketFD = CE_List[miss[activeMission].services[i].componentID].FD; 
     599            //Set the FD and ID of the service to refer to the component where the service exists 
     600        } 
     601        //Nothing to be done for conditionals at this stage 
     602    } 
     603 
     604 
     605    //printf("\nhere ---%d, %d---\n", miss[activeMission].services[0].componentID, miss[activeMission].services[1].componentID); 
     606} 
     607 
     608/* CALLED BY: PerformActiveMission 
     609 * INPUTS: |sourceID| ID of the service that is being processed 
     610 * OUTPUTS: <none> 
     611 * 
     612 * DESCRIPTION: This is a helper method for the "PerformActiveMission" function 
     613 * NOTE: This function has changed durrastically from the previous implementation 
     614 * Takes an ID of a service 
     615 * For that service, finds inputs in DB and forwords those on to the engine after sending comm-starting messages 
     616 * Afterwords, listenes for the outputs so that it can store those in the database for future services or the overall output 
     617 */ 
     618void 
     619ServiceManagementLayer::TransactData(int32_t sourceID) 
     620{ 
     621    printf("TransactData Occuring\n"); 
     622    char buffer[256]; 
     623    std::string data; 
     624    char* cols[] = {(char *)"Tag", (char *)"Data"}; 
     625    int i = 0; 
     626    fd_set sockSet; 
     627    struct timeval selTimeout; 
     628    //Transmission starting messages 
     629    SendMessage(miss[activeMission].services[sourceID].socketFD, "request_optimization_service"); 
     630    SendMessage(miss[activeMission].services[sourceID].socketFD, miss[activeMission].services[sourceID].name.c_str()); 
     631    //Find and load the input data 
     632    while(i < 3 && !miss[activeMission].services[sourceID].input[i].empty()){ 
     633        //printf("pulling input data out of DB for ID#=%d\n", sourceID); 
     634        strcpy(_data_DB->command, "select "); 
     635        strcat(_data_DB->command, _data_DB->tablename); 
     636        strcat(_data_DB->command, ".* from "); 
     637        strcat(_data_DB->command, _data_DB->tablename); 
     638        strcat(_data_DB->command, " where Tag=="); 
     639        sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].services[sourceID].input[i].c_str()); 
     640        sqlite3_stmt * pStatement; 
     641        int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
     642        if (rc == SQLITE_OK){  
     643            if (sqlite3_step(pStatement) == SQLITE_ROW) 
     644                 data.append((const char*) sqlite3_column_text(pStatement, 1));  
     645            else { 
     646                    printf("data_DB:: Data not yet in DB.\n"); 
     647                    rc=31337; 
     648            } 
     649        }  
     650        else { 
     651            printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command); 
     652        } 
     653 
     654        sqlite3_finalize(pStatement); 
     655        char *data_ch = (char *) data.c_str(); 
     656        //Tokenize the data and pass it along 
     657        char *token = strtok(data_ch, "@"); 
     658        while(token){ 
     659            SendMessage(miss[activeMission].services[sourceID].socketFD, token); 
     660            token = strtok(NULL, "@"); 
     661        } 
     662        //printf("done pulling input data out of DB for ID#=%d\n", sourceID); 
     663        i++; 
     664    } 
     665 
     666    int32_t j = 0; 
     667    FD_ZERO(&sockSet); 
     668    FD_SET(miss[activeMission].services[sourceID].socketFD, &sockSet); 
     669    //TODO neccessary? 
     670    selTimeout.tv_sec = 5; 
     671    selTimeout.tv_usec = 0; 
     672    //Use select command to force wait for processing to finish 
     673    select(miss[activeMission].services[sourceID].socketFD + 1, &sockSet, NULL, NULL, &selTimeout); 
     674    while(j < 3 && !miss[activeMission].services[sourceID].output[j].empty()){ 
     675        int rc; 
     676        data.clear(); 
     677        while(true){ 
     678            //Read the data incrementally and deliminate it with the "@" symbol 
     679            memset(buffer, 0, 256); 
     680            ReadMessage(miss[activeMission].services[sourceID].socketFD, buffer); 
     681            if(strcmp(buffer, "output_finished")==0) 
     682                break; 
     683            data.append(buffer); 
     684            data.append("@");; 
     685        } 
     686        printf("SML: putting output data into DB for ID#=%d\n", sourceID); 
     687 
     688        strcpy(_data_DB->command, "insert or replace into "); 
     689        strcat(_data_DB->command, _data_DB->tablename);  
     690        strcat(_data_DB->command, " ("); 
     691        strcat(_data_DB->command, cols[0]); 
     692        strcat(_data_DB->command, ", "); 
     693        strcat(_data_DB->command, cols[1]); 
     694        strcat(_data_DB->command, ") "); 
     695        strcat(_data_DB->command, " values('");  
     696        strcat(_data_DB->command, miss[activeMission].services[sourceID].output[j].c_str()); 
     697        strcat(_data_DB->command, "', '"); 
     698        strcat(_data_DB->command, data.c_str()); 
     699        strcat(_data_DB->command, "');"); 
     700        char *errorMsg; 
     701        rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg); 
     702        if( rc!=SQLITE_OK && rc!=101 ) 
     703            fprintf(stderr, "SQL error: %s\n", errorMsg); 
     704        printf("SML: done putting ouptut data into DB for ID#='%d', data=%s\n", sourceID, data.c_str()); 
     705        j++; 
     706    } 
     707    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"); 
     725} 
     726 
     727 
     728 
     729/* CALLED BY: MessageHandler 
     730 * INPUTS: <none> 
     731 * OUTPUTS: <none> 
     732 * 
     733 * DESCRIPTION: This function works by first sending the inputs from the shell to the appropriate components 
     734 * The first service should begin immeadiately, as should any others who have all of their input paramaters 
     735 * When they complete, the output path is found and the data is transfered as it becomes available 
     736 * Presumably at this point the second function has all of it's paramaters, so it begins to compute, and the cycle repeats 
     737 * If the generated output is an overall output, it is sent on to the shell 
     738 * "if" and "while" statements are handled by setting up a faux service that has a true input, a false input, and a boolean flag 
     739 * If the true input is non-NULL and the flag is true, the statements execute 
     740 * Likewise, if the false input is non-NULL and the flag is false, the statements execute 
     741 * These flags are set during execution any time one of these faux services' inputs appear in an output statement   
     742 *   
     743 *  
     744 * Rules for active missions (currently) 
     745 * -Three inputs/outputs per service and per mission 
     746 * -Inputs simply define a path, so multiple variables can be transmitted over the same input 
     747 * -Each component that sends data must include the "output_finished" statement after each output is finished sending it's data 
     748 * -All ordering constraints have been relaxed in this version; all data is stored locally and only sent when requested 
     749 * -If support fully implemented 
     750 * -Conditions must be boolean flags 
     751 * -Flags are set by putting either the character string "true" or "false" on the buffer 
     752 * -IMPORTANT: DB uses '@' to seperate individual statements; using '@' in the data stream will result in incorrect behavior 
     753 */ 
     754void 
     755ServiceManagementLayer::PerformActiveMission() 
     756{ 
     757    int i = 0; 
     758    std::string data; 
     759    std::string input; 
     760    std::string check; 
     761    char buffer[256]; 
     762    fd_set sockSet; 
     763    char* cols[] = {(char *)"Tag", (char *)"Data"}; 
     764    struct timeval selTimeout; 
     765    //Get the inputs 
     766    while(i < 3 && !miss[activeMission].input[i].empty()){ 
     767            //New data being added to DB 
     768        printf("inserting data from shell\n"); 
     769        int rc; 
     770        while(true){ 
     771            memset(buffer, 0, 256); 
     772            ReadMessage(shellSocketFD, buffer); 
     773            if(strcmp(buffer, "input_finished")==0) 
     774                break; 
     775            data.append(buffer); 
     776            data.append("@"); 
     777            /*FD_ZERO(&sockSet); 
     778            FD_SET(miss[activeMission].services[sourceID].socketFD, &sockSet); 
     779            selTimeout.tv_sec = 0; 
     780            selTimeout.tv_usec = 50000; 
     781            //Check if there is a message on the CE socket ready to be processed 
     782            rc=select(miss[activeMission].services[sourceID].socketFD + 1, &sockSet, NULL, NULL, &selTimeout);*/ 
     783        }//while(rc!=0);; 
     784 
     785        strcpy(_data_DB->command, "insert into "); 
     786        strcat(_data_DB->command, _data_DB->tablename);  
     787        strcat(_data_DB->command, " ("); 
     788        strcat(_data_DB->command, cols[0]); 
     789        strcat(_data_DB->command, ", "); 
     790        strcat(_data_DB->command, cols[1]); 
     791        strcat(_data_DB->command, ") "); 
     792        strcat(_data_DB->command, " values('"); 
     793        strcat(_data_DB->command, miss[activeMission].input[i].c_str()); 
     794        strcat(_data_DB->command, "', '"); 
     795        strcat(_data_DB->command, data.c_str()); 
     796        strcat(_data_DB->command, "');"); 
     797        char *errorMsg; 
     798        rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg); 
     799        if( rc!=SQLITE_OK && rc!=101 ) 
     800            fprintf(stderr, "SQL error: %s\n", errorMsg); 
     801        printf("SML: finished adding data from shell on input %d\n", i); 
     802        i++; 
     803 
     804    } 
     805    i=0; 
     806    data.clear(); 
     807    while(i < miss[activeMission].numServices) 
     808    { 
     809        if(miss[activeMission].services[i].name.compare("if")==0) 
     810        { 
     811            printf("if detected\n"); 
     812            input.clear(); 
     813            check.clear(); 
     814            if(!miss[activeMission].services[i].input[0].empty()){ 
     815                input=miss[activeMission].services[i].input[0]; 
     816                check = "true@"; 
     817            } 
     818            else{ 
     819                input=miss[activeMission].services[i].input[1]; 
     820                check = "false@"; 
     821            } 
     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); 
     828            strcat(_data_DB->command, " where Tag=="); 
     829            sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str()); 
     830            sqlite3_stmt * pStatement; 
     831            int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
     832            if (rc == SQLITE_OK){  
     833                if (sqlite3_step(pStatement) == SQLITE_ROW) 
     834                     data = (const char *) sqlite3_column_text(pStatement, 1);  
     835                else { 
     836                        printf("1 data_DB:: Data not yet in DB. %d\n", i); 
     837                        rc=31337; 
     838                } 
     839            } else { 
     840                printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command); 
     841            } 
     842 
     843            sqlite3_finalize(pStatement); 
     844            printf("data=%s, check=%s\n", data.c_str(), check.c_str()); 
     845            if(data.compare(check)==0){ 
     846                printf("if taken\n"); 
     847                for(int k = i + 1; k <= i + miss[activeMission].services[i].num_conds; k++) 
     848                    TransactData(k); 
     849            } 
     850            else{ 
     851                i+=miss[activeMission].services[i].num_conds; 
     852                printf("if not taken %d\n", miss[activeMission].services[i].num_conds); 
     853            } 
     854        } 
     855        else if(miss[activeMission].services[i].name.compare("while")==0) 
     856        { 
     857            printf("while detected\n"); 
     858            while(true){ 
     859                    data.clear(); 
     860                    input.clear(); 
     861                    check.clear(); 
     862                    if(!miss[activeMission].services[i].input[0].empty()){ 
     863                        input=miss[activeMission].services[i].input[0]; 
     864                        check = "true@"; 
     865                    } 
     866                    else{ 
     867                        input=miss[activeMission].services[i].input[1]; 
     868                        check = "false@"; 
     869                    } 
     870                    strcpy(_data_DB->command, "select "); 
     871                    strcat(_data_DB->command, _data_DB->tablename); 
     872                    strcat(_data_DB->command, ".* from "); 
     873                    strcat(_data_DB->command, _data_DB->tablename); 
     874                    strcat(_data_DB->command, " where Tag=="); 
     875                    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str()); 
     876                    sqlite3_stmt * pStatement; 
     877                    int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
     878                    if (rc == SQLITE_OK){  
     879                        if (sqlite3_step(pStatement) == SQLITE_ROW) 
     880                            data = (const char *) sqlite3_column_text(pStatement, 1);  
     881                        else  
     882                            printf("1 data_DB:: Data not yet in DB. %d\n", i); 
     883                    }  
     884                    else { 
     885                        printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command); 
     886                    } 
     887 
     888                    sqlite3_finalize(pStatement); 
     889                    printf("data=%s, check=%s\n", data.c_str(), check.c_str()); 
     890                    if(data.compare(check)==0){ 
     891                        printf("while taken\n"); 
     892                        for(int k = i + 1; k <= i + miss[activeMission].services[i].num_conds; k++) 
     893                            TransactData(k); 
     894                    } 
     895                    else{ 
     896                        i+=miss[activeMission].services[i].num_conds; 
     897                        printf("while not taken %d\n", miss[activeMission].services[i].num_conds); 
     898                        break; 
     899                    } 
     900            } 
     901        } 
     902        else{ 
     903            TransactData(i);} 
     904        i++; 
     905    } 
     906    i=0; 
     907    data.clear(); 
     908    //get the ouptuts 
     909    while(i < 3 && !miss[activeMission].output[i].empty()){ 
     910        printf("sending output data to shell\n"); 
     911        strcpy(_data_DB->command, "select "); 
     912        strcat(_data_DB->command, _data_DB->tablename); 
     913        strcat(_data_DB->command, ".* from "); 
     914        strcat(_data_DB->command, _data_DB->tablename); 
     915        strcat(_data_DB->command, " where Tag=="); 
     916        sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].output[i].c_str()); 
     917        sqlite3_stmt * pStatement; 
     918        int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL); 
     919        if (rc == SQLITE_OK){  
     920            if (sqlite3_step(pStatement) == SQLITE_ROW) 
     921                 data.append((const char*) sqlite3_column_text(pStatement, 1));  
     922            else { 
     923                    printf("data_DB:: Data not yet in DB.\n"); 
     924                    rc=31337; 
     925            } 
     926        }  
     927        else { 
     928            printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command); 
     929        } 
     930        sqlite3_finalize(pStatement); 
     931        char *data_ch = (char *) data.c_str(); 
     932        char *token = strtok(data_ch, "@"); 
     933        while(token){ 
     934            SendMessage(shellSocketFD, token); 
     935            token = strtok(NULL, "@"); 
     936        } 
     937        i++; 
     938        printf("done sending output data to shell\n"); 
     939    } 
     940} 
     941 
     942 
     943/* CALLED BY: MessageHandler 
     944 * INPUTS: <none> 
     945 * OUTPUTS: <none> 
     946 * 
     947 * DESCRIPTION: Print a list of the services currently registered and the ID's of the components that registered them 
     948 */ 
    172949void  
    173950ServiceManagementLayer::ListServices() 
    174951{ 
    175 } 
    176  
    177  
     952    // generate commandi 
     953    strcpy(_services_DB->command, "select "); 
     954    strcat(_services_DB->command, _services_DB->tablename); 
     955    strcat(_services_DB->command, ".* from "); 
     956    strcat(_services_DB->command, _services_DB->tablename); 
     957    strcat(_services_DB->command, ";"); 
     958 
     959    // execute print (select all)  command     
     960    char *errorMsg; 
     961    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
     962    if( rc!=SQLITE_OK && rc!=101 ) 
     963        fprintf(stderr, "SQL error: %s\n", errorMsg); 
     964    printf("database %s, table %s:\n", _services_DB->filename, _services_DB->tablename); 
     965} 
     966 
     967/* CALLED BY: Reset 
     968 * INPUTS: <none> 
     969 * OUTPUTS: <none> 
     970 * 
     971 * DESCRIPTION: Clear and reinitialize the mission array, then reload the configuration file 
     972 */ 
    178973void  
    179974ServiceManagementLayer::ReloadConfiguration() 
    180975{ 
    181976    LOG("ServiceManagementLayer:: Reloading Configuration.\n"); 
    182 } 
    183  
    184  
     977    free(miss); 
     978    miss = new Mission[10]; 
     979    for(int i = 0; i < 10; i++) 
     980        miss[i].services = new Service[20]; 
     981    LoadConfiguration(_SML_Config, miss); 
     982} 
     983 
     984/* CALLED BY: constructor 
     985 * INPUTS: |SML_Config| Address (either relitive or full) of the XML file containing mission data 
     986 *         |mList| Mission array to be modified  
     987 * OUTPUTS: <none> 
     988 * 
     989 * DESCRIPTION: IMPORTANT - See formatting instructions for correct parsing of data 
     990 * Can currently handle 3 inputs and 3 outputs per service, but easily expandable 
     991 * Also, can handle one layer of nested conditional statements, but could 
     992 * be expanded to meet additional needs.  Only support now is for straight bool flags, 
     993 * but support could be added for more complex conditionals later. 
     994 *  
     995 * Components assigned to mission during "set active mission" stage so that 
     996 * components can still continue to register after the configuration is loaded 
     997 */ 
    185998void  
    186 ServiceManagementLayer::LoadConfiguration() 
    187 { 
    188     LOG("ServiceManagementLayer:: Loading Configuration.\n"); 
    189 } 
    190  
     999ServiceManagementLayer::LoadConfiguration(const char *SML_Config, Mission* &mList) 
     1000{ 
     1001    TiXmlElement *pMission; 
     1002    TiXmlElement *pService; 
     1003    TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3; 
     1004    TiXmlHandle hRoot(0); 
     1005    //printf("ServiceManagementLayer:: Loading Configuration.\n"); 
     1006    TiXmlDocument doc("."); 
     1007    doc.LoadFile(SML_Config); 
     1008    bool loadOkay = doc.LoadFile(); 
     1009    if(!loadOkay) 
     1010        printf("Loading SML configuration failed: %s\n", SML_Config); 
     1011 
     1012    TiXmlHandle hDoc(&doc); 
     1013     
     1014    pMission = hDoc.FirstChildElement().Element(); 
     1015 
     1016    if(!pMission)  
     1017        printf("No valid root!"); 
     1018 
     1019    hRoot = TiXmlHandle(pMission); 
     1020    pService = pMission->FirstChildElement(); 
     1021    int32_t mission_num = 0; 
     1022    //Iterate through the missions 
     1023    for(pChild0 = pMission->FirstChildElement(); pChild0 ; \ 
     1024        pChild0 = pChild0->NextSiblingElement())  
     1025    { 
     1026        int32_t service_num = 0; 
     1027        uint16_t cond_array[] = {0, 0}; 
     1028        //printf("mission_num = %d\n", mission_num); 
     1029        memset(cond_array, 0, 2); 
     1030         
     1031        for(pChild1  = pChild0->FirstChildElement(); pChild1; \ 
     1032            pChild1  = pChild1->NextSiblingElement())  
     1033        { 
     1034            int32_t conditional_0 = service_num; 
     1035            for(pChild2 = pChild1->FirstChildElement(); \ 
     1036                pChild2; pChild2 = pChild2->NextSiblingElement()) 
     1037            { 
     1038                service_num++; 
     1039                int32_t conditional_1 = service_num; 
     1040                for(pChild3 = pChild2->FirstChildElement(); \ 
     1041                    pChild3; pChild3 = pChild3 ->NextSiblingElement()) 
     1042                { 
     1043                    service_num++; 
     1044                    mList[mission_num].services[service_num].name = pChild3->Attribute("name"); 
     1045                    if(pChild3->Attribute("input1")) 
     1046                        mList[mission_num].services[service_num].input[0] = pChild3->Attribute("input1"); 
     1047                    if(pChild3->Attribute("input2")) 
     1048                        mList[mission_num].services[service_num].input[1] = pChild3->Attribute("input2"); 
     1049                    if(pChild3->Attribute("input3")) 
     1050                        mList[mission_num].services[service_num].input[2] = pChild3->Attribute("input3"); 
     1051                    if(pChild3->Attribute("output1")) 
     1052                        mList[mission_num].services[service_num].output[0] = pChild3->Attribute("output1"); 
     1053                    if(pChild3->Attribute("output2")) 
     1054                        mList[mission_num].services[service_num].output[1] = pChild3->Attribute("output2"); 
     1055                    if(pChild3->Attribute("output3")) 
     1056                        mList[mission_num].services[service_num].output[2] = pChild3->Attribute("output3"); 
     1057                    cond_array[1]++; 
     1058                } 
     1059 
     1060                if(conditional_1 != service_num){ 
     1061                    mList[mission_num].services[conditional_1].name = pChild2->Value(); 
     1062                    if(pChild2->Attribute("input_t")) 
     1063                        mList[mission_num].services[conditional_1].input[0] = pChild2->Attribute("input_t"); 
     1064                    if(pChild2->Attribute("input_f")) 
     1065                        mList[mission_num].services[conditional_1].input[1] = pChild2->Attribute("input_f"); 
     1066                } 
     1067                else{ 
     1068                    mList[mission_num].services[conditional_1].name = pChild2->Attribute("name"); 
     1069                    if(pChild2->Attribute("input1")) 
     1070                        mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input1"); 
     1071                    if(pChild2->Attribute("input2")) 
     1072                        mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input2"); 
     1073                    if(pChild2->Attribute("input3")) 
     1074                        mList[mission_num].services[service_num].input[2] = pChild2->Attribute("input3"); 
     1075                    if(pChild2->Attribute("output1")) 
     1076                        mList[mission_num].services[service_num].output[0] = pChild2->Attribute("output1"); 
     1077                    if(pChild2->Attribute("output2")) 
     1078                        mList[mission_num].services[service_num].output[1] = pChild2->Attribute("output2"); 
     1079                    if(pChild2->Attribute("output3")) 
     1080                        mList[mission_num].services[service_num].output[2] = pChild2->Attribute("output3"); 
     1081                } 
     1082 
     1083                mList[mission_num].services[conditional_1].num_conds = cond_array[1]; 
     1084                cond_array[1] = 0; 
     1085                cond_array[0]++; 
     1086            } 
     1087            if(conditional_0 != service_num){ 
     1088                mList[mission_num].services[conditional_0].name = pChild1->Value(); 
     1089                    if(pChild1->Attribute("input_t")) 
     1090                        mList[mission_num].services[conditional_0].input[0] = pChild1->Attribute("input_t"); 
     1091                    if(pChild1->Attribute("input_f")) 
     1092                        mList[mission_num].services[conditional_0].input[1] = pChild1->Attribute("input_f"); 
     1093                //printf("---input_t=%s\n", mList[mission_num].services[conditional_0].input[0].c_str()); 
     1094            } 
     1095            else{ 
     1096                mList[mission_num].services[conditional_0].name = pChild1->Attribute("name"); 
     1097                if(pChild1->Attribute("input1")) 
     1098                    mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input1"); 
     1099                if(pChild1->Attribute("input2")) 
     1100                    mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input2"); 
     1101                if(pChild1->Attribute("input3")) 
     1102                    mList[mission_num].services[service_num].input[2] = pChild1->Attribute("input3"); 
     1103                if(pChild1->Attribute("output1")) 
     1104                    mList[mission_num].services[service_num].output[0] = pChild1->Attribute("output1"); 
     1105                if(pChild1->Attribute("output2")) 
     1106                    mList[mission_num].services[service_num].output[1] = pChild1->Attribute("output2"); 
     1107                if(pChild1->Attribute("output3")) 
     1108                    mList[mission_num].services[service_num].output[2] = pChild1->Attribute("output3"); 
     1109            } 
     1110 
     1111            mList[mission_num].services[conditional_0].num_conds = cond_array[0]; 
     1112            cond_array[0] = 0; 
     1113            service_num++; 
     1114        } 
     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()); 
     1117 
     1118        mList[mission_num].numServices = service_num; 
     1119        mList[mission_num].name = pChild0->Attribute("name"); 
     1120        mList[mission_num].missionID = atoi(pChild0->Attribute("id")); 
     1121        if(pChild0->Attribute("input1")) 
     1122            mList[mission_num].input[0] = pChild0->Attribute("input1"); 
     1123        if(pChild0->Attribute("input2")) 
     1124            mList[mission_num].input[1] = pChild0->Attribute("input2"); 
     1125        if(pChild0->Attribute("input3")) 
     1126            mList[mission_num].input[2] = pChild0->Attribute("input3"); 
     1127        if(pChild0->Attribute("output1")) 
     1128            mList[mission_num].output[0] = pChild0->Attribute("output1"); 
     1129        if(pChild0->Attribute("output2")) 
     1130            mList[mission_num].output[1] = pChild0->Attribute("output2"); 
     1131        if(pChild0->Attribute("output3")) 
     1132            mList[mission_num].output[2] = pChild0->Attribute("output3"); 
     1133        //printf("mis, input1=%s, output1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str()); 
     1134        mission_num++; 
     1135    } 
     1136} 
     1137 
     1138/* CALLED BY: MessageHandler 
     1139 * INPUTS: |ID| The ID number of the engine to be registered 
     1140 * OUTPUTS: <none> 
     1141 * 
     1142 * DESCRIPTION: Sends a registration message onto the shell and sends the ACK back to the component 
     1143 */ 
     1144void 
     1145ServiceManagementLayer::RegisterCognitiveEngine(int32_t ID) 
     1146{ 
     1147    SendMessage(shellSocketFD, "register_engine_cognitive"); 
     1148    LOG("ServiceManagementLayer:: CE registration message forwarded to shell.\n"); 
     1149    char buffer[256]; 
     1150    memset(buffer, 0, 256); 
     1151    ReadMessage(shellSocketFD, buffer); 
     1152    SendMessage(CE_List[ID].FD, buffer); 
     1153 
     1154    //TODO UNCOMMENT!!! 
     1155    //TODO UNCOMMENT!!! 
     1156    /*TransferRadioConfiguration(ID); 
     1157    memset(buffer, 0, 256); 
     1158    TransferExperience(ID); 
     1159    memset(buffer, 0, 256);*/ 
     1160    numberOfCognitiveEngines++; 
     1161    CE_Present = true; 
     1162} 
     1163 
     1164/* CALLED BY: MessageHandler 
     1165 * INPUTS: |ID| The ID number of the engine to have it's services deregistered 
     1166 * OUTPUTS: <none> 
     1167 * 
     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 */ 
     1173void  
     1174ServiceManagementLayer::DeregisterServices(int32_t ID) 
     1175{ 
     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 
     1220    strcpy(_services_DB->command, "DELETE FROM "); 
     1221    strcat(_services_DB->command, _services_DB->tablename); 
     1222    strcat(_services_DB->command, " WHERE "); 
     1223    strcat(_services_DB->command, "ID_Num"); 
     1224    strcat(_services_DB->command, "=="); 
     1225    sprintf(_services_DB->command, "%s%d;", _services_DB->command, ID); 
     1226    char *errorMsg; 
     1227    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
     1228    if( rc!=SQLITE_OK && rc!=101 ) 
     1229        fprintf(stderr, "SQL error: %s\n", errorMsg); 
     1230 
     1231 
     1232    CE_List[ID].FD = -1; 
     1233    CE_List[ID].ID_num = -1; 
     1234 
     1235    LOG("Cognitive Radio Shell:: CE Socket closed for engine #%d.\n", ID); 
     1236} 
     1237 
     1238 
     1239/* CALLED BY: test class 
     1240 * INPUTS: <none> 
     1241 * OUTPUTS: <none> 
     1242 * 
     1243 * DESCRIPTION: Sets up a server socket and listens for communication on either that or the shell socket 
     1244 */ 
     1245void 
     1246ServiceManagementLayer::StartSMLServer() 
     1247{ 
     1248    struct timeval selTimeout; 
     1249    int32_t running = 1; 
     1250    int32_t port, rc, new_sd = 1; 
     1251    int32_t desc_ready = 1; 
     1252    int32_t timeout = 10; 
     1253                //If there is, call the MessageHandler with the Shell_Msg code of -1 
     1254    fd_set sockSet, shellSet; 
     1255 
     1256    cogEngSrv = CreateTCPServerSocket(2044); 
     1257    int32_t maxDescriptor = cogEngSrv; 
     1258 
     1259    if(InitializeTCPServerPort(cogEngSrv) == -1) 
     1260        ERROR(1,"Error initializing primary port\n"); 
     1261 
     1262    int i = 10000000;  //TODO change to "running" if endpoint can be reached 
     1263    while (i>0) { 
     1264        i--; 
     1265        /* Zero socket descriptor vector and set for server sockets */ 
     1266        /* This must be reset every time select() is called */ 
     1267        FD_ZERO(&sockSet); 
     1268        FD_SET(cogEngSrv, &sockSet); 
     1269        for(int k = 0; k < Current_ID; k++){ 
     1270            if(CE_List[k].ID_num != -1) 
     1271                FD_SET(CE_List[k].FD, &sockSet); 
     1272        } 
     1273            //printf("k=%d, CID=%d\n", k, CE_List[k].FD); 
     1274 
     1275        /* Timeout specification */ 
     1276        /* This must be reset every time select() is called */ 
     1277        selTimeout.tv_sec = 0;       /* timeout (secs.) */ 
     1278        selTimeout.tv_usec = 0;            /* 0 microseconds */ 
     1279        //Changed both to zero so that select will check messages from the shell instead of blocking 
     1280        //when there is no command from the CE's to be processed 
     1281 
     1282        //Check if there is a message on the socket waiting to be read 
     1283        rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout);  
     1284        //printf("rc=%d\n", rc); 
     1285        if(rc == 0){ 
     1286            //LOG("No echo requests for %i secs...Server still alive\n", timeout); 
     1287         
     1288            FD_ZERO(&shellSet); 
     1289            FD_SET(shellSocketFD, &shellSet); 
     1290            selTimeout.tv_sec = 0; 
     1291            selTimeout.tv_usec = 0; 
     1292            //Check if there is a message on the shell socket ready to be processed 
     1293            int rc2 = select(shellSocketFD + 1, &shellSet, NULL, NULL, &selTimeout); 
     1294            //printf("rc2=%d\n", rc2); 
     1295                //If there is, call the MessageHandler with the Shell_Msg code of -1 
     1296            if(FD_ISSET(shellSocketFD, &shellSet)){ 
     1297                //printf("shell_msg, %d\n", rc2); 
     1298                MessageHandler(-1);} 
     1299        } 
     1300        else {  
     1301            desc_ready = rc; 
     1302            for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) { 
     1303                if(FD_ISSET(port, &sockSet)) { 
     1304                    desc_ready -= 1; 
     1305 
     1306                    //Check if request is new or on an existing open descriptor 
     1307                    if(port == cogEngSrv) {  
     1308                        //If new, assign it a descriptor and give it an ID 
     1309                        new_sd = AcceptTCPConnection(port); 
     1310                          
     1311                        if(new_sd < 0)  
     1312                            break; 
     1313 
     1314                        CE_List[Current_ID].FD = new_sd; 
     1315                        CE_List[Current_ID].ID_num = Current_ID; 
     1316                        MessageHandler(Current_ID); 
     1317                        Current_ID++; 
     1318         
     1319                        FD_SET(new_sd,&sockSet); 
     1320                        if(new_sd > maxDescriptor)  
     1321                           maxDescriptor = new_sd; 
     1322                    }  
     1323                    else { 
     1324                        //If old, figure out which ID it coresponds to and handle it accordingly 
     1325                        for(int16_t z = 0; z < Current_ID; z++) 
     1326                        { 
     1327                                if(CE_List[z].FD == port){ 
     1328                                        MessageHandler(z);} 
     1329                        } 
     1330                    } 
     1331                } 
     1332            } 
     1333        } 
     1334    }         
     1335 
     1336    /* Close sockets */ 
     1337    close(cogEngSrv); 
     1338 
     1339    //delete &cogEngSrv; 
     1340    return; 
     1341} 
     1342 
     1343 
     1344 
     1345 
     1346    /*TiXmlElement *pMission; 
     1347    TiXmlElement *pService; 
     1348    TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3; 
     1349    TiXmlHandle hRoot(0); 
     1350    //printf("ServiceManagementLayer:: Loading Configuration.\n"); 
     1351    TiXmlDocument doc("."); 
     1352    doc.LoadFile(SML_Config); 
     1353    bool loadOkay = doc.LoadFile(); 
     1354    if(!loadOkay) 
     1355        printf("Loading SML configuration failed: %s\n", SML_Config); 
     1356 
     1357    TiXmlHandle hDoc(&doc); 
     1358     
     1359    pMission = hDoc.FirstChildElement().Element(); 
     1360 
     1361    if(!pMission)  
     1362        printf("No valid root!"); 
     1363 
     1364    hRoot = TiXmlHandle(pMission); 
     1365    pService = pMission->FirstChildElement(); 
     1366    int32_t mission_num = 0; 
     1367    //Iterate through the missions 
     1368    for(pChild0 = pMission->FirstChildElement(); pChild0 ; \ 
     1369        pChild0 = pChild0->NextSiblingElement())  
     1370    { 
     1371        int32_t service_num = 0; 
     1372        uint16_t cond_array[] = {0, 0}; 
     1373        //printf("mission_num = %d\n", mission_num); 
     1374        memset(cond_array, 0, 2); 
     1375         
     1376        for(pChild1  = pChild0->FirstChildElement(); pChild1; \ 
     1377            pChild1  = pChild1->NextSiblingElement())  
     1378        { 
     1379            int32_t conditional_0 = service_num; 
     1380            for(pChild2 = pChild1->FirstChildElement(); \ 
     1381                pChild2; pChild2 = pChild2->NextSiblingElement()) 
     1382            { 
     1383                service_num++; 
     1384                int32_t conditional_1 = service_num; 
     1385                for(pChild3 = pChild2->FirstChildElement(); \ 
     1386                    pChild3; pChild3 = pChild3 ->NextSiblingElement()) 
     1387                { 
     1388                    service_num++; 
     1389                    mList[mission_num].services[service_num].name = pChild3->Attribute("name"); 
     1390                    if(pChild3->Attribute("input1")) 
     1391                        mList[mission_num].services[service_num].input[0] = pChild3->Attribute("input1"); 
     1392                    if(pChild3->Attribute("input2")) 
     1393                        mList[mission_num].services[service_num].input[1] = pChild3->Attribute("input2"); 
     1394                    if(pChild3->Attribute("input3")) 
     1395                        mList[mission_num].services[service_num].input[2] = pChild3->Attribute("input3"); 
     1396                    if(pChild3->Attribute("output1")) 
     1397                        mList[mission_num].services[service_num].output[0] = pChild3->Attribute("output1"); 
     1398                    if(pChild3->Attribute("output2")) 
     1399                        mList[mission_num].services[service_num].output[1] = pChild3->Attribute("output2"); 
     1400                    if(pChild3->Attribute("output3")) 
     1401                        mList[mission_num].services[service_num].output[2] = pChild3->Attribute("output3"); 
     1402                    cond_array[1]++; 
     1403                } 
     1404 
     1405                if(conditional_1 != service_num){ 
     1406                    mList[mission_num].services[conditional_1].name = pChild2->Value(); 
     1407                    if(pChild2->Attribute("input_t")) 
     1408                        mList[mission_num].services[conditional_1].input[0] = pChild2->Attribute("input_t"); 
     1409                    if(pChild2->Attribute("input_f")) 
     1410                        mList[mission_num].services[conditional_1].input[1] = pChild2->Attribute("input_f"); 
     1411                } 
     1412                else{ 
     1413                    mList[mission_num].services[service_num].name = pChild2->Attribute("name"); 
     1414                    if(pChild2->Attribute("input1")) 
     1415                        mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input1"); 
     1416                    if(pChild2->Attribute("input2")) 
     1417                        mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input2"); 
     1418                    if(pChild2->Attribute("input3")) 
     1419                        mList[mission_num].services[service_num].input[2] = pChild2->Attribute("input3"); 
     1420                    if(pChild2->Attribute("output1")) 
     1421                        mList[mission_num].services[service_num].output[0] = pChild2->Attribute("output1"); 
     1422                    if(pChild2->Attribute("output2")) 
     1423                        mList[mission_num].services[service_num].output[1] = pChild2->Attribute("output2"); 
     1424                    if(pChild2->Attribute("output3")) 
     1425                        mList[mission_num].services[service_num].output[2] = pChild2->Attribute("output3"); 
     1426                } 
     1427 
     1428                mList[mission_num].services[conditional_1].num_conds = cond_array[1]; 
     1429                cond_array[1] = 0; 
     1430                cond_array[0]++; 
     1431            } 
     1432            if(conditional_0 != service_num){ 
     1433                mList[mission_num].services[conditional_0].name = pChild1->Value(); 
     1434                if(pChild1->Attribute("input_t")) 
     1435                    mList[mission_num].services[conditional_0].input[0] = pChild1->Attribute("input_t"); 
     1436                if(pChild1->Attribute("input_f")) 
     1437                    mList[mission_num].services[conditional_0].input[1] = pChild1->Attribute("input_f"); 
     1438                printf("if detected %s, %s\n", pChild1->Attribute("input_f"), pChild1->Attribute("input_t")); 
     1439            } 
     1440            else{ 
     1441                mList[mission_num].services[service_num].name = pChild1->Attribute("name"); 
     1442                if(pChild1->Attribute("input1")) 
     1443                    mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input1"); 
     1444                if(pChild1->Attribute("input2")) 
     1445                    mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input2"); 
     1446                if(pChild1->Attribute("input3")) 
     1447                    mList[mission_num].services[service_num].input[2] = pChild1->Attribute("input3"); 
     1448                if(pChild1->Attribute("output1")) 
     1449                    mList[mission_num].services[service_num].output[0] = pChild1->Attribute("output1"); 
     1450                if(pChild1->Attribute("output2")) 
     1451                    mList[mission_num].services[service_num].output[1] = pChild1->Attribute("output2"); 
     1452                if(pChild1->Attribute("output3")) 
     1453                    mList[mission_num].services[service_num].output[2] = pChild1->Attribute("output3"); 
     1454            } 
     1455 
     1456            mList[mission_num].services[conditional_0].num_conds = cond_array[0]; 
     1457            //printf("hello\n"); 
     1458             
     1459            cond_array[0] = 0; 
     1460            service_num++; 
     1461        } 
     1462        for(int i = 0; i < service_num; i++) 
     1463                printf("input1=%s, output1=%s\n", mList[mission_num].services[i].input[0].c_str(), mList[mission_num].services[service_num].output[0].c_str()); 
     1464 
     1465        mList[mission_num].numServices = service_num; 
     1466        mList[mission_num].name = pChild0->Attribute("name"); 
     1467        mList[mission_num].missionID = atoi(pChild0->Attribute("id")); 
     1468        if(pChild0->Attribute("input1")) 
     1469            mList[mission_num].input[0] = pChild0->Attribute("input1"); 
     1470        if(pChild0->Attribute("input2")) 
     1471            mList[mission_num].input[1] = pChild0->Attribute("input2"); 
     1472        if(pChild0->Attribute("input3")) 
     1473            mList[mission_num].input[2] = pChild0->Attribute("input3"); 
     1474        if(pChild0->Attribute("output1")) 
     1475            mList[mission_num].output[0] = pChild0->Attribute("output1"); 
     1476        if(pChild0->Attribute("output2")) 
     1477            mList[mission_num].output[1] = pChild0->Attribute("output2"); 
     1478        if(pChild0->Attribute("output3")) 
     1479            mList[mission_num].output[2] = pChild0->Attribute("output3"); 
     1480        printf("input1=%s, outpu1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str()); 
     1481        mission_num++; 
     1482    }*/ 
  • vtcross/trunk/src/service_management_layer/sml_demo.cpp

    r200 r278  
    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