Index: vtcross/branches/engineTreeFix/src/cognitive_engines/Makefile.am
===================================================================
--- vtcross/branches/engineTreeFix/src/cognitive_engines/Makefile.am	(revision 525)
+++ vtcross/branches/engineTreeFix/src/cognitive_engines/Makefile.am	(revision 526)
@@ -22,3 +22,3 @@
 libce_a_SOURCES = CognitiveEngine.cpp
 
-SUBDIRS = . CBR_CE #DSA_CE OSSIE_DEMO_CE
+SUBDIRS = . CBR_CE DSA_CE #OSSIE_DEMO_CE
Index: vtcross/branches/engineTreeFix/src/cognitive_engines/DSA_CE/DSA_CognitiveEngine.cpp
===================================================================
--- vtcross/branches/engineTreeFix/src/cognitive_engines/DSA_CE/DSA_CognitiveEngine.cpp	(revision 521)
+++ vtcross/branches/engineTreeFix/src/cognitive_engines/DSA_CE/DSA_CognitiveEngine.cpp	(revision 526)
@@ -44,9 +44,81 @@
 #define UTILITY 4
 
-
-static CBR *myCBR;
-
-
-CognitiveEngine::CognitiveEngine()
+class DSA_CE : public CognitiveEngine
+{
+    public:
+        /*! Default constructor. */
+        DSA_CE();
+
+        /*! Default destructor. */
+        ~DSA_CE();
+
+        /*! \brief Preferred constructor.
+         *
+         * Overloaded constructor that creates a cognitive engine object and
+         * connects it to either the shell or an SML, depening on the SML bool.
+         *
+         * The 'numFields' parameter sets how large the parameter, observable,
+         * and utility arrays should be upon instantiation.
+         */
+        DSA_CE(const char* serverName, const char* serverPort, \
+                const int32_t numFields, const bool SML);
+
+        void RegisterServices();
+        void DeregisterServices();
+
+        /*! \brief Request that the CE optimize a set of parameters. 
+         *
+         * Find the most optimal set of transmission parameters given certain
+         * observables and possibly a service if the SML component is present
+         * and active. */
+        Parameter *GetSolution(Observable *observables, \
+                Parameter *currentParameters);
+        Parameter *GetSolution(Observable *observables, \
+                Parameter *currentParameters, std::string service);
+
+        /*! \brief Receive feedback from the radio 
+         *
+         * Receive a feedback from the radio regarding the performance of a
+         * certain set of parameters, possibly associated with a service.
+         *
+         * Feedback is a single set of performance statistics that is achieved
+         * corresponding to a specific set of transmission parameters.  Feedback
+         * helps a Cognitive Engine make better future decisions based upon 
+         * more accurate performance statistics. 
+         */
+        void ReceiveFeedback(Observable *observables,Parameter *parameters);
+        void ReceiveFeedback(Observable *observables, Parameter *parameters, \
+                std::string service);
+
+
+        /*! \brief Initialize the CE and prepare it for operation. 
+         *
+         * BuildCognitiveEngine performs the CE implementation specific work
+         * that defines the internals of a CE.  For example, a CBR CE engine
+         * would build the case-base reasoner or create the database, a neural
+         * network based CE may perform the initial training, a GA based CE
+         * may build the chromosome structure.
+         */
+        void BuildCognitiveEngine();
+
+        /*! \brief Each of these functions responds to a specific command.
+         *
+         * These functions are left principally un-implemented. It is the duty
+         * of child classes to implement these functions, as they define the
+         * cognitive engine's functionality.
+         */
+        void PerformUpdatePerformance();
+        void PerformRequestOptimizationService();
+        void PerformRequestOptimization();
+        void PerformQueryComponentType();
+        void PerformConnectSML();
+        void PerformDisconnectSML();
+        void PerformResetEngineCognitive();
+        void PerformShutdownEngineCognitive();
+
+        CBR *myCBR;
+};
+
+DSA_CE::DSA_CE()
 {
     LOG("Creating Cognitive Engine.\n");
@@ -57,5 +129,5 @@
 
 
-CognitiveEngine::~CognitiveEngine()
+DSA_CE::~DSA_CE()
 {
     delete myCBR;
@@ -67,53 +139,204 @@
 
 
-CognitiveEngine::CognitiveEngine(const char *serverName, const char *serverPort, \
-        const bool SML)
+DSA_CE::DSA_CE(const char* serverName, const char* serverPort, \
+                const int32_t numFields, const bool SML) \
+        : CognitiveEngine(serverName, serverPort, numFields, 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) {
+    srand ( time(NULL) );
+}
+
+
+void 
+DSA_CE::RegisterServices()
+{
+    LOG("Cognitive Engine:: Registering services.\n");
+
+    SendMessage(commandSocketFD, "register_service");
+    SendMessage(commandSocketFD, "test_srv");
+
+    SendMessage(commandSocketFD, "register_service");
+    SendMessage(commandSocketFD, "test_srv1");
+
+    SendMessage(commandSocketFD, "register_service");
+    SendMessage(commandSocketFD, "test_srv2");
+
+    SendMessage(commandSocketFD, "register_service");
+    SendMessage(commandSocketFD, "test_srv3");
+
+}
+
+//Combined with deregister component since those two things must happen togeather
+void 
+DSA_CE::DeregisterServices()
+{
+    LOG("Cognitive Engine:: Deregistering services.\n");
+
+    SendMessage(commandSocketFD, "deregister_service");
+    SendMessage(commandSocketFD, "test_srv");
+
+    SendMessage(commandSocketFD, "deregister_service");
+    SendMessage(commandSocketFD, "test_srv1");
+
+    SendMessage(commandSocketFD, "deregister_service");
+    SendMessage(commandSocketFD, "test_srv2");
+
+    SendMessage(commandSocketFD, "deregister_service");
+    SendMessage(commandSocketFD, "test_srv3");
+
+}
+
+/* The core of the CE is this function */
+Parameter* 
+DSA_CE::GetSolution(Observable *observables, Parameter *currentParameters)
+{
+    LOG("Cognitive Engine:: Generating solution.\n");
+
+    /* Put together the CBR search array */
+
+    uint32_t channel = 0;
+    string searchNames[1];
+    string sumSearchName;
+    float searchVals[1];
+    float utilArray[(int)pList[0].max];
+    int searchOps[1];
+    uint32_t numberColumns = radioInfo->numUtilities + radioInfo->numParameters + \
+                             radioInfo->numObservables + 1;
+
+    float returnValues[numberColumns];
+    float sumRetVals[numberColumns];
+   
+    // Total sum of utilities in sumRetVals[0]
+
+    string channel_name = "channel";
+    string utility_name = "utility";
+
+    for(int32_t i = 0 ; i < pList[0].max ; i++ ) {
+        searchNames[0] = pList[0].name;
+        searchOps[0] = 0;
+        searchVals[0] = i+1; 
+    
+        uint32_t rc = myCBR->Search(searchNames, searchOps, searchVals,
+            1, returnValues);
+
+    if(rc == 31337) {
+        // No entry - must add
         
-        RegisterComponent();
-        LOG("Cognitive Engine connected to SML at %s.\n", serverName);
-        ReceiveRadioConfiguration();
-        ReceiveExperience();
+            string rowNames[numberColumns];
+            size_t rowIndex = 0;
+            for(size_t j = 0; j < radioInfo->numUtilities; j++) {
+                rowNames[rowIndex] = uList[j].name;
+                rowIndex++;
+            }
+            for(size_t j = 0; j < radioInfo->numParameters; j++) {
+                rowNames[rowIndex] = pList[j].name;
+            if(pList[j].name == "channel")
+                returnValues[rowIndex] = i+1;
+                rowIndex++;
+            }
+            for(size_t j = 0; j < radioInfo->numObservables; j++) {
+                rowNames[rowIndex] = oList[j].name;
+                rowIndex++;
+            }
+   
+            rowNames[rowIndex] = utility_name;
+        returnValues[rowIndex] = 500;
+
+                /* Add the new optimized set to the CBR database */
+
+            myCBR->AddRow(rowNames, returnValues, numberColumns);
+    }
+
+    utilArray[i] = returnValues[UTILITY]; 
+    }
+
+    printf("Current Channel Utility Scores\n");
+    printf("1: %f\t7: %f\t8: %f\t14: %f\n",utilArray[0],utilArray[1],utilArray[2],utilArray[3]);    
+    // Get sum of all the channel utilities.
+    sumSearchName = utility_name;
+    uint32_t rc = myCBR->SearchSum(sumSearchName, sumRetVals);
+
+    // Psuedo random channel selection based upon utility.
+    int k = rand() % (int)sumRetVals[0];
+    int cdf_total(0);
+
+    for ( int i = 0; i < pList[0].max; i++ ) {
+        cdf_total += utilArray[i];
+        if(k < cdf_total) {
+        channel = i + 1;
+        break;
+    }
+    }
         
-        RegisterServices();
-    }
-    else {
-        RegisterComponent();
-        LOG("Cognitive Engine connected to shell at %s.\n", serverName);
-        ReceiveRadioConfiguration();
-        ReceiveExperience();
-    }
-}
+    searchNames[0] = pList[0].name;
+    searchOps[0] = 0;
+    searchVals[0] = channel; 
+  
+    rc = myCBR->Search(searchNames, searchOps, searchVals,
+        1, returnValues);
+
+
+    //returnValues[CHANNEL] = rand() % (int)pList[0].max + (int)pList[0].min; 
+    returnValues[CHANNEL] = channel;
+
+    printf("Cognitive Engine:: ..---===***### Channel %i has been selected ###***===---..\n",channel); 
+    /* Package up the new set of parameters in order to add
+       the new entry into the CBR database.  */
+
+    size_t returnValueIndex = 0;
+    for(size_t i = 0; i < radioInfo->numUtilities; i++) {
+        uList[i].value = returnValues[returnValueIndex];
+        returnValueIndex++;
+    }
+    for(size_t i = 0; i < radioInfo->numParameters; i++) {
+        pList[i].value = returnValues[returnValueIndex];
+        returnValueIndex++;
+    }
+    for(size_t i = 0; i < radioInfo->numObservables; i++) {
+        oList[i].value = returnValues[returnValueIndex];
+        returnValueIndex++;
+    }
+    //returnValues[returnValueIndex] = 0;
+
+    string allNames[numberColumns];
+    size_t allNameIndex = 0;
+    for(size_t i = 0; i < radioInfo->numUtilities; i++) {
+        allNames[allNameIndex] = uList[i].name;
+        returnValues[allNameIndex] = uList[i].target;
+        allNameIndex++;
+    }
+    for(size_t i = 0; i < radioInfo->numParameters; i++) {
+        allNames[allNameIndex] = pList[i].name;
+        allNameIndex++;
+    }
+    for(size_t i = 0; i < radioInfo->numObservables; i++) {
+        allNames[allNameIndex] = oList[i].name;
+    //    returnValues[allNameIndex] = 0;
+        allNameIndex++;
+    }
+   
+    allNames[allNameIndex] = utility_name;
+
+    /* Add the new optimized set to the CBR database */
+    //myCBR->AddRow(allNames, returnValues, returnValueIndex+1);
+
+
+    /* Return the set of new parameter values.  */
+    return pList;
+}
+
+
+Parameter* 
+DSA_CE::GetSolution(Observable *observables, \
+        Parameter *currentParameters, std::string service)
+{
+    LOG("Cognitive Engine:: Generating solution for %s service.\n", service.c_str());
+
+    return pList;
+}
+
 
 void 
-CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters)
+DSA_CE::ReceiveFeedback(Observable *observables, Parameter *parameters)
 { 
    LOG("Cognitive Engine:: Receiving feedback.\n");
@@ -124,6 +347,6 @@
     uint32_t obsColumns = radioInfo->numObservables + 1;
     uint32_t numberTotalColumns = radioInfo->numUtilities +
-        			radioInfo->numParameters +
-        			radioInfo->numObservables + 1;
+                    radioInfo->numParameters +
+                    radioInfo->numObservables + 1;
 
     float valList[numberColumns];
@@ -190,17 +413,17 @@
 
     if((observables[COMMUNICATION_TIME].value != 0) || (observables[ENERGY].value > Detection_Threshold)) { 
-	printf("Cognitive Engine:: Possible PU Detection - Decrementing Utility. %f %f",observables[COMMUNICATION_TIME].value,observables[ENERGY].value);
-	newUtilityValue = newUtilityValue + punishment;
+    printf("Cognitive Engine:: Possible PU Detection - Decrementing Utility. %f %f",observables[COMMUNICATION_TIME].value,observables[ENERGY].value);
+    newUtilityValue = newUtilityValue + punishment;
     } else {
-	printf("Cognitive Engine:: Scan Mode:: No PU detected - Incrementing Utility.");
-	newUtilityValue = newUtilityValue + reward;
+    printf("Cognitive Engine:: Scan Mode:: No PU detected - Incrementing Utility.");
+    newUtilityValue = newUtilityValue + reward;
     }
 
     if(newUtilityValue <= 100)
-	newUtilityValue = 100;
+    newUtilityValue = 100;
 
     // Put limit on utility score to prevent a positive feedback loop 
     if(newUtilityValue >= 800)
-	newUtilityValue = 800;
+    newUtilityValue = 800;
 
     obsVals[obsValueIndex] = newUtilityValue;
@@ -225,629 +448,13 @@
 
 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, "update_performance") == 0) {
-            
-            /* Receive Set of current Parameters */
-            memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD, buffer);
-            uint32_t numParameters = atoi(buffer);
-   
-            Parameter *p = new Parameter[numParameters];
- 
-            for(size_t i = 0; i < numParameters; i++) {
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                p[i].name = std::string(buffer);
-    
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                p[i].value = atof(buffer);
-            } 
-
-            /* Receive Set of Observables */
-            memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD, buffer);
-            uint32_t numObservables = atoi(buffer);
-   
-            Observable *o = new Observable[numObservables];
- 
-            for(size_t i = 0; i < numObservables; i++) {
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                o[i].name = std::string(buffer);
-    
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                o[i].value = atof(buffer);
-            }  
-
-            ReceiveFeedback(o,p);
-
-            delete [] o;
-            delete [] p;
-        } 
-        else if(strcmp(buffer, "request_optimization_service") == 0) {
-	    // THIS IS CURRENTLY IN DEMO MODE            
-
-
-            /* Receive Set of Observables */
-            LOG("\nCognitive Engine:: Receiving service name\n");
-
-            memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD,buffer);
-            LOG("\nCognitive Engine:: Got service name, %s\n", buffer);
-
-            /* Receive Set of Observables */
-            LOG("\nCognitive Engine:: Receiving Observable Parameters\n");
-
-            memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD,buffer);
-            uint32_t numObservables = atoi(buffer);
-   
-            Observable *o = new Observable[numObservables];
- 
-            for(size_t i = 0; i < numObservables; i++) {
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                o[i].name = std::string(buffer);
-    
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                o[i].value = atof(buffer);
-            }  
-
-            /* Receive Set of current Parameters */
-            LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n");
-
-            memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD, buffer);
-            uint32_t numCurrentParameters = atoi(buffer);
-   
-            Parameter *cp = new Parameter[numCurrentParameters];
- 
-            for(size_t i = 0; i < numCurrentParameters; i++) {
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                cp[i].name = std::string(buffer);
-    
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                cp[i].value = atof(buffer);
-            }  
-            LOG("Cognitive Engine:: Processing parameters....\n");
-
-            // TODO need to actually do something with the observables here
-           
-            LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n");
-            char numParametersChar[10];
-            sprintf(numParametersChar, "%i", radioInfo->numParameters);
-            SendMessage(commandSocketFD, numParametersChar);
-            for(size_t i = 0; i < radioInfo->numParameters; i++) {
-		SendMessage(commandSocketFD, "test");
-		SendMessage(commandSocketFD, "00");
-            }
-
-            delete [] o;
-            delete [] cp;
-        }
-        else if(strcmp(buffer, "request_optimization") == 0) {
-            
-            /* Receive Set of Observables */
-            LOG("\nCognitive Engine:: Receiving Observable Parameters\n");
-
-            memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD,buffer);
-            uint32_t numObservables = atoi(buffer);
-   
-            Observable *o = new Observable[numObservables];
- 
-            for(size_t i = 0; i < numObservables; i++) {
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                o[i].name = std::string(buffer);
-    
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                o[i].value = atof(buffer);
-            }  
-
-            /* Receive Set of current Parameters */
-            LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n");
-
-            memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD, buffer);
-            uint32_t numCurrentParameters = atoi(buffer);
-   
-            Parameter *cp = new Parameter[numCurrentParameters];
- 
-            for(size_t i = 0; i < numCurrentParameters; i++) {
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                cp[i].name = std::string(buffer);
-    
-                memset(buffer, 0, 256);
-                ReadMessage(commandSocketFD, buffer);
-                cp[i].value = atof(buffer);
-            }  
-            LOG("Cognitive Engine:: Processing parameters....\n");
-
-            Parameter *solutionSet;
-            
-            solutionSet = GetSolution(o,cp);
-
-            // TODO need to actually do something with the observables here
-           
-            LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n");
-            char numParametersChar[10];
-            char solutionValue[50];
-            sprintf(numParametersChar, "%i", radioInfo->numParameters);
-            SendMessage(commandSocketFD, numParametersChar);
-            for(size_t i = 0; i < radioInfo->numParameters; i++) {
-                SendMessage(commandSocketFD, solutionSet[i].name.c_str());
-                memset(solutionValue, 0, 50);
-                sprintf(solutionValue, "%f", solutionSet[i].value);
-                SendMessage(commandSocketFD, solutionValue);
-            }
-
-            delete [] o;
-            delete [] cp;
-        }
-        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?
+DSA_CE::ReceiveFeedback(Observable *observables, Parameter *parameters, \
+    std::string service)
+{
+    LOG("Cognitive Engine:: Receiving feedback.\n");
 }
 
 
 void
-CognitiveEngine::Reset()
-{
-    LOG("Resetting Cognitive Engine.\n");
-
-    if(SML_present) {
-        DeregisterServices();
-        DeregisterComponent();
-    } 
-    else {
-        DeregisterComponent();
-    }
-}
-
-
-void
-CognitiveEngine::RegisterComponent()
-{
-    char buffer[256];
-    SendMessage(commandSocketFD, "register_engine_cognitive");
-    LOG("Cognitive Engine:: Registration message sent to shell.\n");
-
-    memset(buffer, 0, 256);
-    ReadMessage(commandSocketFD, buffer);
-}
-
-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, "test_srv");
-
-    SendMessage(commandSocketFD, "register_service");
-    SendMessage(commandSocketFD, "test_srv1");
-
-    SendMessage(commandSocketFD, "register_service");
-    SendMessage(commandSocketFD, "test_srv2");
-
-    SendMessage(commandSocketFD, "register_service");
-    SendMessage(commandSocketFD, "test_srv3");
-
-}
-
-//Combined with deregister component since those two things must happen togeather
-void 
-CognitiveEngine::DeregisterServices()
-{
-    LOG("Cognitive Engine:: Deregistering services.\n");
-
-    SendMessage(commandSocketFD, "deregister_service");
-    SendMessage(commandSocketFD, "test_srv");
-
-    SendMessage(commandSocketFD, "deregister_service");
-    SendMessage(commandSocketFD, "test_srv1");
-
-    SendMessage(commandSocketFD, "deregister_service");
-    SendMessage(commandSocketFD, "test_srv2");
-
-    SendMessage(commandSocketFD, "deregister_service");
-    SendMessage(commandSocketFD, "test_srv3");
-
-}
-
-void 
-CognitiveEngine::ReceiveRadioConfiguration()
-{
-    LOG("Cognitive Engine:: Receiving Radio Configuration.\n");
-    
-    char buffer[256];
- 
-    /* Receive Set of Utilities */
-    memset(buffer, 0, 256);
-    ReadMessage(commandSocketFD, buffer);
-    radioInfo->numUtilities = atoi(buffer);
-
-    for(size_t i = 0; i < radioInfo->numUtilities; i++) {
-        memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD, 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);
-    
-    for(size_t i = 0; i < radioInfo->numParameters; i++) {
-        memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD, buffer);
-        pList[i].name = std::string(buffer);
-    
-        memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD, buffer);
-        pList[i].units = std::string(buffer);
-
-        memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD, buffer);
-        pList[i].min = atof(buffer);
-    
-        memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD, buffer);
-        pList[i].max = atof(buffer);
-    
-        memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD, buffer);
-        pList[i].step = atof(buffer);
-    
-        memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD,buffer);
-        pList[i].numAffects = atoi(buffer);
-    
-        for(size_t j = 0; j < pList[i].numAffects; j++) {
-            memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD,buffer);
-            // TODO for + if{break} = while?
-            for(size_t 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);
-            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(size_t i = 0; i < radioInfo->numObservables; i++) {
-        memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD, buffer);
-        oList[i].name = std::string(buffer);
-    
-        memset(buffer, 0, 256);
-        ReadMessage(commandSocketFD, buffer);
-        oList[i].numAffects = atoi(buffer);
-    
-        for(size_t j = 0; j < oList[i].numAffects; j++) {
-            memset(buffer, 0, 256);
-            ReadMessage(commandSocketFD, buffer);
-            // TODO for + if{break} = while?
-            for(size_t 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);
-            oList[i].affection_list[j].relation = std::string(buffer);   
-        }
-    }
-
-    SendMessage(commandSocketFD, "receive_config_ack");
-
-    BuildCognitiveEngine();
-}
-
-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");
-}
-
-
-/* The core of the CE is this function */
-
-Parameter* 
-CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters)
-{
-    LOG("Cognitive Engine:: Generating solution.\n");
-
-    /* Put together the CBR search array */
-
-    uint32_t channel = 0;
-    string searchNames[1];
-    string sumSearchName;
-    float searchVals[1];
-    float utilArray[(int)pList[0].max];
-    int searchOps[1];
-    uint32_t numberColumns = radioInfo->numUtilities + radioInfo->numParameters + \
-                             radioInfo->numObservables + 1;
-
-    float returnValues[numberColumns];
-    float sumRetVals[numberColumns];
-   
-    // Total sum of utilities in sumRetVals[0]
-
-    string channel_name = "channel";
-    string utility_name = "utility";
-
-    for(int32_t i = 0 ; i < pList[0].max ; i++ ) {
-        searchNames[0] = pList[0].name;
-        searchOps[0] = 0;
-        searchVals[0] = i+1; 
-    
-        uint32_t rc = myCBR->Search(searchNames, searchOps, searchVals,
-            1, returnValues);
-
-	if(rc == 31337) {
-		// No entry - must add
-		
-    		string rowNames[numberColumns];
-    		size_t rowIndex = 0;
-    		for(size_t j = 0; j < radioInfo->numUtilities; j++) {
-        		rowNames[rowIndex] = uList[j].name;
-        		rowIndex++;
-    		}
-    		for(size_t j = 0; j < radioInfo->numParameters; j++) {
-        		rowNames[rowIndex] = pList[j].name;
-			if(pList[j].name == "channel")
-				returnValues[rowIndex] = i+1;
-        		rowIndex++;
-    		}
-    		for(size_t j = 0; j < radioInfo->numObservables; j++) {
-        		rowNames[rowIndex] = oList[j].name;
-        		rowIndex++;
-    		}
-   
-    		rowNames[rowIndex] = utility_name;
-		returnValues[rowIndex] = 500;
-
-                /* Add the new optimized set to the CBR database */
-
-    		myCBR->AddRow(rowNames, returnValues, numberColumns);
-	}
-
-	utilArray[i] = returnValues[UTILITY]; 
-    }
-
-    printf("Current Channel Utility Scores\n");
-    printf("1: %f\t7: %f\t8: %f\t14: %f\n",utilArray[0],utilArray[1],utilArray[2],utilArray[3]);    
-    // Get sum of all the channel utilities.
-    sumSearchName = utility_name;
-    uint32_t rc = myCBR->SearchSum(sumSearchName, sumRetVals);
-
-    // Psuedo random channel selection based upon utility.
-    int k = rand() % (int)sumRetVals[0];
-    int cdf_total(0);
-
-    for ( int i = 0; i < pList[0].max; i++ ) {
-        cdf_total += utilArray[i];
-        if(k < cdf_total) {
-		channel = i + 1;
-		break;
-	}
-    }
-        
-    searchNames[0] = pList[0].name;
-    searchOps[0] = 0;
-    searchVals[0] = channel; 
-  
-    rc = myCBR->Search(searchNames, searchOps, searchVals,
-        1, returnValues);
-
-
-    //returnValues[CHANNEL] = rand() % (int)pList[0].max + (int)pList[0].min; 
-    returnValues[CHANNEL] = channel;
-
-    printf("Cognitive Engine:: ..---===***### Channel %i has been selected ###***===---..\n",channel); 
-    /* Package up the new set of parameters in order to add
-       the new entry into the CBR database.  */
-
-    size_t returnValueIndex = 0;
-    for(size_t i = 0; i < radioInfo->numUtilities; i++) {
-        uList[i].value = returnValues[returnValueIndex];
-        returnValueIndex++;
-    }
-    for(size_t i = 0; i < radioInfo->numParameters; i++) {
-        pList[i].value = returnValues[returnValueIndex];
-        returnValueIndex++;
-    }
-    for(size_t i = 0; i < radioInfo->numObservables; i++) {
-        oList[i].value = returnValues[returnValueIndex];
-        returnValueIndex++;
-    }
-    //returnValues[returnValueIndex] = 0;
-
-    string allNames[numberColumns];
-    size_t allNameIndex = 0;
-    for(size_t i = 0; i < radioInfo->numUtilities; i++) {
-        allNames[allNameIndex] = uList[i].name;
-        returnValues[allNameIndex] = uList[i].target;
-        allNameIndex++;
-    }
-    for(size_t i = 0; i < radioInfo->numParameters; i++) {
-        allNames[allNameIndex] = pList[i].name;
-        allNameIndex++;
-    }
-    for(size_t i = 0; i < radioInfo->numObservables; i++) {
-        allNames[allNameIndex] = oList[i].name;
-    //    returnValues[allNameIndex] = 0;
-        allNameIndex++;
-    }
-   
-    allNames[allNameIndex] = utility_name;
-
-    /* Add the new optimized set to the CBR database */
-    //myCBR->AddRow(allNames, returnValues, returnValueIndex+1);
-
-
-    /* Return the set of new parameter values.  */
-    return pList;
-}
-
-
-Parameter* 
-CognitiveEngine::GetSolution(Observable *observables, \
-        Parameter *currentParameters, std::string service)
-{
-    LOG("Cognitive Engine:: Generating solution for %s service.\n", service.c_str());
-
-    return pList;
-}
-
-
-void 
-CognitiveEngine::ReceiveFeedback(Observable *observables, Parameter *parameters, \
-    std::string service)
-{
-    LOG("Cognitive Engine:: Receiving feedback.\n");
-}
-
-
-void
-CognitiveEngine::BuildCognitiveEngine()
+DSA_CE::BuildCognitiveEngine()
 {
     string filename = "ex1";
@@ -886,2 +493,256 @@
 }
 
+
+void 
+DSA_CE::PerformUpdatePerformance()
+{
+    /* Receive Set of current Parameters */
+    char buffer[256];
+    memset(buffer, 0, 256);
+    ReadMessage(commandSocketFD, buffer);
+    uint32_t numParameters = atoi(buffer);
+
+    Parameter *p = new Parameter[numParameters];
+
+    for(size_t i = 0; i < numParameters; i++) {
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        p[i].name = std::string(buffer);
+
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        p[i].value = atof(buffer);
+    } 
+
+    /* Receive Set of Observables */
+    memset(buffer, 0, 256);
+    ReadMessage(commandSocketFD, buffer);
+    uint32_t numObservables = atoi(buffer);
+
+    Observable *o = new Observable[numObservables];
+
+    for(size_t i = 0; i < numObservables; i++) {
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        o[i].name = std::string(buffer);
+
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        o[i].value = atof(buffer);
+    }  
+
+    ReceiveFeedback(o,p);
+
+    delete [] o;
+    delete [] p;
+}
+
+
+void
+DSA_CE::PerformRequestOptimizationService()
+{
+    // THIS IS CURRENTLY IN DEMO MODE            
+
+
+    /* Receive Set of Observables */
+    LOG("\nCognitive Engine:: Receiving service name\n");
+
+    char buffer[256];
+    memset(buffer, 0, 256);
+    ReadMessage(commandSocketFD,buffer);
+    LOG("\nCognitive Engine:: Got service name, %s\n", buffer);
+
+    /* Receive Set of Observables */
+    LOG("\nCognitive Engine:: Receiving Observable Parameters\n");
+
+    memset(buffer, 0, 256);
+    ReadMessage(commandSocketFD,buffer);
+    uint32_t numObservables = atoi(buffer);
+
+    Observable *o = new Observable[numObservables];
+
+    for(size_t i = 0; i < numObservables; i++) {
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        o[i].name = std::string(buffer);
+
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        o[i].value = atof(buffer);
+    }  
+
+    /* Receive Set of current Parameters */
+    LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n");
+
+    memset(buffer, 0, 256);
+    ReadMessage(commandSocketFD, buffer);
+    uint32_t numCurrentParameters = atoi(buffer);
+
+    Parameter *cp = new Parameter[numCurrentParameters];
+
+    for(size_t i = 0; i < numCurrentParameters; i++) {
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        cp[i].name = std::string(buffer);
+
+        memset(buffer, 0, 256);
+        ReadMessage(commandSocketFD, buffer);
+        cp[i].value = atof(buffer);
+    }  
+    LOG("Cognitive Engine:: Processing parameters....\n");
+
+    // TODO need to actually do something with the observables here
+   
+    LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n");
+    char numParametersChar[10];
+    sprintf(numParametersChar, "%i", radioInfo->numParameters);
+    SendMessage(commandSocketFD, numParametersChar);
+    for(size_t i = 0; i < radioInfo->numParameters; i++) {
+        SendMessage(commandSocketFD, "test");
+        SendMessage(commandSocketFD, "00");
+    }
+
+    delete [] o;
+    delete [] cp;
+}
+
+
+void
+DSA_CE::PerformRequestOptimization()
+{
+	/* Receive Set of Observables */
+	LOG("\nCognitive Engine:: Receiving Observable Parameters\n");
+
+    char buffer[256];
+	memset(buffer, 0, 256);
+	ReadMessage(commandSocketFD,buffer);
+	uint32_t numObservables = atoi(buffer);
+
+	Observable *o = new Observable[numObservables];
+
+	for(size_t i = 0; i < numObservables; i++) {
+		memset(buffer, 0, 256);
+		ReadMessage(commandSocketFD, buffer);
+		o[i].name = std::string(buffer);
+
+		memset(buffer, 0, 256);
+		ReadMessage(commandSocketFD, buffer);
+		o[i].value = atof(buffer);
+	}  
+
+	/* Receive Set of current Parameters */
+	LOG("Cognitive Engine:: Receiving Current Transmission Parameters\n");
+
+	memset(buffer, 0, 256);
+	ReadMessage(commandSocketFD, buffer);
+	uint32_t numCurrentParameters = atoi(buffer);
+
+	Parameter *cp = new Parameter[numCurrentParameters];
+
+	for(size_t i = 0; i < numCurrentParameters; i++) {
+		memset(buffer, 0, 256);
+		ReadMessage(commandSocketFD, buffer);
+		cp[i].name = std::string(buffer);
+
+		memset(buffer, 0, 256);
+		ReadMessage(commandSocketFD, buffer);
+		cp[i].value = atof(buffer);
+	}  
+	LOG("Cognitive Engine:: Processing parameters....\n");
+
+	Parameter *solutionSet;
+	
+	solutionSet = GetSolution(o,cp);
+
+	// TODO need to actually do something with the observables here
+   
+	LOG("Cognitive Engine:: Sending Optimal Parameters to Application.\n");
+	char numParametersChar[10];
+	char solutionValue[50];
+	sprintf(numParametersChar, "%i", radioInfo->numParameters);
+	SendMessage(commandSocketFD, numParametersChar);
+	for(size_t i = 0; i < radioInfo->numParameters; i++) {
+		SendMessage(commandSocketFD, solutionSet[i].name.c_str());
+		memset(solutionValue, 0, 50);
+		sprintf(solutionValue, "%f", solutionSet[i].value);
+		SendMessage(commandSocketFD, solutionValue);
+	}
+
+	delete [] o;
+	delete [] cp;
+}
+
+
+void
+DSA_CE::PerformQueryComponentType()
+{
+	SendComponentType();
+}
+
+
+void
+DSA_CE::PerformConnectSML()
+{
+	/* 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);
+	}
+}
+
+
+void
+DSA_CE::PerformDisconnectSML()
+{
+	/* 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);
+	}
+}
+
+
+void
+DSA_CE::PerformResetEngineCognitive()
+{
+	Reset();
+}
+
+
+void
+DSA_CE::PerformShutdownEngineCognitive()
+{
+	Shutdown();
+}
+ 
Index: vtcross/branches/engineTreeFix/src/cognitive_engines/DSA_CE/Makefile.am
===================================================================
--- vtcross/branches/engineTreeFix/src/cognitive_engines/DSA_CE/Makefile.am	(revision 469)
+++ vtcross/branches/engineTreeFix/src/cognitive_engines/DSA_CE/Makefile.am	(revision 526)
@@ -22,4 +22,5 @@
 
 DSA_Demo_SOURCES = DSA_CognitiveEngine.cpp DSA_Demo.cpp
-DSA_Demo_LDADD = -lsqlite3 $(CROSS_SOCKETCOMM_LA) 
+DSA_Demo_DEPENDENCIES = ../libce.a
+DSA_Demo_LDADD = -lsqlite3 ../libce.a $(CROSS_SOCKETCOMM_LA) 
 
Index: vtcross/branches/engineTreeFix/src/cognitive_engines/DSA_CE/DSA_Demo.cpp
===================================================================
--- vtcross/branches/engineTreeFix/src/cognitive_engines/DSA_CE/DSA_Demo.cpp	(revision 465)
+++ vtcross/branches/engineTreeFix/src/cognitive_engines/DSA_CE/DSA_Demo.cpp	(revision 526)
@@ -41,5 +41,5 @@
        ERROR(1, "Usage: %s hostname port\n", argv[0]);
     
-    CognitiveEngine cognitiveEngine(argv[1], argv[2], false);
+    CognitiveEngine cognitiveEngine(argv[1], argv[2], 10, false);
 
     LOG("Waiting for signal...\n");
