[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> |
---|
| 9 | |
---|
[125] | 10 | #include <vtcross/cbr.h> |
---|
[77] | 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; |
---|
[79] | 28 | float value; |
---|
[77] | 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; |
---|
[79] | 47 | float value; |
---|
[77] | 48 | }; |
---|
| 49 | |
---|
| 50 | struct Observable { |
---|
| 51 | //string name; |
---|
| 52 | char name[50]; |
---|
| 53 | struct Affect affection_list[10]; |
---|
| 54 | int numAffects; |
---|
[79] | 55 | float value; |
---|
[77] | 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"); |
---|
[99] | 75 | //printf("ReadMessage:%s %d\n",buffer,n); |
---|
[77] | 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 | |
---|
[79] | 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 | |
---|
[83] | 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 | |
---|
[77] | 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 | |
---|
[83] | 283 | // send ack back to server |
---|
| 284 | char buffer[256]; |
---|
| 285 | strcpy(buffer, "xml received"); |
---|
| 286 | SendMessage(sockfd, buffer); |
---|
[95] | 287 | //printf("xml reception done\n"); |
---|
[77] | 288 | |
---|
| 289 | return 1; |
---|
| 290 | } |
---|
| 291 | |
---|
[79] | 292 | |
---|
[88] | 293 | int GetExperienceSize(int sockfd, int *num_rows, int *num_cols) |
---|
| 294 | { |
---|
| 295 | char buffer[256]; |
---|
| 296 | |
---|
| 297 | // number of rows |
---|
| 298 | bzero(buffer,256); |
---|
| 299 | ReadMessage(sockfd,buffer); |
---|
| 300 | *num_rows = atoi(buffer); |
---|
| 301 | // number of columns |
---|
| 302 | bzero(buffer,256); |
---|
| 303 | ReadMessage(sockfd,buffer); |
---|
| 304 | *num_cols = atoi(buffer); |
---|
| 305 | return 1; |
---|
| 306 | } |
---|
| 307 | |
---|
[95] | 308 | int GetRequest(int sockfd) |
---|
| 309 | { |
---|
| 310 | char buffer[256]; |
---|
[88] | 311 | |
---|
[95] | 312 | bzero(buffer,256); |
---|
| 313 | ReadMessage(sockfd,buffer); |
---|
| 314 | |
---|
| 315 | return 1; |
---|
| 316 | } |
---|
| 317 | |
---|
[88] | 318 | int GetExperience(int sockfd, int num_rows, int num_cols, |
---|
[89] | 319 | //float **past_exp) |
---|
| 320 | float past_exp[num_rows][num_cols]) |
---|
[88] | 321 | { |
---|
| 322 | char buffer[256]; |
---|
| 323 | int i, j; |
---|
| 324 | |
---|
| 325 | // read experience |
---|
| 326 | for (i = 0; i < num_rows; i++){ |
---|
| 327 | for (j = 0; j < num_cols; j++){ |
---|
| 328 | bzero(buffer,256); |
---|
| 329 | ReadMessage(sockfd,buffer); |
---|
| 330 | // printf("experience: %s\n", buffer); |
---|
| 331 | past_exp[i][j] = atof(buffer); |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | return 1; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | |
---|
[79] | 339 | void print_current_config(struct Utility uList[], |
---|
| 340 | struct Parameter pList[], |
---|
| 341 | struct Observable oList[], |
---|
| 342 | struct CE_Info * ce_info) |
---|
[77] | 343 | { |
---|
| 344 | int i = 0; |
---|
| 345 | int j = 0; |
---|
| 346 | |
---|
| 347 | // utilities |
---|
| 348 | for(i = 0; i < ce_info->numUtilities ; i++) { |
---|
| 349 | printf("Utility: %s\n", uList[i].name); |
---|
| 350 | printf(" Units: %s\n", uList[i].units); |
---|
| 351 | printf(" Goal: %s\n", uList[i].goal); |
---|
| 352 | printf(" Target: %f\n", uList[i].target); |
---|
| 353 | /*cout << "Utility: " << uList[i].name << endl; |
---|
| 354 | cout << " Units: " << uList[i].units << endl; |
---|
| 355 | cout << " Goal: " << uList[i].goal << endl; |
---|
| 356 | cout << " Target: " << uList[i].target << endl;*/ |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | // parameters |
---|
| 360 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
| 361 | printf("Paramter: %s\n", pList[i].name); |
---|
| 362 | printf(" Units: %s\n", pList[i].units); |
---|
| 363 | printf(" Min: %f\n", pList[i].min); |
---|
| 364 | printf(" Max: %f\n", pList[i].max); |
---|
| 365 | printf(" Step: %f\n", pList[i].step); |
---|
| 366 | /*cout << "Parameter: " << pList[i].name << endl; |
---|
| 367 | cout << " Units: " << pList[i].units << endl; |
---|
| 368 | cout << " Min: " << pList[i].min << endl; |
---|
| 369 | cout << " Max: " << pList[i].max << endl; |
---|
| 370 | cout << " Step: " << pList[i].step << endl;*/ |
---|
| 371 | for(j = 0; j < pList[i].numAffects; j++) { |
---|
| 372 | printf(" Affect: %s -> %s\n", pList[i].affection_list[j].u.name, pList[i].affection_list[j].relation); |
---|
| 373 | //cout << " Affect: " << pList[i].affection_list[j].u.name << " -> " << pList[i].affection_list[j].relation << endl; |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | // observables |
---|
| 378 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
| 379 | printf("Observable: %s\n", oList[i].name); |
---|
| 380 | //cout << "Observable: " << oList[i].name << endl; |
---|
| 381 | for(j = 0; j < oList[i].numAffects; j++) { |
---|
| 382 | printf(" Affect: %s -> %s\n", oList[i].affection_list[j].u.name, oList[i].affection_list[j].relation); |
---|
| 383 | //cout << " Affect: " << oList[i].affection_list[j].u.name << " -> " << oList[i].affection_list[j].relation << endl; |
---|
| 384 | } |
---|
| 385 | } |
---|
| 386 | } |
---|
| 387 | |
---|
[79] | 388 | int RunCBREngine(struct Utility uList[], struct Parameter pList[], |
---|
| 389 | struct Observable oList[], struct CE_Info *ce_info, |
---|
[89] | 390 | //int num_cols, int num_rows, float ** past_exp) |
---|
| 391 | int num_cols, int num_rows, float past_exp[num_rows][num_cols]) |
---|
[77] | 392 | { |
---|
[79] | 393 | |
---|
[88] | 394 | int i, j; |
---|
[77] | 395 | |
---|
[79] | 396 | // get column names |
---|
[89] | 397 | /*char *cols[50] = {"throughput", "spectral_efficiency", |
---|
| 398 | "log10_ber", "mod_scheme", "tx_power", "SNR", "utility"};*/ |
---|
[88] | 399 | //char cols[num_cols][50]; |
---|
[99] | 400 | |
---|
| 401 | |
---|
[79] | 402 | char **cols; |
---|
| 403 | cols = (char **)malloc(sizeof(char)*num_cols); |
---|
[88] | 404 | j = 0; |
---|
[79] | 405 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
| 406 | cols[j] = malloc(strlen(uList[i].name)+1); |
---|
| 407 | cols[j] = uList[i].name; |
---|
[88] | 408 | // strcpy(cols[j], uList[i].name); |
---|
[79] | 409 | j++; |
---|
[77] | 410 | } |
---|
[79] | 411 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
| 412 | cols[j] = malloc(strlen(pList[i].name)+1); |
---|
| 413 | cols[j] = pList[i].name; |
---|
[88] | 414 | // strcpy(cols[j], pList[i].name); |
---|
[79] | 415 | j++; |
---|
[77] | 416 | } |
---|
[79] | 417 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
| 418 | cols[j] = malloc(strlen(oList[i].name)+1); |
---|
| 419 | cols[j] = oList[i].name; |
---|
[88] | 420 | // strcpy(cols[j], oList[i].name); |
---|
[79] | 421 | j++; |
---|
| 422 | } |
---|
| 423 | cols[j] = malloc(strlen("utility")+1); |
---|
| 424 | cols[j] = "utility"; |
---|
[77] | 425 | |
---|
[99] | 426 | //printf("column names:"); |
---|
| 427 | //for (i = 0; i<num_cols; i++){ |
---|
| 428 | // printf(" %s",cols[i]); |
---|
| 429 | //} |
---|
| 430 | //printf("\n"); |
---|
[77] | 431 | |
---|
| 432 | int rc; |
---|
| 433 | |
---|
| 434 | // create cbr database/table |
---|
[79] | 435 | //printf("create cbr database\n"); |
---|
[77] | 436 | char filename[] = {"ex1"}, tablename[] = {"data"}; |
---|
| 437 | cbr mycbr = cbr_create(filename, tablename, cols, num_cols); |
---|
| 438 | //cbr mycbr = cbr_create("ex1", "data", cols, num_cols); |
---|
[89] | 439 | |
---|
[77] | 440 | // add row here |
---|
[88] | 441 | float vals[num_cols]; |
---|
| 442 | /*// sample table entry |
---|
| 443 | vals[0] = 1e3f; // throughput |
---|
| 444 | vals[1] = 1; // spectral_efficiency |
---|
| 445 | vals[2] = -3.50; // log10_ber |
---|
| 446 | vals[3] = 1; // mod_scheme |
---|
| 447 | vals[4] = -3.5f; // tx_power |
---|
| 448 | vals[5] = 10.0f; // SNR |
---|
| 449 | vals[6] = 0.762; // utility*/ |
---|
| 450 | for (i = 0; i < num_rows; i++){ |
---|
| 451 | for (j = 0; j < num_cols; j++){ |
---|
| 452 | vals[j] = past_exp[i][j]; |
---|
| 453 | } |
---|
| 454 | rc = cbr_add_row(mycbr, cols, vals, num_cols); |
---|
| 455 | } |
---|
[77] | 456 | |
---|
| 457 | // print |
---|
| 458 | printf("cbr table\n"); |
---|
| 459 | cbr_print(mycbr); |
---|
| 460 | |
---|
| 461 | // simple search: find entry where... |
---|
| 462 | // log10_ber < 1e-2 and SNR > 5 |
---|
[83] | 463 | //char * search_names[] = {"log10_ber", "SNR"}; |
---|
| 464 | char ** search_names; |
---|
| 465 | search_names |
---|
| 466 | = (char **)malloc(sizeof(char)*(ce_info->numUtilities)); |
---|
| 467 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
| 468 | search_names[i] |
---|
| 469 | = malloc(sizeof(char)*(strlen(uList[i].name)+1)); |
---|
| 470 | search_names[i] = uList[i].name; |
---|
| 471 | } |
---|
| 472 | int search_ops[] = {GT, GT, LT}; |
---|
| 473 | //float search_vals[] = {1e-2f, 5.f}; |
---|
| 474 | float *search_vals; |
---|
| 475 | search_vals = malloc(sizeof(float)*(ce_info->numUtilities)); |
---|
| 476 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
| 477 | search_vals[i] = uList[i].target; |
---|
| 478 | } |
---|
[77] | 479 | float retvals[num_cols]; |
---|
| 480 | rc = cbr_search(mycbr, search_names, search_ops, |
---|
[88] | 481 | search_vals, ce_info->numUtilities, retvals); |
---|
[77] | 482 | |
---|
[79] | 483 | j = 0; |
---|
| 484 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
| 485 | uList[i].value = retvals[j]; |
---|
| 486 | j++; |
---|
| 487 | } |
---|
| 488 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
| 489 | pList[i].value = retvals[j]; |
---|
| 490 | j++; |
---|
| 491 | } |
---|
| 492 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
| 493 | oList[i].value = retvals[j]; |
---|
| 494 | j++; |
---|
| 495 | } |
---|
| 496 | |
---|
[77] | 497 | // clean up database |
---|
[79] | 498 | //printf("clean cbr database\n"); |
---|
[77] | 499 | cbr_free(mycbr); |
---|
| 500 | |
---|
[79] | 501 | return 1; |
---|
| 502 | } |
---|
| 503 | |
---|
[83] | 504 | |
---|
| 505 | int SendCEResults(int sockfd, struct Utility uList[], struct Parameter pList[], |
---|
| 506 | struct Observable oList[], struct CE_Info *ce_info) |
---|
| 507 | { |
---|
| 508 | // send results back to server |
---|
| 509 | char var[50]; |
---|
| 510 | int i; |
---|
| 511 | |
---|
| 512 | /*// utility |
---|
| 513 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
| 514 | sprintf(var, "%f", uList[i].value); |
---|
| 515 | SendMessage(sockfd, var); |
---|
| 516 | }*/ |
---|
| 517 | |
---|
| 518 | // parameter |
---|
| 519 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
| 520 | sprintf(var, "%f", pList[i].value); |
---|
| 521 | SendMessage(sockfd, var); |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | /*// observable |
---|
| 525 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
| 526 | sprintf(var, "%f", oList[i].value); |
---|
| 527 | SendMessage(sockfd, var); |
---|
| 528 | }*/ |
---|
| 529 | |
---|
| 530 | return 1; |
---|
| 531 | } |
---|
| 532 | |
---|
[99] | 533 | int RegisterCE(int sockfd, struct Utility uList[], struct Parameter pList[], |
---|
| 534 | struct Observable oList[], struct CE_Info *ce_info) |
---|
| 535 | { |
---|
| 536 | // Send register message to cognitive radio |
---|
| 537 | SendMessage(sockfd, "c_register"); |
---|
| 538 | return 1; |
---|
| 539 | |
---|
| 540 | |
---|
| 541 | |
---|
| 542 | return 1; |
---|
| 543 | } |
---|
[83] | 544 | |
---|
[79] | 545 | // main client socket |
---|
| 546 | int main(int argc, char *argv[]) |
---|
| 547 | { |
---|
| 548 | struct Utility uList[10]; |
---|
| 549 | struct Parameter pList[10]; |
---|
| 550 | struct Observable oList[10]; |
---|
| 551 | struct CE_Info ce_info; |
---|
[99] | 552 | char buffer[256]; |
---|
[79] | 553 | |
---|
| 554 | // setup client socket connection |
---|
| 555 | int sockfd; |
---|
| 556 | sockfd = ClientSocket(argc, argv); |
---|
[99] | 557 | |
---|
| 558 | RegisterCE(sockfd,uList,pList,oList,&ce_info); |
---|
[79] | 559 | // get xml info from server |
---|
| 560 | GetXMLFromServer(sockfd, uList, pList, oList, &ce_info); |
---|
[95] | 561 | printf("Received Radio Operation Profile from Server Successfully.\n\n"); |
---|
| 562 | //print_current_config(uList, pList, oList, &ce_info); |
---|
| 563 | |
---|
[89] | 564 | // get experience size from server |
---|
[88] | 565 | int num_rows, num_cols; |
---|
| 566 | GetExperienceSize(sockfd, &num_rows, &num_cols); |
---|
[99] | 567 | |
---|
[88] | 568 | // get experience |
---|
| 569 | int i, j; |
---|
[89] | 570 | float past_exp[num_rows][num_cols]; |
---|
[88] | 571 | GetExperience(sockfd, num_rows, num_cols, past_exp); |
---|
[95] | 572 | printf("Received Previous Radio Experience from Server Successfully.\n\n"); |
---|
[79] | 573 | // cbr operation |
---|
[88] | 574 | //unsigned int num_cols; |
---|
[79] | 575 | // get number of columns |
---|
| 576 | num_cols = ce_info.numUtilities + ce_info.numParameters; |
---|
| 577 | num_cols = num_cols + ce_info.numObservables; |
---|
[88] | 578 | num_cols = num_cols + 1; // overall utility |
---|
[95] | 579 | //while(1) { |
---|
| 580 | |
---|
| 581 | // Wait until request is received from server to perform optimization |
---|
| 582 | //printf("Waiting for Request for optimization from Server\n\n"); |
---|
| 583 | //GetRequest(sockfd); |
---|
| 584 | //printf("Received optimization request from server\n\n"); |
---|
[99] | 585 | RunCBREngine(uList, pList, oList, &ce_info, num_cols, num_rows, past_exp); |
---|
[95] | 586 | |
---|
| 587 | printf("Sending optimization results to server.\n\n"); |
---|
| 588 | // send results back to server |
---|
| 589 | SendCEResults(sockfd, uList, pList, oList, &ce_info); |
---|
[99] | 590 | |
---|
[95] | 591 | //} |
---|
[79] | 592 | |
---|
[95] | 593 | return 0; |
---|
[77] | 594 | } |
---|