Changeset 267

Show
Ignore:
Timestamp:
05/29/09 17:14:28 (15 years ago)
Author:
wrodgers
Message:

Added full service and mission support. Still needs debugging to work correctly, but it's close.

Location:
vtcross/branches/wrodgers
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • vtcross/branches/wrodgers/ServiceManagementLayer.cpp

    r255 r267  
    55 */ 
    66 
    7 /* When a CE connects, the address of it's local socket is stored and it is assigned an ID number. 
    8  * Until it sends a message ending communication, any communications from it are processed with 
    9  * that ID number.  The "sendto" command uses the address data to send messages directly back to 
    10  * it's local socket, allowing us to get away with a single-socket implementation on the SML-side. 
     7/* Inter-component communication handled by sockets and FD's.   
     8 * Server support has been completely implemented and tested. 
    119 * 
    12  * Services are stored in a SQLite DB by the ID of the CE that registered them. 
     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. 
    1312 * 
    14  * Mission support has not been implemented yet, but will be soon. 
     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.  Mission support has been completely implemented but mostly untested 
     16 * (except for the XML parsing, which has been fully tested). 
     17 * 
     18 * It is important that in the XML configuration file, inputs and outputs are listed 
     19 * in the order that they appear.  See the documentation for the "PerformActiveMission" 
     20 * method for more information. 
     21 * 
     22 *  
    1523 */ 
    1624 
    17 /* KNOWN ISSUE: Note that this implementation would be unstable for CE's on any sort of network 
    18  * since it relies on asyncronous communication occuring linearly and with one partner at a time. 
    19  * The breakdown occurs specifically when the CE has to send ack messages back to the SML; 
    20  * since there may be other messages queued on the socket since communication started, 
    21  * there is no guarentee that the message would be the first up and I have yet to discover a method 
    22  * for peaking more than 1 item into the message queue without starting to pop messages off first. 
    23  * Still a work in progress though... 
    24  */ 
    25  
    26 #include <cstdlib> 
     25#include <stdlib.h> 
     26#include <string.h> 
     27#include <stdio.h> 
    2728#include <cstring> 
    2829#include <stdint.h> 
    2930 
    30 #include "../../trunk/src/include/vtcross/common.h" 
     31/*#include "../../trunk/src/include/vtcross/common.h" 
     32 
    3133#include "../../trunk/src/include/vtcross/components.h" 
    3234#include "../../trunk/src/include/vtcross/containers.h" 
    3335#include "../../trunk/src/include/vtcross/debug.h" 
    3436#include "../../trunk/src/include/vtcross/error.h" 
    35 #include "../../trunk/src/include/vtcross/socketcomm.h" 
     37#include "../../trunk/src/include/vtcross/socketcomm.h"*/ 
     38 
     39#include "vtcross/common.h" 
     40 
     41#include "vtcross/components.h" 
     42#include "vtcross/containers.h" 
     43#include "vtcross/debug.h" 
     44#include "vtcross/error.h" 
     45#include "vtcross/socketcomm.h" 
    3646 
    3747 
     
    4050#include <stdint.h> 
    4151#include <math.h> 
    42  
    4352 
    4453#include <arpa/inet.h> 
     
    5362#include <sys/wait.h> 
    5463 
    55 #include "../../trunk/src/include/tinyxml/tinyxml.h" 
    56 #include "../../trunk/src/include/tinyxml/tinystr.h" 
     64/*#include "../../trunk/src/include/tinyxml/tinyxml.h" 
     65#include "../../trunk/src/include/tinyxml/tinystr.h"*/ 
     66 
     67#include "tinyxml/tinyxml.h" 
     68#include "tinyxml/tinystr.h" 
     69 
    5770#include "sqlite3.h" 
     71//#include "sqlite3ext.h" 
    5872 
    5973typedef struct services_s * services_DB; 
     
    6882 
    6983services_DB _services_DB; 
    70  
     84const char *_SML_Config; 
     85 
     86int callback(void *notUsed, int argc, char **argv, char **azColName){ 
     87    int i; 
     88    for(i=0; i<argc; i++){ 
     89        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); 
     90    } 
     91    printf("\n"); 
     92    return 0; 
     93} 
    7194 
    7295 
     
    79102    CE_Present = false; 
    80103    cogEngSrv = 1; 
     104} 
     105 
     106 
     107ServiceManagementLayer::~ServiceManagementLayer() 
     108{ 
     109    char *errorMsg; 
     110    strcpy(_services_DB->command, "drop table "); 
     111    strcat(_services_DB->command, _services_DB->tablename); 
     112    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
     113    if( rc!=SQLITE_OK && rc!=101 ) 
     114        fprintf(stderr, "SQL error: %s\n", errorMsg); 
     115    strcpy(_services_DB->command, "vacuum"); 
     116    rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
     117    if( rc!=SQLITE_OK && rc!=101 ) 
     118        fprintf(stderr, "SQL error: %s\n", errorMsg); 
     119    free(_services_DB); 
     120} 
     121 
     122 
     123ServiceManagementLayer::ServiceManagementLayer(const char* SML_Config, \ 
     124        const char* serverName, const char* serverPort) 
     125{ 
     126    LOG("Creating Service Management Layer.\n"); 
     127    _SML_Config = SML_Config; 
     128 
     129    ConnectToShell(serverName, serverPort); 
     130    CE_List = (CE_Reg *) malloc(10*sizeof(struct CE_Reg)); 
     131    CE_List = new CE_Reg[10]; 
     132 
     133    miss = new Mission[10]; 
     134    for(int i = 0; i < 10; i++) 
     135        miss[i].services = new Service[20]; 
    81136 
    82137    Current_ID = 0; 
    83     LoadConfiguration(); 
    84 } 
    85  
    86  
    87 ServiceManagementLayer::~ServiceManagementLayer() 
    88 { 
    89 } 
    90  
    91  
    92 ServiceManagementLayer::ServiceManagementLayer(const char* serverName, \ 
    93         const char* serverPort) 
    94 { 
    95     LOG("Creating Service Management Layer.\n"); 
    96  
    97     ConnectToShell(serverName, serverPort); 
    98     CE_List = new CE_Reg[10]; 
    99  
    100     LoadConfiguration(); 
     138 
     139    LoadConfiguration(SML_Config, miss); 
    101140    CreateServicesDB(); 
    102141} 
    103142 
     143//VERIFIED May 26 
    104144void 
    105 CreateServicesDB() 
     145ServiceManagementLayer::CreateServicesDB() 
    106146{ 
    107147    _services_DB = (services_DB) malloc(sizeof(struct services_s)); 
     148    char *errorMsg; 
    108149 
    109150    // create database 
     
    117158    //_services_DB->db = NULL; 
    118159    sqlite3_open(_services_DB->filename, &(_services_DB->db)); 
    119     char* cols[] = {"ID_Num", "Service_Name"}; 
     160    char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"}; 
    120161 
    121162    // create table 
     
    131172    strcat(_services_DB->command, _services_DB->tablename); 
    132173    strcat(_services_DB->command, "("); 
     174    //strcat(_services_DB->command, "SRVID INTEGER PRIMARY KEY, "); 
    133175    strcat(_services_DB->command, cols[0]); 
    134176    strcat(_services_DB->command, " INT, "); 
     
    138180 
    139181    // execute create table command 
    140     sqlite3_exec(_services_DB->db, _services_DB->command, NULL, 0, NULL); 
     182  sqlite3_stmt *ppStmt;  /* OUT: Statement handle */ 
     183  const char *pzTail;     /* OUT: Pointer to unused portion of zSql */ 
     184 
     185    int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command, 128, &ppStmt, &pzTail); 
     186    if( rc!=SQLITE_OK && rc!=101 ) 
     187        printf("SQL error(1): %d\n", rc); 
     188    rc = sqlite3_step(ppStmt); 
     189    if( rc!=SQLITE_OK && rc!=101 ) 
     190        printf("SQL error(2): %d\n", rc); 
    141191} 
    142192 
     
    155205{ 
    156206    shellSocketFD = ClientSocket(serverName, serverPort); 
     207     
     208    //Set the shell socket to non-block mode 
     209    /*int oldflags = fcntl (shellSocketFD, F_GETFL, 0); 
     210    oldflags |= O_NONBLOCK; 
     211    fcntl (shellSocketFD, F_SETFL, oldflags);*/ 
    157212 
    158213    RegisterComponent(); 
    159214} 
    160215 
    161  
    162 void  
    163 ServiceManagementLayer::ShellSignalHandler() 
    164 { 
    165     char buffer[256]; 
    166  
    167     memset(buffer, 0, 256);  
    168     ReadMessage(shellSocketFD, buffer); 
    169  
     216void 
     217ServiceManagementLayer::MessageHandler(int32_t ID) 
     218{ 
     219    char buffer[256];    
     220    memset(buffer, 0, 256);   
     221    int32_t _FD;  
     222    //printf("ID= %d\n", ID);  
     223     
     224    if(ID != -1) 
     225        _FD = CE_List[ID].FD; 
     226    else  
     227        _FD = shellSocketFD; 
     228 
     229    //printf("FD=%d, shellFD=%d", _FD, shellSocketFD); 
     230    ssize_t msgLength = recv(_FD, buffer, 256, MSG_PEEK); 
     231    if(msgLength == 0){ 
     232        //printf("Error reading from socket.\n"); 
     233        //sleep(5);      
     234        return; 
     235    } 
     236 
     237 
     238    size_t i; 
     239    for(i = 0; i < 256; i++) { 
     240        if(strcmp(&buffer[i], "\0") == 0) 
     241            break; 
     242    } 
     243    // Read the message into msgBuffer 
     244    recv(_FD, buffer, i + 1, 0); 
     245    //printf("********* %s **********\n", buffer); 
    170246    // TODO 
    171247    // If we send integer op codes rather than strings, this process will be 
    172248    // MUCH faster since instead of donig string compares we can simply 
    173249    // switch on the integer value... 
    174     if(strcmp(buffer, "register_service") == 0) { 
     250    /*if(strcmp(buffer, "register_service") == 0) { 
    175251        if(strcmp(buffer, "policy_geo") == 0) { 
    176252        } 
     
    191267        else if(strcmp(buffer, "policy_spacial") == 0) { 
    192268        } 
    193     } 
    194     else if(strcmp(buffer, "query_component_type") == 0) { 
     269    }*/ 
     270    if(strcmp(buffer, "query_component_type") == 0) { 
    195271        SendComponentType(); 
    196272    } 
     
    201277        Shutdown(); 
    202278    } 
    203 } 
    204  
    205 void 
    206 ServiceManagementLayer::CESignalHandler(int32_t ID) 
    207 { 
    208     char buffer[256];    
    209     memset(buffer, 0, 256);      
    210     // 
    211     ReadMessage(cogEngSrv, buffer); 
    212     if(strcmp(buffer, "register_engine_cognitive") == 0) { 
    213          RegisterCognitiveEngine(ID); 
     279    else if(strcmp(buffer, "register_engine_cognitive") == 0) { 
     280        RegisterCognitiveEngine(ID); 
    214281    } 
    215282    else if(strcmp(buffer, "register_service") == 0) { 
    216          ReceiveServices(ID); 
    217     } 
    218  
    219  
     283        ReceiveServices(ID); 
     284    } 
     285    else if(strcmp(buffer, "send_component_type") == 0) { 
     286        SendComponentType(); 
     287    } 
     288    else if(strcmp(buffer, "list_services") == 0) { 
     289        ListServices(); 
     290    } 
     291    else if(strcmp(buffer, "set_active_mission") == 0) { 
     292        SetActiveMission(); 
     293    } 
     294    else if(strcmp(buffer, "request_optimization") == 0) { 
     295        PerformActiveMission(); 
     296    } 
    220297} 
    221298 
     
    233310{ 
    234311    DeregisterComponent(); 
    235     LoadConfiguration(); 
     312    LoadConfiguration(_SML_Config, miss); 
    236313} 
    237314 
     
    269346    char buffer[256]; 
    270347    //Send data until the CE sends an ACK message back 
    271     while(rc == 0){ 
     348    while(rc==0){ 
    272349        memset(buffer, 0, 256); 
    273350        //Receive data from Shell 
    274351        ReadMessage(shellSocketFD, buffer); 
    275352        //Send data to CE 
    276         sendto(cogEngSrv, buffer, 256, NULL, (struct sockaddr*) CE_List[ID].sock_ptr, CE_List[ID].sock_len);  
    277         FD_ZERO(&sockSet); 
    278         FD_SET(cogEngSrv, &sockSet); 
    279         selTimeout.tv_sec = 0; 
    280         selTimeout.tv_usec = 0; 
    281         //Check if there is a message on the CE socket ready to be processed 
    282         rc = select(cogEngSrv + 1, &sockSet, NULL, NULL, &selTimeout); 
     353        SendMessage(CE_List[ID].FD, buffer); 
     354        FD_ZERO(&sockSet); 
     355        FD_SET(CE_List[ID].FD, &sockSet); 
     356        selTimeout.tv_sec = 0; 
     357        selTimeout.tv_usec = 0; 
     358        //Check if there is a message on the CE socket ready to be processed 
     359        rc=select(CE_List[ID].FD + 1, &sockSet, NULL, NULL, &selTimeout); 
    283360    } 
    284361    memset(buffer, 0, 256); 
    285     //Once there is a message on the CE socket, read it and forward it on to the shell 
    286     ReadMessage(cogEngSrv, buffer); 
     362    ReadMessage(CE_List[ID].FD, buffer); 
    287363    SendMessage(shellSocketFD, buffer); 
    288364} 
     
    297373    char buffer[256]; 
    298374    //Send data until the CE sends an ACK message back 
    299     while(rc == 0){ 
     375    while(rc==0){ 
    300376        memset(buffer, 0, 256); 
    301377        //Receive data from Shell 
    302378        ReadMessage(shellSocketFD, buffer); 
    303379        //Send data to CE 
    304         sendto(cogEngSrv, buffer, 256, NULL, (struct sockaddr*) CE_List[ID].sock_ptr, CE_List[ID].sock_len);  
    305         FD_ZERO(&sockSet); 
    306         FD_SET(cogEngSrv, &sockSet); 
    307         selTimeout.tv_sec = 0; 
    308         selTimeout.tv_usec = 0; 
    309         //Check if there is a message on the CE socket ready to be processed 
    310         rc = select(cogEngSrv + 1, &sockSet, NULL, NULL, &selTimeout); 
     380        SendMessage(CE_List[ID].FD, buffer); 
     381        FD_ZERO(&sockSet); 
     382        FD_SET(CE_List[ID].FD, &sockSet); 
     383        selTimeout.tv_sec = 0; 
     384        selTimeout.tv_usec = 0; 
     385        //Check if there is a message on the CE socket ready to be processed 
     386        rc = select(CE_List[ID].FD + 1, &sockSet, NULL, NULL, &selTimeout); 
    311387    } 
    312388    memset(buffer, 0, 256); 
    313     //Once there is a message on the CE socket, read it and forward it on to the shell 
    314     ReadMessage(cogEngSrv, buffer); 
     389    ReadMessage(CE_List[ID].FD, buffer); 
    315390    SendMessage(shellSocketFD, buffer); 
    316391} 
    317392 
    318  
     393//VERIFIED May 26 
    319394void  
    320395ServiceManagementLayer::ReceiveServices(int32_t ID) 
     
    322397    char buffer[256]; 
    323398    memset(buffer, 0, 256); 
    324     ReadMessage(cogEngSrv, buffer); 
    325     char* cols[] = {"ID_Num", "Service_Name"}; 
     399    ReadMessage(CE_List[ID].FD, buffer); 
     400    char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"}; 
    326401     
    327402    // generate command 
     
    336411    strcat(_services_DB->command, " values("); 
    337412    sprintf(_services_DB->command, "%s%d", _services_DB->command, ID); 
    338     strcat(_services_DB->command, ", "); 
     413    strcat(_services_DB->command, ", '"); 
    339414    strcat(_services_DB->command, buffer); 
    340     strcat(_services_DB->command, ");"); 
     415    strcat(_services_DB->command, "');"); 
    341416     
    342417    //printf("search command: %s\n", _services_DB->command); 
    343418    // execute add command 
    344     sqlite3_exec(_services_DB->db, _services_DB->command, NULL, 0, NULL); 
    345  
    346 } 
    347  
    348  
     419    char *errorMsg; 
     420    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
     421    if( rc!=SQLITE_OK && rc!=101 ) 
     422        fprintf(stderr, "SQL error: %s\n", errorMsg); 
     423    printf("SML: Registering service '%s' from component number '%d'\n", buffer, ID); 
     424 
     425} 
     426 
     427//This method associates the services that components provide with the services that are requested in the mission 
     428//Each service in the mission is given the ID and FD of a component that has registered to provide that service  
     429//Deregistration is okay until this method is called without a reload, but if deregistration occurs after this 
     430// method is called it needs to be called again even if other engines also provide the services 
     431 
     432//Verified May 29 
    349433void  
    350434ServiceManagementLayer::SetActiveMission() 
    351435{ 
    352 } 
    353  
    354  
     436    char buffer[256]; 
     437    memset(buffer, 0, 256); 
     438    ReadMessage(shellSocketFD, buffer); 
     439    int32_t missID = atoi(buffer); 
     440    for(activeMission = 0; activeMission < 10; activeMission++) 
     441    { 
     442        if(miss[activeMission].missionID == missID) 
     443            break; 
     444    } 
     445    for(int i = 0; i < miss[activeMission].numServices; i++) 
     446    {    
     447        if(miss[activeMission].services[i].name.compare("if") && miss[activeMission].services[i].name.compare("while")){ 
     448        strcpy(_services_DB->command, "select "); 
     449        strcat(_services_DB->command, _services_DB->tablename); 
     450        strcat(_services_DB->command, ".* from "); 
     451        strcat(_services_DB->command, _services_DB->tablename); 
     452        strcat(_services_DB->command, " where Service_Name=="); 
     453        sprintf(_services_DB->command, "%s'%s';", _services_DB->command, miss[activeMission].services[i].name.c_str()); 
     454         
     455 
     456        sqlite3_stmt * pStatement; 
     457        int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command, -1, &pStatement, NULL); 
     458        if (rc == SQLITE_OK){  
     459            if (sqlite3_step(pStatement) == SQLITE_ROW) 
     460                 miss[activeMission].services[i].componentID =  sqlite3_column_int(pStatement, 0);  
     461            else { 
     462                    printf("services_DB:: Mission requires service not provided by any connected component.\n"); 
     463                    rc=31337; 
     464            } 
     465        } else { 
     466            printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_services_DB->command); 
     467        } 
     468 
     469        sqlite3_finalize(pStatement); 
     470        miss[activeMission].services[i].socketFD = CE_List[miss[activeMission].services[i].componentID].FD;} 
     471    } 
     472    printf("\nhere ---%d, %d---\n", miss[activeMission].services[0].componentID, miss[activeMission].services[2].componentID); 
     473} 
     474 
     475//This is a helper method for the "PerformActiveMission" function 
     476//It takes the ID's of a source and a destination and does a raw transfer between them 
     477//It knows when to stop when an "ack" signal returns from the destination component 
     478//An ID of "-1" coresponds to the shell 
     479void 
     480ServiceManagementLayer::TransactData(int32_t sourceID, int32_t destID) 
     481{ 
     482    printf("transact occuring between %d and %d\n", sourceID, destID); 
     483    int32_t FD_src, FD_dest; 
     484    struct timeval selTimeout; 
     485    if(sourceID==-1) 
     486        FD_src=shellSocketFD; 
     487    else 
     488        FD_src=CE_List[sourceID].FD; 
     489    if(destID==-1) 
     490        FD_dest=shellSocketFD; 
     491    else 
     492        FD_dest=CE_List[destID].FD; 
     493 
     494    int32_t rc = 0; 
     495    fd_set sockSet; 
     496    char buffer[256]; 
     497    //Send data until the CE sends an ACK message back 
     498    while(rc==0){ 
     499        memset(buffer, 0, 256); 
     500        //Receive data from Shell 
     501        printf("reading data from %d\n", FD_src); 
     502        ReadMessage(FD_src, buffer); 
     503        //Send data to CE 
     504        printf("sending data to %d\n", FD_dest); 
     505        SendMessage(FD_dest, buffer); 
     506        FD_ZERO(&sockSet); 
     507        FD_SET(FD_dest, &sockSet); 
     508        selTimeout.tv_sec = 0; 
     509        selTimeout.tv_usec = 50; 
     510        //Check if there is a message on the CE socket ready to be processed 
     511        rc=select(FD_dest + 1, &sockSet, NULL, NULL, &selTimeout); 
     512    } 
     513    memset(buffer, 0, 256); 
     514    ReadMessage(FD_dest, buffer); 
     515    printf("done transact data %s\n", buffer); 
     516} 
     517 
     518// Rules for active missions (currently) 
     519// -Three inputs/outputs per service and per mission 
     520// -Inputs simply define a path, so multiple variables can be transmitted over the same input 
     521// -Each component that takes input is responsible for sending an "ack" signal when transmission is complete 
     522//   -Transmission will continue until the "ack" signal is recieved 
     523// -Any input from shell to a CE must be the first input 
     524// -CE-CE inputs must be listed in the order the ouptuts are generated 
     525// -No ability to store data locally at this time, so all CE's must be ready to receive data as soon as it is sent 
     526// -Shell should be ready for output in order that it is generated 
     527// -If/While supported and nesting too (except nested while loops) 
     528// -Conditions must be boolean flags 
     529// -Flags are set by putting either the character string "true" or "false" on the buffer 
     530 
     531// This function works by first sending the inputs from the shell to the appropriate components 
     532// The first service should begin immeadiately, as should any others who have all of their input paramaters 
     533// When they complete, the output path is found and the data is transfered as it becomes available 
     534// Presumably at this point the second function has all of it's paramaters, so it begins to compute, and the cycle repeats 
     535// If the generated output is an overall output, it is sent on to the shell 
     536// "if" and "while" statements are handled by setting up a faux service that has a true input, a false input, and a boolean flag 
     537// If the true input is non-NULL and the flag is true, the statements execute 
     538// Likewise, if the false input is non-NULL and the flag is false, the statements execute 
     539// These flags are set during execution any time one of these faux services' inputs appear in an output statement   
     540//  
     541void 
     542ServiceManagementLayer::PerformActiveMission() 
     543{ 
     544    int i = 0; 
     545    char *buffer; 
     546    if(!miss[activeMission].input[0].empty()){ 
     547        printf("mission input 1 not empty\n"); 
     548        for(int j = 0; j < miss[activeMission].numServices; j++) 
     549        { 
     550            if(miss[activeMission].services[j].input[0].compare(miss[activeMission].input[0]) == 0){ 
     551                if(miss[activeMission].services[j].num_conds > 0){ 
     552                    memset(buffer, 0, 256); 
     553                    ReadMessage(shellSocketFD, buffer); 
     554                    if(strcmp(buffer, "false")==0) 
     555                        miss[activeMission].services[j].cond_flag=false; 
     556                    else 
     557                        miss[activeMission].services[j].cond_flag=true; 
     558                    break; 
     559                } 
     560                SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); 
     561                SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); 
     562                TransactData(-1, miss[activeMission].services[j].componentID); 
     563            }    
     564        } 
     565    } 
     566    if(!miss[activeMission].input[1].empty()){ 
     567        for(int j = 0; j < miss[activeMission].numServices; j++) 
     568        { 
     569            if(miss[activeMission].services[j].input[1].compare(miss[activeMission].input[0]) == 0){ 
     570                if(miss[activeMission].services[j].num_conds > 0){ 
     571                    memset(buffer, 0, 256); 
     572                    ReadMessage(shellSocketFD, buffer); 
     573                    if(strcmp(buffer, "false")==0) 
     574                        miss[activeMission].services[j].cond_flag=false; 
     575                    else 
     576                        miss[activeMission].services[j].cond_flag=true; 
     577                    break; 
     578                } 
     579                SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); 
     580                SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); 
     581                TransactData(-1, miss[activeMission].services[j].componentID); 
     582            }    
     583            if(miss[activeMission].services[j].input[1].compare(miss[activeMission].input[1]) == 0) 
     584                TransactData(-1, miss[activeMission].services[j].componentID);   
     585        } 
     586    } 
     587    if(!miss[activeMission].input[2].empty()){ 
     588        for(int j = 0; j < miss[activeMission].numServices; j++) 
     589        { 
     590            if(miss[activeMission].services[j].input[2].compare(miss[activeMission].input[0]) == 0){ 
     591                if(miss[activeMission].services[j].num_conds > 0){ 
     592                    memset(buffer, 0, 256); 
     593                    ReadMessage(shellSocketFD, buffer); 
     594                    if(strcmp(buffer, "false")==0) 
     595                        miss[activeMission].services[j].cond_flag=false; 
     596                    else 
     597                        miss[activeMission].services[j].cond_flag=true; 
     598                    break; 
     599                } 
     600                SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); 
     601                SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); 
     602                TransactData(-1, miss[activeMission].services[j].componentID); 
     603            }    
     604            if(miss[activeMission].services[j].input[2].compare(miss[activeMission].input[1]) == 0) 
     605                TransactData(-1, miss[activeMission].services[j].componentID); 
     606            if(miss[activeMission].services[j].input[2].compare(miss[activeMission].input[2]) == 0) 
     607                TransactData(-1, miss[activeMission].services[j].componentID);   
     608        } 
     609    } 
     610    while(i != miss[activeMission].numServices) 
     611    {    
     612        if(miss[activeMission].services[i].name.compare("while")==0) 
     613        { 
     614            int32_t stmt_i = i; 
     615            i+=miss[activeMission].services[stmt_i].num_conds; 
     616            while((miss[activeMission].services[stmt_i].cond_flag && !miss[activeMission].services[stmt_i].input[0].empty()) ||\ 
     617                  (!miss[activeMission].services[stmt_i].cond_flag && !miss[activeMission].services[stmt_i].input[1].empty())){ 
     618                for(int32_t k=stmt_i+1; k <= stmt_i+miss[activeMission].services[stmt_i].num_conds; k++){ 
     619                    if(strcmp(miss[activeMission].services[i].name.c_str(), "if")==0){ 
     620                        int32_t stmt_k = k; 
     621                        if((miss[activeMission].services[stmt_k].cond_flag && !miss[activeMission].services[stmt_k].input[0].empty()) ||\ 
     622                           (!miss[activeMission].services[stmt_k].cond_flag && !miss[activeMission].services[stmt_k].input[1].empty())){ 
     623                            for(int n = stmt_k+1; n <= stmt_k+miss[activeMission].services[stmt_k].num_conds; n++){ 
     624                                for(int m = 0; m < 3; m++){ 
     625                                    for(int j = 0; j < 3; j++){ 
     626                                        if(miss[activeMission].services[k].output[m].compare(miss[activeMission].output[j])==0 && \ 
     627                                           !miss[activeMission].services[k].output[m].empty()){ 
     628                                            TransactData(miss[activeMission].services[k].componentID, -1); 
     629                                            break; 
     630                                        } 
     631                                    } 
     632                                    for(int j = 0; j < miss[activeMission].numServices; j++){ 
     633                                        if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[0]) == 0 && \ 
     634                                           !miss[activeMission].services[k].output[m].empty()){ 
     635                                            if(miss[activeMission].services[j].num_conds > 0){ 
     636                                                memset(buffer, 0, 256); 
     637                                                ReadMessage(miss[activeMission].services[k].socketFD, buffer); 
     638                                                if(strcmp(buffer, "false")==0) 
     639                                                    miss[activeMission].services[j].cond_flag=false; 
     640                                                else 
     641                                                    miss[activeMission].services[j].cond_flag=true; 
     642                                                break; 
     643                                            } 
     644                                            SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); 
     645                                            SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); 
     646                                            TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); 
     647                                            break;       
     648                                        }        
     649                                        if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[1]) == 0 && \ 
     650                                           !miss[activeMission].services[k].output[m].empty()){ 
     651                                            if(miss[activeMission].services[j].num_conds > 0){ 
     652                                                memset(buffer, 0, 256); 
     653                                                ReadMessage(miss[activeMission].services[k].socketFD, buffer); 
     654                                                if(strcmp(buffer, "false")==0) 
     655                                                    miss[activeMission].services[j].cond_flag=false; 
     656                                                else 
     657                                                    miss[activeMission].services[j].cond_flag=true; 
     658                                                break; 
     659                                            } 
     660                                            TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); 
     661                                            break;       
     662                                        }        
     663                                        if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[2]) == 0){ 
     664                                            TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); 
     665                                            break;       
     666                                        } 
     667                                    }    
     668                                } 
     669                            } 
     670                        } 
     671                    } 
     672                    else{ 
     673                        for(int m = 0; m < 3; m++) 
     674                        { 
     675                            for(int j = 0; j < 3; j++){ 
     676                                if(miss[activeMission].services[k].output[m].compare(miss[activeMission].output[j])==0 && \ 
     677                                   !miss[activeMission].services[k].output[m].empty()){ 
     678                                    TransactData(miss[activeMission].services[k].componentID, -1); 
     679                                    break; 
     680                                } 
     681                            } 
     682                            for(int j = 0; j < miss[activeMission].numServices; j++){ 
     683                                if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[0]) == 0 && \ 
     684                                   !miss[activeMission].services[k].output[m].empty()){ 
     685                                    if(miss[activeMission].services[j].num_conds > 0){ 
     686                                                memset(buffer, 0, 256); 
     687                                        ReadMessage(miss[activeMission].services[k].socketFD, buffer); 
     688                                        if(strcmp(buffer, "false")==0) 
     689                                            miss[activeMission].services[j].cond_flag=false; 
     690                                        else 
     691                                            miss[activeMission].services[j].cond_flag=true; 
     692                                        break; 
     693                                    } 
     694                                    SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); 
     695                                    SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); 
     696                                    TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); 
     697                                    break;       
     698                                }        
     699                                if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[1]) == 0 && \ 
     700                                   !miss[activeMission].services[k].output[m].empty()){ 
     701                                    if(miss[activeMission].services[j].num_conds > 0){ 
     702                                                memset(buffer, 0, 256); 
     703                                        ReadMessage(miss[activeMission].services[k].socketFD, buffer); 
     704                                        if(strcmp(buffer, "false")==0) 
     705                                            miss[activeMission].services[j].cond_flag=false; 
     706                                        else 
     707                                            miss[activeMission].services[j].cond_flag=true; 
     708                                        break; 
     709                                    } 
     710                                    TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); 
     711                                    break;       
     712                                }        
     713                                if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[2]) == 0 && \ 
     714                                   !miss[activeMission].services[k].output[m].empty()){ 
     715                                    TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); 
     716                                    break;       
     717                                } 
     718                            }    
     719                        } 
     720                    } 
     721                } 
     722            } 
     723        } 
     724        else if(miss[activeMission].services[i].name.compare("if")==0) 
     725        { 
     726            int32_t stmt_i = i; 
     727            i+=miss[activeMission].services[stmt_i].num_conds; 
     728            if((miss[activeMission].services[stmt_i].cond_flag && !miss[activeMission].services[stmt_i].input[0].empty()) ||\ 
     729                  (!miss[activeMission].services[stmt_i].cond_flag && !miss[activeMission].services[stmt_i].input[1].empty())){ 
     730                for(int32_t k=stmt_i+1; k <= stmt_i+miss[activeMission].services[stmt_i].num_conds; k++){ 
     731                    if(strcmp(miss[activeMission].services[i].name.c_str(), "if")==0){ 
     732                        int32_t stmt_k = k; 
     733                        if((miss[activeMission].services[stmt_k].cond_flag && !miss[activeMission].services[stmt_k].input[0].empty()) ||\ 
     734                           (!miss[activeMission].services[stmt_k].cond_flag && !miss[activeMission].services[stmt_k].input[1].empty())){ 
     735                            for(int n = stmt_k+1; n <= stmt_k+miss[activeMission].services[stmt_k].num_conds; n++){ 
     736                                for(int m = 0; m < 3; m++) 
     737                                { 
     738                                    for(int j = 0; j < 3; j++){ 
     739                                        if(miss[activeMission].services[k].output[m].compare(miss[activeMission].output[j])==0 && \ 
     740                                           !miss[activeMission].services[k].output[m].empty()){ 
     741                                            TransactData(miss[activeMission].services[k].componentID, -1); 
     742                                            break; 
     743                                        } 
     744                                    } 
     745                                    for(int j = 0; j < miss[activeMission].numServices; j++){ 
     746                                        if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[0]) == 0 && \ 
     747                                           !miss[activeMission].services[k].output[m].empty()){ 
     748                                            if(miss[activeMission].services[j].num_conds > 0){ 
     749                                                memset(buffer, 0, 256); 
     750                                                ReadMessage(miss[activeMission].services[k].socketFD, buffer); 
     751                                                if(strcmp(buffer, "false")==0) 
     752                                                    miss[activeMission].services[j].cond_flag=false; 
     753                                                else 
     754                                                    miss[activeMission].services[j].cond_flag=true; 
     755                                                break; 
     756                                            } 
     757                                            SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); 
     758                                            SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); 
     759                                            TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); 
     760                                            break;       
     761                                        }        
     762                                        if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[1]) == 0 && \ 
     763                                           !miss[activeMission].services[k].output[m].empty()){ 
     764                                            if(miss[activeMission].services[j].num_conds > 0){ 
     765                                                memset(buffer, 0, 256); 
     766                                                ReadMessage(miss[activeMission].services[k].socketFD, buffer); 
     767                                                if(strcmp(buffer, "false")==0) 
     768                                                    miss[activeMission].services[j].cond_flag=false; 
     769                                                else 
     770                                                    miss[activeMission].services[j].cond_flag=true; 
     771                                                break; 
     772                                            } 
     773                                            TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); 
     774                                            break;       
     775                                        }        
     776                                        if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[2]) == 0 && \ 
     777                                           !miss[activeMission].services[k].output[m].empty()){ 
     778                                            TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); 
     779                                            break;       
     780                                        } 
     781                                    }    
     782                                } 
     783                            } 
     784                        } 
     785                    } 
     786                    else{ 
     787                        for(int m = 0; m < 3; m++) 
     788                        { 
     789                            for(int j = 0; j < 3; j++){ 
     790                                if(miss[activeMission].services[k].output[m].compare(miss[activeMission].output[j])==0 && \ 
     791                                   !miss[activeMission].services[k].output[m].empty()){ 
     792                                    TransactData(miss[activeMission].services[k].componentID, -1); 
     793                                    break; 
     794                                } 
     795                            } 
     796                            for(int j = 0; j < miss[activeMission].numServices; j++){ 
     797                                if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[0]) == 0 && \ 
     798                                   !miss[activeMission].services[k].output[m].empty()){ 
     799                                    if(miss[activeMission].services[j].num_conds > 0){ 
     800                                        ReadMessage(miss[activeMission].services[k].socketFD, buffer); 
     801                                        if(strcmp(buffer, "false")==0) 
     802                                            miss[activeMission].services[j].cond_flag=false; 
     803                                        else 
     804                                            miss[activeMission].services[j].cond_flag=true; 
     805                                        break; 
     806                                    } 
     807                                    SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); 
     808                                    SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); 
     809                                    TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); 
     810                                    break;       
     811                                }        
     812                                if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[1]) == 0 && \ 
     813                                   !miss[activeMission].services[k].output[m].empty()){ 
     814                                    if(miss[activeMission].services[j].num_conds > 0){ 
     815                                        ReadMessage(miss[activeMission].services[k].socketFD, buffer); 
     816                                        if(strcmp(buffer, "false")==0) 
     817                                            miss[activeMission].services[j].cond_flag=false; 
     818                                        else 
     819                                            miss[activeMission].services[j].cond_flag=true; 
     820                                        break; 
     821                                    } 
     822                                    TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); 
     823                                    break;       
     824                                }        
     825                                if(miss[activeMission].services[k].output[m].compare(miss[activeMission].services[j].input[2]) == 0 && \ 
     826                                   !miss[activeMission].services[k].output[m].empty()){ 
     827                                    TransactData(miss[activeMission].services[k].componentID, miss[activeMission].services[j].componentID); 
     828                                    break;       
     829                                } 
     830                            }    
     831                        } 
     832                    } 
     833                } 
     834            } 
     835        } 
     836        else{ 
     837            for(int m = 0; m < 3; m++) 
     838            { 
     839                printf("\ninside SAM\n"); 
     840                for(int j = 0; j < 3; j++){ 
     841                    if(miss[activeMission].services[i].output[m].compare(miss[activeMission].output[j])==0 && \ 
     842                       !miss[activeMission].services[i].output[m].empty()){ 
     843                        printf("i=%d, m=%d, j=%d\n", i, m, j); 
     844                        TransactData(miss[activeMission].services[i].componentID, -1); 
     845                        break; 
     846                    } 
     847                } 
     848                for(int j = 0; j < miss[activeMission].numServices; j++){ 
     849                    if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[0]) == 0 && \ 
     850                       !miss[activeMission].services[i].output[m].empty()){ 
     851                        if(miss[activeMission].services[j].num_conds > 0){ 
     852                            ReadMessage(miss[activeMission].services[i].socketFD, buffer); 
     853                            if(strcmp(buffer, "false")==0) 
     854                                miss[activeMission].services[j].cond_flag=false; 
     855                            else 
     856                                miss[activeMission].services[j].cond_flag=true; 
     857                            break; 
     858                        } 
     859                        SendMessage(miss[activeMission].services[j].socketFD, "request_optimization_service"); 
     860                        SendMessage(miss[activeMission].services[j].socketFD, miss[activeMission].services[j].name.c_str()); 
     861                        TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID); 
     862                        break;   
     863                    }    
     864                    if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[1]) == 0 && \ 
     865                       !miss[activeMission].services[i].output[m].empty()){ 
     866                        if(miss[activeMission].services[j].num_conds > 0){ 
     867                            ReadMessage(miss[activeMission].services[i].socketFD, buffer); 
     868                            if(strcmp(buffer, "false")==0) 
     869                                miss[activeMission].services[j].cond_flag=false; 
     870                            else 
     871                                miss[activeMission].services[j].cond_flag=true; 
     872                            break; 
     873                        } 
     874                        TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID); 
     875                        break;   
     876                    }    
     877                    if(miss[activeMission].services[i].output[m].compare(miss[activeMission].services[j].input[2]) == 0 && \ 
     878                       !miss[activeMission].services[i].output[m].empty()){ 
     879                        TransactData(miss[activeMission].services[i].componentID, miss[activeMission].services[j].componentID); 
     880                        break;   
     881                    } 
     882                }        
     883            } 
     884        } 
     885        i++; 
     886    } 
     887} 
     888 
     889 
     890//Verified May 26 
    355891void  
    356892ServiceManagementLayer::ListServices() 
     
    363899    strcat(_services_DB->command, ";"); 
    364900 
    365     // execute print (select all)  command 
    366     sqlite3_exec(_services_DB->db, _services_DB->command, NULL, 0, NULL); 
     901    // execute print (select all)  command     
     902    char *errorMsg; 
     903    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg); 
     904    if( rc!=SQLITE_OK && rc!=101 ) 
     905        fprintf(stderr, "SQL error: %s\n", errorMsg); 
    367906    printf("database %s, table %s:\n", _services_DB->filename, _services_DB->tablename); 
    368907} 
     
    373912{ 
    374913    LOG("ServiceManagementLayer:: Reloading Configuration.\n"); 
    375 } 
    376  
    377  
    378 void  
    379 ServiceManagementLayer::LoadConfiguration() 
    380 { 
    381     LOG("ServiceManagementLayer:: Loading Configuration.\n"); 
    382 } 
    383  
     914    free(miss); 
     915    miss = new Mission[10]; 
     916    for(int i = 0; i < 10; i++) 
     917        miss[i].services = new Service[20]; 
     918    LoadConfiguration(_SML_Config, miss); 
     919} 
     920 
     921//IMPORTANT - See formatting instructions for correct parsing of data 
     922//Can currently handle 3 inputs and 3 outputs per service, but easily expandable 
     923//Also, can handle one layer of nested conditional statements, but could 
     924//be expanded to meet additional needs.  Only support now is for straight bool flags, 
     925//but support could be added for more complex conditionals later. 
     926 
     927//Components assigned to mission during "set active mission" stage so that 
     928//components can still continue to register after the configuration is loaded 
     929 
     930//Verified May 26 
     931void  
     932ServiceManagementLayer::LoadConfiguration(const char *SML_Config, Mission* &mList) 
     933{ 
     934    TiXmlElement *pMission; 
     935    TiXmlElement *pService; 
     936    TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3; 
     937    TiXmlHandle hRoot(0); 
     938    //printf("ServiceManagementLayer:: Loading Configuration.\n"); 
     939    TiXmlDocument doc("."); 
     940    doc.LoadFile(SML_Config); 
     941    bool loadOkay = doc.LoadFile(); 
     942    if(!loadOkay) 
     943        printf("Loading SML configuration failed: %s\n", SML_Config); 
     944 
     945    TiXmlHandle hDoc(&doc); 
     946     
     947    pMission = hDoc.FirstChildElement().Element(); 
     948 
     949    if(!pMission)  
     950        printf("No valid root!"); 
     951 
     952    hRoot = TiXmlHandle(pMission); 
     953    pService = pMission->FirstChildElement(); 
     954    int32_t mission_num = 0; 
     955    //Iterate through the missions 
     956    for(pChild0 = pMission->FirstChildElement(); pChild0 ; \ 
     957        pChild0 = pChild0->NextSiblingElement())  
     958    { 
     959        int32_t service_num = 0; 
     960        uint16_t cond_array[] = {0, 0}; 
     961        //printf("mission_num = %d\n", mission_num); 
     962        memset(cond_array, 0, 2); 
     963         
     964        for(pChild1  = pChild0->FirstChildElement(); pChild1; \ 
     965            pChild1  = pChild1->NextSiblingElement())  
     966        { 
     967            int32_t conditional_0 = service_num; 
     968            for(pChild2 = pChild1->FirstChildElement(); \ 
     969                pChild2; pChild2 = pChild2->NextSiblingElement()) 
     970            { 
     971                service_num++; 
     972                int32_t conditional_1 = service_num; 
     973                for(pChild3 = pChild2->FirstChildElement(); \ 
     974                    pChild3; pChild3 = pChild3 ->NextSiblingElement()) 
     975                { 
     976                    service_num++; 
     977                    mList[mission_num].services[service_num].name = pChild3->Attribute("name"); 
     978                    if(pChild3->Attribute("input1")) 
     979                        mList[mission_num].services[service_num].input[0] = pChild3->Attribute("input1"); 
     980                    if(pChild3->Attribute("input2")) 
     981                        mList[mission_num].services[service_num].input[1] = pChild3->Attribute("input2"); 
     982                    if(pChild3->Attribute("input3")) 
     983                        mList[mission_num].services[service_num].input[2] = pChild3->Attribute("input3"); 
     984                    if(pChild3->Attribute("output1")) 
     985                        mList[mission_num].services[service_num].output[0] = pChild3->Attribute("output1"); 
     986                    if(pChild3->Attribute("output2")) 
     987                        mList[mission_num].services[service_num].output[1] = pChild3->Attribute("output2"); 
     988                    if(pChild3->Attribute("output3")) 
     989                        mList[mission_num].services[service_num].output[2] = pChild3->Attribute("output3"); 
     990                    cond_array[1]++; 
     991                } 
     992 
     993                if(conditional_1 != service_num){ 
     994                    mList[mission_num].services[conditional_1].name = pChild2->Value(); 
     995                    if(pChild2->Attribute("input_t")) 
     996                        mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input_t"); 
     997                    if(pChild2->Attribute("input_f")) 
     998                        mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input_f"); 
     999                } 
     1000                else{ 
     1001                    mList[mission_num].services[conditional_1].name = pChild2->Attribute("name"); 
     1002                    if(pChild2->Attribute("input1")) 
     1003                        mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input1"); 
     1004                    if(pChild2->Attribute("input2")) 
     1005                        mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input2"); 
     1006                    if(pChild2->Attribute("input3")) 
     1007                        mList[mission_num].services[service_num].input[2] = pChild2->Attribute("input3"); 
     1008                    if(pChild2->Attribute("output1")) 
     1009                        mList[mission_num].services[service_num].output[0] = pChild2->Attribute("output1"); 
     1010                    if(pChild2->Attribute("output2")) 
     1011                        mList[mission_num].services[service_num].output[1] = pChild2->Attribute("output2"); 
     1012                    if(pChild2->Attribute("output3")) 
     1013                        mList[mission_num].services[service_num].output[2] = pChild2->Attribute("output3"); 
     1014                } 
     1015 
     1016                mList[mission_num].services[conditional_1].num_conds = cond_array[1]; 
     1017                cond_array[1] = 0; 
     1018                cond_array[0]++; 
     1019            } 
     1020            if(conditional_0 != service_num){ 
     1021                mList[mission_num].services[conditional_0].name = pChild1->Value(); 
     1022                    if(pChild1->Attribute("input_t")) 
     1023                        mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input_t"); 
     1024                    if(pChild1->Attribute("input_f")) 
     1025                        mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input_f");} 
     1026            else{ 
     1027                mList[mission_num].services[conditional_0].name = pChild1->Attribute("name"); 
     1028                if(pChild1->Attribute("input1")) 
     1029                    mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input1"); 
     1030                if(pChild1->Attribute("input2")) 
     1031                    mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input2"); 
     1032                if(pChild1->Attribute("input3")) 
     1033                    mList[mission_num].services[service_num].input[2] = pChild1->Attribute("input3"); 
     1034                if(pChild1->Attribute("output1")) 
     1035                    mList[mission_num].services[service_num].output[0] = pChild1->Attribute("output1"); 
     1036                if(pChild1->Attribute("output2")) 
     1037                    mList[mission_num].services[service_num].output[1] = pChild1->Attribute("output2"); 
     1038                if(pChild1->Attribute("output3")) 
     1039                    mList[mission_num].services[service_num].output[2] = pChild1->Attribute("output3"); 
     1040            } 
     1041 
     1042            mList[mission_num].services[conditional_0].num_conds = cond_array[0]; 
     1043            //printf("hello\n"); 
     1044            cond_array[0] = 0; 
     1045            service_num++; 
     1046        } 
     1047        for(int i = 0; i < service_num; i++) 
     1048                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()); 
     1049 
     1050        mList[mission_num].numServices = service_num; 
     1051        mList[mission_num].name = pChild0->Attribute("name"); 
     1052        mList[mission_num].missionID = atoi(pChild0->Attribute("id")); 
     1053        if(pChild0->Attribute("input1")) 
     1054            mList[mission_num].input[0] = pChild0->Attribute("input1"); 
     1055        if(pChild0->Attribute("input2")) 
     1056            mList[mission_num].input[1] = pChild0->Attribute("input2"); 
     1057        if(pChild0->Attribute("input3")) 
     1058            mList[mission_num].input[2] = pChild0->Attribute("input3"); 
     1059        if(pChild0->Attribute("output1")) 
     1060            mList[mission_num].output[0] = pChild0->Attribute("output1"); 
     1061        if(pChild0->Attribute("output2")) 
     1062            mList[mission_num].output[1] = pChild0->Attribute("output2"); 
     1063        if(pChild0->Attribute("output3")) 
     1064            mList[mission_num].output[2] = pChild0->Attribute("output3"); 
     1065        printf("mis, input1=%s, output1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str()); 
     1066        mission_num++; 
     1067    } 
     1068    /*TiXmlElement *pMission; 
     1069    TiXmlElement *pService; 
     1070    TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3; 
     1071    TiXmlHandle hRoot(0); 
     1072    //printf("ServiceManagementLayer:: Loading Configuration.\n"); 
     1073    TiXmlDocument doc("."); 
     1074    doc.LoadFile(SML_Config); 
     1075    bool loadOkay = doc.LoadFile(); 
     1076    if(!loadOkay) 
     1077        printf("Loading SML configuration failed: %s\n", SML_Config); 
     1078 
     1079    TiXmlHandle hDoc(&doc); 
     1080     
     1081    pMission = hDoc.FirstChildElement().Element(); 
     1082 
     1083    if(!pMission)  
     1084        printf("No valid root!"); 
     1085 
     1086    hRoot = TiXmlHandle(pMission); 
     1087    pService = pMission->FirstChildElement(); 
     1088    int32_t mission_num = 0; 
     1089    //Iterate through the missions 
     1090    for(pChild0 = pMission->FirstChildElement(); pChild0 ; \ 
     1091        pChild0 = pChild0->NextSiblingElement())  
     1092    { 
     1093        int32_t service_num = 0; 
     1094        uint16_t cond_array[] = {0, 0}; 
     1095        //printf("mission_num = %d\n", mission_num); 
     1096        memset(cond_array, 0, 2); 
     1097         
     1098        for(pChild1  = pChild0->FirstChildElement(); pChild1; \ 
     1099            pChild1  = pChild1->NextSiblingElement())  
     1100        { 
     1101            int32_t conditional_0 = service_num; 
     1102            for(pChild2 = pChild1->FirstChildElement(); \ 
     1103                pChild2; pChild2 = pChild2->NextSiblingElement()) 
     1104            { 
     1105                service_num++; 
     1106                int32_t conditional_1 = service_num; 
     1107                for(pChild3 = pChild2->FirstChildElement(); \ 
     1108                    pChild3; pChild3 = pChild3 ->NextSiblingElement()) 
     1109                { 
     1110                    service_num++; 
     1111                    mList[mission_num].services[service_num].name = pChild3->Attribute("name"); 
     1112                    if(pChild3->Attribute("input1")) 
     1113                        mList[mission_num].services[service_num].input[0] = pChild3->Attribute("input1"); 
     1114                    if(pChild3->Attribute("input2")) 
     1115                        mList[mission_num].services[service_num].input[1] = pChild3->Attribute("input2"); 
     1116                    if(pChild3->Attribute("input3")) 
     1117                        mList[mission_num].services[service_num].input[2] = pChild3->Attribute("input3"); 
     1118                    if(pChild3->Attribute("output1")) 
     1119                        mList[mission_num].services[service_num].output[0] = pChild3->Attribute("output1"); 
     1120                    if(pChild3->Attribute("output2")) 
     1121                        mList[mission_num].services[service_num].output[1] = pChild3->Attribute("output2"); 
     1122                    if(pChild3->Attribute("output3")) 
     1123                        mList[mission_num].services[service_num].output[2] = pChild3->Attribute("output3"); 
     1124                    cond_array[1]++; 
     1125                } 
     1126 
     1127                if(conditional_1 != service_num){ 
     1128                    mList[mission_num].services[conditional_1].name = pChild2->Value(); 
     1129                    if(pChild2->Attribute("input_t")) 
     1130                        mList[mission_num].services[conditional_1].input[0] = pChild2->Attribute("input_t"); 
     1131                    if(pChild2->Attribute("input_f")) 
     1132                        mList[mission_num].services[conditional_1].input[1] = pChild2->Attribute("input_f"); 
     1133                } 
     1134                else{ 
     1135                    mList[mission_num].services[service_num].name = pChild2->Attribute("name"); 
     1136                    if(pChild2->Attribute("input1")) 
     1137                        mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input1"); 
     1138                    if(pChild2->Attribute("input2")) 
     1139                        mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input2"); 
     1140                    if(pChild2->Attribute("input3")) 
     1141                        mList[mission_num].services[service_num].input[2] = pChild2->Attribute("input3"); 
     1142                    if(pChild2->Attribute("output1")) 
     1143                        mList[mission_num].services[service_num].output[0] = pChild2->Attribute("output1"); 
     1144                    if(pChild2->Attribute("output2")) 
     1145                        mList[mission_num].services[service_num].output[1] = pChild2->Attribute("output2"); 
     1146                    if(pChild2->Attribute("output3")) 
     1147                        mList[mission_num].services[service_num].output[2] = pChild2->Attribute("output3"); 
     1148                } 
     1149 
     1150                mList[mission_num].services[conditional_1].num_conds = cond_array[1]; 
     1151                cond_array[1] = 0; 
     1152                cond_array[0]++; 
     1153            } 
     1154            if(conditional_0 != service_num){ 
     1155                mList[mission_num].services[conditional_0].name = pChild1->Value(); 
     1156                if(pChild1->Attribute("input_t")) 
     1157                    mList[mission_num].services[conditional_0].input[0] = pChild1->Attribute("input_t"); 
     1158                if(pChild1->Attribute("input_f")) 
     1159                    mList[mission_num].services[conditional_0].input[1] = pChild1->Attribute("input_f"); 
     1160                printf("if detected %s, %s\n", pChild1->Attribute("input_f"), pChild1->Attribute("input_t")); 
     1161            } 
     1162            else{ 
     1163                mList[mission_num].services[service_num].name = pChild1->Attribute("name"); 
     1164                if(pChild1->Attribute("input1")) 
     1165                    mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input1"); 
     1166                if(pChild1->Attribute("input2")) 
     1167                    mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input2"); 
     1168                if(pChild1->Attribute("input3")) 
     1169                    mList[mission_num].services[service_num].input[2] = pChild1->Attribute("input3"); 
     1170                if(pChild1->Attribute("output1")) 
     1171                    mList[mission_num].services[service_num].output[0] = pChild1->Attribute("output1"); 
     1172                if(pChild1->Attribute("output2")) 
     1173                    mList[mission_num].services[service_num].output[1] = pChild1->Attribute("output2"); 
     1174                if(pChild1->Attribute("output3")) 
     1175                    mList[mission_num].services[service_num].output[2] = pChild1->Attribute("output3"); 
     1176            } 
     1177 
     1178            mList[mission_num].services[conditional_0].num_conds = cond_array[0]; 
     1179            //printf("hello\n"); 
     1180             
     1181            cond_array[0] = 0; 
     1182            service_num++; 
     1183        } 
     1184        for(int i = 0; i < service_num; i++) 
     1185                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()); 
     1186 
     1187        mList[mission_num].numServices = service_num; 
     1188        mList[mission_num].name = pChild0->Attribute("name"); 
     1189        mList[mission_num].missionID = atoi(pChild0->Attribute("id")); 
     1190        if(pChild0->Attribute("input1")) 
     1191            mList[mission_num].input[0] = pChild0->Attribute("input1"); 
     1192        if(pChild0->Attribute("input2")) 
     1193            mList[mission_num].input[1] = pChild0->Attribute("input2"); 
     1194        if(pChild0->Attribute("input3")) 
     1195            mList[mission_num].input[2] = pChild0->Attribute("input3"); 
     1196        if(pChild0->Attribute("output1")) 
     1197            mList[mission_num].output[0] = pChild0->Attribute("output1"); 
     1198        if(pChild0->Attribute("output2")) 
     1199            mList[mission_num].output[1] = pChild0->Attribute("output2"); 
     1200        if(pChild0->Attribute("output3")) 
     1201            mList[mission_num].output[2] = pChild0->Attribute("output3"); 
     1202        printf("input1=%s, outpu1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str()); 
     1203        mission_num++; 
     1204    }*/ 
     1205} 
     1206 
     1207//Verified w/o Transfers May 26 
    3841208void 
    3851209ServiceManagementLayer::RegisterCognitiveEngine(int32_t ID) 
     
    3881212    LOG("ServiceManagementLayer:: CE registration message forwarded to shell.\n"); 
    3891213 
    390     TransferRadioConfiguration(ID); 
    391     TransferExperience(ID); 
     1214    //printf("ServiceManagementLayer:: CE registration message forwarded to shell.\n"); 
     1215 
     1216    //TransferRadioConfiguration(ID); 
     1217    //TransferExperience(ID); 
    3921218 
    3931219    numberOfCognitiveEngines++; 
     
    4251251    memset(buffer, 0, 256); 
    4261252    ReadMessage(shellSocketFD, buffer); 
    427     sendto(cogEngSrv, buffer, 256, NULL, (struct sockaddr*) CE_List[ID].sock_ptr, CE_List[ID].sock_len); 
     1253    SendMessage(CE_List[ID].FD, buffer); 
    4281254    if(strcmp("deregister_ack", buffer) != 0) { 
    4291255        ERROR(1, "SML:: Failed to close CE socket\n"); 
    4301256    } 
    431     CE_List[ID].sock_ptr = NULL; 
     1257    CE_List[ID].FD = -1; 
    4321258    CE_List[ID].ID_num = -1; 
    4331259 
     
    4351261} 
    4361262 
    437 /* The basic idea here is that when any time a message is sent over the socket, the sender's address is sent too. 
    438  * We take advantage of this in order to uniquely identify the component for future communications since 
    439  * there is only one socket on the SML-side. 
    440  */ 
     1263 
     1264//VERIFIED May 21 
    4411265void 
    4421266ServiceManagementLayer::StartSMLServer() 
     
    4471271    int32_t desc_ready = 1; 
    4481272    int32_t timeout = 10; 
    449     fd_set sockSet; 
    450  
    451     cogEngSrv = CreateTCPServerSocket(CEPort); 
     1273                //If there is, call the MessageHandler with the Shell_Msg code of -1 
     1274    fd_set sockSet, shellSet; 
     1275 
     1276    cogEngSrv = CreateTCPServerSocket(2036); 
    4521277    int32_t maxDescriptor = cogEngSrv; 
    4531278 
     
    4551280        ERROR(1,"Error initializing primary port\n"); 
    4561281 
    457     while (running) { 
     1282 
     1283    /*int FD = ClientSocket("127.0.0.1", "2013");  
     1284    SendMessage(FD, "register_engine_cognitive");    
     1285    SendMessage(FD, "register_service"); 
     1286    SendMessage(FD, "test_srv");  
     1287    SendMessage(FD, "list_services"); 
     1288    int FD2 = ClientSocket("127.0.0.1", "2013");  
     1289    SendMessage(FD2, "register_engine_cognitive");    
     1290    SendMessage(FD2, "register_service"); 
     1291    SendMessage(FD2, "test_srv1");  
     1292    SendMessage(FD2, "list_services");*/ 
     1293     //SendMessage(shellSocketFD, "list_services");*/ 
     1294    //printf("num = %d, %d, %d\n", one, two, three); 
     1295     
     1296 
     1297    int i = 10000000; 
     1298    while (i>0) { 
     1299        i--; 
    4581300        /* Zero socket descriptor vector and set for server sockets */ 
    4591301        /* This must be reset every time select() is called */ 
    4601302        FD_ZERO(&sockSet); 
    4611303        FD_SET(cogEngSrv, &sockSet); 
     1304        for(int k = 0; k < Current_ID; k++) 
     1305            FD_SET(CE_List[k].FD, &sockSet); 
     1306            //printf("k=%d, CID=%d\n", k, CE_List[k].FD); 
    4621307 
    4631308        /* Timeout specification */ 
     
    4691314 
    4701315        //Check if there is a message on the socket waiting to be read 
    471         rc = select(cogEngSrv + 1, &sockSet, NULL, NULL, &selTimeout); 
     1316        rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout);  
     1317        //printf("rc=%d\n", rc); 
    4721318        if(rc == 0){ 
    473             //If not, check instead for messages from the shell 
    474             ShellSignalHandler();        
     1319            //LOG("No echo requests for %i secs...Server still alive\n", timeout); 
     1320         
     1321            FD_ZERO(&shellSet); 
     1322            FD_SET(shellSocketFD, &shellSet); 
     1323            selTimeout.tv_sec = 0; 
     1324            selTimeout.tv_usec = 0; 
     1325            //Check if there is a message on the shell socket ready to be processed 
     1326            int rc2 = select(shellSocketFD + 1, &shellSet, NULL, NULL, &selTimeout); 
     1327            //printf("rc2=%d\n", rc2); 
     1328                //If there is, call the MessageHandler with the Shell_Msg code of -1 
     1329            if(FD_ISSET(shellSocketFD, &shellSet)){ 
     1330                //printf("shell_msg, %d\n", rc2); 
     1331                MessageHandler(-1);} 
    4751332        } 
    476         else { 
    477             //If so, process the address information and pass the component ID on to the handler 
    478             char buffer[256]; 
    479             memset(buffer, 0, 256); 
    480             struct sockaddr_in sock_addr; 
    481             memset((void *) &sock_addr, 0, sizeof(sock_addr)); 
    482             socklen_t sock_len; 
    483             //Peak at the next message on the socket to determine its address of orgin 
    484             recvfrom(cogEngSrv, buffer, 256, MSG_PEEK, (struct sockaddr*) &sock_addr, &sock_len); 
    485             bool found = false; 
    486             //Is it from a previously logged address? 
    487             for(int i = 0; i < Current_ID; i++){ 
    488                 if((*CE_List[i].sock_ptr).sin_addr.s_addr == sock_addr.sin_addr.s_addr){ 
    489                         //If so, pass the ID number of that component into the signal handler to process the message 
    490                         found=true; 
    491                         CESignalHandler(i); 
    492                 } 
    493             } 
    494             //If not, log the address and pass the ID number of that component into the signal handler 
    495             if(!found){ 
    496                 CE_List[Current_ID].sock_ptr = &sock_addr; 
    497                 CE_List[Current_ID].ID_num = Current_ID; 
    498                 CE_List[Current_ID].sock_len = sock_len; 
    499                 CESignalHandler(Current_ID); 
    500                 Current_ID++; 
    501             } 
    502         } 
    503     }         
    504  
    505     /* Close sockets */ 
    506     close(cogEngSrv); 
    507  
    508     return; 
    509 } 
    510  
    511 /* 
    512  
    513  
    514  
    515 rc = select(cogEngSrv + 1, &sockSet, NULL, NULL, &selTimeout);  
    516         if(rc == 0){ 
    517             LOG("No echo requests for %i secs...Server still alive\n", timeout); 
    518             ShellSignalHandler();        
    519         } 
    520         else { 
     1333        else {  
    5211334            desc_ready = rc; 
    522  
    5231335            for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) { 
    5241336                if(FD_ISSET(port, &sockSet)) { 
     
    5271339                    //Check if request is new or on an existing open descriptor 
    5281340                    if(port == cogEngSrv) {  
    529                         do { 
     1341        //printf("here1\n"); 
     1342                       // do { 
    5301343                            new_sd = AcceptTCPConnection(port); 
     1344                          
     1345                            //printf("sd = %d\n", new_sd); 
    5311346                            if(new_sd < 0)  
    5321347                                break; 
     1348                            //Set not to block 
     1349                            /*int oldflags = fcntl (new_sd, F_GETFL, 0); 
     1350                            oldflags |= O_NONBLOCK; 
     1351                            fcntl (new_sd, F_SETFL, oldflags);*/ 
     1352 
    5331353                            
    5341354                            CE_List[Current_ID].FD = new_sd; 
    535                             CE_List[Current_ID].IDNum = Current_ID; 
    536                             HandleMessage(CE_List[Current_ID]); 
     1355                            CE_List[Current_ID].ID_num = Current_ID; 
     1356                            MessageHandler(Current_ID); 
    5371357                            Current_ID++; 
    5381358         
    539                             FD_SET(new_sd,&sockSet); 
     1359                            FD_SET(new_sd,&sockSet); 
    5401360                            if(new_sd > maxDescriptor)  
    5411361                                maxDescriptor = new_sd; 
    5421362                            //LOG("New incoming connection - %i\n\n",new_sd); 
    543                         } while(new_sd != -1); 
     1363                       // } while(new_sd != -1); 
    5441364                    }  
    5451365                    else { 
     1366                        //printf("here2\n"); 
    5461367                        //LOG("Request on already open descriptor.\n\n"); 
    547                         for(int16_t i = 0; i < Current_ID; i++) 
     1368                        for(int16_t z = 0; z < Current_ID; z++) 
    5481369                        { 
    549                                 if(CE_List[i].FD == port) 
    550                                         HandleMessage(CE_List[i]); 
     1370                                if(CE_List[z].FD == port){ 
     1371                                        //printf("z=%d\n", z); 
     1372                                        MessageHandler(z);} 
    5511373                        } 
    5521374                    } 
    5531375                } 
    5541376            } 
    555     } 
    556 */ 
    557  
    558  
     1377        } 
     1378    }         
     1379 
     1380    /* Close sockets */ 
     1381    close(cogEngSrv); 
     1382 
     1383    //delete &cogEngSrv; 
     1384    return; 
     1385}