Index: /vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp
===================================================================
--- /vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp	(revision 289)
+++ /vtcross/trunk/src/service_management_layer/ServiceManagementLayer.cpp	(revision 290)
@@ -600,5 +600,5 @@
     {	
 	//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("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
@@ -648,5 +648,5 @@
 ServiceManagementLayer::TransactData(int32_t sourceID)
 {
-    LOG("ServiceManagementLayer:: Data transaction occuring.\n");
+   // LOG("ServiceManagementLayer:: Data transaction occuring.\n");
     char buffer[256];
     std::string data;
@@ -679,4 +679,11 @@
                 else {
 		    //TODO could do shell output here if not in DB
+	    	data.append("1@");
+	    	data.append(miss[activeMission].services[sourceID].output[0]);
+	    	data.append("@");
+	    	data.append(miss[activeMission].services[sourceID].output[0]);
+	    	data.append("@");
+		//printf("data = %s\n", data.c_str());
+	    	
 	            printf("data_DB:: Data not yet in DB.\n");
 		    rc=31337;
@@ -695,14 +702,6 @@
 	    }
 	}
-	//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");
+       // LOG("ServiceManagementLayer:: Finished with data transaction.\n");
 	return;
    } 
@@ -850,5 +849,5 @@
     }
     //printf("done transact data!\n");
-    LOG("ServiceManagementLayer:: Finished with data transaction.\n");
+   // LOG("ServiceManagementLayer:: Finished with data transaction.\n");
 
 
@@ -962,5 +961,5 @@
     ReadMessage(shellSocketFD, buffer);
     t=atoi(buffer);
-    for(int k = 0; k < t; k++) {
+    for(int m = 0; m < t; m++) {
 	//printf("data=%s\n", data_obsv.c_str());
         memset(buffer1, 0, 256);
@@ -1002,6 +1001,6 @@
 
    // printf("done\n");
-    i=0;
-    int32_t numstatements = 0;
+    i=0; 
+    int32_t numstatements[3] = {0,0,0};
     data.clear();
     while(i < miss[activeMission].numServices)
@@ -1015,6 +1014,6 @@
 	    for(t = 0; t < 5; t++){
 		if(!miss[activeMission].services[i].output[t].empty()){
-		    //printf("i-numstmts = %d\n", i-numstatements-1);
-		    input=miss[activeMission].services[i-numstatements-1].output[t];
+		    //printf("i-numstmts-1 = %d\n", i-numstatements[0]-1);
+		    input=miss[activeMission].services[i-numstatements[0]-1].output[t];
 		    strcpy(_data_DB->command, "SELECT ");
 		    strcat(_data_DB->command, _data_DB->tablename);
@@ -1045,22 +1044,195 @@
 	    }
 	    //printf("L0:--- %s  %s---\n", miss[activeMission].services[i].output[t].c_str(), token);
-	    if(!miss[activeMission].services[i].output[t].compare(token)){
+	    //TODO change to strstr
+	    if(strstr(miss[activeMission].services[i].output[t].c_str(), token)){
 		//printf("L0:if taken\n");
 		for(uint16_t k = i+1; k <= i+miss[activeMission].services[i].num_conds; k++){ 
-		    //printf("transacting data for k=%d\n", k);
-		    TransactData(k);
+		        //printf("transacting data for k=%d\n", k);
+		    //printf("%s---%d\n", miss[activeMission].services[k].name.c_str(), k);
+		    if(miss[activeMission].services[k].name.compare("if")==0){
+			//printf("L1:if detected\n");
+			    input.clear();
+			    check.clear();
+			    for(t = 0; t < 5; t++){
+				if(!miss[activeMission].services[k].output[t].empty()){
+				    //printf("i-numstmts = %d\n", i-numstatements-1);
+				    input=miss[activeMission].services[k-numstatements[1]-1].output[t];
+				    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.\n");
+						rc=31337;
+					}
+				    } else {
+					printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+				    }
+			    	    sqlite3_finalize(pStatement);
+				    //printf("data=%s\n", data.c_str());
+				    token = strtok((char *)data.c_str(), "@");
+				    token = strtok(NULL, "@");
+				    token = strtok(NULL, "@");
+				    //printf("data=%s\n", token);
+				    break;
+				}
+			    }
+			    //printf("L1:--- %s  %s---\n", miss[activeMission].services[k].output[t].c_str(), token);
+			    //TODO change to strstr
+			    if(strstr(miss[activeMission].services[k].output[t].c_str(), token)){
+				//printf("L1:if taken\n");
+				for(uint16_t j = k+1; j <= k+miss[activeMission].services[k].num_conds; j++){ 
+				    //printf("transacting data for k=%d\n", k);
+				    //printf("%s---%d\n", miss[activeMission].services[j].name.c_str(), j);
+				    if(miss[activeMission].services[j].name.compare("if")==0){
+					//printf("L2:if detected\n");
+					    input.clear();
+					    check.clear();
+					    for(t = 0; t < 5; t++){
+						if(!miss[activeMission].services[j].output[t].empty()){
+						    //printf("i-numstmts = %d\n", i-numstatements-1);
+						    input=miss[activeMission].services[j-numstatements[2]-1].output[t];
+						    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.\n");
+								rc=31337;
+							}
+						    } else {
+							printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+						    }
+					    	    sqlite3_finalize(pStatement);
+						    //printf("data=%s\n", data.c_str());
+						    token = strtok((char *)data.c_str(), "@");
+						    token = strtok(NULL, "@");
+						    token = strtok(NULL, "@");
+						    //printf("data=%s\n", token);
+						    break;
+						}
+					    }
+					    //printf("L2:--- %s  %s---\n", miss[activeMission].services[j].output[t].c_str(), token);
+					    //TODO change to strstr
+					    if(strstr(miss[activeMission].services[j].output[t].c_str(), token)){
+						//printf("L1:if taken\n");
+						for(uint16_t l = j+1; l <= j+miss[activeMission].services[j].num_conds; l++){ 
+						    //printf("transacting data for k=%d\n", k);
+						    TransactData(l);
+						}
+					    }
+					    else
+						//printf("L2: if not taken\n");
+						numstatements[2] +=miss[activeMission].services[j].num_conds+1;
+						j+=miss[activeMission].services[j].num_conds;
+					    //printf("doneif %d, %d, %d\n", numstatements, miss[activeMission].services[i].num_conds, i);
+					}
+					else{
+					    //printf("NO L2 COND!\n");
+					    numstatements[2]=0;
+				    	    TransactData(j);
+					}
+				}
+			    }
+			    else
+				//printf("L1: if not taken\n");
+			        numstatements[1] +=miss[activeMission].services[k].num_conds+1;
+			        k+=miss[activeMission].services[k].num_conds;
+			    //printf("doneif %d, %d, %d\n", numstatements, miss[activeMission].services[i].num_conds, i);
+			} else if(miss[activeMission].services[k].name.compare("dowhile")==0){
+			    //printf("while detected\n");
+			    while(true){
+				uint16_t m;
+				for(uint16_t m = k+1; m <= k+miss[activeMission].services[k].num_conds; m++){ 
+				    TransactData(m);
+				    printf("transact! %d\n", m);
+				}
+				    data.clear();
+				    printf("L1:while detected %d, %d\n", miss[activeMission].services[k].num_conds, k);
+				    input.clear();
+				    check.clear();
+				    int t;
+				    for(t = 0; t < 5; t++){
+					if(!miss[activeMission].services[k-1].output[t].empty()){
+					    input=miss[activeMission].services[m].output[t];
+					    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.\n");
+							rc=31337;
+						}
+					    } else {
+						printf("data_DB:: Error executing SQL statement. rc = %i\n%s\n",rc,_data_DB->command);
+					    }
+				    	    sqlite3_finalize(pStatement);
+					    //printf("data=%s\n", data.c_str());
+					    token = strtok((char *)data.c_str(), "@");
+					    token = strtok(NULL, "@");
+					    token = strtok(NULL, "@");
+					    //printf("data=%s\n", token);
+					    break;
+					}
+				    }
+				    printf("L1:--- %s  %s---\n", miss[activeMission].services[k].output[t].c_str(), token);
+				    if(strstr(miss[activeMission].services[k].output[t].c_str(), token)){
+					printf("L1:do it again!\n");
+				    }
+				    else
+					break;
+			    }
+			    k+=miss[activeMission].services[k].num_conds;
+			    //printf("donewhile\n");
+			}
+			else{
+			    //printf("NO L1 COND!\n");
+			    numstatements[1]=0;
+		    	    TransactData(k);
+			}
+		
+            		//numstatements[0] +=miss[activeMission].services[i].num_conds+1;
+	    		//i+=miss[activeMission].services[i].num_conds;
+	    //printf("doneif %d, %d, %d\n", numstatements, miss[activeMission].services[i].num_conds, i);
 		}
 	    }
-            numstatements +=miss[activeMission].services[i].num_conds+1;
+	   // else
+		//printf("LO if not taken\n");
+            numstatements[0] +=miss[activeMission].services[i].num_conds+1;
 	    i+=miss[activeMission].services[i].num_conds;
 	    //printf("doneif %d, %d, %d\n", numstatements, miss[activeMission].services[i].num_conds, i);
 	}
-	else if(miss[activeMission].services[i].name.compare("while")==0)
+	else if(miss[activeMission].services[i].name.compare("dowhile")==0)
 	{
-	    numstatements=0;
+	    numstatements[0]=0;
 	    //printf("while detected\n");
 	    while(true){
+		uint16_t k;
+		    for(k = i+1; k <= i+miss[activeMission].services[i].num_conds; k++){ 
+			TransactData(k);
+		    }
 		    data.clear();
-		    printf("L0:if detected\n");
+		    //printf("L0:while detected %d, %d\n", k, miss[activeMission].services[i].num_conds);
 		    input.clear();
 		    check.clear();
@@ -1068,5 +1240,6 @@
 		    for(t = 0; t < 5; t++){
 			if(!miss[activeMission].services[i].output[t].empty()){
-			    input=miss[activeMission].services[i-1].output[t];
+			    input=miss[activeMission].services[k-1].output[t];
+			    //printf("input=%s\n", input.c_str());
 			    strcpy(_data_DB->command, "SELECT ");
 			    strcat(_data_DB->command, _data_DB->tablename);
@@ -1096,10 +1269,7 @@
 			}
 		    }
-		    printf("L0:--- %s  %s---\n", miss[activeMission].services[i].output[t].c_str(), token);
-		    if(!miss[activeMission].services[i].output[t].compare(token)){
-			printf("L0:if taken\n");
-			for(uint16_t k = i+i; k <= i+miss[activeMission].services[i].num_conds; k++){ 
-			    TransactData(k);
-			}
+		    //printf("L0:--- %s  %s---\n", miss[activeMission].services[i].output[t].c_str(), token);
+		    if(strstr(miss[activeMission].services[i].output[t].c_str(), token)){
+			//printf("L0:while taken again!\n");
 		    }
 		    else
@@ -1110,5 +1280,5 @@
 	}
 	else{
-	    numstatements=0;
+	    numstatements[0]=0;
 	    //printf("L0 Neither if nor while\n");
 	    TransactData(i);}
@@ -1223,5 +1393,5 @@
     TiXmlElement *pChild0, *pChild1, *pChild2, *pChild3, *pChild4;
     TiXmlHandle hRoot(0);
-    //printf("ServiceManagementLayer:: Loading Configuration.\n");
+    printf("ServiceManagementLayer:: Loading Configuration.\n");
     TiXmlDocument doc(".");
     doc.LoadFile(SML_Config);
@@ -1259,13 +1429,17 @@
 		int32_t conditional_1 = service_num;
 		for(pChild3 = pChild2->FirstChildElement(); \
-		    pChild3; pChild3 = pChild3 ->NextSiblingElement())
+		    pChild3; pChild3 = pChild3->NextSiblingElement())
 		{
 		    service_num++;
 		    int32_t conditional_2 = service_num;
 			for(pChild4 = pChild3->FirstChildElement(); \
-			    pChild4; pChild4 = pChild4 ->NextSiblingElement())
+			    pChild4; pChild4 = pChild4->NextSiblingElement())
 			{
-			    service_num++;
-			    mList[mission_num].services[service_num].name = pChild4->Attribute("name");
+		   	    service_num++;
+			    if(pChild4->Attribute("name"))
+			        mList[mission_num].services[service_num].name = pChild4->Attribute("name");
+			    else
+			        mList[mission_num].services[service_num].name = pChild4->Value();
+				
 		            if(pChild4->Attribute("input1"))
 				mList[mission_num].services[service_num].input[0] = pChild4->Attribute("input1");
@@ -1323,5 +1497,4 @@
 
 		}
-		//printf("here\n");
 		if(!strcmp(pChild2->Value(), "shell") || conditional_1 != service_num) {
 		    mList[mission_num].services[conditional_1].name = pChild2->Value();
@@ -1350,5 +1523,4 @@
                     if(pChild2->Attribute("output5"))
 			mList[mission_num].services[conditional_1].output[4] = pChild2->Attribute("output5");
-	    //printf("2%s %s\n", mList[mission_num].services[conditional_1].output[0].c_str(), mList[mission_num].services[conditional_1].output[1].c_str());
 
 		mList[mission_num].services[conditional_1].num_conds = cond_array[1];
@@ -1385,5 +1557,4 @@
                 if(pChild1->Attribute("output5"))
 		    mList[mission_num].services[service_num].output[4] = pChild1->Attribute("output4");
-	    //printf("1%s %s\n", mList[mission_num].services[conditional_0].output[0].c_str(), mList[mission_num].services[conditional_0].output[1].c_str());
 	    mList[mission_num].services[conditional_0].num_conds = cond_array[0];
             cond_array[0] = 0;
