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 GetXMLFromServer(int sockfd, struct Utility uList[], struct Parameter pList[], |
---|
255 | struct Observable oList[], struct CE_Info *ce_info) |
---|
256 | { |
---|
257 | // get utilities |
---|
258 | GetUtility(sockfd, uList, ce_info); |
---|
259 | |
---|
260 | // get parameters |
---|
261 | GetParameter(sockfd, pList, ce_info); |
---|
262 | |
---|
263 | // read obervables |
---|
264 | GetObservable(sockfd, oList, ce_info); |
---|
265 | |
---|
266 | printf("xml reception done\n"); |
---|
267 | |
---|
268 | return 1; |
---|
269 | } |
---|
270 | |
---|
271 | |
---|
272 | int SendMessage(int socketfd, char *message) |
---|
273 | { |
---|
274 | int n; |
---|
275 | |
---|
276 | strcat(message, "\0000"); |
---|
277 | // Write message back to client |
---|
278 | //n = write(socketfd,message,(strlen(message)+1)); |
---|
279 | n = send(socketfd,message,(strlen(message)+1),0); |
---|
280 | if (n<0) |
---|
281 | error("Error sending to server\n"); |
---|
282 | else if(n == 0) |
---|
283 | printf("Server closed the socket.\n"); |
---|
284 | |
---|
285 | return n; |
---|
286 | } |
---|
287 | |
---|
288 | |
---|
289 | void print_current_config(struct Utility uList[], |
---|
290 | struct Parameter pList[], |
---|
291 | struct Observable oList[], |
---|
292 | struct CE_Info * ce_info) |
---|
293 | { |
---|
294 | int i = 0; |
---|
295 | int j = 0; |
---|
296 | |
---|
297 | // utilities |
---|
298 | for(i = 0; i < ce_info->numUtilities ; i++) { |
---|
299 | printf("Utility: %s\n", uList[i].name); |
---|
300 | printf(" Units: %s\n", uList[i].units); |
---|
301 | printf(" Goal: %s\n", uList[i].goal); |
---|
302 | printf(" Target: %f\n", uList[i].target); |
---|
303 | /*cout << "Utility: " << uList[i].name << endl; |
---|
304 | cout << " Units: " << uList[i].units << endl; |
---|
305 | cout << " Goal: " << uList[i].goal << endl; |
---|
306 | cout << " Target: " << uList[i].target << endl;*/ |
---|
307 | } |
---|
308 | |
---|
309 | // parameters |
---|
310 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
311 | printf("Paramter: %s\n", pList[i].name); |
---|
312 | printf(" Units: %s\n", pList[i].units); |
---|
313 | printf(" Min: %f\n", pList[i].min); |
---|
314 | printf(" Max: %f\n", pList[i].max); |
---|
315 | printf(" Step: %f\n", pList[i].step); |
---|
316 | /*cout << "Parameter: " << pList[i].name << endl; |
---|
317 | cout << " Units: " << pList[i].units << endl; |
---|
318 | cout << " Min: " << pList[i].min << endl; |
---|
319 | cout << " Max: " << pList[i].max << endl; |
---|
320 | cout << " Step: " << pList[i].step << endl;*/ |
---|
321 | for(j = 0; j < pList[i].numAffects; j++) { |
---|
322 | printf(" Affect: %s -> %s\n", pList[i].affection_list[j].u.name, pList[i].affection_list[j].relation); |
---|
323 | //cout << " Affect: " << pList[i].affection_list[j].u.name << " -> " << pList[i].affection_list[j].relation << endl; |
---|
324 | } |
---|
325 | } |
---|
326 | |
---|
327 | // observables |
---|
328 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
329 | printf("Observable: %s\n", oList[i].name); |
---|
330 | //cout << "Observable: " << oList[i].name << endl; |
---|
331 | for(j = 0; j < oList[i].numAffects; j++) { |
---|
332 | printf(" Affect: %s -> %s\n", oList[i].affection_list[j].u.name, oList[i].affection_list[j].relation); |
---|
333 | //cout << " Affect: " << oList[i].affection_list[j].u.name << " -> " << oList[i].affection_list[j].relation << endl; |
---|
334 | } |
---|
335 | } |
---|
336 | } |
---|
337 | |
---|
338 | int RunCBREngine(struct Utility uList[], struct Parameter pList[], |
---|
339 | struct Observable oList[], struct CE_Info *ce_info, |
---|
340 | int num_cols) |
---|
341 | { |
---|
342 | |
---|
343 | int i; |
---|
344 | |
---|
345 | // get column names |
---|
346 | //char *cols[50]; |
---|
347 | char **cols; |
---|
348 | cols = (char **)malloc(sizeof(char)*num_cols); |
---|
349 | int j = 0; |
---|
350 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
351 | cols[j] = malloc(strlen(uList[i].name)+1); |
---|
352 | cols[j] = uList[i].name; |
---|
353 | j++; |
---|
354 | } |
---|
355 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
356 | cols[j] = malloc(strlen(pList[i].name)+1); |
---|
357 | cols[j] = pList[i].name; |
---|
358 | j++; |
---|
359 | } |
---|
360 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
361 | cols[j] = malloc(strlen(oList[i].name)+1); |
---|
362 | cols[j] = oList[i].name; |
---|
363 | j++; |
---|
364 | } |
---|
365 | cols[j] = malloc(strlen("utility")+1); |
---|
366 | cols[j] = "utility"; |
---|
367 | |
---|
368 | // sample table entry |
---|
369 | float vals[num_cols]; |
---|
370 | vals[0] = 10e3f; // throughput |
---|
371 | vals[1] = 1; // spectral_efficiency |
---|
372 | vals[2] = 1e-3f; // log10_ber |
---|
373 | vals[3] = 1; // mod_scheme |
---|
374 | vals[4] = -3.0f; // tx_power |
---|
375 | vals[5] = 10.0f; // SNR |
---|
376 | vals[6] = 0.762; // utility |
---|
377 | |
---|
378 | printf("number of columns: %d\n", num_cols); |
---|
379 | printf("column names:"); |
---|
380 | for (i = 0; i<num_cols; i++){ |
---|
381 | printf(" %s",cols[i]); |
---|
382 | } |
---|
383 | printf("\n"); |
---|
384 | |
---|
385 | int rc; |
---|
386 | |
---|
387 | // create cbr database/table |
---|
388 | //printf("create cbr database\n"); |
---|
389 | char filename[] = {"ex1"}, tablename[] = {"data"}; |
---|
390 | cbr mycbr = cbr_create(filename, tablename, cols, num_cols); |
---|
391 | //cbr mycbr = cbr_create("ex1", "data", cols, num_cols); |
---|
392 | |
---|
393 | // add row here |
---|
394 | //printf("add row to cbr table\n"); |
---|
395 | rc = cbr_add_row(mycbr, cols, vals, num_cols); |
---|
396 | |
---|
397 | // print |
---|
398 | printf("cbr table\n"); |
---|
399 | cbr_print(mycbr); |
---|
400 | |
---|
401 | // simple search: find entry where... |
---|
402 | // log10_ber < 1e-2 and SNR > 5 |
---|
403 | char * search_names[] = {"log10_ber", "SNR"}; |
---|
404 | int search_ops[] = {LT, GT}; |
---|
405 | float search_vals[] = {1e-2f, 5.f}; |
---|
406 | float retvals[num_cols]; |
---|
407 | rc = cbr_search(mycbr, search_names, search_ops, |
---|
408 | search_vals, 2, retvals); |
---|
409 | |
---|
410 | j = 0; |
---|
411 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
412 | uList[i].value = retvals[j]; |
---|
413 | j++; |
---|
414 | } |
---|
415 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
416 | pList[i].value = retvals[j]; |
---|
417 | j++; |
---|
418 | } |
---|
419 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
420 | oList[i].value = retvals[j]; |
---|
421 | j++; |
---|
422 | } |
---|
423 | |
---|
424 | // clean up database |
---|
425 | //printf("clean cbr database\n"); |
---|
426 | cbr_free(mycbr); |
---|
427 | |
---|
428 | return 1; |
---|
429 | } |
---|
430 | |
---|
431 | // main client socket |
---|
432 | int main(int argc, char *argv[]) |
---|
433 | { |
---|
434 | struct Utility uList[10]; |
---|
435 | struct Parameter pList[10]; |
---|
436 | struct Observable oList[10]; |
---|
437 | struct CE_Info ce_info; |
---|
438 | |
---|
439 | // setup client socket connection |
---|
440 | int sockfd; |
---|
441 | sockfd = ClientSocket(argc, argv); |
---|
442 | |
---|
443 | // get xml info from server |
---|
444 | GetXMLFromServer(sockfd, uList, pList, oList, &ce_info); |
---|
445 | print_current_config(uList, pList, oList, &ce_info); |
---|
446 | |
---|
447 | // cbr operation |
---|
448 | unsigned int num_cols; |
---|
449 | // get number of columns |
---|
450 | num_cols = ce_info.numUtilities + ce_info.numParameters; |
---|
451 | num_cols = num_cols + ce_info.numObservables; |
---|
452 | num_cols = num_cols + 1; |
---|
453 | RunCBREngine(uList, pList, oList, &ce_info, num_cols); |
---|
454 | // print out results |
---|
455 | int i; |
---|
456 | printf("search result: "); |
---|
457 | for (i=0;i<ce_info.numUtilities; i++) |
---|
458 | printf("%f, ",uList[i].value); |
---|
459 | for (i=0;i<ce_info.numParameters; i++) |
---|
460 | printf("%f, ",pList[i].value); |
---|
461 | for (i=0;i<ce_info.numObservables; i++) |
---|
462 | printf("%f, ",oList[i].value); |
---|
463 | printf("\n"); |
---|
464 | |
---|
465 | // send results back to server |
---|
466 | char counter[50]; |
---|
467 | char var[50]; |
---|
468 | // utility |
---|
469 | sprintf(counter, "%d", ce_info.numUtilities); |
---|
470 | SendMessage(sockfd, counter); |
---|
471 | // send utilities |
---|
472 | for (i = 0; i < ce_info.numUtilities; i++){ |
---|
473 | sprintf(var, "%f", uList[i].value); |
---|
474 | SendMessage(sockfd, var); |
---|
475 | } |
---|
476 | |
---|
477 | // parameter |
---|
478 | sprintf(counter, "%d", ce_info.numParameters); |
---|
479 | SendMessage(sockfd, counter); |
---|
480 | // send parameters |
---|
481 | for (i = 0; i < ce_info.numParameters; i++){ |
---|
482 | sprintf(var, "%f", pList[i].value); |
---|
483 | SendMessage(sockfd, var); |
---|
484 | } |
---|
485 | |
---|
486 | // observable |
---|
487 | sprintf(counter, "%d", ce_info.numObservables); |
---|
488 | SendMessage(sockfd, counter); |
---|
489 | // send utilities |
---|
490 | for (i = 0; i < ce_info.numObservables; i++){ |
---|
491 | sprintf(var, "%f", oList[i].value); |
---|
492 | SendMessage(sockfd, var); |
---|
493 | } |
---|
494 | |
---|
495 | |
---|
496 | return 0; |
---|
497 | } |
---|