root/vtcross/trunk/src/libvtcross/libvtcross.cpp @ 228

Revision 228, 2.8 KB (checked in by trnewman, 15 years ago)

Added sending current parameters in the libvt request optimization function.

Added guts to the CBR so it actually creates an sql db and searches it.

Line 
1/* Virginia Tech Cognitive Radio Open Source Systems
2 * Virginia Tech, 2009
3 *
4 * LICENSE INFORMATION GOES HERE
5 */
6
7/* Implementation file for the VCROSS Cognitive Radio public API defined in
8 * include/libvtcross.h.
9 *
10 * MORE INFO HERE
11 */
12
13#include <cstdlib>
14
15#include "vtcross/common.h"
16#include "vtcross/debug.h"
17#include "vtcross/libvtcross.h"
18
19
20uint32_t
21ConnectToRemoteComponent()
22{
23        uint32_t socket;
24    socket = ClientSocket("localhost", "40000");
25   
26    return socket;
27}
28
29uint32_t
30GetNumParameters()
31{
32        uint32_t socketfd, numParameters;
33    char buffer[256];
34
35    socketfd = ConnectToRemoteComponent();
36    SendMessage(socketfd,"get_number_parameters");
37   
38    memset(buffer, 0, 256);
39    ReadMessage(socketfd, buffer);
40    numParameters = atoi(buffer);
41       
42        return numParameters;
43}
44
45/* Given a certain set of observables, ask the radio to find the optimum radio
46 * parameters and return them.
47 *
48 * TODO I'm a little confused about this function... why would anyone need to
49 * use this?  Shouldn't this be internal to the radio operation?
50 *
51 * TODO this function is returning a pointer to allocated memory, which is fine,
52 * but we need to document this and make sure the caller is deallocating the
53 * memory when it is done using it.
54 */
55Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables,
56                Parameter *currentParameters, uint32_t numCurrentParameters) {
57    uint32_t i,socketfd,numParameters;
58    char var[50];
59    char counter[55];
60        char buffer[256];
61
62    uint32_t socketFD = ConnectToRemoteComponent();
63    SendMessage(socketFD, "request_optimization");
64
65    /* Get number of observables to send.  This information needs to be
66     * sent to the Cognitive Radio Shell also.   
67     */
68
69    // Send Observables
70    sprintf(counter, "%i", numObservables);
71    SendMessage(socketFD, counter);
72    for(size_t i = 0; i < numObservables; i++) {
73        SendMessage(socketFD, radioObservables[i].name.c_str());
74        sprintf(var, "%f", radioObservables[i].value);
75        SendMessage(socketFD, var);     
76    }
77
78    // Send Observables
79    memset(counter, 0, 55);
80    sprintf(counter,"%i",numCurrentParameters);
81    SendMessage(socketfd,counter);
82    for(i = 0; i < numCurrentParameters; i++) {
83        SendMessage(socketfd,currentParameters[i].name.c_str());
84        sprintf(var,"%f",currentParameters[i].value);
85        SendMessage(socketfd,var);     
86    }
87    /* Receive Set of Parameters */
88    memset(buffer, 0, 256);
89    ReadMessage(socketFD, buffer);
90    int32_t numParameters = atoi(buffer);
91    Parameter *pList = new Parameter[numParameters];
92   
93    for(ssize_t i = 0; i < numParameters; i++) {
94        memset(buffer, 0, 256);
95        ReadMessage(socketFD, buffer);
96        pList[i].name = std::string(buffer);
97       
98                memset(buffer, 0, 256);
99        ReadMessage(socketFD, buffer);
100        pList[i].value = atof(buffer);
101    }   
102
103    return pList;
104}
105
Note: See TracBrowser for help on using the browser.