[76] | 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 | |
---|
| 8 | #include "cbr.h" |
---|
| 9 | |
---|
| 10 | using namespace std; |
---|
| 11 | |
---|
| 12 | struct CE_Info { |
---|
| 13 | int numUtilities; |
---|
| 14 | int numParameters; |
---|
| 15 | int numObservables; |
---|
| 16 | }; |
---|
| 17 | |
---|
| 18 | struct Utility { |
---|
| 19 | char name[50]; |
---|
| 20 | char units[50]; |
---|
| 21 | char goal[50]; |
---|
| 22 | /* string name; |
---|
| 23 | string units; |
---|
| 24 | string goal;*/ |
---|
| 25 | float target; |
---|
| 26 | }; |
---|
| 27 | |
---|
| 28 | struct Affect { |
---|
| 29 | Utility u; |
---|
| 30 | //string relation; |
---|
| 31 | char relation[50]; |
---|
| 32 | }; |
---|
| 33 | |
---|
| 34 | struct Parameter { |
---|
| 35 | /*string name; |
---|
| 36 | string units;*/ |
---|
| 37 | char name[50]; |
---|
| 38 | char units[50]; |
---|
| 39 | float min; |
---|
| 40 | int numAffects; |
---|
| 41 | Affect affection_list[10]; |
---|
| 42 | float max; |
---|
| 43 | float step; |
---|
| 44 | }; |
---|
| 45 | |
---|
| 46 | struct Observable { |
---|
| 47 | //string name; |
---|
| 48 | char name[50]; |
---|
| 49 | Affect affection_list[10]; |
---|
| 50 | int numAffects; |
---|
| 51 | }; |
---|
| 52 | |
---|
| 53 | // error handling |
---|
| 54 | void error(char *msg) |
---|
| 55 | { |
---|
| 56 | perror(msg); |
---|
| 57 | exit(0); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | void ReadMessage(int socket,char * buffer) { |
---|
| 61 | int i,n; |
---|
| 62 | |
---|
| 63 | n = recv(socket,buffer,256,MSG_PEEK); |
---|
| 64 | for(i=0;i<256;i++){ |
---|
| 65 | if(strcmp(&buffer[i],"\0") == 0) break; |
---|
| 66 | } |
---|
| 67 | n = recv(socket,buffer,i+1,0); |
---|
| 68 | if (n < 0) |
---|
| 69 | error("ERROR reading from socket"); |
---|
| 70 | // printf("ReadMessage:%s %d\n",buffer,n); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | int GetUtility(int sockfd, Utility uList[], CE_Info *ce_info) |
---|
| 75 | { |
---|
| 76 | char buffer[256]; |
---|
| 77 | int i; |
---|
| 78 | |
---|
| 79 | // read unitilities |
---|
| 80 | // numUtilities |
---|
| 81 | bzero(buffer,256); |
---|
| 82 | ReadMessage(sockfd,buffer); |
---|
| 83 | ce_info->numUtilities = atoi(buffer); |
---|
| 84 | //printf("number of utilities: %d\n", ce_info->numUtilities); |
---|
| 85 | |
---|
| 86 | for (i = 0; i < ce_info->numUtilities; i++){ |
---|
| 87 | bzero(buffer,256); |
---|
| 88 | ReadMessage(sockfd,buffer); |
---|
| 89 | //printf("Name: %s\n", buffer); |
---|
| 90 | strcpy(uList[i].name, buffer); |
---|
| 91 | |
---|
| 92 | bzero(buffer,256); |
---|
| 93 | ReadMessage(sockfd,buffer); |
---|
| 94 | //printf("Units: %s\n", buffer); |
---|
| 95 | strcpy(uList[i].units, buffer); |
---|
| 96 | |
---|
| 97 | bzero(buffer,256); |
---|
| 98 | ReadMessage(sockfd,buffer); |
---|
| 99 | //printf("Goal: %s\n", buffer); |
---|
| 100 | strcpy(uList[i].goal, buffer); |
---|
| 101 | |
---|
| 102 | bzero(buffer,256); |
---|
| 103 | ReadMessage(sockfd,buffer); |
---|
| 104 | //printf("Target: %s\n", buffer); |
---|
| 105 | uList[i].target = atof(buffer); |
---|
| 106 | } |
---|
| 107 | return 1; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | int GetParameter(int sockfd, Parameter pList[], CE_Info *ce_info) |
---|
| 112 | { |
---|
| 113 | char buffer[256]; |
---|
| 114 | int i, j; |
---|
| 115 | |
---|
| 116 | // numParameters |
---|
| 117 | bzero(buffer,256); |
---|
| 118 | ReadMessage(sockfd,buffer); |
---|
| 119 | ce_info->numParameters = atoi(buffer); |
---|
| 120 | //printf("number of parameters: %d\n", ce_info->numParameters); |
---|
| 121 | |
---|
| 122 | for (i = 0; i < ce_info->numParameters; i++){ |
---|
| 123 | bzero(buffer,256); |
---|
| 124 | ReadMessage(sockfd,buffer); |
---|
| 125 | //printf("Name: %s\n", buffer); |
---|
| 126 | strcpy(pList[i].name, buffer); |
---|
| 127 | |
---|
| 128 | bzero(buffer,256); |
---|
| 129 | ReadMessage(sockfd,buffer); |
---|
| 130 | //printf("Units: %s\n", buffer); |
---|
| 131 | strcpy(pList[i].units, buffer); |
---|
| 132 | |
---|
| 133 | bzero(buffer,256); |
---|
| 134 | ReadMessage(sockfd,buffer); |
---|
| 135 | //printf("Min: %s\n", buffer); |
---|
| 136 | pList[i].min = atof(buffer); |
---|
| 137 | |
---|
| 138 | bzero(buffer,256); |
---|
| 139 | ReadMessage(sockfd,buffer); |
---|
| 140 | //printf("Max: %s\n", buffer); |
---|
| 141 | pList[i].max = atof(buffer); |
---|
| 142 | |
---|
| 143 | bzero(buffer,256); |
---|
| 144 | ReadMessage(sockfd,buffer); |
---|
| 145 | //printf("Step: %s\n", buffer); |
---|
| 146 | pList[i].step = atof(buffer); |
---|
| 147 | |
---|
| 148 | // numAffects |
---|
| 149 | bzero(buffer,256); |
---|
| 150 | ReadMessage(sockfd,buffer); |
---|
| 151 | pList[i].numAffects = atoi(buffer); |
---|
| 152 | //printf("number of affects: %d\n", pList[i].numAffects); |
---|
| 153 | |
---|
| 154 | for (j = 0; j < pList[i].numAffects; j++){ |
---|
| 155 | bzero(buffer,256); |
---|
| 156 | ReadMessage(sockfd,buffer); |
---|
| 157 | //printf("Utility name: %s\n", buffer); |
---|
| 158 | strcpy(pList[i].affection_list[j].u.name, buffer); |
---|
| 159 | |
---|
| 160 | bzero(buffer,256); |
---|
| 161 | ReadMessage(sockfd,buffer); |
---|
| 162 | //printf("Relation: %s\n", buffer); |
---|
| 163 | strcpy(pList[i].affection_list[j].relation, buffer); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | return 1; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | int GetObservable(int sockfd, Observable oList[], CE_Info *ce_info) |
---|
| 171 | { |
---|
| 172 | char buffer[256]; |
---|
| 173 | int i,j; |
---|
| 174 | |
---|
| 175 | // numParameters |
---|
| 176 | bzero(buffer,256); |
---|
| 177 | ReadMessage(sockfd,buffer); |
---|
| 178 | ce_info->numObservables = atoi(buffer); |
---|
| 179 | //printf("number of observables: %d\n", ce_info->numObservables); |
---|
| 180 | |
---|
| 181 | for (i = 0; i < ce_info->numObservables; i++){ |
---|
| 182 | bzero(buffer,256); |
---|
| 183 | ReadMessage(sockfd,buffer); |
---|
| 184 | //printf("Name: %s\n", buffer); |
---|
| 185 | strcpy(oList[i].name, buffer); |
---|
| 186 | |
---|
| 187 | // numAffects |
---|
| 188 | bzero(buffer,256); |
---|
| 189 | ReadMessage(sockfd,buffer); |
---|
| 190 | oList[i].numAffects = atoi(buffer); |
---|
| 191 | //printf("number of affects: %d\n", oList[i].numAffects); |
---|
| 192 | |
---|
| 193 | for (j = 0; j < oList[i].numAffects; j++){ |
---|
| 194 | bzero(buffer,256); |
---|
| 195 | ReadMessage(sockfd,buffer); |
---|
| 196 | //printf("Utility name: %s\n", buffer); |
---|
| 197 | strcpy(oList[i].affection_list[j].u.name, buffer); |
---|
| 198 | |
---|
| 199 | bzero(buffer,256); |
---|
| 200 | ReadMessage(sockfd,buffer); |
---|
| 201 | //printf("Relation: %s\n", buffer); |
---|
| 202 | strcpy(oList[i].affection_list[j].relation, buffer); |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | return 1; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | int GetXMLFromServer(int sockfd, Utility uList[], Parameter pList[], |
---|
| 210 | Observable oList[], CE_Info *ce_info) |
---|
| 211 | { |
---|
| 212 | // get utilities |
---|
| 213 | GetUtility(sockfd, uList, ce_info); |
---|
| 214 | |
---|
| 215 | // get parameters |
---|
| 216 | GetParameter(sockfd, pList, ce_info); |
---|
| 217 | |
---|
| 218 | // read obervables |
---|
| 219 | GetObservable(sockfd, oList, ce_info); |
---|
| 220 | |
---|
| 221 | printf("xml reception done\n"); |
---|
| 222 | |
---|
| 223 | return 1; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | void print_current_config(Utility uList[], Parameter pList[], |
---|
| 227 | Observable oList[], CE_Info * ce_info) |
---|
| 228 | { |
---|
| 229 | int i = 0; |
---|
| 230 | int j = 0; |
---|
| 231 | |
---|
| 232 | // utilities |
---|
| 233 | for(i = 0; i < ce_info->numUtilities ; i++) { |
---|
| 234 | cout << "Utility: " << uList[i].name << endl; |
---|
| 235 | cout << " Units: " << uList[i].units << endl; |
---|
| 236 | cout << " Goal: " << uList[i].goal << endl; |
---|
| 237 | cout << " Target: " << uList[i].target << endl; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | // parameters |
---|
| 241 | for(i = 0; i < ce_info->numParameters; i++) { |
---|
| 242 | cout << "Parameter: " << pList[i].name << endl; |
---|
| 243 | cout << " Units: " << pList[i].units << endl; |
---|
| 244 | cout << " Min: " << pList[i].min << endl; |
---|
| 245 | cout << " Max: " << pList[i].max << endl; |
---|
| 246 | cout << " Step: " << pList[i].step << endl; |
---|
| 247 | for(j = 0; j < pList[i].numAffects; j++) { |
---|
| 248 | cout << " Affect: " << pList[i].affection_list[j].u.name << " -> " << pList[i].affection_list[j].relation << endl; |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | // observables |
---|
| 253 | for(i = 0; i < ce_info->numObservables; i++) { |
---|
| 254 | cout << "Observable: " << oList[i].name << endl; |
---|
| 255 | for(j = 0; j < oList[i].numAffects; j++) { |
---|
| 256 | cout << " Affect: " << oList[i].affection_list[j].u.name << " -> " << oList[i].affection_list[j].relation << endl; |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | // main client socket |
---|
| 263 | int main(int argc, char *argv[]) |
---|
| 264 | { |
---|
| 265 | Utility uList[10]; |
---|
| 266 | Parameter pList[10]; |
---|
| 267 | Observable oList[10]; |
---|
| 268 | CE_Info ce_info; |
---|
| 269 | |
---|
| 270 | int sockfd, portno; |
---|
| 271 | struct sockaddr_in serv_addr; |
---|
| 272 | struct hostent *server; |
---|
| 273 | |
---|
| 274 | // setup client socket connection |
---|
| 275 | if (argc < 3) { |
---|
| 276 | fprintf(stderr,"usage: %s hostname port\n", argv[0]); |
---|
| 277 | exit(0); |
---|
| 278 | } |
---|
| 279 | // server name |
---|
| 280 | server = gethostbyname(argv[1]); |
---|
| 281 | if (server == NULL) { |
---|
| 282 | fprintf(stderr,"ERROR, no such host\n"); |
---|
| 283 | exit(0); |
---|
| 284 | } |
---|
| 285 | // port no. |
---|
| 286 | portno = atoi(argv[2]); |
---|
| 287 | // socket file descriptor |
---|
| 288 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
| 289 | if (sockfd < 0) |
---|
| 290 | error("ERROR opening socket"); |
---|
| 291 | // initiate struct socketaddr_in |
---|
| 292 | bzero((char *) &serv_addr, sizeof(serv_addr)); |
---|
| 293 | serv_addr.sin_family = AF_INET; |
---|
| 294 | serv_addr.sin_port = htons(portno); |
---|
| 295 | bcopy((char *)server->h_addr, |
---|
| 296 | (char *)&serv_addr.sin_addr.s_addr, |
---|
| 297 | server->h_length); |
---|
| 298 | // cast sockaddr_in to sockaddr |
---|
| 299 | if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) |
---|
| 300 | error("ERROR connecting"); |
---|
| 301 | |
---|
| 302 | // get xml info from server |
---|
| 303 | GetXMLFromServer(sockfd, uList, pList, oList, &ce_info); |
---|
| 304 | |
---|
| 305 | //printf("utility:\n name: %s, units: %s, goal: %s, target: %f\n", |
---|
| 306 | // uList[0].name, uList[0].units, uList[0].goal, uList[0].target); |
---|
| 307 | |
---|
| 308 | print_current_config(uList, pList, oList, &ce_info); |
---|
| 309 | |
---|
| 310 | // cbr operation |
---|
| 311 | int i; |
---|
| 312 | unsigned int num_cols; |
---|
| 313 | |
---|
| 314 | num_cols = ce_info.numUtilities + ce_info.numParameters; |
---|
| 315 | num_cols = num_cols + ce_info.numObservables; |
---|
| 316 | printf("number of columns: %d\n", num_cols); |
---|
| 317 | |
---|
| 318 | char *cols[] = {"throughput", "spectral_efficiency", |
---|
| 319 | "log10_ber", "mod_scheme", "tx_power", "SNR"}; |
---|
| 320 | |
---|
| 321 | printf("column names:"); |
---|
| 322 | for (i = 0; i<num_cols; i++){ |
---|
| 323 | printf(" %s",cols[i]); |
---|
| 324 | } |
---|
| 325 | printf("\n"); |
---|
| 326 | |
---|
| 327 | |
---|
| 328 | float vals[num_cols]; |
---|
| 329 | vals[0] = 10e3f; // throughput |
---|
| 330 | vals[1] = 1; // spectral_efficiency |
---|
| 331 | vals[2] = 1e-3f; // log10_ber |
---|
| 332 | vals[3] = 1; // mod_scheme |
---|
| 333 | vals[4] = -3.0f; // tx_power |
---|
| 334 | vals[5] = 10.0f; // SNR |
---|
| 335 | //vals[6] = 0.762; // utility |
---|
| 336 | |
---|
| 337 | int rc; |
---|
| 338 | |
---|
| 339 | // create cbr database/table |
---|
| 340 | cbr mycbr = cbr_create("ex1", "data", cols, num_cols); |
---|
| 341 | |
---|
| 342 | // add row here |
---|
| 343 | rc = cbr_add_row(mycbr, cols, vals, num_cols); |
---|
| 344 | |
---|
| 345 | // print |
---|
| 346 | cbr_print(mycbr); |
---|
| 347 | |
---|
| 348 | // simple search: find entry where... |
---|
| 349 | // log10_ber < 1e-2 and SNR > 5 |
---|
| 350 | char * search_names[] = {"log10_ber", "SNR"}; |
---|
| 351 | int search_ops[] = {LT, GT}; |
---|
| 352 | float search_vals[] = {1e-2f, 100.f}; |
---|
| 353 | float retvals[num_cols]; |
---|
| 354 | rc = cbr_search(mycbr, search_names, search_ops, |
---|
| 355 | search_vals, 2, retvals); |
---|
| 356 | |
---|
| 357 | // clean up database |
---|
| 358 | cbr_free(mycbr); |
---|
| 359 | |
---|
| 360 | // print returned values |
---|
| 361 | printf("search result: "); |
---|
| 362 | for (i=0;i<num_cols; i++) |
---|
| 363 | //for (i=0;i<mycbr->num_columns; i++) |
---|
| 364 | printf("%f, ",retvals[i]); |
---|
| 365 | printf("\n"); |
---|
| 366 | |
---|
| 367 | printf("done at client.\n"); |
---|
| 368 | return 0; |
---|
| 369 | |
---|
| 370 | |
---|
| 371 | return 0; |
---|
| 372 | } |
---|