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

Revision 232, 4.4 KB (checked in by bhilburn, 15 years ago)

Changed more \t over to spaces. Please check your vimrcs!!!

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    // TODO why is this hardcoded like this??
24       return ClientSocket("localhost", "40000");
25}
26
27// TODO the following three functions all do exactly the same thing.  Why not
28// simply combine them?
29uint32_t
30GetNumObservables()
31{
32    char buffer[256];
33
34    uint32_t socketfd = ConnectToRemoteComponent();
35    SendMessage(socketfd, "get_number_observables");
36   
37    memset(buffer, 0, 256);
38    ReadMessage(socketfd, buffer);
39    uint32_t numObservables = atoi(buffer);
40   
41    return numObservables;
42}
43
44uint32_t
45GetNumUtilities()
46{
47    char buffer[256];
48
49    uint32_t socketfd = ConnectToRemoteComponent();
50    SendMessage(socketfd, "get_number_utilities");
51   
52    memset(buffer, 0, 256);
53    ReadMessage(socketfd, buffer);
54    uint32_t numUtilities = atoi(buffer);
55   
56    return numUtilities;
57}
58
59uint32_t
60GetNumParameters()
61{
62    char buffer[256];
63
64    uint32_t socketfd = ConnectToRemoteComponent();
65    SendMessage(socketfd, "get_number_parameters");
66   
67    memset(buffer, 0, 256);
68    ReadMessage(socketfd, buffer);
69    uint32_t numParameters = atoi(buffer);
70   
71    return numParameters;
72}
73// end previous TODO
74
75/* Given a certain set of observables, ask the radio to find the optimum radio
76 * parameters and return them.
77 *
78 * TODO I'm a little confused about this function... why would anyone need to
79 * use this?  Shouldn't this be internal to the radio operation?
80 *
81 * TODO this function is returning a pointer to allocated memory, which is fine,
82 * but we need to document this and make sure the caller is deallocating the
83 * memory when it is done using it.
84 */
85Parameter*
86GetOptimalParameters(Observable *radioObservables, uint32_t numObservables,
87        Parameter *currentParameters, uint32_t numCurrentParameters)
88{
89    char var[50];
90    char counter[55];
91    char buffer[256];
92
93    uint32_t socketFD = ConnectToRemoteComponent();
94    SendMessage(socketFD, "request_optimization");
95
96    /* Get number of observables to send.  This information needs to be
97     * sent to the Cognitive Radio Shell also.   
98     */
99
100    /* Send Observables */
101    sprintf(counter, "%i", numObservables);
102    SendMessage(socketFD, counter);
103    for(size_t i = 0; i < numObservables; i++) {
104        SendMessage(socketFD, radioObservables[i].name.c_str());
105        sprintf(var, "%f", radioObservables[i].value);
106        SendMessage(socketFD, var);   
107    }
108
109    /* Send Parameters */
110    memset(counter, 0, 55);
111    sprintf(counter, "%i", numCurrentParameters);
112    SendMessage(socketFD, counter);
113    for(size_t i = 0; i < numCurrentParameters; i++) {
114        SendMessage(socketFD,currentParameters[i].name.c_str());
115        sprintf(var,"%f",currentParameters[i].value);
116        SendMessage(socketFD,var);   
117    }
118
119    /* Receive Set of Parameters */
120    memset(buffer, 0, 256);
121    ReadMessage(socketFD, buffer);
122    uint32_t numParameters = atoi(buffer);
123    Parameter *pList = new Parameter[numParameters];
124   
125    for(ssize_t i = 0; i < numParameters; i++) {
126        memset(buffer, 0, 256);
127        ReadMessage(socketFD, buffer);
128        pList[i].name = std::string(buffer);
129       
130        memset(buffer, 0, 256);
131        ReadMessage(socketFD, buffer);
132        pList[i].value = atof(buffer);
133    }   
134
135    return pList;
136}
137
138bool
139UpdateParameterPerformance(Parameter *p, uint32_t numParameters, Observable *o,
140        uint32_t numObservables)
141{
142    char counter[55];
143    char var[50];
144
145    uint32_t socketFD = ConnectToRemoteComponent();
146    SendMessage(socketFD, "update_performance");
147   
148    // Send Parameters
149    memset(counter, 0, 55);
150    sprintf(counter, "%i", numParameters);
151    SendMessage(socketFD, counter);
152   
153    for(size_t i = 0; i < numParameters; i++) {
154        SendMessage(socketFD, p[i].name.c_str());
155        sprintf(var, "%f", p[i].value);
156        SendMessage(socketFD, var);   
157    }
158   
159    // Send Observables
160    sprintf(counter, "%i", numObservables);
161    SendMessage(socketFD, counter);
162    for(size_t i = 0; i < numObservables; i++) {
163        SendMessage(socketFD, o[i].name.c_str());
164        sprintf(var, "%f", o[i].value);
165        SendMessage(socketFD, var);   
166    }
167}
Note: See TracBrowser for help on using the browser.