Index: /vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp
===================================================================
--- /vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp	(revision 443)
+++ /vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp	(revision 444)
@@ -115,5 +115,5 @@
     int32_t rc;         /* sqlite command return code */
 
-    _services_DB->command="drop table ";
+    _services_DB->command = "drop table ";
     _services_DB->command.append(_services_DB->tablename);
     rc = sqlite3_exec(_services_DB->db, _services_DB->command.c_str(), callback, 0, &errorMsg);
@@ -121,5 +121,5 @@
         WARNING("ServiceManagementLayer::Destructor services 'drop table' error: %s\n", errorMsg);
 
-    _services_DB->command="vacuum";
+    _services_DB->command = "vacuum";
     rc = sqlite3_exec(_services_DB->db, _services_DB->command.c_str(), callback, 0, &errorMsg);
     if((rc != SQLITE_OK) && (rc != 101))
@@ -128,5 +128,5 @@
     free(_services_DB);
 
-    _data_DB->command="drop table ";
+    _data_DB->command = "drop table ";
     _data_DB->command.append(_data_DB->tablename);
     rc = sqlite3_exec(_data_DB->db, _data_DB->command.c_str(), callback, 0, &errorMsg);
@@ -134,5 +134,5 @@
         WARNING("ServiceManagementLayer::Destructor data 'drop table' error: %s\n", errorMsg);
 
-    _data_DB->command="vacuum";
+    _data_DB->command = "vacuum";
     rc = sqlite3_exec(_data_DB->db, _data_DB->command.c_str(), callback, 0, &errorMsg);
     if((rc != SQLITE_OK) && (rc != 101))
@@ -186,5 +186,5 @@
     sqlite3_open(_services_DB->filename.c_str(), &(_services_DB->db));
 
-    char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"};
+    char *cols[] = {(char *)"ID_Num", (char *)"Service_Name"};
 
     _services_DB->tablename="Services";
@@ -242,8 +242,8 @@
     sqlite3_open(_data_DB->filename.c_str(), &(_data_DB->db));
 
-    char* cols[] = {(char *)"Tag", (char *)"Data"};
-
-    _data_DB->tablename="Data";
-    _data_DB->command="DROP TABLE IF EXISTS Data;";     
+    char *cols[] = {(char *)"Tag", (char *)"Data"};
+
+    _data_DB->tablename = "Data";
+    _data_DB->command = "DROP TABLE IF EXISTS Data;";     
 
     rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), 128, &ppStmt, &pzTail);
@@ -258,5 +258,5 @@
 
     /* Generate command */
-    _data_DB->command="CREATE TABLE ";
+    _data_DB->command = "CREATE TABLE ";
     _data_DB->command.append(_data_DB->tablename);
     _data_DB->command.append("(");
@@ -293,5 +293,5 @@
 /* CALLED BY: constructor
  * INPUTS: |serverName| the IPv4 name of the server (127.0.0.1 for localhost)
- *       |serverPort| the port on the server to connect to
+ *         |serverPort| the port on the server to connect to
  * OUTPUTS: <none>
  *
@@ -323,9 +323,9 @@
     
     if(ID != -1)
-    _FD = CE_List[ID].FD;
+        _FD = CE_List[ID].FD;
     else 
-    _FD = shellSocketFD;
+        _FD = shellSocketFD;
+
     ReadMessage(_FD, buffer);
-    //printf("MH_buffer = %s\n", buffer);
     
     //--------Policy Engine Stuff - no policy engine support in this version-------//
@@ -368,26 +368,26 @@
     }
     else if(strcmp(buffer, "register_engine_cognitive") == 0) {
-         RegisterCognitiveEngine(ID);
+        RegisterCognitiveEngine(ID);
     }
     else if(strcmp(buffer, "register_service") == 0) {
-     ReceiveServices(ID);
+        ReceiveServices(ID);
     }
     else if(strcmp(buffer, "send_component_type") == 0) {
-     SendComponentType();
+        SendComponentType();
     }
     else if(strcmp(buffer, "list_services") == 0) {
-     ListServices();
+        ListServices();
     }
     else if(strcmp(buffer, "set_active_mission") == 0) {
-    SetActiveMission();
+        SetActiveMission();
     }
     else if(strcmp(buffer, "request_optimization") == 0) {
-    PerformActiveMission();
+        PerformActiveMission();
     }
     else if(strcmp(buffer, "deregister_engine_cognitive") == 0) {
-    DeregisterCognitiveEngine(ID);
+        DeregisterCognitiveEngine(ID);
     }
     else if(strcmp(buffer, "deregister_service") == 0) {
-    DeregisterServices(ID);
+        DeregisterServices(ID);
     }
 }
@@ -431,5 +431,4 @@
     SendMessage(shellSocketFD, "register_sml");
     LOG("ServiceManagementLayer:: Registration message sent.\n");
-    //printf("SSFD = %d\n", shellSocketFD);
 }
 
@@ -460,35 +459,38 @@
  * for an "ack" message from the CE after every sent message 
  * to know when to stop communication.
- */
-
-//Modified to check the incoming message buffer rather than the outgoing message buffer to avoid a portion of the delay
-//May change this again to handle data more inteligently, taking advantage of it's properties.
+ * 
+ * NOTE: Modified to check the incoming message buffer rather than the outgoing 
+ * message buffer to avoid a portion of the delay. May change this again to handle 
+ * data more inteligently, taking advantage of it's properties.
+ */
 void 
 ServiceManagementLayer::TransferRadioConfiguration(int32_t ID)
 {
-    //printf("transRadConfig\n");
     struct timeval selTimeout;
     fd_set sockSet;
     int32_t rc = 1;
     char buffer[256];
-    //Send data until the CE sends an ACK message back
-    while(rc!=0){
+
+    /* Send data until the CE sends an ACK message back */
+    while(rc != 0) {
         memset(buffer, 0, 256);
-    //Receive data from Shell
-    ReadMessage(shellSocketFD, buffer);
-    //printf("buffer = %s\n", buffer);
-    //Send data to CE
-    SendMessage(CE_List[ID].FD, buffer);
+
+        /* Receive data from Shell */
+        ReadMessage(shellSocketFD, buffer);
+
+        /* Send data to CE */
+        SendMessage(CE_List[ID].FD, buffer);
         FD_ZERO(&sockSet);
         FD_SET(shellSocketFD, &sockSet);
         selTimeout.tv_sec = 0;
         selTimeout.tv_usec = 5000;
-        //Check if there is a message on the shell ready to be processed
-        rc=select(shellSocketFD + 1, &sockSet, NULL, NULL, &selTimeout);
-    }
+
+        /* Check if there is a message on the shell ready to be processed */
+        rc = select(shellSocketFD + 1, &sockSet, NULL, NULL, &selTimeout);
+    }
+
     memset(buffer, 0, 256);
     ReadMessage(CE_List[ID].FD, buffer);
     SendMessage(shellSocketFD, buffer);
-    //printf("transfer done!\n");
 }
 
@@ -499,8 +501,9 @@
  *
  * DESCRIPTION: Simmilar to TransferRadioConfig, just with Experience data
- */
-
-//Modified to check the incoming message buffer rather than the outgoing message buffer to avoid a portion of the delay
-//May change this again to handle data more inteligently, taking advantage of it's properties.
+ * 
+ * NOTE: Modified to check the incoming message buffer rather than the outgoing 
+ * message buffer to avoid a portion of the delay. May change this again to handle 
+ * data more inteligently, taking advantage of it's properties.
+ */
 void 
 ServiceManagementLayer::TransferExperience(int32_t ID)
@@ -510,22 +513,23 @@
     int32_t rc = 1;
     char buffer[256];
-    //Send data until the CE sends an ACK message back
-    while(rc!=0){
-    //printf("transfering...\n");
+    /* Send data until the CE sends an ACK message back */
+    while(rc != 0) {
         memset(buffer, 0, 256);
-    //Receive data from Shell
-    ReadMessage(shellSocketFD, buffer);
-    //printf("buffer = %s\n", buffer);
-    //Send data to CE
-    SendMessage(CE_List[ID].FD, buffer);
+
+        /* Receive data from Shell */
+        ReadMessage(shellSocketFD, buffer);
+
+        /* Send data to CE */
+        SendMessage(CE_List[ID].FD, buffer);
         FD_ZERO(&sockSet);
         FD_SET(shellSocketFD, &sockSet);
         selTimeout.tv_sec = 0;
         selTimeout.tv_usec = 5000;
-        //Check if there is a message on the shell ready to be processed
-        rc=select(shellSocketFD + 1, &sockSet, NULL, NULL, &selTimeout);
-    }
+
+        /* Check if there is a message on the shell ready to be processed */
+        rc = select(shellSocketFD + 1, &sockSet, NULL, NULL, &selTimeout);
+    }
+
     memset(buffer, 0, 256);
-    //printf("done trans exp!\n");
     ReadMessage(CE_List[ID].FD, buffer);
     SendMessage(shellSocketFD, buffer);
@@ -544,8 +548,9 @@
     memset(buffer, 0, 256);
     ReadMessage(CE_List[ID].FD, buffer);
-    char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"};
-    //printf("RS_buffer = %s\n", buffer);
-    // generate command
-    _services_DB->command="insert into ";
+
+    char *cols[] = {(char *) "ID_Num", (char *) "Service_Name"};
+
+    /* Generate command */
+    _services_DB->command = "insert into ";
     _services_DB->command.append(_services_DB->tablename); 
     _services_DB->command.append(" (");
@@ -555,7 +560,9 @@
     _services_DB->command.append(") ");
     _services_DB->command.append(" values(");
+
     char temp[3];
-    memset(temp,0,3);
+    memset(temp, 0, 3);
     sprintf(temp, "%d", ID);
+
     _services_DB->command.append(temp);
     _services_DB->command.append(", '");
@@ -563,12 +570,9 @@
     _services_DB->command.append("');");
     
-    //printf("search command: %s\n", _services_DB->command);
-    // execute add command
+    /* Execute add command */
     char *errorMsg;
     int rc = sqlite3_exec(_services_DB->db, _services_DB->command.c_str(), callback, 0, &errorMsg);
-    if( rc!=SQLITE_OK && rc!=101 )
-        fprintf(stderr, "ServiceManagementLayer::RecieveServices DB Error %s\n", errorMsg);
-    /*sprintf(outBuffer, "SML: Registering service '%s' from component number '%d'", buffer, ID);
-    LOG(outBuffer);*/
+    if((rc != SQLITE_OK) && (rc != 101))
+        WARNING("ServiceManagementLayer::RecieveServices DB Error %s\n", errorMsg);
 }
 
@@ -577,7 +581,8 @@
  * OUTPUTS: <none>
  *
- * DESCRIPTION: This method associates the services that components provide with the services that are requested in the mission
- * Each service in the mission is given the ID and FD of a component that has registered to provide that service 
- * Deregistration is okay until this method is called without a reload, but if deregistration occurs after this
+ * DESCRIPTION: This method associates the services that components provide with the 
+ * services that are requested in the mission. Each service in the mission is given 
+ * the ID and FD of a component that has registered to provide that service. Deregistration 
+ * is okay until this method is called without a reload, but if deregistration occurs after this
  * method is called it needs to be called again even if other engines also provide the services
  */
@@ -588,51 +593,56 @@
     memset(buffer, 0, 256);
     ReadMessage(shellSocketFD, buffer);
+
     uint32_t missID = atoi(buffer);
-    for(activeMission = 0; activeMission < 10; activeMission++)
-    {
-    //Find the active mission by comparing mission ID's
-    if(miss[activeMission].missionID == missID)
-        break;
-    }
-
-    LOG("ServiceManagementLayer:: Received Set Active Mission command: %i.\n",missID);
-    //For each service in the mission
-    for(uint16_t i = 0; i < miss[activeMission].numServices; i++)
-    {    
-    //Check whether the current service is an actual service or a conditional
-    if(miss[activeMission].services[i].name.compare("if") && miss[activeMission].services[i].name.compare("dowhile") && \
-       miss[activeMission].services[i].name.compare("shell")){
-        //If it is a service, search the database of registered services to find the ID of the component that registered it
-            _services_DB->command="select ";
-           _services_DB->command.append(_services_DB->tablename);
-            _services_DB->command.append(".* from ");
-            _services_DB->command.append( _services_DB->tablename);
-            _services_DB->command.append(" where Service_Name=='");
-        _services_DB->command.append(miss[activeMission].services[i].name);
-        _services_DB->command.append("';");
+    for(activeMission = 0; activeMission < 10; activeMission++) {
+        /* Find the active mission by comparing mission ID's */
+        if(miss[activeMission].missionID == missID)
+            break;
+    }
+
+    LOG("ServiceManagementLayer:: Received Set Active Mission command: %i.\n", missID);
+
+    /* For each service in the mission */
+    for(size_t i = 0; i < miss[activeMission].numServices; i++) {
+        /* Check whether the current service is an actual service or a conditional */
+        if(miss[activeMission].services[i].name.compare("if") && \
+                miss[activeMission].services[i].name.compare("dowhile") && \
+                miss[activeMission].services[i].name.compare("shell")) {
+                /* If it is a service, search the database of registered services to find 
+                 * the ID of the component that registered it */
+                _services_DB->command="select ";
+                _services_DB->command.append(_services_DB->tablename);
+                _services_DB->command.append(".* from ");
+                _services_DB->command.append( _services_DB->tablename);
+                _services_DB->command.append(" where Service_Name=='");
+                _services_DB->command.append(miss[activeMission].services[i].name);
+                _services_DB->command.append("';");
     
-            sqlite3_stmt * pStatement;
-            int rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command.c_str(), -1, &pStatement, NULL);
-            if (rc == SQLITE_OK){ 
-                if (sqlite3_step(pStatement) == SQLITE_ROW)
-                 miss[activeMission].services[i].componentID =  sqlite3_column_int(pStatement, 0); 
-                else {
-                printf("services_DB:: Mission requires service %s not provided by any connected component.\n",miss[activeMission].services[i].name.c_str());
-            rc=31337;
-            }
-             } else {
-            printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_services_DB->command.c_str());
-            }
-        //printf("s_name=%s\n",miss[activeMission].services[i].name.c_str());
-            sqlite3_finalize(pStatement);
-        miss[activeMission].services[i].socketFD = CE_List[miss[activeMission].services[i].componentID].FD;
-        //Set the FD and ID of the service to refer to the component where the service exists
-    }
-    //Nothing to be done for conditionals at this stage
+                sqlite3_stmt * pStatement;
+                int32_t rc = sqlite3_prepare_v2(_services_DB->db, _services_DB->command.c_str(), \
+                        -1, &pStatement, NULL);
+                if(rc == SQLITE_OK) { 
+                    if (sqlite3_step(pStatement) == SQLITE_ROW)
+                        miss[activeMission].services[i].componentID = sqlite3_column_int(pStatement, 0); 
+                    else {
+                        WARNING("services_DB:: Mission requires service %s ", \
+                                miss[activeMission].services[i].name.c_str());
+                        WARNING("not provided by any connected component.\n")
+                        rc = 31337;
+                    }
+                } else {
+                    WARNING("services_DB:: Error executing SQL statement. rc = %i\n%s\n", \
+                            rc, _services_DB->command.c_str());
+                }
+
+                sqlite3_finalize(pStatement);
+                miss[activeMission].services[i].socketFD = \
+                    CE_List[miss[activeMission].services[i].componentID].FD;
+        }
+        /* TODO Nothing to be done for conditionals at this stage */
     }
  
     SendMessage(shellSocketFD, "ack");
     LOG("ServiceManagementLayer:: Done setting active mission.\n");
-    //printf("\nhere ---%d, %d---\n", miss[activeMission].services[0].componentID, miss[activeMission].services[1].componentID);
 }
 
@@ -642,67 +652,74 @@
  *
  * DESCRIPTION: This is a helper method for the "PerformActiveMission" function
- * NOTE: This function has changed durrastically from the previous implementation
- * Takes an ID of a service
- * For that service, finds inputs in DB and forwords those on to the engine after sending comm-starting messages
- * Afterwords, listenes for the outputs so that it can store those in the database for future services or the overall output
+ * NOTE: This function has changed drastically from the previous implementation
+ * 
+ * Takes an ID of a service. For that service, finds inputs in DB and forwords 
+ * those on to the engine after sending comm-starting messages. Afterwords, listenes 
+ * for the outputs so that it can store those in the database for future services or 
+ * the overall output
  */
 void
 ServiceManagementLayer::TransactData(int32_t sourceID)
 {
-   // LOG("ServiceManagementLayer:: Data transaction occuring.\n");
     char buffer[256];
     std::string data;
-    char* cols[] = {(char *)"Tag", (char *)"Data"};
-    int i = 0;
+    char *cols[] = {(char *) "Tag", (char *) "Data"};
     char *token;
 
-   //Send a message directly to the shell
-   if(miss[activeMission].services[sourceID].name.find("shell")!=string::npos)
-   { 
-    int k = 0;
-    shellFound=true;
-    while(k<10 && !miss[activeMission].input[k].empty()){
-        k++;
-    }
-    sprintf(buffer, "%d", k);
-    SendMessage(shellSocketFD, buffer);
-    for(int t = 0; t < k; t++){
-        memset(buffer, 0 , 256);
-            _data_DB->command="select ";
+   /* Send a message directly to the shell */
+   if(miss[activeMission].services[sourceID].name.find("shell") != string::npos) {
+       shellFound=true;
+
+       int32_t k = 0;
+       while((k < 10) && (!miss[activeMission].input[k].empty())) {
+           k++;
+       }
+
+       sprintf(buffer, "%d", k);
+       SendMessage(shellSocketFD, buffer);
+       for(int32_t t = 0; t < k; t++) {
+           memset(buffer, 0 , 256);
+           _data_DB->command="select ";
            _data_DB->command.append(_data_DB->tablename);
-            _data_DB->command.append(".* from ");
-            _data_DB->command.append(_data_DB->tablename);
-            _data_DB->command.append(" where Tag=='");
-        _data_DB->command.append(miss[activeMission].input[t]);
-        _data_DB->command.append("';");
-            sqlite3_stmt * pStatement;
-            int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), -1, &pStatement, NULL);
-            if (rc == SQLITE_OK){ 
-                if (sqlite3_step(pStatement) == SQLITE_ROW)
-                data=((const char*) sqlite3_column_text(pStatement, 1)); 
-                else {
-                printf("3data_DB:: Data not yet in DB., %s\n", _data_DB->command.c_str());
-            rc=31337;
-            }
-            } 
-        else {
-            printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command.c_str());
-            }
-            sqlite3_finalize(pStatement);
-        token = strtok((char *)data.c_str(), "@");
-        token = strtok(NULL, "@");
-        SendMessage(shellSocketFD, token);
-        token = strtok(NULL, "@");
-        SendMessage(shellSocketFD, token);
-    }
-    return;
+           _data_DB->command.append(".* from ");
+           _data_DB->command.append(_data_DB->tablename);
+           _data_DB->command.append(" where Tag=='");
+           _data_DB->command.append(miss[activeMission].input[t]);
+           _data_DB->command.append("';");
+           sqlite3_stmt * pStatement;
+
+           int32_t rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command.c_str(), \
+                   -1, &pStatement, NULL);
+           if(rc == SQLITE_OK) { 
+               if(sqlite3_step(pStatement) == SQLITE_ROW)
+                   data=((const char*) sqlite3_column_text(pStatement, 1)); 
+               else {
+                   LOG("3data_DB:: Data not yet in DB., %s\n", _data_DB->command.c_str());
+                   rc = 31337;
+               }
+           } 
+           else {
+               WARNING("data_DB:: Error executing SQL statement. rc = %i\n%s\n", \
+                       rc,_data_DB->command.c_str());
+           }
+
+           sqlite3_finalize(pStatement);
+           token = strtok((char *) data.c_str(), "@");
+           token = strtok(NULL, "@");
+           SendMessage(shellSocketFD, token);
+           token = strtok(NULL, "@");
+           SendMessage(shellSocketFD, token);
+       }
+
+       return;
    } 
 
-    //If this is a service command and not a shell command...
-    //Transmission starting messages
+    /* If this is a service command and not a shell command... */
+    /* Transmission starting messages */
     SendMessage(miss[activeMission].services[sourceID].socketFD, "request_optimization_service");
-    SendMessage(miss[activeMission].services[sourceID].socketFD, miss[activeMission].services[sourceID].name.c_str());
-
-   //If the service takes a parameter, feed that parameter in
+    SendMessage(miss[activeMission].services[sourceID].socketFD, \
+            miss[activeMission].services[sourceID].name.c_str());
+
+   /* If the service takes a parameter, feed that parameter in
    if(!miss[activeMission].services[sourceID].parameter.empty()){
     //printf("sending parameter!\n");
@@ -714,4 +731,5 @@
 
     //Load and transmit the input data
+    int i = 0;
     while(i < 10 && !miss[activeMission].services[sourceID].input[i].empty()){
         _data_DB->command="select ";
