Index: vtcross/branches/trnewman/CR_engines/CBR/src/main_cognitive_engine.c
===================================================================
--- vtcross/branches/trnewman/CR_engines/CBR/src/main_cognitive_engine.c	(revision 77)
+++ vtcross/branches/trnewman/CR_engines/CBR/src/main_cognitive_engine.c	(revision 79)
@@ -26,4 +26,5 @@
     string goal;*/
     float target;
+    float value;
 };
 
@@ -44,4 +45,5 @@
     float max;
     float step;
+    float value;
 };      
         
@@ -51,4 +53,5 @@
     struct Affect affection_list[10];
     int numAffects;
+    float value;
 };
 
@@ -209,4 +212,44 @@
 
 
+// setup client socket connection
+int ClientSocket(int argc, char** argv)
+{
+    int sockfd;
+    int portno;
+    struct sockaddr_in serv_addr;
+    struct hostent *server;
+    
+    // setup client socket connection
+    if (argc < 3) {
+       fprintf(stderr,"usage: %s hostname port\n", argv[0]);
+       exit(0);
+    }
+    // server name
+    server = gethostbyname(argv[1]);
+    if (server == NULL) {
+        fprintf(stderr,"ERROR, no such host\n");
+        exit(0);
+    }
+    // port no.
+    portno = atoi(argv[2]);
+    // socket file descriptor
+    sockfd = socket(AF_INET, SOCK_STREAM, 0);
+    if (sockfd < 0) 
+        error("ERROR opening socket");
+    // initiate struct socketaddr_in
+    bzero((char *) &serv_addr, sizeof(serv_addr));
+    serv_addr.sin_family = AF_INET;
+    serv_addr.sin_port = htons(portno);
+    bcopy((char *)server->h_addr, 
+         (char *)&serv_addr.sin_addr.s_addr,
+         server->h_length);
+    // cast sockaddr_in to sockaddr
+    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
+        error("ERROR connecting");
+
+    return sockfd;
+}
+
+    
 int GetXMLFromServer(int sockfd, struct Utility uList[], struct Parameter pList[], 
         struct Observable oList[], struct CE_Info *ce_info)
@@ -226,6 +269,26 @@
 }
 
-void print_current_config(struct Utility uList[], struct Parameter pList[], 
-        struct Observable oList[], struct CE_Info * ce_info) 
+
+int SendMessage(int socketfd, char *message) 
+{
+    int n;
+     
+    strcat(message, "\0000");
+    // Write message back to client
+    //n = write(socketfd,message,(strlen(message)+1));
+    n = send(socketfd,message,(strlen(message)+1),0);
+    if (n<0)
+        error("Error sending to server\n");
+    else if(n == 0)
+        printf("Server closed the socket.\n");
+    
+    return n;
+}
+
+
+void print_current_config(struct Utility uList[], 
+        struct Parameter pList[], 
+        struct Observable oList[], 
+        struct CE_Info * ce_info) 
 {    
     int i = 0;
@@ -273,64 +336,35 @@
 }
 
-
-// main client socket
-int main(int argc, char *argv[])
-{
-    struct Utility uList[10];
-    struct Parameter pList[10];
-    struct Observable oList[10];
-    struct CE_Info ce_info;
-
-    int sockfd, portno;
-    struct sockaddr_in serv_addr;
-    struct hostent *server;
-
-    // setup client socket connection
-    if (argc < 3) {
-       fprintf(stderr,"usage: %s hostname port\n", argv[0]);
-       exit(0);
-    }
-    // server name
-    server = gethostbyname(argv[1]);
-    if (server == NULL) {
-        fprintf(stderr,"ERROR, no such host\n");
-        exit(0);
-    }
-    // port no.
-    portno = atoi(argv[2]);
-    // socket file descriptor
-    sockfd = socket(AF_INET, SOCK_STREAM, 0);
-    if (sockfd < 0) 
-        error("ERROR opening socket");
-    // initiate struct socketaddr_in
-    bzero((char *) &serv_addr, sizeof(serv_addr));
-    serv_addr.sin_family = AF_INET;
-    serv_addr.sin_port = htons(portno);
-    bcopy((char *)server->h_addr, 
-         (char *)&serv_addr.sin_addr.s_addr,
-         server->h_length);
-    // cast sockaddr_in to sockaddr
-    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
-        error("ERROR connecting");
-    
-    // get xml info from server
-    GetXMLFromServer(sockfd, uList, pList, oList, &ce_info);
-
-    //printf("utility:\n name: %s, units: %s, goal: %s, target: %f\n", 
-    //        uList[0].name, uList[0].units, uList[0].goal, uList[0].target);
-    
-    print_current_config(uList, pList, oList, &ce_info); 
-
-    // cbr operation
+int RunCBREngine(struct Utility uList[], struct Parameter pList[],
+        struct Observable oList[], struct CE_Info *ce_info, 
+        int num_cols)
+{
+    
     int i;
-    unsigned int num_cols;
-
-    num_cols = ce_info.numUtilities + ce_info.numParameters;
-    num_cols = num_cols + ce_info.numObservables;
-    num_cols = num_cols + 1;
-
-    char *cols[] = {"throughput", "spectral_efficiency",
-        "log10_ber", "mod_scheme", "tx_power", "SNR", "utility"};
-    
+
+    // get column names
+    //char *cols[50];
+    char **cols;
+    cols = (char **)malloc(sizeof(char)*num_cols);
+    int j = 0;
+    for (i = 0; i < ce_info->numUtilities; i++){
+        cols[j] = malloc(strlen(uList[i].name)+1);
+        cols[j] = uList[i].name;
+        j++;
+    }
+    for (i = 0; i < ce_info->numParameters; i++){
+        cols[j] = malloc(strlen(pList[i].name)+1);
+        cols[j] = pList[i].name;
+        j++;
+    }
+    for (i = 0; i < ce_info->numObservables; i++){
+        cols[j] = malloc(strlen(oList[i].name)+1);
+        cols[j] = oList[i].name;
+        j++;
+    }
+    cols[j] = malloc(strlen("utility")+1);
+    cols[j] = "utility";
+    
+    // sample table entry
     float vals[num_cols];
     vals[0] = 10e3f;    // throughput
@@ -342,20 +376,4 @@
     vals[6] = 0.762;    // utility
     
-    /*num_cols = 6;
-    char * cols[] = {
-        "BER", "throughput",
-        "mod_scheme", "tx_power",
-        "noise_power", "path_loss"
-    };
-
-    float vals[num_cols];
-    vals[0] = 1e-3f;    // BER
-    vals[1] = 10e3f;    // throughput
-    vals[2] = 1;        // mod_scheme
-    vals[3] = -3.0f;    // tx_power
-    vals[4] = -50.0f;   // noise_power
-    vals[5] = 125.0f;   // path_loss
-    //vals[6] = 0.762;    // utility*/
-    
     printf("number of columns: %d\n", num_cols);
     printf("column names:");
@@ -368,5 +386,5 @@
 
     // create cbr database/table
-    printf("create cbr database\n");
+    //printf("create cbr database\n");
     char filename[] = {"ex1"}, tablename[] = {"data"};
     cbr mycbr = cbr_create(filename, tablename, cols, num_cols);
@@ -374,5 +392,5 @@
     
     // add row here
-    printf("add row to cbr table\n");
+    //printf("add row to cbr table\n");
     rc = cbr_add_row(mycbr, cols, vals, num_cols);
     
@@ -380,13 +398,4 @@
     printf("cbr table\n");
     cbr_print(mycbr);
-    
-    /*// simple search: find entry where...
-    //   BER < 1e-2 and path_loss > 100
-    char * search_names[] = {"BER", "path_loss"};
-    int search_ops[] = {LT, GT};
-    float search_vals[] = {1e-2f, 100.f};
-    float retvals[num_cols];
-    printf("search cbr\n");
-    rc = cbr_search(mycbr, search_names, search_ops, search_vals, 2,retvals);*/
     
     // simple search: find entry where...
@@ -399,18 +408,89 @@
             search_vals, 2, retvals);
 
-    // print returned values
+    j = 0;
+    for (i = 0; i < ce_info->numUtilities; i++){
+        uList[i].value = retvals[j];
+        j++;
+    }
+    for (i = 0; i < ce_info->numParameters; i++){
+        pList[i].value = retvals[j];
+        j++;
+    }
+    for (i = 0; i < ce_info->numObservables; i++){
+        oList[i].value = retvals[j];
+        j++;
+    }
+
+    // clean up database
+    //printf("clean cbr database\n");
+    cbr_free(mycbr);
+    
+    return 1;
+}
+
+// main client socket
+int main(int argc, char *argv[])
+{
+    struct Utility uList[10];
+    struct Parameter pList[10];
+    struct Observable oList[10];
+    struct CE_Info ce_info;
+
+    // setup client socket connection
+    int sockfd;
+    sockfd = ClientSocket(argc, argv);
+    
+    // get xml info from server
+    GetXMLFromServer(sockfd, uList, pList, oList, &ce_info);
+    print_current_config(uList, pList, oList, &ce_info); 
+
+    // cbr operation
+    unsigned int num_cols;
+    // get number of columns
+    num_cols = ce_info.numUtilities + ce_info.numParameters;
+    num_cols = num_cols + ce_info.numObservables;
+    num_cols = num_cols + 1;
+    RunCBREngine(uList, pList, oList, &ce_info, num_cols);    
+    // print out results
+    int i;
     printf("search result: ");
-    for (i=0;i<num_cols; i++)
-    //for (i=0;i<mycbr->num_columns; i++)
-    printf("%f, ",retvals[i]);
+    for (i=0;i<ce_info.numUtilities; i++)
+        printf("%f, ",uList[i].value);
+    for (i=0;i<ce_info.numParameters; i++)
+        printf("%f, ",pList[i].value);
+    for (i=0;i<ce_info.numObservables; i++)
+        printf("%f, ",oList[i].value);
     printf("\n");
     
-    // clean up database
-    printf("clean cbr database\n");
-    cbr_free(mycbr);
-    
-    printf("done at client.\n");
-    return 0;
-    
+    // send results back to server
+    char counter[50];
+    char var[50];
+    // utility
+    sprintf(counter, "%d", ce_info.numUtilities);
+    SendMessage(sockfd, counter);
+    // send utilities
+    for (i = 0; i < ce_info.numUtilities; i++){
+        sprintf(var, "%f", uList[i].value);
+        SendMessage(sockfd, var);
+    }
+
+    // parameter
+    sprintf(counter, "%d", ce_info.numParameters);
+    SendMessage(sockfd, counter);
+    // send parameters
+    for (i = 0; i < ce_info.numParameters; i++){
+        sprintf(var, "%f", pList[i].value);
+        SendMessage(sockfd, var);
+    }
+
+    // observable
+    sprintf(counter, "%d", ce_info.numObservables);
+    SendMessage(sockfd, counter);
+    // send utilities
+    for (i = 0; i < ce_info.numObservables; i++){
+        sprintf(var, "%f", oList[i].value);
+        SendMessage(sockfd, var);
+    }
+
 
   return 0;
