Index: /vtcross/trunk/src/cognitive_engines/Makefile.am
===================================================================
--- /vtcross/trunk/src/cognitive_engines/Makefile.am	(revision 161)
+++ /vtcross/trunk/src/cognitive_engines/Makefile.am	(revision 218)
@@ -1,10 +1,9 @@
 INCLUDES = -I$(top_srcdir)/src/include
 
-AM_CFLAGS = -Wall -g
+AM_CXXFLAGS = -Wall -g
 
 bin_PROGRAMS = cbr_demo
 
-cbr_demo_SOURCES = cbr.c cbr_demo.c
-cbr_demo_LDADD = -lsqlite3
+cbr_demo_SOURCES = CognitiveEngine.cpp cbr_demo.cpp
+cbr_demo_LDADD = -lsqlite3 ../lib/socketcomm/libsocketcomm.a
 
-
Index: /vtcross/trunk/src/cognitive_engines/cbr_demo.c
===================================================================
--- /vtcross/trunk/src/cognitive_engines/cbr_demo.c	(revision 181)
+++ /vtcross/trunk/src/cognitive_engines/cbr_demo.c	(revision 218)
@@ -540,8 +540,4 @@
     SendMessage(sockfd, "register_engine_cognitive");
     return 1;
-    
-    
-    
-    return 1;
 }
 
Index: /vtcross/trunk/src/cognitive_engines/cbr_demo.cpp
===================================================================
--- /vtcross/trunk/src/cognitive_engines/cbr_demo.cpp	(revision 218)
+++ /vtcross/trunk/src/cognitive_engines/cbr_demo.cpp	(revision 218)
@@ -0,0 +1,42 @@
+/* Virginia Tech Cognitive Radio Open Source Systems
+ * Virginia Tech, 2009
+ *
+ * TODO LICENSE INFORMATION GOES HERE
+ */
+
+/* TODO DESCRIPTION OF FILE.
+ *
+ * This file is a temporary demo of a policy engine using some of our socket
+ * communication functions. This is *not* an actual implementation of our
+ * defined PolicyEngine class.
+ */
+
+
+#include <cstdlib>
+#include <cstring>
+#include <stdint.h>
+
+#include "vtcross/common.h"
+#include "vtcross/components.h"
+#include "vtcross/containers.h"
+#include "vtcross/debug.h"
+#include "vtcross/error.h"
+#include "vtcross/socketcomm.h"
+
+
+int32_t 
+main(int32_t argc, char *argv[])
+{
+    if(argc < 3)
+       ERROR(1, "Usage: %s hostname port\n", argv[0]);
+    
+    CognitiveEngine cognitiveEngine(argv[1], argv[2], false);
+
+    LOG("Waiting for signal...\n");
+    while(1) {
+//        cognitiveEngine.WaitForSignal();
+    }
+
+    return 0;
+}
+
Index: /vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp
===================================================================
--- /vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp	(revision 218)
+++ /vtcross/trunk/src/cognitive_engines/CognitiveEngine.cpp	(revision 218)
@@ -0,0 +1,418 @@
+/* Virginia Tech Cognitive Radio Open Source Systems
+ * Virginia Tech, 2009
+ *
+ * LICENSE INFORMATION GOES HERE
+ */
+
+/* DESCRIPTION OF FILE.
+ */
+
+
+#include <cstdlib>
+#include <cstring>
+#include <stdint.h>
+
+#include "vtcross/common.h"
+#include "vtcross/components.h"
+#include "vtcross/containers.h"
+#include "vtcross/debug.h"
+#include "vtcross/error.h"
+#include "vtcross/socketcomm.h"
+
+
+CognitiveEngine::CognitiveEngine()
+{
+    LOG("Creating Cognitive Engine.\n");
+    SML_present = false;
+    commandSocketFD = -1;
+}
+
+
+CognitiveEngine::~CognitiveEngine()
+{
+    delete [] pList;
+    delete [] oList;
+    delete [] uList;
+    delete [] radioInfo;
+}
+
+
+CognitiveEngine::CognitiveEngine(const char* serverName, const char* serverPort, \
+        const bool SML)
+{
+    LOG("Creating Cognitive Engine.\n");
+
+    pList = new Parameter[10];
+    oList = new Observable[10];
+    uList = new Utility[10];
+    radioInfo = new Radio_Info;
+
+    ConnectToRemoteComponent(serverName, serverPort, SML);
+}
+
+
+void
+CognitiveEngine::SendComponentType()
+{
+    SendMessage(commandSocketFD, "response_engine_cognitive");
+    LOG("Cognitive Engine responded to GetRemoteComponentType query.\n");
+}
+
+
+void
+CognitiveEngine::ConnectToRemoteComponent(const char* serverName, \
+        const char* serverPort, const bool SML)
+{
+    commandSocketFD = ClientSocket(serverName, serverPort);
+
+    SML_present = SML;
+
+    if(SML) {
+        RegisterComponent();
+        RegisterServices();
+        LOG("Cognitive Engine connected to SML at %s.\n", serverName);
+        ReceiveRadioConfiguration();
+        ReceiveExperience();
+    }
+    else {
+        RegisterComponent();
+        LOG("Cognitive Engine connected to shell at %s.\n", serverName);
+        ReceiveRadioConfiguration();
+        ReceiveExperience();
+    }
+}
+
+
+void 
+CognitiveEngine::WaitForSignal()
+{
+    char buffer[256];
+
+    while(true) {
+        memset(buffer, 0, 256);
+       
+        ReadMessage(commandSocketFD, buffer);
+
+        // TODO this is ugly... is there a better way? Doesn't strcmp compare the
+        // whole string?  We only need to compare until we find a single different
+        // byte...
+        //
+        // If we send integer op codes rather than strings, this process will be
+        // MUCH faster since instead of donig string compares we can simply
+        // switch on the integer value...
+        if(strcmp(buffer, "get_solution") == 0) {
+        
+
+        }
+        else if(strcmp(buffer, "query_component_type") == 0) {
+            SendComponentType();
+        }
+        else if(strcmp(buffer, "connect_sml") == 0) {
+            /* This command implies that we are disconnecting from the shell and
+             * connecting to a SML component. */
+            char serverName[256];
+            char serverPort[256];
+            // TODO is this going to end up being too slow?
+            memset(serverName, 0, 256);
+            memset(serverPort, 0, 256);
+
+            ReadMessage(commandSocketFD, serverName);
+            ReadMessage(commandSocketFD, serverPort);
+
+            /* Only continue if we are currently connected to a shell. */
+            if(!SML_present) {
+                DeregisterComponent();
+
+                shutdown(commandSocketFD, 2);
+                close(commandSocketFD);
+
+                ConnectToRemoteComponent(serverName, serverPort, true);
+            }
+        }
+        else if(strcmp(buffer, "disconnect_sml") == 0) {
+            /* This command implies that we are disconnecting from the SML and
+             * connecting to a shell component. */
+            char serverName[256];
+            char serverPort[256];
+            // TODO is this going to end up being too slow?
+            memset(serverName, 0, 256);
+            memset(serverPort, 0, 256);
+
+            ReadMessage(commandSocketFD, serverName);
+            ReadMessage(commandSocketFD, serverPort);
+
+            /* We only want to do this if we are actually connected to an SML
+             * currently. */
+            if(SML_present) {
+                DeregisterServices();
+
+                shutdown(commandSocketFD, 2);
+                close(commandSocketFD);
+
+                ConnectToRemoteComponent(serverName, serverPort, false);
+            }
+        }
+        else if(strcmp(buffer, "reset_engine_cognitive") == 0) {
+            Reset();
+        }
+        else if(strcmp(buffer, "shutdown_engine_cognitive") == 0) {
+            Shutdown();
+        }
+    }
+}
+
+
+void 
+CognitiveEngine::Shutdown()
+{
+    if(SML_present) {
+        DeregisterServices();
+        DeregisterComponent();
+    } 
+    else {
+        DeregisterComponent();
+    }
+    // TODO should something else be happening here?
+}
+
+
+void
+CognitiveEngine::Reset()
+{
+    LOG("Resetting Cognitive Engine.\n");
+
+    if(SML_present) {
+        DeregisterServices();
+        DeregisterComponent();
+    } 
+    else {
+        DeregisterComponent();
+    }
+}
+
+
+void
+CognitiveEngine::RegisterComponent()
+{
+    SendMessage(commandSocketFD, "register_engine_cognitive");
+    LOG("Cognitive Engine:: Registration message sent to shell.\n");
+}
+
+void 
+CognitiveEngine::DeregisterComponent()
+{
+    SendMessage(commandSocketFD, "deregister_engine_cognitive");
+    LOG("Cognitive Engine:: Deregistration message sent.\n");
+
+    shutdown(commandSocketFD, 2);
+    close(commandSocketFD);
+    commandSocketFD = -1;
+    LOG("Cognitive Engine:: Shell socket closed.\n");
+}
+
+
+void 
+CognitiveEngine::RegisterServices()
+{
+    LOG("Cognitive Engine:: Registering services.\n");
+
+    SendMessage(commandSocketFD, "register_service");
+    SendMessage(commandSocketFD, "jam_wifi");
+
+    SendMessage(commandSocketFD, "register_service");
+    SendMessage(commandSocketFD, "signal_detection");
+
+    SendMessage(commandSocketFD, "register_service");
+    SendMessage(commandSocketFD, "max_throughput");
+
+    SendMessage(commandSocketFD, "register_service");
+    SendMessage(commandSocketFD, "jam_bluetooth");
+}
+
+
+void 
+CognitiveEngine::DeregisterServices()
+{
+    LOG("Cognitive Engine:: Deregistering services.\n");
+
+    SendMessage(commandSocketFD, "deregister_service");
+    SendMessage(commandSocketFD, "jam_wifi");
+
+    SendMessage(commandSocketFD, "deregister_service");
+    SendMessage(commandSocketFD, "signal_detection");
+
+    SendMessage(commandSocketFD, "deregister_service");
+    SendMessage(commandSocketFD, "max_throughput");
+
+    SendMessage(commandSocketFD, "deregister_service");
+    SendMessage(commandSocketFD, "jam_bluetooth");
+
+}
+
+void 
+CognitiveEngine::ReceiveRadioConfiguration()
+{
+    LOG("Cognitive Engine:: Receiving Radio Configuration.\n");
+    
+    char buffer[256];
+    uint32_t i,j,k;   
+ 
+    /* Receive Set of Utilities */
+    memset(buffer, 0, 256);
+    ReadMessage(commandSocketFD,buffer);
+    radioInfo->numUtilities = atoi(buffer);
+    
+    for (i = 0; i < radioInfo->numUtilities; i++){
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD,buffer);
+        LOG("socket_comm::Get Utility - Name: %s\n", buffer);
+        uList[i].name = std::string(buffer);
+    
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD,buffer);
+        uList[i].units = std::string(buffer);
+
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD,buffer);
+        uList[i].goal = std::string(buffer);
+    
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD,buffer);
+        uList[i].target = atof(buffer);
+    }
+
+    /* Receive Set of Parameters */
+    memset(buffer, 0, 256);
+    ReadMessage(commandSocketFD, buffer);
+    radioInfo->numParameters = atoi(buffer);
+    LOG("socket_comm::GetParameter - Number of parameters: %d\n", \
+            radioInfo->numParameters);
+    
+    for(size_t i = 0; i < radioInfo->numParameters; i++) {
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        LOG("socket_comm::GetParameter - Name: %s\n", buffer);
+        pList[i].name = std::string(buffer);
+    
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        LOG("socket_comm::GetParameter - Units: %s\n", buffer);
+        pList[i].units = std::string(buffer);
+
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        LOG("socket_comm::GetParameter - Min: %s\n", buffer);
+        pList[i].min = atof(buffer);
+    
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        LOG("socket_comm::GetParameter - Max: %s\n", buffer);
+        pList[i].max = atof(buffer);
+    
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        LOG("socket_comm::GetParameter - Step: %s\n", buffer);
+        pList[i].step = atof(buffer);
+    
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD,buffer);
+        LOG("socket_comm::GetParameter - Number of Affects: %s\n", buffer);
+        pList[i].numAffects = atoi(buffer);
+    
+        for (j = 0; j < pList[i].numAffects; j++){
+            memset(buffer, 0, 256);
+            ReadMessage(commandSocketFD,buffer);
+            LOG("socket_comm::GetParameter - Affect Name: %s\n", buffer);
+            for (k = 0; k < radioInfo->numUtilities; k++){
+	        if(uList[k].name == std::string(buffer)){    
+                    pList[i].affection_list[j].u = &uList[k];   
+                    break;
+                }
+            }
+
+            memset(buffer, 0, 256);
+            ReadMessage(commandSocketFD,buffer);
+            LOG("socket_comm::GetParameter - Affect Relation: %s\n", buffer);
+            pList[i].affection_list[j].relation = std::string(buffer);   
+        }
+    }   
+
+    /* Receive Set of Observables */
+    memset(buffer, 0, 256);
+    ReadMessage(commandSocketFD,buffer);
+    radioInfo->numObservables = atoi(buffer);
+    
+    for (i = 0; i < radioInfo->numObservables; i++){
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD,buffer);
+        LOG("socket_comm::Get Observable - Name: %s\n", buffer);
+        oList[i].name = std::string(buffer);
+    
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD,buffer);
+        LOG("socket_comm::Get Observable - Number Affects: %s\n", buffer);
+        oList[i].numAffects = atoi(buffer);
+    
+        for (j = 0; j < oList[i].numAffects; j++){
+            memset(buffer, 0, 256);
+            ReadMessage(commandSocketFD,buffer);
+            LOG("socket_comm::Get Observable - Affect Name: %s\n", buffer);
+            for (k = 0; k < radioInfo->numUtilities; k++){
+	        if(uList[k].name == std::string(buffer)){    
+                    oList[i].affection_list[j].u = &uList[k];   
+                    break;
+                }
+            }
+ 
+            memset(buffer, 0, 256);
+            ReadMessage(commandSocketFD,buffer);
+            LOG("socket_comm::Get Observable - Affect Relation: %s\n", buffer);
+            oList[i].affection_list[j].relation = std::string(buffer);   
+        }
+    }
+
+    SendMessage(commandSocketFD, "receive_config_ack");
+}
+
+void 
+CognitiveEngine::ReceiveExperience()
+{
+    LOG("Cognitive Engine:: Receiving Experience Report.\n");
+    char buffer[256];
+    uint32_t numberExp;
+    
+    /* Receive number of experience entries */
+    memset(buffer, 0, 256);
+    ReadMessage(commandSocketFD,buffer);
+    numberExp = atoi(buffer);
+
+    LOG("Cognitive Engine:: Waiting for %i number of entries.\n",numberExp);
+ 
+    SendMessage(commandSocketFD, "receive_exp_ack");
+}
+
+void 
+CognitiveEngine::GetSolution(Observable *observables)
+{
+    LOG("Cognitive Engine:: Generating solution.\n");
+}
+
+void 
+CognitiveEngine::GetSolution(Observable *observables, std::string service)
+{
+    LOG("Cognitive Engine:: Generating solution for %s service.\n",service.c_str());
+}
+
+void 
+CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \
+    Utility *utilities)
+{
+    LOG("Cognitive Engine:: Receiving feedback.\n");
+}
+
+void 
+CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \
+    Utility *utilities, std::string service)
+{
+    LOG("Cognitive Engine:: Receiving feedback.\n");
+}
Index: /vtcross/trunk/src/shell/CognitiveRadioShell.cpp
===================================================================
--- /vtcross/trunk/src/shell/CognitiveRadioShell.cpp	(revision 215)
+++ /vtcross/trunk/src/shell/CognitiveRadioShell.cpp	(revision 218)
@@ -111,16 +111,91 @@
 
 
-void
-CognitiveRadioShell::SendRadioConfiguration(int32_t socketFD)
+bool
+CognitiveRadioShell::SendRadioConfiguration(int32_t socketfd)
 {
     LOG("Cognitive Radio Shell:: Sending radio configuration to Cognitive Engine.\n");
-}
-
-
-void
+    uint32_t i,j;
+    char counter[55];
+    char var[50];
+
+    // utilities
+    // Send number of utilities
+    sprintf(counter,"%d",radio_info->numUtilities);
+    SendMessage(socketfd,counter);
+    // send utility	
+    for(i = 0; i < radio_info->numUtilities; i++) {
+        SendMessage(socketfd, u[i].name.c_str());
+        SendMessage(socketfd, u[i].units.c_str());
+        SendMessage(socketfd, u[i].goal.c_str());
+        sprintf(var,"%f", u[i].target);
+        SendMessage(socketfd,var);
+    }
+
+    // parameters
+    sprintf(counter,"%i",radio_info->numParameters);
+    SendMessage(socketfd,counter);
+    for(i = 0; i < radio_info->numParameters; i++) {
+        SendMessage(socketfd,p[i].name.c_str());
+        SendMessage(socketfd,p[i].units.c_str());
+        sprintf(var,"%f",p[i].min);
+        SendMessage(socketfd,var);
+        sprintf(var,"%f",p[i].max);
+        SendMessage(socketfd,var);
+        sprintf(var,"%f",p[i].step);
+        SendMessage(socketfd,var);
+
+        sprintf(counter,"%i",p[i].numAffects); 
+        SendMessage(socketfd,counter);
+        for(j = 0; j < p[i].numAffects; j++) {
+            SendMessage(socketfd,p[i].affection_list[j].u->name.c_str());
+            SendMessage(socketfd,p[i].affection_list[j].relation.c_str());
+        }
+    }
+
+    // observables
+    sprintf(counter,"%i",radio_info->numObservables);
+    SendMessage(socketfd,counter);
+    for(i = 0; i < radio_info->numObservables; i++) {
+        SendMessage(socketfd,o[i].name.c_str());
+		
+        sprintf(counter,"%i",o[i].numAffects); 
+        SendMessage(socketfd,counter);
+        for(j = 0; j < o[i].numAffects; j++) {
+            SendMessage(socketfd,o[i].affection_list[j].u->name.c_str());
+            SendMessage(socketfd,o[i].affection_list[j].relation.c_str());
+        }
+    }
+	
+    // Receive ACK for utils
+    char buffer[256];
+    memset(buffer, 0, 256); 
+    ReadMessage(socketfd, buffer);
+
+    if(strcmp(buffer, "receive_config_ack") != 0) {
+        LOG("Cognitive Radio Shell:: Unexpected response: %s\n",buffer);
+        return 0;
+    }
+    return 1;
+
+}
+
+bool
 CognitiveRadioShell::SendRadioExperience(int32_t socketFD)
 {
 
     LOG("Cognitive Radio Shell:: Sending radio experience to Cognitive Engine.\n");
+    int32_t numberExp = 4;
+    
+    std::string numberExpString = numberExp;
+    SendMessage(socketFD, numberExp); 
+
+    char buffer[256];
+    memset(buffer, 0, 256); 
+    ReadMessage(socketFD, buffer);
+    if(strcmp(buffer, "receive_exp_ack") != 0) {
+        LOG("Cognitive Radio Shell:: Unexpected response: %s\n",buffer);
+        return 0;
+    }
+    return 1;
 }
 
@@ -214,5 +289,5 @@
     size_t item_count = 0;
     size_t affect_count = 0;
-    int32_t attribute_count = 0;
+    uint32_t attribute_count = 0;
     bool match_found = false;
 
