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