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