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 | float value; |
---|
29 | }; |
---|
30 | |
---|
31 | struct Affect { |
---|
32 | struct Utility u; |
---|
33 | //string relation; |
---|
34 | char relation[50]; |
---|
35 | }; |
---|
36 | |
---|
37 | struct Parameter { |
---|
38 | /*string name; |
---|
39 | string units;*/ |
---|
40 | char name[50]; |
---|
41 | char units[50]; |
---|
42 | float min; |
---|
43 | int numAffects; |
---|
44 | struct Affect affection_list[10]; |
---|
45 | float max; |
---|
46 | float step; |
---|
47 | float value; |
---|
48 | }; |
---|
49 | |
---|
50 | struct Observable { |
---|
51 | //string name; |
---|
52 | char name[50]; |
---|
53 | struct Affect affection_list[10]; |
---|
54 | int numAffects; |
---|
55 | float value; |
---|
56 | }; |
---|
57 | |
---|
58 | // error handling |
---|
59 | void error(char *msg) |
---|
60 | { |
---|
61 | perror(msg); |
---|
62 | exit(0); |
---|
63 | } |
---|
64 | |
---|
65 | void ReadMessage(int socket,char * buffer) { |
---|
66 | int i,n; |
---|
67 | |
---|
68 | n = recv(socket,buffer,256,MSG_PEEK); |
---|
69 | for(i=0;i<256;i++){ |
---|
70 | if(strcmp(&buffer[i],"\0") == 0) break; |
---|
71 | } |
---|
72 | n = recv(socket,buffer,i+1,0); |
---|
73 | if (n < 0) |
---|
74 | error("ERROR reading from socket"); |
---|
75 | // printf("ReadMessage:%s %d\n",buffer,n); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | int GetUtility(int sockfd, struct Utility uList[], struct CE_Info *ce_info) |
---|
80 | { |
---|
81 | char buffer[256]; |
---|
82 | int i; |
---|
83 | |
---|
84 | // read unitilities |
---|
85 | // numUtilities |
---|
86 | bzero(buffer,256); |
---|
87 | ReadMessage(sockfd,buffer); |
---|
88 | ce_info->numUtilities = atoi(buffer); |
---|
89 | //printf("number of utilities: %d\n", ce_info->numUtilities); |
---|
90 | |
---|
91 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
92 | bzero(buffer,256); |
---|
93 | ReadMessage(sockfd,buffer); |
---|
94 | //printf("Name: %s\n", buffer); |
---|
95 | strcpy(uList[i].name, buffer); |
---|
96 | |
---|
97 | bzero(buffer,256); |
---|
98 | ReadMessage(sockfd,buffer); |
---|
99 | //printf("Units: %s\n", buffer); |
---|
100 | strcpy(uList[i].units, buffer); |
---|
101 | |
---|
102 | bzero(buffer,256); |
---|
103 | ReadMessage(sockfd,buffer); |
---|
104 | //printf("Goal: %s\n", buffer); |
---|
105 | strcpy(uList[i].goal, buffer); |
---|
106 | |
---|
107 | bzero(buffer,256); |
---|
108 | ReadMessage(sockfd,buffer); |
---|
109 | //printf("Target: %s\n", buffer); |
---|
110 | uList[i].target = atof(buffer); |
---|
111 | } |
---|
112 | return 1; |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | int GetParameter(int sockfd, struct Parameter pList[], struct CE_Info *ce_info) |
---|
117 | { |
---|
118 | char buffer[256]; |
---|
119 | int i, j; |
---|
120 | |
---|
121 | // numParameters |
---|
122 | bzero(buffer,256); |
---|
123 | ReadMessage(sockfd,buffer); |
---|
124 | ce_info->numParameters = atoi(buffer); |
---|
125 | //printf("number of parameters: %d\n", ce_info->numParameters); |
---|
126 | |
---|
127 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
128 | bzero(buffer,256); |
---|
129 | ReadMessage(sockfd,buffer); |
---|
130 | //printf("Name: %s\n", buffer); |
---|
131 | strcpy(pList[i].name, buffer); |
---|
132 | |
---|
133 | bzero(buffer,256); |
---|
134 | ReadMessage(sockfd,buffer); |
---|
135 | //printf("Units: %s\n", buffer); |
---|
136 | strcpy(pList[i].units, buffer); |
---|
137 | |
---|
138 | bzero(buffer,256); |
---|
139 | ReadMessage(sockfd,buffer); |
---|
140 | //printf("Min: %s\n", buffer); |
---|
141 | pList[i].min = atof(buffer); |
---|
142 | |
---|
143 | bzero(buffer,256); |
---|
144 | ReadMessage(sockfd,buffer); |
---|
145 | //printf("Max: %s\n", buffer); |
---|
146 | pList[i].max = atof(buffer); |
---|
147 | |
---|
148 | bzero(buffer,256); |
---|
149 | ReadMessage(sockfd,buffer); |
---|
150 | //printf("Step: %s\n", buffer); |
---|
151 | pList[i].step = atof(buffer); |
---|
152 | |
---|
153 | // numAffects |
---|
154 | bzero(buffer,256); |
---|
155 | ReadMessage(sockfd,buffer); |
---|
156 | pList[i].numAffects = atoi(buffer); |
---|
157 | //printf("number of affects: %d\n", pList[i].numAffects); |
---|
158 | |
---|
159 | for (j = 0; j < pList[i].numAffects; j++){ |
---|
160 | bzero(buffer,256); |
---|
161 | ReadMessage(sockfd,buffer); |
---|
162 | //printf("Utility name: %s\n", buffer); |
---|
163 | strcpy(pList[i].affection_list[j].u.name, buffer); |
---|
164 | |
---|
165 | bzero(buffer,256); |
---|
166 | ReadMessage(sockfd,buffer); |
---|
167 | //printf("Relation: %s\n", buffer); |
---|
168 | strcpy(pList[i].affection_list[j].relation, buffer); |
---|
169 | } |
---|
170 | } |
---|
171 | return 1; |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | int GetObservable(int sockfd, struct Observable oList[], struct CE_Info *ce_info) |
---|
176 | { |
---|
177 | char buffer[256]; |
---|
178 | int i,j; |
---|
179 | |
---|
180 | // numParameters |
---|
181 | bzero(buffer,256); |
---|
182 | ReadMessage(sockfd,buffer); |
---|
183 | ce_info->numObservables = atoi(buffer); |
---|
184 | //printf("number of observables: %d\n", ce_info->numObservables); |
---|
185 | |
---|
186 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
187 | bzero(buffer,256); |
---|
188 | ReadMessage(sockfd,buffer); |
---|
189 | //printf("Name: %s\n", buffer); |
---|
190 | strcpy(oList[i].name, buffer); |
---|
191 | |
---|
192 | // numAffects |
---|
193 | bzero(buffer,256); |
---|
194 | ReadMessage(sockfd,buffer); |
---|
195 | oList[i].numAffects = atoi(buffer); |
---|
196 | //printf("number of affects: %d\n", oList[i].numAffects); |
---|
197 | |
---|
198 | for (j = 0; j < oList[i].numAffects; j++){ |
---|
199 | bzero(buffer,256); |
---|
200 | ReadMessage(sockfd,buffer); |
---|
201 | //printf("Utility name: %s\n", buffer); |
---|
202 | strcpy(oList[i].affection_list[j].u.name, buffer); |
---|
203 | |
---|
204 | bzero(buffer,256); |
---|
205 | ReadMessage(sockfd,buffer); |
---|
206 | //printf("Relation: %s\n", buffer); |
---|
207 | strcpy(oList[i].affection_list[j].relation, buffer); |
---|
208 | } |
---|
209 | } |
---|
210 | return 1; |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | // setup client socket connection |
---|
215 | int ClientSocket(int argc, char** argv) |
---|
216 | { |
---|
217 | int sockfd; |
---|
218 | int portno; |
---|
219 | struct sockaddr_in serv_addr; |
---|
220 | struct hostent *server; |
---|
221 | |
---|
222 | // setup client socket connection |
---|
223 | if (argc < 3) { |
---|
224 | fprintf(stderr,"usage: %s hostname port\n", argv[0]); |
---|
225 | exit(0); |
---|
226 | } |
---|
227 | // server name |
---|
228 | server = gethostbyname(argv[1]); |
---|
229 | if (server == NULL) { |
---|
230 | fprintf(stderr,"ERROR, no such host\n"); |
---|
231 | exit(0); |
---|
232 | } |
---|
233 | // port no. |
---|
234 | portno = atoi(argv[2]); |
---|
235 | // socket file descriptor |
---|
236 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
237 | if (sockfd < 0) |
---|
238 | error("ERROR opening socket"); |
---|
239 | // initiate struct socketaddr_in |
---|
240 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
---|
241 | serv_addr.sin_family = AF_INET; |
---|
242 | serv_addr.sin_port = htons(portno); |
---|
243 | bcopy((char *)server->h_addr, |
---|
244 | (char *)&serv_addr.sin_addr.s_addr, |
---|
245 | server->h_length); |
---|
246 | // cast sockaddr_in to sockaddr |
---|
247 | if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) |
---|
248 | error("ERROR connecting"); |
---|
249 | |
---|
250 | return sockfd; |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | int SendMessage(int socketfd, char *message) |
---|
255 | { |
---|
256 | int n; |
---|
257 | |
---|
258 | strcat(message, "\0000"); |
---|
259 | // Write message back to client |
---|
260 | //n = write(socketfd,message,(strlen(message)+1)); |
---|
261 | n = send(socketfd,message,(strlen(message)+1),0); |
---|
262 | if (n<0) |
---|
263 | error("Error sending to server\n"); |
---|
264 | else if(n == 0) |
---|
265 | printf("Server closed the socket.\n"); |
---|
266 | |
---|
267 | return n; |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | int GetXMLFromServer(int sockfd, struct Utility uList[], struct Parameter pList[], |
---|
272 | struct Observable oList[], struct CE_Info *ce_info) |
---|
273 | { |
---|
274 | // get utilities |
---|
275 | GetUtility(sockfd, uList, ce_info); |
---|
276 | |
---|
277 | // get parameters |
---|
278 | GetParameter(sockfd, pList, ce_info); |
---|
279 | |
---|
280 | // read obervables |
---|
281 | GetObservable(sockfd, oList, ce_info); |
---|
282 | |
---|
283 | // send ack back to server |
---|
284 | char buffer[256]; |
---|
285 | strcpy(buffer, "xml received"); |
---|
286 | SendMessage(sockfd, buffer); |
---|
287 | printf("xml reception done\n"); |
---|
288 | |
---|
289 | return 1; |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | void print_current_config(struct Utility uList[], |
---|
294 | struct Parameter pList[], |
---|
295 | struct Observable oList[], |
---|
296 | struct CE_Info * ce_info) |
---|
297 | { |
---|
298 | int i = 0; |
---|
299 | int j = 0; |
---|
300 | |
---|
301 | // utilities |
---|
302 | for(i = 0; i < ce_info->numUtilities ; i++) { |
---|
303 | printf("Utility: %s\n", uList[i].name); |
---|
304 | printf(" Units: %s\n", uList[i].units); |
---|
305 | printf(" Goal: %s\n", uList[i].goal); |
---|
306 | printf(" Target: %f\n", uList[i].target); |
---|
307 | /*cout << "Utility: " << uList[i].name << endl; |
---|
308 | cout << " Units: " << uList[i].units << endl; |
---|
309 | cout << " Goal: " << uList[i].goal << endl; |
---|
310 | cout << " Target: " << uList[i].target << endl;*/ |
---|
311 | } |
---|
312 | |
---|
313 | // parameters |
---|
314 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
315 | printf("Paramter: %s\n", pList[i].name); |
---|
316 | printf(" Units: %s\n", pList[i].units); |
---|
317 | printf(" Min: %f\n", pList[i].min); |
---|
318 | printf(" Max: %f\n", pList[i].max); |
---|
319 | printf(" Step: %f\n", pList[i].step); |
---|
320 | /*cout << "Parameter: " << pList[i].name << endl; |
---|
321 | cout << " Units: " << pList[i].units << endl; |
---|
322 | cout << " Min: " << pList[i].min << endl; |
---|
323 | cout << " Max: " << pList[i].max << endl; |
---|
324 | cout << " Step: " << pList[i].step << endl;*/ |
---|
325 | for(j = 0; j < pList[i].numAffects; j++) { |
---|
326 | printf(" Affect: %s -> %s\n", pList[i].affection_list[j].u.name, pList[i].affection_list[j].relation); |
---|
327 | //cout << " Affect: " << pList[i].affection_list[j].u.name << " -> " << pList[i].affection_list[j].relation << endl; |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | // observables |
---|
332 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
333 | printf("Observable: %s\n", oList[i].name); |
---|
334 | //cout << "Observable: " << oList[i].name << endl; |
---|
335 | for(j = 0; j < oList[i].numAffects; j++) { |
---|
336 | printf(" Affect: %s -> %s\n", oList[i].affection_list[j].u.name, oList[i].affection_list[j].relation); |
---|
337 | //cout << " Affect: " << oList[i].affection_list[j].u.name << " -> " << oList[i].affection_list[j].relation << endl; |
---|
338 | } |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | int RunCBREngine(struct Utility uList[], struct Parameter pList[], |
---|
343 | struct Observable oList[], struct CE_Info *ce_info, |
---|
344 | int num_cols) |
---|
345 | { |
---|
346 | |
---|
347 | int i; |
---|
348 | |
---|
349 | // get column names |
---|
350 | //char *cols[50]; |
---|
351 | char **cols; |
---|
352 | cols = (char **)malloc(sizeof(char)*num_cols); |
---|
353 | int j = 0; |
---|
354 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
355 | cols[j] = malloc(strlen(uList[i].name)+1); |
---|
356 | cols[j] = uList[i].name; |
---|
357 | j++; |
---|
358 | } |
---|
359 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
360 | cols[j] = malloc(strlen(pList[i].name)+1); |
---|
361 | cols[j] = pList[i].name; |
---|
362 | j++; |
---|
363 | } |
---|
364 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
365 | cols[j] = malloc(strlen(oList[i].name)+1); |
---|
366 | cols[j] = oList[i].name; |
---|
367 | j++; |
---|
368 | } |
---|
369 | cols[j] = malloc(strlen("utility")+1); |
---|
370 | cols[j] = "utility"; |
---|
371 | |
---|
372 | // sample table entry |
---|
373 | float vals[num_cols]; |
---|
374 | vals[0] = 10e3f; // throughput |
---|
375 | vals[1] = 1; // spectral_efficiency |
---|
376 | vals[2] = 1e-3f; // log10_ber |
---|
377 | vals[3] = 1; // mod_scheme |
---|
378 | vals[4] = -3.0f; // tx_power |
---|
379 | vals[5] = 10.0f; // SNR |
---|
380 | vals[6] = 0.762; // utility |
---|
381 | |
---|
382 | printf("number of columns: %d\n", num_cols); |
---|
383 | printf("column names:"); |
---|
384 | for (i = 0; i<num_cols; i++){ |
---|
385 | printf(" %s",cols[i]); |
---|
386 | } |
---|
387 | printf("\n"); |
---|
388 | |
---|
389 | int rc; |
---|
390 | |
---|
391 | // create cbr database/table |
---|
392 | //printf("create cbr database\n"); |
---|
393 | char filename[] = {"ex1"}, tablename[] = {"data"}; |
---|
394 | cbr mycbr = cbr_create(filename, tablename, cols, num_cols); |
---|
395 | //cbr mycbr = cbr_create("ex1", "data", cols, num_cols); |
---|
396 | |
---|
397 | // add row here |
---|
398 | //printf("add row to cbr table\n"); |
---|
399 | rc = cbr_add_row(mycbr, cols, vals, num_cols); |
---|
400 | |
---|
401 | // print |
---|
402 | printf("cbr table\n"); |
---|
403 | cbr_print(mycbr); |
---|
404 | |
---|
405 | // simple search: find entry where... |
---|
406 | // log10_ber < 1e-2 and SNR > 5 |
---|
407 | //char * search_names[] = {"log10_ber", "SNR"}; |
---|
408 | char ** search_names; |
---|
409 | search_names |
---|
410 | = (char **)malloc(sizeof(char)*(ce_info->numUtilities)); |
---|
411 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
412 | search_names[i] |
---|
413 | = malloc(sizeof(char)*(strlen(uList[i].name)+1)); |
---|
414 | search_names[i] = uList[i].name; |
---|
415 | } |
---|
416 | int search_ops[] = {GT, GT, LT}; |
---|
417 | //float search_vals[] = {1e-2f, 5.f}; |
---|
418 | float *search_vals; |
---|
419 | search_vals = malloc(sizeof(float)*(ce_info->numUtilities)); |
---|
420 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
421 | search_vals[i] = uList[i].target; |
---|
422 | } |
---|
423 | float retvals[num_cols]; |
---|
424 | rc = cbr_search(mycbr, search_names, search_ops, |
---|
425 | search_vals, 2, retvals); |
---|
426 | |
---|
427 | j = 0; |
---|
428 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
429 | uList[i].value = retvals[j]; |
---|
430 | j++; |
---|
431 | } |
---|
432 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
433 | pList[i].value = retvals[j]; |
---|
434 | j++; |
---|
435 | } |
---|
436 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
437 | oList[i].value = retvals[j]; |
---|
438 | j++; |
---|
439 | } |
---|
440 | |
---|
441 | // clean up database |
---|
442 | //printf("clean cbr database\n"); |
---|
443 | cbr_free(mycbr); |
---|
444 | |
---|
445 | return 1; |
---|
446 | } |
---|
447 | |
---|
448 | |
---|
449 | int SendCEResults(int sockfd, struct Utility uList[], struct Parameter pList[], |
---|
450 | struct Observable oList[], struct CE_Info *ce_info) |
---|
451 | { |
---|
452 | // send results back to server |
---|
453 | char var[50]; |
---|
454 | int i; |
---|
455 | |
---|
456 | /*// utility |
---|
457 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
458 | sprintf(var, "%f", uList[i].value); |
---|
459 | SendMessage(sockfd, var); |
---|
460 | }*/ |
---|
461 | |
---|
462 | // parameter |
---|
463 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
464 | sprintf(var, "%f", pList[i].value); |
---|
465 | SendMessage(sockfd, var); |
---|
466 | } |
---|
467 | |
---|
468 | /*// observable |
---|
469 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
470 | sprintf(var, "%f", oList[i].value); |
---|
471 | SendMessage(sockfd, var); |
---|
472 | }*/ |
---|
473 | |
---|
474 | return 1; |
---|
475 | } |
---|
476 | |
---|
477 | |
---|
478 | // main client socket |
---|
479 | int main(int argc, char *argv[]) |
---|
480 | { |
---|
481 | struct Utility uList[10]; |
---|
482 | struct Parameter pList[10]; |
---|
483 | struct Observable oList[10]; |
---|
484 | struct CE_Info ce_info; |
---|
485 | |
---|
486 | // setup client socket connection |
---|
487 | int sockfd; |
---|
488 | sockfd = ClientSocket(argc, argv); |
---|
489 | |
---|
490 | // get xml info from server |
---|
491 | GetXMLFromServer(sockfd, uList, pList, oList, &ce_info); |
---|
492 | print_current_config(uList, pList, oList, &ce_info); |
---|
493 | |
---|
494 | // cbr operation |
---|
495 | unsigned int num_cols; |
---|
496 | // get number of columns |
---|
497 | num_cols = ce_info.numUtilities + ce_info.numParameters; |
---|
498 | num_cols = num_cols + ce_info.numObservables; |
---|
499 | num_cols = num_cols + 1; |
---|
500 | RunCBREngine(uList, pList, oList, &ce_info, num_cols); |
---|
501 | // print out results |
---|
502 | int i; |
---|
503 | printf("search result: "); |
---|
504 | for (i=0;i<ce_info.numUtilities; i++) |
---|
505 | printf("%f, ",uList[i].value); |
---|
506 | for (i=0;i<ce_info.numParameters; i++) |
---|
507 | printf("%f, ",pList[i].value); |
---|
508 | for (i=0;i<ce_info.numObservables; i++) |
---|
509 | printf("%f, ",oList[i].value); |
---|
510 | printf("\n"); |
---|
511 | |
---|
512 | // send results back to server |
---|
513 | SendCEResults(sockfd, uList, pList, oList, &ce_info); |
---|
514 | |
---|
515 | /* char counter[50]; |
---|
516 | char var[50]; |
---|
517 | // utility |
---|
518 | for (i = 0; i < ce_info.numUtilities; i++){ |
---|
519 | sprintf(var, "%f", uList[i].value); |
---|
520 | SendMessage(sockfd, var); |
---|
521 | } |
---|
522 | // parameter |
---|
523 | for (i = 0; i < ce_info.numParameters; i++){ |
---|
524 | sprintf(var, "%f", pList[i].value); |
---|
525 | SendMessage(sockfd, var); |
---|
526 | } |
---|
527 | // observable |
---|
528 | for (i = 0; i < ce_info.numObservables; i++){ |
---|
529 | sprintf(var, "%f", oList[i].value); |
---|
530 | SendMessage(sockfd, var); |
---|
531 | }*/ |
---|
532 | |
---|
533 | |
---|
534 | return 0; |
---|
535 | } |
---|