Index: vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp
===================================================================
--- vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp	(revision 285)
+++ vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp	(revision 287)
@@ -20,5 +20,5 @@
 //TODO Better shutdown
 //TODO Verify Deregister services
-//TODO printf's
+//TODO printf's 
 
 #include <stdlib.h>
@@ -170,10 +170,9 @@
 
     _services_DB = (services_DB) malloc(sizeof(struct services_s));
-    char *errorMsg;
+   // char *errorMsg;
 
     // create database
 
     // copy filename
-    unsigned int i=0;
     strcpy(_services_DB->filename, "Services_Table");
 
@@ -195,5 +194,5 @@
     rc = sqlite3_step(ppStmt);
     if( rc!=SQLITE_OK && rc!=101 )
-        printf("ServiceManagementLayer::CreateServicesDB 'step' error %d\n");
+        printf("ServiceManagementLayer::CreateServicesDB 'step' error\n");
 
     // number of columns in the table
@@ -217,5 +216,5 @@
     rc = sqlite3_step(ppStmt);
     if( rc!=SQLITE_OK && rc!=101 )
-        printf("ServiceManagementLayer::CreateServicesDB 'step' error %d\n");
+        printf("ServiceManagementLayer::CreateServicesDB 'step' error\n");
 }
 
@@ -230,12 +229,12 @@
 {
     _data_DB = (data_DB) malloc(sizeof(struct data_s));
-    char *errorMsg;
+    //char *errorMsg;
+  sqlite3_stmt *ppStmt;  /* OUT: Statement handle */
+  const char *pzTail;     /* OUT: Pointer to unused portion of zSql */
 
     // create database
 
     // copy filename
-    unsigned int i=0;
     strcpy(_data_DB->filename, "Data_Table");
-
     // execute create database command
     // database handle
@@ -248,4 +247,13 @@
     // copy tablename
     strcpy(_data_DB->tablename, "Data");
+    sprintf(_data_DB->command, "DROP TABLE IF EXISTS Data;");     
+
+    int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, 128, &ppStmt, &pzTail);
+    if( rc!=SQLITE_OK && rc!=101 )
+        printf("ServiceManagementLayer::CreateServicesDB 'prepare_stmt' error %d\n", rc);
+    rc = sqlite3_step(ppStmt);
+    if( rc!=SQLITE_OK && rc!=101 )
+        printf("ServiceManagementLayer::CreateServicesDB 'step' error\n");
+
 
     // number of columns in the table
@@ -265,13 +273,11 @@
 
     // execute create table command
-  sqlite3_stmt *ppStmt;  /* OUT: Statement handle */
-  const char *pzTail;     /* OUT: Pointer to unused portion of zSql */
-
-    int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, 128, &ppStmt, &pzTail);
+
+    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, 128, &ppStmt, &pzTail);
     if( rc!=SQLITE_OK && rc!=101 )
         printf("ServiceManagementLayer::CreateDataDB 'prepare_stmt' error %d\n", rc);
     rc = sqlite3_step(ppStmt);
     if( rc!=SQLITE_OK && rc!=101 )
-        printf("ServiceManagementLayer::CreateDataDB 'step' error %d\n");
+        printf("ServiceManagementLayer::CreateDataDB 'step' error\n");
 }
 
@@ -541,5 +547,5 @@
     ReadMessage(CE_List[ID].FD, buffer);
     char* cols[] = {(char *)"ID_Num", (char *)"Service_Name"};
-    printf("RS_buffer = %s\n", buffer);
+    //printf("RS_buffer = %s\n", buffer);
     // generate command
     strcpy(_services_DB->command, "insert into ");
@@ -562,5 +568,4 @@
     if( rc!=SQLITE_OK && rc!=101 )
         fprintf(stderr, "ServiceManagementLayer::RecieveServices DB Error %s\n", errorMsg);
-    char *outBuffer;
     /*sprintf(outBuffer, "SML: Registering service '%s' from component number '%d'", buffer, ID);
     LOG(outBuffer);*/
@@ -582,5 +587,5 @@
     memset(buffer, 0, 256);
     ReadMessage(shellSocketFD, buffer);
-    int32_t missID = atoi(buffer);
+    uint32_t missID = atoi(buffer);
     for(activeMission = 0; activeMission < 10; activeMission++)
     {
@@ -590,10 +595,11 @@
     }
 
-    LOG("ServiceManagementLayer:: Received Set Active Mission command: %i\n",missID);
+    LOG("ServiceManagementLayer:: Received Set Active Mission command: %i.\n",missID);
     //For each service in the mission
-    for(int i = 0; i < miss[activeMission].numServices; i++)
+    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("while")){
+	if(miss[activeMission].services[i].name.compare("if") && miss[activeMission].services[i].name.compare("while") && \
+	   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
     	    strcpy(_services_DB->command, "select ");
@@ -610,5 +616,5 @@
 	             miss[activeMission].services[i].componentID =  sqlite3_column_int(pStatement, 0); 
                 else {
-	            printf("services_DB:: Mission requires service not provided by any connected component.\n");
+	            printf("services_DB:: Mission requires service %s not provided by any connected component.\n",miss[activeMission].services[i].name.c_str());
 		    rc=31337;
 	        }
@@ -616,5 +622,5 @@
 	        printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_services_DB->command);
             }
-
+		//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;
@@ -625,5 +631,5 @@
  
     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,5 +648,5 @@
 ServiceManagementLayer::TransactData(int32_t sourceID)
 {
-    printf("TransactData Occuring\n");
+    LOG("ServiceManagementLayer:: Data transaction occuring.\n");
     char buffer[256];
     std::string data;
@@ -648,10 +654,64 @@
     int i = 0;
     fd_set sockSet;
+    char *token;
     struct timeval selTimeout;
+
+   //Send a message directly to the shell
+   //printf("name = %s\n", miss[activeMission].services[sourceID].name.c_str());
+   if(miss[activeMission].services[sourceID].name.find("shell")!=string::npos)
+   {
+	//printf("caught shell\n");
+	//If the name on the output doesn't start with "~", search the DB to find the output that should be returned
+	if(miss[activeMission].services[sourceID].output[0].find("~") == string::npos){
+	   // printf("taken1\n");
+	    memset(buffer, 0 , 256);
+            strcpy(_data_DB->command, "select ");
+   	    strcat(_data_DB->command, _data_DB->tablename);
+    	    strcat(_data_DB->command, ".* from ");
+    	    strcat(_data_DB->command, _data_DB->tablename);
+    	    strcat(_data_DB->command, " where Tag==");
+	    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].services[sourceID].output[0].c_str());
+            sqlite3_stmt * pStatement;
+            int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+            if (rc == SQLITE_OK){ 
+                if (sqlite3_step(pStatement) == SQLITE_ROW)
+	            data.append((const char*) sqlite3_column_text(pStatement, 1)); 
+                else {
+	            printf("data_DB:: Data not yet in DB.\n");
+		    rc=31337;
+	        }
+            } 
+	    else {
+	        printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+            }
+            sqlite3_finalize(pStatement);
+	    token = strtok((char *)data.c_str(), "@");
+	    SendMessage(shellSocketFD, token);
+	    token = strtok(NULL, "@");
+	    while(token){
+	        SendMessage(shellSocketFD, token);
+	        token = strtok(NULL, "@");
+	    }
+	}
+	//Otherwise, just return the value specified by output TODO (in 1 - <output name> - <output name> format)
+	else{
+	    printf("taken2\n");
+	    memset(buffer, 0, 256);
+	    SendMessage(shellSocketFD, "1");
+	    miss[activeMission].services[sourceID].output[0].erase(0);
+	    SendMessage(shellSocketFD, miss[activeMission].services[sourceID].output[0].c_str());
+	    SendMessage(shellSocketFD, miss[activeMission].services[sourceID].output[0].c_str());
+	}
+	//printf("done shell\n");
+	return;
+   } 
+
+
+
     //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());
     //Find and load the input data
-    while(i < 3 && !miss[activeMission].services[sourceID].input[i].empty()){
+    while(i < 5 && !miss[activeMission].services[sourceID].input[i].empty()){
 	//printf("pulling input data out of DB for ID#=%d\n", sourceID);
         strcpy(_data_DB->command, "select ");
@@ -660,5 +720,8 @@
     	strcat(_data_DB->command, _data_DB->tablename);
     	strcat(_data_DB->command, " where Tag==");
-	sprintf(_data_DB->command, "%s'%s';", _data_DB->command, miss[activeMission].services[sourceID].input[i].c_str());
+	char temp[100];
+	strcpy(temp, miss[activeMission].services[sourceID].input[i].c_str());
+	char *temp2 = strtok(temp, "+");
+	sprintf(_data_DB->command, "%s'%s';", _data_DB->command, temp2);
         sqlite3_stmt * pStatement;
         int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
@@ -674,17 +737,55 @@
 	    printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
         }
-
         sqlite3_finalize(pStatement);
 	char *data_ch = (char *) data.c_str();
+	int32_t numStatements = 0;
+	temp2 = strtok(NULL, "+");
+	while(temp2){
+	    numStatements++;
+	    temp2 = strtok(NULL, "+");
+	}
+	//printf("here1%d\n", numStatements);
+	char temp4[10];
+	memset(temp4, 0, 10);
+	sprintf(temp4, "%d", numStatements);
 	//Tokenize the data and pass it along
-	char *token = strtok(data_ch, "@");
+	//printf("here2 %s\n", temp4);
+	if(strstr(miss[activeMission].services[sourceID].input[i].c_str(), "+")){
+	    SendMessage(miss[activeMission].services[sourceID].socketFD, temp4);
+	    token = strtok(data_ch, "@");
+	}
+	else{
+	    token = strtok(data_ch, "@");
+	    SendMessage(miss[activeMission].services[sourceID].socketFD, token);
+	}
+	token = strtok(NULL, "@");
+
+
+	//Either have to send whole block of memory or just one piece
 	while(token){
-	    SendMessage(miss[activeMission].services[sourceID].socketFD, token);
+	    if(strstr(miss[activeMission].services[sourceID].input[i].c_str(), token))
+	    {
+		//printf("1tokenizing %s %s!\n", miss[activeMission].services[sourceID].input[i].c_str(), token);
+	        SendMessage(miss[activeMission].services[sourceID].socketFD, token);
+	        token = strtok(NULL, "@");
+	        SendMessage(miss[activeMission].services[sourceID].socketFD, token);
+	    }
+	    else if(!strstr(miss[activeMission].services[sourceID].input[i].c_str(), "+"))
+	    {
+		//printf("t not detected tokenizing %s %s!\n", miss[activeMission].services[sourceID].input[i].c_str(), token);
+	        SendMessage(miss[activeMission].services[sourceID].socketFD, token);
+	        token = strtok(NULL, "@");
+	        SendMessage(miss[activeMission].services[sourceID].socketFD, token);
+	    }
+	    else{
+		//printf("3tokenizing %s %s!\n", miss[activeMission].services[sourceID].input[i].c_str(), token);
+	        token = strtok(NULL, "@");}
 	    token = strtok(NULL, "@");
 	}
+	//printf("done\n");
 	//printf("done pulling input data out of DB for ID#=%d\n", sourceID);
 	i++;
-    }
-
+	data.clear();
+    }
     int32_t j = 0;
     FD_ZERO(&sockSet);
@@ -695,17 +796,35 @@
     //Use select command to force wait for processing to finish
     select(miss[activeMission].services[sourceID].socketFD + 1, &sockSet, NULL, NULL, &selTimeout);
-    while(j < 3 && !miss[activeMission].services[sourceID].output[j].empty()){
+	//printf("done\n");
+   //TODO rewrite part of data on output?
+   //TODO true false format?
+    while(j < 5 && !miss[activeMission].services[sourceID].output[j].empty()){
 	int rc;
-        data.clear();
-	while(true){
+	memset(buffer, 0, 256);
+	ReadMessage(miss[activeMission].services[sourceID].socketFD, buffer);
+       // printf("waiting\n");	    
+	data.append(buffer);
+	data.append("@");
+	int t = atoi(buffer);
+	//printf("%d\n", t);
+	for(int k = 0; k < t; k++){
 	    //Read the data incrementally and deliminate it with the "@" symbol
 	    memset(buffer, 0, 256);
 	    ReadMessage(miss[activeMission].services[sourceID].socketFD, buffer);
-	    if(strcmp(buffer, "output_finished")==0)
-		break;
-	    data.append(buffer);
-	    data.append("@");;
+	    if(!strcmp(buffer, "TF")){
+		ReadMessage(miss[activeMission].services[sourceID].socketFD, buffer);
+	        data.append(buffer);
+	        data.append("@");
+	    }
+	    else{
+	        data.append(buffer);
+	        data.append("@");
+	        memset(buffer, 0, 256);
+	        ReadMessage(miss[activeMission].services[sourceID].socketFD, buffer);
+	        data.append(buffer);
+	        data.append("@");
+	    }
 	}
-	printf("SML: putting output data into DB for ID#=%d\n", sourceID);
+	//printf("SML: putting output data into DB for ID#=%d\n", sourceID);
 
 	strcpy(_data_DB->command, "insert or replace into ");
@@ -725,11 +844,13 @@
    	if( rc!=SQLITE_OK && rc!=101 )
    	    fprintf(stderr, "SQL error: %s\n", errorMsg);
-        printf("SML: done putting ouptut data into DB for ID#='%d', data=%s\n", sourceID, data.c_str());
+        //printf("S: done putting ouptut data into DB for ID#='%d', data=%s\n", sourceID, data.c_str());
 	j++;
-    }
-    printf("done transact data!\n");
-
-
-    printf("\n\n\n");
+        data.clear();
+    }
+    //printf("done transact data!\n");
+    LOG("ServiceManagementLayer:: Finished with data transaction.\n");
+
+
+    /*printf("\n\n\n");
     // generate commandi
     strcpy(_data_DB->command, "select ");
@@ -745,5 +866,5 @@
         fprintf(stderr, "SQL error: %s\n", errorMsg);
     printf("database %s, table %s:\n", _data_DB->filename, _data_DB->tablename);
-    printf("\n\n\n");
+    printf("\n\n\n");*/
 }
 
@@ -775,34 +896,39 @@
  * -IMPORTANT: DB uses '@' to seperate individual statements; using '@' in the data stream will result in incorrect behavior
  */
+
+//IF-IF-IF
+//IF-IF-WHILE
+//IF-WHILE
+//WHILE
 void
 ServiceManagementLayer::PerformActiveMission()
 {
-    int i = 0;
-    std::string data;
+    uint16_t i = 0;
+    std::string data_param, data_obsv, data;
     std::string input;
     std::string check;
     char buffer[256];
-    fd_set sockSet;
     char* cols[] = {(char *)"Tag", (char *)"Data"};
-    struct timeval selTimeout;
     //Get the inputs
-    while(i < 3 && !miss[activeMission].input[i].empty()){
-	    //New data being added to DB
-	printf("inserting data from shell\n");
-	int rc;
-	while(true){
-	    memset(buffer, 0, 256);
-	    ReadMessage(shellSocketFD, buffer);
-	    if(strcmp(buffer, "input_finished")==0)
-		break;
-	    data.append(buffer);
-	    data.append("@");
-	    /*FD_ZERO(&sockSet);
-	    FD_SET(miss[activeMission].services[sourceID].socketFD, &sockSet);
-	    selTimeout.tv_sec = 0;
-	    selTimeout.tv_usec = 50000;
-	    //Check if there is a message on the CE socket ready to be processed
-	    rc=select(miss[activeMission].services[sourceID].socketFD + 1, &sockSet, NULL, NULL, &selTimeout);*/
-	}//while(rc!=0);;
+    memset(buffer, 0, 256);
+    ReadMessage(shellSocketFD, buffer);
+    LOG("ServiceManagementLayer:: Received PerformActiveMission command.\n");
+        data_obsv.append(buffer);
+        data_obsv.append("@");
+
+    int t = atoi(buffer);
+   // printf("****%d****\n", t);
+    for(int k = 0; k < t; k++) {
+        memset(buffer, 0, 256);
+        ReadMessage(shellSocketFD, buffer);
+        data_obsv.append(buffer);
+        data_obsv.append("@");
+   
+        memset(buffer, 0, 256);
+        ReadMessage(shellSocketFD, buffer);
+        data_obsv.append(buffer);
+        data_obsv.append("@");
+	//printf("data=%s\n", data_obsv.c_str());
+    }
 
 	strcpy(_data_DB->command, "insert into ");
@@ -813,17 +939,65 @@
 	strcat(_data_DB->command, cols[1]);
 	strcat(_data_DB->command, ") ");
-	strcat(_data_DB->command, " values('");
-	strcat(_data_DB->command, miss[activeMission].input[i].c_str());
-	strcat(_data_DB->command, "', '");
-	strcat(_data_DB->command, data.c_str());
+	strcat(_data_DB->command, " values('observables', '");
+	strcat(_data_DB->command, data_obsv.c_str());
 	strcat(_data_DB->command, "');");
     	char *errorMsg;
+   	int rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg);
+   	if( rc!=SQLITE_OK && rc!=101 )
+   	    fprintf(stderr, "SQL error: %s\n", errorMsg);
+
+    /* Receive Set of Parameters */
+    memset(buffer, 0, 256);
+    ReadMessage(shellSocketFD, buffer);
+        data_param.append(buffer);
+        data_param.append("@");
+    t=atoi(buffer);
+    for(int k = 0; k < t; k++) {
+        memset(buffer, 0, 256);
+        ReadMessage(shellSocketFD, buffer);
+        data_param.append(buffer);
+        data_param.append("@");
+   
+        memset(buffer, 0, 256);
+        ReadMessage(shellSocketFD, buffer);
+        data_param.append(buffer);
+        data_param.append("@");
+    }
+	//printf("data=%s\n", data_param.c_str());
+
+	strcpy(_data_DB->command, "insert into ");
+    	strcat(_data_DB->command, _data_DB->tablename); 
+      	strcat(_data_DB->command, " (");
+    	strcat(_data_DB->command, cols[0]);
+    	strcat(_data_DB->command, ", ");
+	strcat(_data_DB->command, cols[1]);
+	strcat(_data_DB->command, ") ");
+	strcat(_data_DB->command, " values('paramaters', '");
+	strcat(_data_DB->command, data_param.c_str());
+	strcat(_data_DB->command, "');"); 
    	rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg);
    	if( rc!=SQLITE_OK && rc!=101 )
    	    fprintf(stderr, "SQL error: %s\n", errorMsg);
-        printf("SML: finished adding data from shell on input %d\n", i);
-        i++;
-
-    }
+
+
+    /*printf("\n\n\n");
+    // generate commandi
+    strcpy(_data_DB->command, "select ");
+    strcat(_data_DB->command, _data_DB->tablename);
+    strcat(_data_DB->command, ".* from ");
+    strcat(_data_DB->command, _data_DB->tablename);
+    strcat(_data_DB->command, ";");
+
+    // execute print (select all)  command  
+    rc = sqlite3_exec(_data_DB->db, _data_DB->command, callback, 0, &errorMsg);
+    if( rc!=SQLITE_OK && rc!=101 )
+        fprintf(stderr, "SQL error: %s\n", errorMsg);
+    printf("database %s, table %s:\n", _data_DB->filename, _data_DB->tablename);
+    printf("\n\n\n");*/
+
+
+
+
+   // printf("done\n");
     i=0;
     data.clear();
@@ -832,16 +1006,16 @@
 	if(miss[activeMission].services[i].name.compare("if")==0)
 	{
-	    printf("if detected\n");
+	   // printf("L0:if detected\n");
 	    input.clear();
 	    check.clear();
 	    if(!miss[activeMission].services[i].input[0].empty()){
 		input=miss[activeMission].services[i].input[0];
-		check = "true@";
+		check = "1@true@";
 	    }
 	    else{
 		input=miss[activeMission].services[i].input[1];
-		check = "false@";
+		check = "1@false@";
 	    }
-            printf("input=%s\n", input.c_str());
+            //printf("L0:input=%s\n", input.c_str());
 
             strcpy(_data_DB->command, "SELECT ");
@@ -852,5 +1026,5 @@
 	    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str());
             sqlite3_stmt * pStatement;
-            int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+            rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
             if (rc == SQLITE_OK){ 
                 if (sqlite3_step(pStatement) == SQLITE_ROW)
@@ -865,18 +1039,222 @@
 
     	    sqlite3_finalize(pStatement);
-	    printf("data=%s, check=%s\n", data.c_str(), check.c_str());
+	   // printf("L0:data=%s, check=%s\n", data.c_str(), check.c_str());
+	   // printf("L0:--- %d  %d---\n", miss[activeMission].services[i].num_conds, i);
 	    if(data.compare(check)==0){
-		printf("if taken\n");
-		for(int k = i + 1; k <= i + miss[activeMission].services[i].num_conds; k++)
-		    TransactData(k);
+		//printf("L0:if taken\n");
+		
+		for(int k = i + 1; k <= i + miss[activeMission].services[i].num_conds; k++){
+			//printf("k=%d\n", k);
+			if(miss[activeMission].services[k].name.compare("if")==0)
+			{
+			    //printf("L1:if detected\n");
+			    input.clear();
+			    check.clear();
+			    if(!miss[activeMission].services[k].input[0].empty()){
+				input=miss[activeMission].services[k].input[0];
+				check = "1@true@";
+			    }
+			    else{
+				input=miss[activeMission].services[k].input[1];
+				check = "1@false@";
+			    }
+			    //printf("input=%s\n", input.c_str());
+
+			    strcpy(_data_DB->command, "SELECT ");
+		   	    strcat(_data_DB->command, _data_DB->tablename);
+			    strcat(_data_DB->command, ".* from ");
+			    strcat(_data_DB->command, _data_DB->tablename);
+		    	    strcat(_data_DB->command, " where Tag==");
+			    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str());
+			    sqlite3_stmt * pStatement;
+			    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+			    if (rc == SQLITE_OK){ 
+				if (sqlite3_step(pStatement) == SQLITE_ROW)
+				     data = (const char *) sqlite3_column_text(pStatement, 1); 
+				else {
+					printf("1 data_DB:: Data not yet in DB. %d\n", k);
+					rc=31337;
+				}
+			    } else {
+				printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+			    }
+
+		    	    sqlite3_finalize(pStatement);
+			    //printf("L1:data=%s, check=%s\n", data.c_str(), check.c_str());
+			    //printf("L1:--- %d  %d---\n", miss[activeMission].services[i].num_conds, k);
+			    if(data.compare(check)==0){
+				//printf("L1:if taken\n");
+				for(int j = k + 1; j <= k + miss[activeMission].services[k].num_conds; j++){
+				   // printf("j=%d\n", k);
+				    //printf("name!!!! = %s\n", miss[activeMission].services[j].name.c_str());
+				    if(miss[activeMission].services[j].name.compare("if")==0)
+				    {
+					   // printf("L2:if detected\n");
+					    input.clear();
+					    check.clear();
+					    if(!miss[activeMission].services[j].input[0].empty()){
+						input=miss[activeMission].services[j].input[0];
+						check = "1@true@";
+					    }
+					    else{
+						input=miss[activeMission].services[j].input[1];
+						check = "1@false@";
+					    }
+					    //printf("L2:input=%s\n", input.c_str());
+
+					    strcpy(_data_DB->command, "SELECT ");
+				   	    strcat(_data_DB->command, _data_DB->tablename);
+					    strcat(_data_DB->command, ".* from ");
+					    strcat(_data_DB->command, _data_DB->tablename);
+				    	    strcat(_data_DB->command, " where Tag==");
+					    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str());
+					    sqlite3_stmt * pStatement;
+					    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+					    if (rc == SQLITE_OK){ 
+						if (sqlite3_step(pStatement) == SQLITE_ROW)
+						     data = (const char *) sqlite3_column_text(pStatement, 1); 
+						else {
+							printf("1 data_DB:: Data not yet in DB. %d\n", k);
+							rc=31337;
+						}
+					    } else {
+						printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+					    }
+
+				    	    sqlite3_finalize(pStatement);
+					   // printf("L2:data=%s, check=%s\n", data.c_str(), check.c_str());
+					   // printf("L2:--- %d  %d---\n", miss[activeMission].services[i].num_conds, k);
+					    if(data.compare(check)==0){
+						//printf("L2:if taken\n");
+						for(int p = j + 1; p <= j + miss[activeMission].services[j].num_conds; p++)
+						    TransactData(p);
+					    }
+					    else{
+						//printf("L2:if not taken %d\n", miss[activeMission].services[j].num_conds);
+					    }
+				   	j+=miss[activeMission].services[j].num_conds;
+				    }
+
+				    else if(miss[activeMission].services[j].name.compare("while")==0)
+				    {
+			    		//printf("while detected\n");
+					while(true){
+					    data.clear();
+					    input.clear();
+					    check.clear();
+					    if(!miss[activeMission].services[j].input[0].empty()){
+						input=miss[activeMission].services[j].input[0];
+						check = "1@true@";
+					    }
+					    else{
+						input=miss[activeMission].services[j].input[1];
+						check = "1@false@";
+					    }
+					    strcpy(_data_DB->command, "select ");
+				   	    strcat(_data_DB->command, _data_DB->tablename);
+				    	    strcat(_data_DB->command, ".* from ");
+				    	    strcat(_data_DB->command, _data_DB->tablename);
+				    	    strcat(_data_DB->command, " where Tag==");
+					    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str());
+					    sqlite3_stmt * pStatement;
+					    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+					    if (rc == SQLITE_OK){ 
+						if (sqlite3_step(pStatement) == SQLITE_ROW)
+						    data = (const char *) sqlite3_column_text(pStatement, 1); 
+						else 
+						    printf("1 data_DB:: Data not yet in DB. %d\n", i);
+					    } 
+					    else {
+						printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+					    }
+
+				    	    sqlite3_finalize(pStatement);
+					    printf("data=%s, check=%s\n", data.c_str(), check.c_str());
+					    if(data.compare(check)==0){
+						printf("while taken\n");
+						for(int p = j + 1; p <= j + miss[activeMission].services[j].num_conds; p++)
+						    TransactData(p);
+					    }
+					    else{
+						printf("while not taken %d\n", miss[activeMission].services[k].num_conds);
+				   		 j+=miss[activeMission].services[j].num_conds;
+						break;
+					    }
+					}
+				    }
+				    else{
+					printf("L2: nocond transact\n");
+				  	TransactData(j);}
+				    j++;
+				}
+			    }
+			    else{
+				//printf("if not taken %d\n", miss[activeMission].services[k].num_conds);
+			    }
+			    k+=miss[activeMission].services[k].num_conds;
+			}
+			else if(miss[activeMission].services[k].name.compare("while")==0)
+			{
+			    //printf("while detected\n");
+			    while(true){
+				    data.clear();
+				    input.clear();
+				    check.clear();
+				    if(!miss[activeMission].services[k].input[0].empty()){
+					input=miss[activeMission].services[k].input[0];
+					check = "1@true@";
+				    }
+				    else{
+					input=miss[activeMission].services[k].input[1];
+					check = "1@false@";
+				    }
+				    strcpy(_data_DB->command, "select ");
+			   	    strcat(_data_DB->command, _data_DB->tablename);
+			    	    strcat(_data_DB->command, ".* from ");
+			    	    strcat(_data_DB->command, _data_DB->tablename);
+			    	    strcat(_data_DB->command, " where Tag==");
+				    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str());
+				    sqlite3_stmt * pStatement;
+				    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+				    if (rc == SQLITE_OK){ 
+					if (sqlite3_step(pStatement) == SQLITE_ROW)
+					    data = (const char *) sqlite3_column_text(pStatement, 1); 
+					else 
+					    printf("1 data_DB:: Data not yet in DB. %d\n", i);
+				    } 
+				    else {
+					printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+				    }
+
+			    	    sqlite3_finalize(pStatement);
+				    printf("data=%s, check=%s\n", data.c_str(), check.c_str());
+				    if(data.compare(check)==0){
+					printf("while taken\n");
+					for(int k = i + 1; k <= i + miss[activeMission].services[i].num_conds; k++)
+					    TransactData(k);
+				    }
+				    else{
+					printf("while not taken %d\n", miss[activeMission].services[k].num_conds);
+				        k+=miss[activeMission].services[k].num_conds;
+					break;
+				    }
+			    }
+			}
+			else{
+			    //printf("L1: nocond transact\n");
+			    TransactData(k);}
+			k++;
+		}
 	    }
 	    else{
-		i+=miss[activeMission].services[i].num_conds;
-		printf("if not taken %d\n", miss[activeMission].services[i].num_conds);
+		printf("La2 if not taken %d\n", miss[activeMission].services[i].num_conds);
 	    }
+	    //printf("i=%d\n", i);
+	    i+=miss[activeMission].services[i].num_conds;
+	    //printf("i=%d\n", i);
 	}
 	else if(miss[activeMission].services[i].name.compare("while")==0)
 	{
-	    printf("while detected\n");
+	    //printf("while detected\n");
 	    while(true){
 		    data.clear();
@@ -885,9 +1263,9 @@
 		    if(!miss[activeMission].services[i].input[0].empty()){
 			input=miss[activeMission].services[i].input[0];
-			check = "true@";
+			check = "1@true@";
 		    }
 		    else{
 			input=miss[activeMission].services[i].input[1];
-			check = "false@";
+			check = "1@false@";
 		    }
 		    strcpy(_data_DB->command, "select ");
@@ -898,5 +1276,5 @@
 		    sprintf(_data_DB->command, "%s'%s';", _data_DB->command, input.c_str());
 		    sqlite3_stmt * pStatement;
-		    int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
+		    rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
 		    if (rc == SQLITE_OK){ 
 		        if (sqlite3_step(pStatement) == SQLITE_ROW)
@@ -910,5 +1288,5 @@
 
 	    	    sqlite3_finalize(pStatement);
-		    printf("data=%s, check=%s\n", data.c_str(), check.c_str());
+		    //printf("data=%s, check=%s\n", data.c_str(), check.c_str());
 		    if(data.compare(check)==0){
 			printf("while taken\n");
@@ -917,6 +1295,6 @@
 		    }
 		    else{
+			printf("while not taken %d\n", miss[activeMission].services[i].num_conds);
 			i+=miss[activeMission].services[i].num_conds;
-			printf("while not taken %d\n", miss[activeMission].services[i].num_conds);
 			break;
 		    }
@@ -924,12 +1302,14 @@
 	}
 	else{
+	   // printf("L0 nocond transact\n");
 	    TransactData(i);}
 	i++;
+	//printf("i=%d\n", i);
     }
     i=0;
     data.clear();
     //get the ouptuts
-    while(i < 3 && !miss[activeMission].output[i].empty()){
-	printf("sending output data to shell\n");
+    while(i < 5 && !miss[activeMission].output[i].empty()){
+	//printf("sending output data to shell\n");
         strcpy(_data_DB->command, "select ");
    	strcat(_data_DB->command, _data_DB->tablename);
@@ -941,5 +1321,5 @@
         int rc = sqlite3_prepare_v2(_data_DB->db, _data_DB->command, -1, &pStatement, NULL);
         if (rc == SQLITE_OK){ 
-            if (sqlite3_step(pStatement) == SQLITE_ROW)
+            if (sqlite3_step(pStatement) == SQLITE_ROW) 
 	         data.append((const char*) sqlite3_column_text(pStatement, 1)); 
             else {
@@ -950,15 +1330,23 @@
 	else {
 	    printf("services_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
-        }
+        } 
+        //printf("here %s\n", data.c_str());
         sqlite3_finalize(pStatement);
 	char *data_ch = (char *) data.c_str();
 	char *token = strtok(data_ch, "@");
+	SendMessage(shellSocketFD, token);
+	token = strtok(NULL, "@");
 	while(token){
 	    SendMessage(shellSocketFD, token);
+	    //printf("token1 = %s\n", token);
+	    token = strtok(NULL, "@");
+	    SendMessage(shellSocketFD, token);
+	    //printf("token2 = %s\n", token);
 	    token = strtok(NULL, "@");
 	}
 	i++;
-	printf("done sending output data to shell\n");
-    }
+        data.clear();
+    }
+    LOG("ServiceManagementLayer:: Done sending output data to shell from PerformActiveMission.\n");
 }
 
@@ -1023,4 +1411,446 @@
 {
     TiXmlElement *pMission;
+    TiXmlElement *pService;
+    TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3, *pChild4;
+    TiXmlHandle hRoot(0);
+    //printf("ServiceManagementLayer:: Loading Configuration.\n");
+    TiXmlDocument doc(".");
+    doc.LoadFile(SML_Config);
+    bool loadOkay = doc.LoadFile();
+    if(!loadOkay)
+        printf("Loading SML configuration failed: %s\n", SML_Config);
+
+    TiXmlHandle hDoc(&doc);
+    
+    pMission = hDoc.FirstChildElement().Element();
+
+    if(!pMission) 
+        printf("No valid root!");
+
+    hRoot = TiXmlHandle(pMission);
+    pService = pMission->FirstChildElement();
+    int32_t mission_num = 0;
+    //Iterate through the missions
+    for(pChild0 = pMission->FirstChildElement(); pChild0 ; \
+        pChild0 = pChild0->NextSiblingElement()) 
+    {
+	int32_t service_num = 0;
+	uint16_t cond_array[] = {0, 0, 0};
+	//printf("mission_num = %d\n", mission_num);
+	//memset(cond_array, 0, 2);
+	
+    	for(pChild1  = pChild0->FirstChildElement(); pChild1; \
+      	    pChild1  = pChild1->NextSiblingElement()) 
+    	{
+	    int32_t conditional_0 = service_num;
+	    for(pChild2 = pChild1->FirstChildElement(); \
+		pChild2; pChild2 = pChild2->NextSiblingElement())
+	    {
+		service_num++;
+		int32_t conditional_1 = service_num;
+		for(pChild3 = pChild2->FirstChildElement(); \
+		    pChild3; pChild3 = pChild3 ->NextSiblingElement())
+		{
+		    service_num++;
+		    int32_t conditional_2 = service_num;
+			for(pChild4 = pChild3->FirstChildElement(); \
+			    pChild4; pChild4 = pChild4 ->NextSiblingElement())
+			{
+			    service_num++;
+			    mList[mission_num].services[service_num].name = pChild4->Attribute("name");
+		            if(pChild4->Attribute("input1"))
+				mList[mission_num].services[service_num].input[0] = pChild4->Attribute("input1");
+		            if(pChild4->Attribute("input2"))
+				mList[mission_num].services[service_num].input[1] = pChild4->Attribute("input2");
+		            if(pChild4->Attribute("input3"))
+				mList[mission_num].services[service_num].input[2] = pChild4->Attribute("input3");
+		            if(pChild4->Attribute("input4"))
+				mList[mission_num].services[service_num].input[3] = pChild4->Attribute("input4");
+		            if(pChild4->Attribute("input5"))
+				mList[mission_num].services[service_num].input[4] = pChild4->Attribute("input5");
+		            if(pChild4->Attribute("output1"))
+				mList[mission_num].services[service_num].output[0] = pChild4->Attribute("output1");
+		            if(pChild4->Attribute("output2"))
+				mList[mission_num].services[service_num].output[1] = pChild4->Attribute("output2");
+		            if(pChild4->Attribute("output3"))
+				mList[mission_num].services[service_num].output[2] = pChild4->Attribute("output3");
+			    if(pChild4->Attribute("output4"))
+				mList[mission_num].services[service_num].output[3] = pChild4->Attribute("output4");
+		            if(pChild4->Attribute("output5"))
+				mList[mission_num].services[service_num].output[4] = pChild4->Attribute("output5");
+			    cond_array[2]++;
+			}
+			if(!strcmp(pChild3->Value(), "shell")) {
+			    mList[mission_num].services[conditional_2].name = pChild3->Value();
+			    mList[mission_num].services[conditional_2].output[0] = pChild3->Attribute("output1");
+		//printf("2shell detected\n");
+			}
+			else if(conditional_2 != service_num){
+			    mList[mission_num].services[conditional_2].name = pChild3->Value();
+		            if(pChild3->Attribute("input_t"))
+				mList[mission_num].services[conditional_2].input[0] = pChild3->Attribute("input_t");
+		            if(pChild3->Attribute("input_f"))
+				mList[mission_num].services[conditional_2].input[1] = pChild3->Attribute("input_f");
+			}
+			else{
+			    mList[mission_num].services[service_num].name = pChild3->Attribute("name");
+		            if(pChild3->Attribute("input1"))
+				mList[mission_num].services[service_num].input[0] = pChild3->Attribute("input1");
+		            if(pChild3->Attribute("input2"))
+				mList[mission_num].services[service_num].input[1] = pChild3->Attribute("input2");
+		            if(pChild3->Attribute("input3"))
+				mList[mission_num].services[service_num].input[2] = pChild3->Attribute("input3");
+		            if(pChild3->Attribute("input4"))
+				mList[mission_num].services[service_num].input[3] = pChild3->Attribute("input4");
+		            if(pChild3->Attribute("input5"))
+				mList[mission_num].services[service_num].input[4] = pChild3->Attribute("input5");
+		            if(pChild3->Attribute("output1"))
+				mList[mission_num].services[service_num].output[0] = pChild3->Attribute("output1");
+		            if(pChild3->Attribute("output2"))
+				mList[mission_num].services[service_num].output[1] = pChild3->Attribute("output2");
+		            if(pChild3->Attribute("output3"))
+				mList[mission_num].services[service_num].output[2] = pChild3->Attribute("output3");
+			    if(pChild3->Attribute("output4"))
+				mList[mission_num].services[service_num].output[3] = pChild3->Attribute("output4");
+		            if(pChild3->Attribute("output5"))
+				mList[mission_num].services[service_num].output[4] = pChild3->Attribute("output5");
+			}
+			mList[mission_num].services[conditional_2].num_conds = cond_array[2];
+			cond_array[1]+=cond_array[2]+1;
+			//printf("cond_array[2]%d\n", cond_array[2]);
+			cond_array[2] = 0;
+
+
+		}
+		//printf("here\n");
+		if(!strcmp(pChild2->Value(), "shell")) {
+		    mList[mission_num].services[conditional_1].name = pChild2->Value();
+		    mList[mission_num].services[conditional_1].output[0] = pChild2->Attribute("output1");
+		//printf("1shell detected\n");
+		}
+		else if(conditional_1 != service_num){
+		    mList[mission_num].services[conditional_1].name = pChild2->Value();
+                    if(pChild2->Attribute("input_t"))
+			mList[mission_num].services[conditional_1].input[0] = pChild2->Attribute("input_t");
+                    if(pChild2->Attribute("input_f"))
+			mList[mission_num].services[conditional_1].input[1] = pChild2->Attribute("input_f");
+		}
+		else{
+		    mList[mission_num].services[service_num].name = pChild2->Attribute("name");
+                    if(pChild2->Attribute("input1"))
+			mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input1");
+                    if(pChild2->Attribute("input2"))
+			mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input2");
+                    if(pChild2->Attribute("input3"))
+			mList[mission_num].services[service_num].input[2] = pChild2->Attribute("input3");
+                    if(pChild2->Attribute("input4"))
+			mList[mission_num].services[service_num].input[3] = pChild2->Attribute("input4");
+                    if(pChild2->Attribute("input5"))
+			mList[mission_num].services[service_num].input[4] = pChild2->Attribute("input5");
+                    if(pChild2->Attribute("output1"))
+			mList[mission_num].services[service_num].output[0] = pChild2->Attribute("output1");
+                    if(pChild2->Attribute("output2"))
+			mList[mission_num].services[service_num].output[1] = pChild2->Attribute("output2");
+                    if(pChild2->Attribute("output3"))
+			mList[mission_num].services[service_num].output[2] = pChild2->Attribute("output3");
+		    if(pChild2->Attribute("output4"))
+			mList[mission_num].services[service_num].output[3] = pChild2->Attribute("output4");
+                    if(pChild2->Attribute("output5"))
+			mList[mission_num].services[service_num].output[4] = pChild2->Attribute("output5");
+		}
+
+		mList[mission_num].services[conditional_1].num_conds = cond_array[1];
+		cond_array[0]+=cond_array[1]+1;
+		//printf("cond_array[1]%d\n", cond_array[1]);
+		cond_array[1] = 0;
+	    }
+	    
+	    if(!strcmp(pChild1->Value(), "shell")) {
+		//printf("shell detected\n");
+	        mList[mission_num].services[conditional_0].name = pChild1->Value();
+	        mList[mission_num].services[conditional_0].output[0] = pChild1->Attribute("output1");
+	    }
+	    else if(conditional_0 != service_num){
+		mList[mission_num].services[conditional_0].name = pChild1->Value();
+                    if(pChild1->Attribute("input_t"))
+			mList[mission_num].services[conditional_0].input[0] = pChild1->Attribute("input_t");
+                    if(pChild1->Attribute("input_f"))
+			mList[mission_num].services[conditional_0].input[1] = pChild1->Attribute("input_f");
+		//printf("---input_t=%s\n", mList[mission_num].services[conditional_0].input[0].c_str());
+	    }
+	    else{
+		mList[mission_num].services[conditional_0].name = pChild1->Attribute("name");
+		//printf("name=%s\n", mList[mission_num].services[conditional_0].name.c_str());
+                if(pChild1->Attribute("input1"))
+	            mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input1");
+                if(pChild1->Attribute("input2"))
+		    mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input2");
+                if(pChild1->Attribute("input3"))
+		    mList[mission_num].services[service_num].input[2] = pChild1->Attribute("input3");
+                if(pChild1->Attribute("input4"))
+		    mList[mission_num].services[service_num].input[3] = pChild1->Attribute("input4");
+                if(pChild1->Attribute("input5"))
+		    mList[mission_num].services[service_num].input[4] = pChild1->Attribute("input5");
+                if(pChild1->Attribute("output1"))
+		    mList[mission_num].services[service_num].output[0] = pChild1->Attribute("output1");
+                if(pChild1->Attribute("output2"))
+		    mList[mission_num].services[service_num].output[1] = pChild1->Attribute("output2");
+                if(pChild1->Attribute("output3"))
+		    mList[mission_num].services[service_num].output[2] = pChild1->Attribute("output3");
+                if(pChild1->Attribute("output4"))
+		    mList[mission_num].services[service_num].output[3] = pChild1->Attribute("output4");
+                if(pChild1->Attribute("output5"))
+		    mList[mission_num].services[service_num].output[4] = pChild1->Attribute("output4");
+	    }
+
+	    mList[mission_num].services[conditional_0].num_conds = cond_array[0];
+            cond_array[0] = 0;
+	    service_num++;
+	}
+	//for(int i = 0; i < service_num; i++)
+	 //printf("ttt%d\n", mList[mission_num].services[i].num_conds);
+	
+	mList[mission_num].numServices = service_num;
+	mList[mission_num].name = pChild0->Attribute("name");
+	mList[mission_num].missionID = atoi(pChild0->Attribute("id"));
+        if(pChild0->Attribute("input1"))
+	    mList[mission_num].input[0] = pChild0->Attribute("input1");
+        if(pChild0->Attribute("input2"))
+	    mList[mission_num].input[1] = pChild0->Attribute("input2");
+        if(pChild0->Attribute("input3"))
+	    mList[mission_num].input[2] = pChild0->Attribute("input3");
+        if(pChild0->Attribute("input4"))
+	    mList[mission_num].input[3] = pChild0->Attribute("input4");
+        if(pChild0->Attribute("input5"))
+	    mList[mission_num].input[4] = pChild0->Attribute("input4");
+        if(pChild0->Attribute("output1"))
+	    mList[mission_num].output[0] = pChild0->Attribute("output1");
+        if(pChild0->Attribute("output2"))
+	    mList[mission_num].output[1] = pChild0->Attribute("output2");
+        if(pChild0->Attribute("output3"))
+	    mList[mission_num].output[2] = pChild0->Attribute("output3");
+        if(pChild0->Attribute("output4"))
+	    mList[mission_num].output[3] = pChild0->Attribute("output4");
+        if(pChild0->Attribute("output5"))
+	    mList[mission_num].output[4] = pChild0->Attribute("output5");
+	//printf("mis, input1=%s, output1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str());
+	//printf("NUMSERVICES = %d\n", mList[mission_num].numServices);
+	mission_num++;
+    }
+}
+
+/* CALLED BY: MessageHandler
+ * INPUTS: |ID| The ID number of the engine to be registered
+ * OUTPUTS: <none>
+ *
+ * DESCRIPTION: Sends a registration message onto the shell and sends the ACK back to the component
+ */
+void
+ServiceManagementLayer::RegisterCognitiveEngine(int32_t ID)
+{
+    //LOG("SML::regcogeng");
+    SendMessage(shellSocketFD, "register_engine_cognitive");
+
+   // printf("SSFD = %d\n", shellSocketFD);
+    LOG("ServiceManagementLayer:: CE registration message forwarded to shell.\n");
+    char buffer[256];
+    memset(buffer, 0, 256);
+    ReadMessage(shellSocketFD, buffer);
+    //printf("ServiceManagementLayer::buffer = %s\n", buffer);
+    SendMessage(CE_List[ID].FD, buffer);
+
+    TransferRadioConfiguration(ID);
+    memset(buffer, 0, 256);
+    //printf("start trans exp\n");
+    TransferExperience(ID);
+    memset(buffer, 0, 256);
+    numberOfCognitiveEngines++;
+    CE_Present = true;
+    //printf("done registering CE!\n");
+}
+
+/* CALLED BY: MessageHandler
+ * INPUTS: |ID| The ID number of the engine to have it's services deregistered
+ * OUTPUTS: <none>
+ *
+ * DESCRIPTION: Deletes individual services from the DB
+ * NOTE THAT this function only needs to be called if service deregistration is going
+ * to be done at a different time than component deregistration; it is handled
+ * more efficiently and directly during that deregistration process.
+ */
+void 
+ServiceManagementLayer::DeregisterServices(int32_t ID)
+{
+    char buffer[256];
+    memset(buffer, 0, 256);
+    ReadMessage(CE_List[ID].FD, buffer); 
+    strcpy(_services_DB->command, "DELETE FROM ");
+    strcat(_services_DB->command, _services_DB->tablename);
+    strcat(_services_DB->command, " WHERE ID_Num IN (SELECT");
+    sprintf(_services_DB->command, " %s %d",_services_DB->command, ID);
+    strcat(_services_DB->command, " FROM ");
+    strcat(_services_DB->command, _services_DB->tablename);
+    strcat(_services_DB->command, " WHERE Service_Name");
+    strcat(_services_DB->command, "==");
+    sprintf(_services_DB->command, "%s'%s');", _services_DB->command, buffer);
+    char *errorMsg;
+    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
+    if( rc!=SQLITE_OK && rc!=101 )
+        fprintf(stderr, "SQL error: %s\n", errorMsg);
+}
+
+/* CALLED BY: MessageHandler
+ * INPUTS: |ID| The ID number of the engine to have it's services deregistered
+ * OUTPUTS: <none>
+ *
+ * DESCRIPTION: Deletes the contact info for the cognitive engine, forwards a deregistration message to the shell
+ * Also, deletes the services from the DB
+ */
+void 
+ServiceManagementLayer::DeregisterCognitiveEngine(int32_t ID)
+{
+    LOG("ServiceManagementLayer:: CE deregistration message forwarded to shell.\n");
+
+    numberOfCognitiveEngines--;
+    if(numberOfCognitiveEngines == 0)
+        CE_Present = false;
+
+    SendMessage(shellSocketFD, "deregister_engine_cognitive");
+    char buffer[256];
+    memset(buffer, 0, 256);
+    ReadMessage(shellSocketFD, buffer);
+    SendMessage(CE_List[ID].FD, buffer);
+    if(strcmp("deregister_ack", buffer) != 0) {
+	ERROR(1, "SML:: Failed to close CE socket\n");
+    }
+
+    //Deregister the services
+    strcpy(_services_DB->command, "DELETE FROM ");
+    strcat(_services_DB->command, _services_DB->tablename);
+    strcat(_services_DB->command, " WHERE ");
+    strcat(_services_DB->command, "ID_Num");
+    strcat(_services_DB->command, "==");
+    sprintf(_services_DB->command, "%s%d;", _services_DB->command, ID);
+    char *errorMsg;
+    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
+    if( rc!=SQLITE_OK && rc!=101 )
+        fprintf(stderr, "SQL error: %s\n", errorMsg);
+
+
+    CE_List[ID].FD = -1;
+    CE_List[ID].ID_num = -1;
+
+    LOG("Cognitive Radio Shell:: CE Socket closed for engine #%d.\n", ID);
+}
+
+
+/* CALLED BY: test class
+ * INPUTS: <none>
+ * OUTPUTS: <none>
+ *
+ * DESCRIPTION: Sets up a server socket and listens for communication on either that or the shell socket
+ */
+void
+ServiceManagementLayer::StartSMLServer()
+{
+    //printf("Ready for CE Signal! (registration done)\n");
+    struct timeval selTimeout;
+    int32_t running = 1;
+    int32_t port, rc, new_sd = 1;
+    int32_t desc_ready = 1;
+		//If there is, call the MessageHandler with the Shell_Msg code of -1
+    fd_set sockSet, shellSet;
+
+    cogEngSrv = CreateTCPServerSocket(SMLport);
+    int32_t maxDescriptor = cogEngSrv;
+
+    if(InitializeTCPServerPort(cogEngSrv) == -1)
+        ERROR(1,"Error initializing primary port\n");
+
+    int i = 10000000;  //TODO change to "running" if endpoint can be reached
+    while (running) {
+        i--;
+        /* Zero socket descriptor vector and set for server sockets */
+        /* This must be reset every time select() is called */
+        FD_ZERO(&sockSet);
+        FD_SET(cogEngSrv, &sockSet);
+        for(uint16_t k = 0; k < Current_ID; k++){
+	    if(CE_List[k].ID_num != -1)
+	        FD_SET(CE_List[k].FD, &sockSet);
+	}
+	    //printf("k=%d, CID=%d\n", k, CE_List[k].FD);
+
+        /* Timeout specification */
+        /* This must be reset every time select() is called */
+        selTimeout.tv_sec = 0;       /* timeout (secs.) */
+        selTimeout.tv_usec = 0;            /* 0 microseconds */
+	//Changed both to zero so that select will check messages from the shell instead of blocking
+	//when there is no command from the CE's to be processed
+
+        //Check if there is a message on the socket waiting to be read
+	rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); 
+        //printf("rc=%d\n", rc);
+        if(rc == 0){
+            //LOG("No echo requests for %i secs...Server still alive\n", timeout);
+	
+            FD_ZERO(&shellSet);
+            FD_SET(shellSocketFD, &shellSet);
+            selTimeout.tv_sec = 0;
+            selTimeout.tv_usec = 0;
+            //Check if there is a message on the shell socket ready to be processed
+            select(shellSocketFD + 1, &shellSet, NULL, NULL, &selTimeout);
+            //printf("rc2=%d\n", rc2);
+		//If there is, call the MessageHandler with the Shell_Msg code of -1
+	    if(FD_ISSET(shellSocketFD, &shellSet)){
+		//printf("shell_msg, %d\n", rc2);
+	        MessageHandler(-1);}
+	}
+        else { 
+            desc_ready = rc;
+            for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) {
+                if(FD_ISSET(port, &sockSet)) {
+                    desc_ready -= 1;
+
+                    //Check if request is new or on an existing open descriptor
+                    if(port == cogEngSrv) { 
+			//If new, assign it a descriptor and give it an ID
+                        new_sd = AcceptTCPConnection(port);
+			 
+                        if(new_sd < 0) 
+                            break;
+
+                        CE_List[Current_ID].FD = new_sd;
+			CE_List[Current_ID].ID_num = Current_ID;
+                        MessageHandler(Current_ID);
+			Current_ID++;
+	
+			FD_SET(new_sd,&sockSet);
+                        if(new_sd > maxDescriptor) 
+                           maxDescriptor = new_sd;
+                    } 
+                    else {
+			//If old, figure out which ID it coresponds to and handle it accordingly
+			for(uint16_t z = 0; z < Current_ID; z++)
+			{
+				if(CE_List[z].FD == port){
+					MessageHandler(z);}
+			}
+                    }
+                }
+            }
+	}
+    }        
+
+    /* Close sockets */
+    close(cogEngSrv);
+
+    //delete &cogEngSrv;
+    return;
+}
+
+
+
+
+    /*TiXmlElement *pMission;
     TiXmlElement *pService;
     TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3;
@@ -1089,354 +1919,4 @@
 		}
 		else{
-		    mList[mission_num].services[conditional_1].name = pChild2->Attribute("name");
-                    if(pChild2->Attribute("input1"))
-			mList[mission_num].services[service_num].input[0] = pChild2->Attribute("input1");
-                    if(pChild2->Attribute("input2"))
-			mList[mission_num].services[service_num].input[1] = pChild2->Attribute("input2");
-                    if(pChild2->Attribute("input3"))
-			mList[mission_num].services[service_num].input[2] = pChild2->Attribute("input3");
-                    if(pChild2->Attribute("output1"))
-			mList[mission_num].services[service_num].output[0] = pChild2->Attribute("output1");
-                    if(pChild2->Attribute("output2"))
-			mList[mission_num].services[service_num].output[1] = pChild2->Attribute("output2");
-                    if(pChild2->Attribute("output3"))
-			mList[mission_num].services[service_num].output[2] = pChild2->Attribute("output3");
-		}
-
-		mList[mission_num].services[conditional_1].num_conds = cond_array[1];
-		cond_array[1] = 0;
-		cond_array[0]++;
-	    }
-	    if(conditional_0 != service_num){
-		mList[mission_num].services[conditional_0].name = pChild1->Value();
-                    if(pChild1->Attribute("input_t"))
-			mList[mission_num].services[conditional_0].input[0] = pChild1->Attribute("input_t");
-                    if(pChild1->Attribute("input_f"))
-			mList[mission_num].services[conditional_0].input[1] = pChild1->Attribute("input_f");
-		//printf("---input_t=%s\n", mList[mission_num].services[conditional_0].input[0].c_str());
-	    }
-	    else{
-		mList[mission_num].services[conditional_0].name = pChild1->Attribute("name");
-                if(pChild1->Attribute("input1"))
-	            mList[mission_num].services[service_num].input[0] = pChild1->Attribute("input1");
-                if(pChild1->Attribute("input2"))
-		    mList[mission_num].services[service_num].input[1] = pChild1->Attribute("input2");
-                if(pChild1->Attribute("input3"))
-		    mList[mission_num].services[service_num].input[2] = pChild1->Attribute("input3");
-                if(pChild1->Attribute("output1"))
-		    mList[mission_num].services[service_num].output[0] = pChild1->Attribute("output1");
-                if(pChild1->Attribute("output2"))
-		    mList[mission_num].services[service_num].output[1] = pChild1->Attribute("output2");
-                if(pChild1->Attribute("output3"))
-		    mList[mission_num].services[service_num].output[2] = pChild1->Attribute("output3");
-	    }
-
-	    mList[mission_num].services[conditional_0].num_conds = cond_array[0];
-            cond_array[0] = 0;
-	    service_num++;
-	}
-	//for(int i = 0; i < service_num; i++)
-	 //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());
-
-	mList[mission_num].numServices = service_num;
-	mList[mission_num].name = pChild0->Attribute("name");
-	mList[mission_num].missionID = atoi(pChild0->Attribute("id"));
-        if(pChild0->Attribute("input1"))
-	    mList[mission_num].input[0] = pChild0->Attribute("input1");
-        if(pChild0->Attribute("input2"))
-	    mList[mission_num].input[1] = pChild0->Attribute("input2");
-        if(pChild0->Attribute("input3"))
-	    mList[mission_num].input[2] = pChild0->Attribute("input3");
-        if(pChild0->Attribute("output1"))
-	    mList[mission_num].output[0] = pChild0->Attribute("output1");
-        if(pChild0->Attribute("output2"))
-	    mList[mission_num].output[1] = pChild0->Attribute("output2");
-        if(pChild0->Attribute("output3"))
-	    mList[mission_num].output[2] = pChild0->Attribute("output3");
-	//printf("mis, input1=%s, output1=%s\n", mList[mission_num].input[0].c_str(), mList[mission_num].output[0].c_str());
-	mission_num++;
-    }
-}
-
-/* CALLED BY: MessageHandler
- * INPUTS: |ID| The ID number of the engine to be registered
- * OUTPUTS: <none>
- *
- * DESCRIPTION: Sends a registration message onto the shell and sends the ACK back to the component
- */
-void
-ServiceManagementLayer::RegisterCognitiveEngine(int32_t ID)
-{
-    //LOG("SML::regcogeng");
-    SendMessage(shellSocketFD, "register_engine_cognitive");
-
-   // printf("SSFD = %d\n", shellSocketFD);
-    LOG("ServiceManagementLayer:: CE registration message forwarded to shell.\n");
-    char buffer[256];
-    memset(buffer, 0, 256);
-    ReadMessage(shellSocketFD, buffer);
-    //printf("ServiceManagementLayer::buffer = %s\n", buffer);
-    SendMessage(CE_List[ID].FD, buffer);
-
-    TransferRadioConfiguration(ID);
-    memset(buffer, 0, 256);
-    //printf("start trans exp\n");
-    TransferExperience(ID);
-    memset(buffer, 0, 256);
-    numberOfCognitiveEngines++;
-    CE_Present = true;
-    //printf("done registering CE!\n");
-}
-
-/* CALLED BY: MessageHandler
- * INPUTS: |ID| The ID number of the engine to have it's services deregistered
- * OUTPUTS: <none>
- *
- * DESCRIPTION: Deletes individual services from the DB
- * NOTE THAT this function only needs to be called if service deregistration is going
- * to be done at a different time than component deregistration; it is handled
- * more efficiently and directly during that deregistration process.
- */
-void 
-ServiceManagementLayer::DeregisterServices(int32_t ID)
-{
-    char buffer[256];
-    memset(buffer, 0, 256);
-    ReadMessage(CE_List[ID].FD, buffer); 
-    strcpy(_services_DB->command, "DELETE FROM ");
-    strcat(_services_DB->command, _services_DB->tablename);
-    strcat(_services_DB->command, " WHERE ID_Num IN (SELECT");
-    sprintf(_services_DB->command, " %s %d",_services_DB->command, ID);
-    strcat(_services_DB->command, " FROM ");
-    strcat(_services_DB->command, _services_DB->tablename);
-    strcat(_services_DB->command, " WHERE Service_Name");
-    strcat(_services_DB->command, "==");
-    sprintf(_services_DB->command, "%s'%s');", _services_DB->command, buffer);
-    char *errorMsg;
-    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
-    if( rc!=SQLITE_OK && rc!=101 )
-        fprintf(stderr, "SQL error: %s\n", errorMsg);
-}
-
-/* CALLED BY: MessageHandler
- * INPUTS: |ID| The ID number of the engine to have it's services deregistered
- * OUTPUTS: <none>
- *
- * DESCRIPTION: Deletes the contact info for the cognitive engine, forwards a deregistration message to the shell
- * Also, deletes the services from the DB
- */
-void 
-ServiceManagementLayer::DeregisterCognitiveEngine(int32_t ID)
-{
-    LOG("ServiceManagementLayer:: CE deregistration message forwarded to shell.\n");
-
-    numberOfCognitiveEngines--;
-    if(numberOfCognitiveEngines == 0)
-        CE_Present = false;
-
-    SendMessage(shellSocketFD, "deregister_engine_cognitive");
-    char buffer[256];
-    memset(buffer, 0, 256);
-    ReadMessage(shellSocketFD, buffer);
-    SendMessage(CE_List[ID].FD, buffer);
-    if(strcmp("deregister_ack", buffer) != 0) {
-	ERROR(1, "SML:: Failed to close CE socket\n");
-    }
-
-    //Deregister the services
-    strcpy(_services_DB->command, "DELETE FROM ");
-    strcat(_services_DB->command, _services_DB->tablename);
-    strcat(_services_DB->command, " WHERE ");
-    strcat(_services_DB->command, "ID_Num");
-    strcat(_services_DB->command, "==");
-    sprintf(_services_DB->command, "%s%d;", _services_DB->command, ID);
-    char *errorMsg;
-    int rc = sqlite3_exec(_services_DB->db, _services_DB->command, callback, 0, &errorMsg);
-    if( rc!=SQLITE_OK && rc!=101 )
-        fprintf(stderr, "SQL error: %s\n", errorMsg);
-
-
-    CE_List[ID].FD = -1;
-    CE_List[ID].ID_num = -1;
-
-    LOG("Cognitive Radio Shell:: CE Socket closed for engine #%d.\n", ID);
-}
-
-
-/* CALLED BY: test class
- * INPUTS: <none>
- * OUTPUTS: <none>
- *
- * DESCRIPTION: Sets up a server socket and listens for communication on either that or the shell socket
- */
-void
-ServiceManagementLayer::StartSMLServer()
-{
-    //printf("Ready for CE Signal! (registration done)\n");
-    struct timeval selTimeout;
-    int32_t running = 1;
-    int32_t port, rc, new_sd = 1;
-    int32_t desc_ready = 1;
-    int32_t timeout = 10;
-		//If there is, call the MessageHandler with the Shell_Msg code of -1
-    fd_set sockSet, shellSet;
-
-    cogEngSrv = CreateTCPServerSocket(SMLport);
-    int32_t maxDescriptor = cogEngSrv;
-
-    if(InitializeTCPServerPort(cogEngSrv) == -1)
-        ERROR(1,"Error initializing primary port\n");
-
-    int i = 10000000;  //TODO change to "running" if endpoint can be reached
-    while (running) {
-        i--;
-        /* Zero socket descriptor vector and set for server sockets */
-        /* This must be reset every time select() is called */
-        FD_ZERO(&sockSet);
-        FD_SET(cogEngSrv, &sockSet);
-        for(int k = 0; k < Current_ID; k++){
-	    if(CE_List[k].ID_num != -1)
-	        FD_SET(CE_List[k].FD, &sockSet);
-	}
-	    //printf("k=%d, CID=%d\n", k, CE_List[k].FD);
-
-        /* Timeout specification */
-        /* This must be reset every time select() is called */
-        selTimeout.tv_sec = 0;       /* timeout (secs.) */
-        selTimeout.tv_usec = 0;            /* 0 microseconds */
-	//Changed both to zero so that select will check messages from the shell instead of blocking
-	//when there is no command from the CE's to be processed
-
-        //Check if there is a message on the socket waiting to be read
-	rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); 
-        //printf("rc=%d\n", rc);
-        if(rc == 0){
-            //LOG("No echo requests for %i secs...Server still alive\n", timeout);
-	
-            FD_ZERO(&shellSet);
-            FD_SET(shellSocketFD, &shellSet);
-            selTimeout.tv_sec = 0;
-            selTimeout.tv_usec = 0;
-            //Check if there is a message on the shell socket ready to be processed
-            int rc2 = select(shellSocketFD + 1, &shellSet, NULL, NULL, &selTimeout);
-            //printf("rc2=%d\n", rc2);
-		//If there is, call the MessageHandler with the Shell_Msg code of -1
-	    if(FD_ISSET(shellSocketFD, &shellSet)){
-		//printf("shell_msg, %d\n", rc2);
-	        MessageHandler(-1);}
-	}
-        else { 
-            desc_ready = rc;
-            for(port = 0; port <= maxDescriptor && desc_ready > 0; port++) {
-                if(FD_ISSET(port, &sockSet)) {
-                    desc_ready -= 1;
-
-                    //Check if request is new or on an existing open descriptor
-                    if(port == cogEngSrv) { 
-			//If new, assign it a descriptor and give it an ID
-                        new_sd = AcceptTCPConnection(port);
-			 
-                        if(new_sd < 0) 
-                            break;
-
-                        CE_List[Current_ID].FD = new_sd;
-			CE_List[Current_ID].ID_num = Current_ID;
-                        MessageHandler(Current_ID);
-			Current_ID++;
-	
-			FD_SET(new_sd,&sockSet);
-                        if(new_sd > maxDescriptor) 
-                           maxDescriptor = new_sd;
-                    } 
-                    else {
-			//If old, figure out which ID it coresponds to and handle it accordingly
-			for(int16_t z = 0; z < Current_ID; z++)
-			{
-				if(CE_List[z].FD == port){
-					MessageHandler(z);}
-			}
-                    }
-                }
-            }
-	}
-    }        
-
-    /* Close sockets */
-    close(cogEngSrv);
-
-    //delete &cogEngSrv;
-    return;
-}
-
-
-
-
-    /*TiXmlElement *pMission;
-    TiXmlElement *pService;
-    TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3;
-    TiXmlHandle hRoot(0);
-    //printf("ServiceManagementLayer:: Loading Configuration.\n");
-    TiXmlDocument doc(".");
-    doc.LoadFile(SML_Config);
-    bool loadOkay = doc.LoadFile();
-    if(!loadOkay)
-        printf("Loading SML configuration failed: %s\n", SML_Config);
-
-    TiXmlHandle hDoc(&doc);
-    
-    pMission = hDoc.FirstChildElement().Element();
-
-    if(!pMission) 
-        printf("No valid root!");
-
-    hRoot = TiXmlHandle(pMission);
-    pService = pMission->FirstChildElement();
-    int32_t mission_num = 0;
-    //Iterate through the missions
-    for(pChild0 = pMission->FirstChildElement(); pChild0 ; \
-        pChild0 = pChild0->NextSiblingElement()) 
-    {
-	int32_t service_num = 0;
-	uint16_t cond_array[] = {0, 0};
-	//printf("mission_num = %d\n", mission_num);
-	memset(cond_array, 0, 2);
-	
-    	for(pChild1  = pChild0->FirstChildElement(); pChild1; \
-      	    pChild1  = pChild1->NextSiblingElement()) 
-    	{
-	    int32_t conditional_0 = service_num;
-	    for(pChild2 = pChild1->FirstChildElement(); \
-		pChild2; pChild2 = pChild2->NextSiblingElement())
-	    {
-		service_num++;
-		int32_t conditional_1 = service_num;
-		for(pChild3 = pChild2->FirstChildElement(); \
-		    pChild3; pChild3 = pChild3 ->NextSiblingElement())
-		{
-		    service_num++;
-		    mList[mission_num].services[service_num].name = pChild3->Attribute("name");
-                    if(pChild3->Attribute("input1"))
-			mList[mission_num].services[service_num].input[0] = pChild3->Attribute("input1");
-                    if(pChild3->Attribute("input2"))
-			mList[mission_num].services[service_num].input[1] = pChild3->Attribute("input2");
-                    if(pChild3->Attribute("input3"))
-			mList[mission_num].services[service_num].input[2] = pChild3->Attribute("input3");
-                    if(pChild3->Attribute("output1"))
-			mList[mission_num].services[service_num].output[0] = pChild3->Attribute("output1");
-                    if(pChild3->Attribute("output2"))
-			mList[mission_num].services[service_num].output[1] = pChild3->Attribute("output2");
-                    if(pChild3->Attribute("output3"))
-			mList[mission_num].services[service_num].output[2] = pChild3->Attribute("output3");
-		    cond_array[1]++;
-		}
-
-		if(conditional_1 != service_num){
-		    mList[mission_num].services[conditional_1].name = pChild2->Value();
-                    if(pChild2->Attribute("input_t"))
-			mList[mission_num].services[conditional_1].input[0] = pChild2->Attribute("input_t");
-                    if(pChild2->Attribute("input_f"))
-			mList[mission_num].services[conditional_1].input[1] = pChild2->Attribute("input_f");
-		}
-		else{
 		    mList[mission_num].services[service_num].name = pChild2->Attribute("name");
                     if(pChild2->Attribute("input1"))
