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

Revision 227, 2.1 KB (checked in by bhilburn, 15 years ago)

Memory leaks, style fixes, variable scoping.

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
29
30/* Given a certain set of observables, ask the radio to find the optimum radio
31 * parameters and return them.
32 *
33 * TODO I'm a little confused about this function... why would anyone need to
34 * use this?  Shouldn't this be internal to the radio operation?
35 *
36 * TODO this function is returning a pointer to allocated memory, which is fine,
37 * but we need to document this and make sure the caller is deallocating the
38 * memory when it is done using it.
39 */
40Parameter*
41GetOptimalParameters(Observable *radioObservables, uint32_t numObservables)
42{
43    char var[50];
44    char counter[55];
45        char buffer[256];
46
47    uint32_t socketFD = ConnectToRemoteComponent();
48    SendMessage(socketFD, "request_optimization");
49
50    /* Get number of observables to send.  This information needs to be
51     * sent to the Cognitive Radio Shell also.   
52     */
53
54    // Send Observables
55    sprintf(counter, "%i", numObservables);
56    SendMessage(socketFD, counter);
57    for(size_t i = 0; i < numObservables; i++) {
58        SendMessage(socketFD, radioObservables[i].name.c_str());
59        sprintf(var, "%f", radioObservables[i].value);
60        SendMessage(socketFD, var);     
61    }
62
63    /* Receive Set of Parameters */
64    memset(buffer, 0, 256);
65    ReadMessage(socketFD, buffer);
66    int32_t numParameters = atoi(buffer);
67    Parameter *pList = new Parameter[numParameters];
68   
69    for(ssize_t i = 0; i < numParameters; i++) {
70        memset(buffer, 0, 256);
71        ReadMessage(socketFD, buffer);
72        pList[i].name = std::string(buffer);
73       
74                memset(buffer, 0, 256);
75        ReadMessage(socketFD, buffer);
76        pList[i].value = atof(buffer);
77    }   
78
79    return pList;
80}
81
Note: See TracBrowser for help on using the browser.