1 | #include <stdio.h> |
---|
2 | #include <sys/types.h> |
---|
3 | #include <sys/socket.h> |
---|
4 | #include <netinet/in.h> |
---|
5 | #include <netdb.h> |
---|
6 | //#include <iostream> |
---|
7 | #include <stdlib.h> |
---|
8 | #include <string.h> |
---|
9 | |
---|
10 | #include "cbr.h" |
---|
11 | |
---|
12 | //using namespace std; |
---|
13 | |
---|
14 | struct CE_Info { |
---|
15 | int numUtilities; |
---|
16 | int numParameters; |
---|
17 | int numObservables; |
---|
18 | }; |
---|
19 | |
---|
20 | struct Utility { |
---|
21 | char name[50]; |
---|
22 | char units[50]; |
---|
23 | char goal[50]; |
---|
24 | /* string name; |
---|
25 | string units; |
---|
26 | string goal;*/ |
---|
27 | float target; |
---|
28 | }; |
---|
29 | |
---|
30 | struct Affect { |
---|
31 | struct Utility u; |
---|
32 | //string relation; |
---|
33 | char relation[50]; |
---|
34 | }; |
---|
35 | |
---|
36 | struct Parameter { |
---|
37 | /*string name; |
---|
38 | string units;*/ |
---|
39 | char name[50]; |
---|
40 | char units[50]; |
---|
41 | float min; |
---|
42 | int numAffects; |
---|
43 | struct Affect affection_list[10]; |
---|
44 | float max; |
---|
45 | float step; |
---|
46 | }; |
---|
47 | |
---|
48 | struct Observable { |
---|
49 | //string name; |
---|
50 | char name[50]; |
---|
51 | struct Affect affection_list[10]; |
---|
52 | int numAffects; |
---|
53 | }; |
---|
54 | |
---|
55 | // error handling |
---|
56 | void error(char *msg) |
---|
57 | { |
---|
58 | perror(msg); |
---|
59 | exit(0); |
---|
60 | } |
---|
61 | |
---|
62 | void ReadMessage(int socket,char * buffer) { |
---|
63 | int i,n; |
---|
64 | |
---|
65 | n = recv(socket,buffer,256,MSG_PEEK); |
---|
66 | for(i=0;i<256;i++){ |
---|
67 | if(strcmp(&buffer[i],"\0") == 0) break; |
---|
68 | } |
---|
69 | n = recv(socket,buffer,i+1,0); |
---|
70 | if (n < 0) |
---|
71 | error("ERROR reading from socket"); |
---|
72 | // printf("ReadMessage:%s %d\n",buffer,n); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | int GetUtility(int sockfd, struct Utility uList[], struct CE_Info *ce_info) |
---|
77 | { |
---|
78 | char buffer[256]; |
---|
79 | int i; |
---|
80 | |
---|
81 | // read unitilities |
---|
82 | // numUtilities |
---|
83 | bzero(buffer,256); |
---|
84 | ReadMessage(sockfd,buffer); |
---|
85 | ce_info->numUtilities = atoi(buffer); |
---|
86 | //printf("number of utilities: %d\n", ce_info->numUtilities); |
---|
87 | |
---|
88 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
89 | bzero(buffer,256); |
---|
90 | ReadMessage(sockfd,buffer); |
---|
91 | //printf("Name: %s\n", buffer); |
---|
92 | strcpy(uList[i].name, buffer); |
---|
93 | |
---|
94 | bzero(buffer,256); |
---|
95 | ReadMessage(sockfd,buffer); |
---|
96 | //printf("Units: %s\n", buffer); |
---|
97 | strcpy(uList[i].units, buffer); |
---|
98 | |
---|
99 | bzero(buffer,256); |
---|
100 | ReadMessage(sockfd,buffer); |
---|
101 | //printf("Goal: %s\n", buffer); |
---|
102 | strcpy(uList[i].goal, buffer); |
---|
103 | |
---|
104 | bzero(buffer,256); |
---|
105 | ReadMessage(sockfd,buffer); |
---|
106 | //printf("Target: %s\n", buffer); |
---|
107 | uList[i].target = atof(buffer); |
---|
108 | } |
---|
109 | return 1; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | int GetParameter(int sockfd, struct Parameter pList[], struct CE_Info *ce_info) |
---|
114 | { |
---|
115 | char buffer[256]; |
---|
116 | int i, j; |
---|
117 | |
---|
118 | // numParameters |
---|
119 | bzero(buffer,256); |
---|
120 | ReadMessage(sockfd,buffer); |
---|
121 | ce_info->numParameters = atoi(buffer); |
---|
122 | //printf("number of parameters: %d\n", ce_info->numParameters); |
---|
123 | |
---|
124 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
125 | bzero(buffer,256); |
---|
126 | ReadMessage(sockfd,buffer); |
---|
127 | //printf("Name: %s\n", buffer); |
---|
128 | strcpy(pList[i].name, buffer); |
---|
129 | |
---|
130 | bzero(buffer,256); |
---|
131 | ReadMessage(sockfd,buffer); |
---|
132 | //printf("Units: %s\n", buffer); |
---|
133 | strcpy(pList[i].units, buffer); |
---|
134 | |
---|
135 | bzero(buffer,256); |
---|
136 | ReadMessage(sockfd,buffer); |
---|
137 | //printf("Min: %s\n", buffer); |
---|
138 | pList[i].min = atof(buffer); |
---|
139 | |
---|
140 | bzero(buffer,256); |
---|
141 | ReadMessage(sockfd,buffer); |
---|
142 | //printf("Max: %s\n", buffer); |
---|
143 | pList[i].max = atof(buffer); |
---|
144 | |
---|
145 | bzero(buffer,256); |
---|
146 | ReadMessage(sockfd,buffer); |
---|
147 | //printf("Step: %s\n", buffer); |
---|
148 | pList[i].step = atof(buffer); |
---|
149 | |
---|
150 | // numAffects |
---|
151 | bzero(buffer,256); |
---|
152 | ReadMessage(sockfd,buffer); |
---|
153 | pList[i].numAffects = atoi(buffer); |
---|
154 | //printf("number of affects: %d\n", pList[i].numAffects); |
---|
155 | |
---|
156 | for (j = 0; j < pList[i].numAffects; j++){ |
---|
157 | bzero(buffer,256); |
---|
158 | ReadMessage(sockfd,buffer); |
---|
159 | //printf("Utility name: %s\n", buffer); |
---|
160 | strcpy(pList[i].affection_list[j].u.name, buffer); |
---|
161 | |
---|
162 | bzero(buffer,256); |
---|
163 | ReadMessage(sockfd,buffer); |
---|
164 | //printf("Relation: %s\n", buffer); |
---|
165 | strcpy(pList[i].affection_list[j].relation, buffer); |
---|
166 | } |
---|
167 | } |
---|
168 | return 1; |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | int GetObservable(int sockfd, struct Observable oList[], struct CE_Info *ce_info) |
---|
173 | { |
---|
174 | char buffer[256]; |
---|
175 | int i,j; |
---|
176 | |
---|
177 | // numParameters |
---|
178 | bzero(buffer,256); |
---|
179 | ReadMessage(sockfd,buffer); |
---|
180 | ce_info->numObservables = atoi(buffer); |
---|
181 | //printf("number of observables: %d\n", ce_info->numObservables); |
---|
182 | |
---|
183 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
184 | bzero(buffer,256); |
---|
185 | ReadMessage(sockfd,buffer); |
---|
186 | //printf("Name: %s\n", buffer); |
---|
187 | strcpy(oList[i].name, buffer); |
---|
188 | |
---|
189 | // numAffects |
---|
190 | bzero(buffer,256); |
---|
191 | ReadMessage(sockfd,buffer); |
---|
192 | oList[i].numAffects = atoi(buffer); |
---|
193 | //printf("number of affects: %d\n", oList[i].numAffects); |
---|
194 | |
---|
195 | for (j = 0; j < oList[i].numAffects; j++){ |
---|
196 | bzero(buffer,256); |
---|
197 | ReadMessage(sockfd,buffer); |
---|
198 | //printf("Utility name: %s\n", buffer); |
---|
199 | strcpy(oList[i].affection_list[j].u.name, buffer); |
---|
200 | |
---|
201 | bzero(buffer,256); |
---|
202 | ReadMessage(sockfd,buffer); |
---|
203 | //printf("Relation: %s\n", buffer); |
---|
204 | strcpy(oList[i].affection_list[j].relation, buffer); |
---|
205 | } |
---|
206 | } |
---|
207 | return 1; |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | int GetXMLFromServer(int sockfd, struct Utility uList[], struct Parameter pList[], |
---|
212 | struct Observable oList[], struct CE_Info *ce_info) |
---|
213 | { |
---|
214 | // get utilities |
---|
215 | GetUtility(sockfd, uList, ce_info); |
---|
216 | |
---|
217 | // get parameters |
---|
218 | GetParameter(sockfd, pList, ce_info); |
---|
219 | |
---|
220 | // read obervables |
---|
221 | GetObservable(sockfd, oList, ce_info); |
---|
222 | |
---|
223 | printf("xml reception done\n"); |
---|
224 | |
---|
225 | return 1; |
---|
226 | } |
---|
227 | |
---|
228 | void print_current_config(struct Utility uList[], struct Parameter pList[], |
---|
229 | struct Observable oList[], struct CE_Info * ce_info) |
---|
230 | { |
---|
231 | int i = 0; |
---|
232 | int j = 0; |
---|
233 | |
---|
234 | // utilities |
---|
235 | for(i = 0; i < ce_info->numUtilities ; i++) { |
---|
236 | printf("Utility: %s\n", uList[i].name); |
---|
237 | printf(" Units: %s\n", uList[i].units); |
---|
238 | printf(" Goal: %s\n", uList[i].goal); |
---|
239 | printf(" Target: %f\n", uList[i].target); |
---|
240 | /*cout << "Utility: " << uList[i].name << endl; |
---|
241 | cout << " Units: " << uList[i].units << endl; |
---|
242 | cout << " Goal: " << uList[i].goal << endl; |
---|
243 | cout << " Target: " << uList[i].target << endl;*/ |
---|
244 | } |
---|
245 | |
---|
246 | // parameters |
---|
247 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
248 | printf("Paramter: %s\n", pList[i].name); |
---|
249 | printf(" Units: %s\n", pList[i].units); |
---|
250 | printf(" Min: %f\n", pList[i].min); |
---|
251 | printf(" Max: %f\n", pList[i].max); |
---|
252 | printf(" Step: %f\n", pList[i].step); |
---|
253 | /*cout << "Parameter: " << pList[i].name << endl; |
---|
254 | cout << " Units: " << pList[i].units << endl; |
---|
255 | cout << " Min: " << pList[i].min << endl; |
---|
256 | cout << " Max: " << pList[i].max << endl; |
---|
257 | cout << " Step: " << pList[i].step << endl;*/ |
---|
258 | for(j = 0; j < pList[i].numAffects; j++) { |
---|
259 | printf(" Affect: %s -> %s\n", pList[i].affection_list[j].u.name, pList[i].affection_list[j].relation); |
---|
260 | //cout << " Affect: " << pList[i].affection_list[j].u.name << " -> " << pList[i].affection_list[j].relation << endl; |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | // observables |
---|
265 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
266 | printf("Observable: %s\n", oList[i].name); |
---|
267 | //cout << "Observable: " << oList[i].name << endl; |
---|
268 | for(j = 0; j < oList[i].numAffects; j++) { |
---|
269 | printf(" Affect: %s -> %s\n", oList[i].affection_list[j].u.name, oList[i].affection_list[j].relation); |
---|
270 | //cout << " Affect: " << oList[i].affection_list[j].u.name << " -> " << oList[i].affection_list[j].relation << endl; |
---|
271 | } |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | |
---|
276 | // main client socket |
---|
277 | int main(int argc, char *argv[]) |
---|
278 | { |
---|
279 | struct Utility uList[10]; |
---|
280 | struct Parameter pList[10]; |
---|
281 | struct Observable oList[10]; |
---|
282 | struct CE_Info ce_info; |
---|
283 | |
---|
284 | int sockfd, portno; |
---|
285 | struct sockaddr_in serv_addr; |
---|
286 | struct hostent *server; |
---|
287 | |
---|
288 | // setup client socket connection |
---|
289 | if (argc < 3) { |
---|
290 | fprintf(stderr,"usage: %s hostname port\n", argv[0]); |
---|
291 | exit(0); |
---|
292 | } |
---|
293 | // server name |
---|
294 | server = gethostbyname(argv[1]); |
---|
295 | if (server == NULL) { |
---|
296 | fprintf(stderr,"ERROR, no such host\n"); |
---|
297 | exit(0); |
---|
298 | } |
---|
299 | // port no. |
---|
300 | portno = atoi(argv[2]); |
---|
301 | // socket file descriptor |
---|
302 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
303 | if (sockfd < 0) |
---|
304 | error("ERROR opening socket"); |
---|
305 | // initiate struct socketaddr_in |
---|
306 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
---|
307 | serv_addr.sin_family = AF_INET; |
---|
308 | serv_addr.sin_port = htons(portno); |
---|
309 | bcopy((char *)server->h_addr, |
---|
310 | (char *)&serv_addr.sin_addr.s_addr, |
---|
311 | server->h_length); |
---|
312 | // cast sockaddr_in to sockaddr |
---|
313 | if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) |
---|
314 | error("ERROR connecting"); |
---|
315 | |
---|
316 | // get xml info from server |
---|
317 | GetXMLFromServer(sockfd, uList, pList, oList, &ce_info); |
---|
318 | |
---|
319 | //printf("utility:\n name: %s, units: %s, goal: %s, target: %f\n", |
---|
320 | // uList[0].name, uList[0].units, uList[0].goal, uList[0].target); |
---|
321 | |
---|
322 | print_current_config(uList, pList, oList, &ce_info); |
---|
323 | |
---|
324 | // cbr operation |
---|
325 | int i; |
---|
326 | unsigned int num_cols; |
---|
327 | |
---|
328 | num_cols = ce_info.numUtilities + ce_info.numParameters; |
---|
329 | num_cols = num_cols + ce_info.numObservables; |
---|
330 | num_cols = num_cols + 1; |
---|
331 | |
---|
332 | char *cols[] = {"throughput", "spectral_efficiency", |
---|
333 | "log10_ber", "mod_scheme", "tx_power", "SNR", "utility"}; |
---|
334 | |
---|
335 | float vals[num_cols]; |
---|
336 | vals[0] = 10e3f; // throughput |
---|
337 | vals[1] = 1; // spectral_efficiency |
---|
338 | vals[2] = 1e-3f; // log10_ber |
---|
339 | vals[3] = 1; // mod_scheme |
---|
340 | vals[4] = -3.0f; // tx_power |
---|
341 | vals[5] = 10.0f; // SNR |
---|
342 | vals[6] = 0.762; // utility |
---|
343 | |
---|
344 | /*num_cols = 6; |
---|
345 | char * cols[] = { |
---|
346 | "BER", "throughput", |
---|
347 | "mod_scheme", "tx_power", |
---|
348 | "noise_power", "path_loss" |
---|
349 | }; |
---|
350 | |
---|
351 | float vals[num_cols]; |
---|
352 | vals[0] = 1e-3f; // BER |
---|
353 | vals[1] = 10e3f; // throughput |
---|
354 | vals[2] = 1; // mod_scheme |
---|
355 | vals[3] = -3.0f; // tx_power |
---|
356 | vals[4] = -50.0f; // noise_power |
---|
357 | vals[5] = 125.0f; // path_loss |
---|
358 | //vals[6] = 0.762; // utility*/ |
---|
359 | |
---|
360 | printf("number of columns: %d\n", num_cols); |
---|
361 | printf("column names:"); |
---|
362 | for (i = 0; i<num_cols; i++){ |
---|
363 | printf(" %s",cols[i]); |
---|
364 | } |
---|
365 | printf("\n"); |
---|
366 | |
---|
367 | int rc; |
---|
368 | |
---|
369 | // create cbr database/table |
---|
370 | printf("create cbr database\n"); |
---|
371 | char filename[] = {"ex1"}, tablename[] = {"data"}; |
---|
372 | cbr mycbr = cbr_create(filename, tablename, cols, num_cols); |
---|
373 | //cbr mycbr = cbr_create("ex1", "data", cols, num_cols); |
---|
374 | |
---|
375 | // add row here |
---|
376 | printf("add row to cbr table\n"); |
---|
377 | rc = cbr_add_row(mycbr, cols, vals, num_cols); |
---|
378 | |
---|
379 | // print |
---|
380 | printf("cbr table\n"); |
---|
381 | cbr_print(mycbr); |
---|
382 | |
---|
383 | /*// simple search: find entry where... |
---|
384 | // BER < 1e-2 and path_loss > 100 |
---|
385 | char * search_names[] = {"BER", "path_loss"}; |
---|
386 | int search_ops[] = {LT, GT}; |
---|
387 | float search_vals[] = {1e-2f, 100.f}; |
---|
388 | float retvals[num_cols]; |
---|
389 | printf("search cbr\n"); |
---|
390 | rc = cbr_search(mycbr, search_names, search_ops, search_vals, 2,retvals);*/ |
---|
391 | |
---|
392 | // simple search: find entry where... |
---|
393 | // log10_ber < 1e-2 and SNR > 5 |
---|
394 | char * search_names[] = {"log10_ber", "SNR"}; |
---|
395 | int search_ops[] = {LT, GT}; |
---|
396 | float search_vals[] = {1e-2f, 5.f}; |
---|
397 | float retvals[num_cols]; |
---|
398 | rc = cbr_search(mycbr, search_names, search_ops, |
---|
399 | search_vals, 2, retvals); |
---|
400 | |
---|
401 | // print returned values |
---|
402 | printf("search result: "); |
---|
403 | for (i=0;i<num_cols; i++) |
---|
404 | //for (i=0;i<mycbr->num_columns; i++) |
---|
405 | printf("%f, ",retvals[i]); |
---|
406 | printf("\n"); |
---|
407 | |
---|
408 | // clean up database |
---|
409 | printf("clean cbr database\n"); |
---|
410 | cbr_free(mycbr); |
---|
411 | |
---|
412 | printf("done at client.\n"); |
---|
413 | return 0; |
---|
414 | |
---|
415 | |
---|
416 | return 0; |
---|
417 | } |
---|