Changeset 166
- Timestamp:
- 03/19/09 23:55:15 (15 years ago)
- Location:
- vtcross/trunk/src
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/trunk/src/lib/socketcomm/socketcomm.cpp
r164 r166 7 7 /* TODO DESCRIPTION OF FILE. 8 8 */ 9 9 10 10 11 #include <cstdlib> -
vtcross/trunk/src/shell/cr_shell.cpp
r161 r166 1 /* Virginia Tech Cognitive Radio Open Source Systems 2 * Virginia Tech, 2009 3 * 4 * TODO LICENSE INFORMATION GOES HERE 5 */ 6 7 /* TODO DESCRIPTION OF FILE. 8 */ 9 10 11 #include <arpa/inet.h> 1 12 #include <iostream> 13 #include <netinet/in.h> 14 #include <netdb.h> 15 #include <fcntl.h> 16 #include <sys/ioctl.h> 17 #include <sys/mman.h> 18 #include <sys/socket.h> 2 19 #include <sys/types.h> 3 20 #include <sys/wait.h> 4 #include <sys/socket.h> 5 #include <netinet/in.h> 6 #include <netdb.h> 7 #include <arpa/inet.h> 8 #include <sys/mman.h> 9 #include <fcntl.h> 10 #include <sys/ioctl.h> 11 12 //#include "socket/ServerSocket.h" 13 //#include "socket/SocketException.h" 14 15 #include "vtcross/containers.h" 16 #include "vtcross/common.h" 21 17 22 #include "tinyxml/tinyxml.h" 18 23 #include "tinyxml/tinystr.h" 19 24 25 #include "vtcross/common.h" 26 #include "vtcross/components.h" 27 #include "vtcross/containers.h" 28 #include "vtcross/debug.h" 29 #include "vtcross/error.h" 30 #include "vtcross/socketcomm.h" 31 32 20 33 using namespace std; 21 34 35 22 36 #define CE_SERVER_PORT 30001 23 #define POLICY_SERVER_PORT 30003 24 25 void DieWithError(char *errorMessage) 26 { 27 perror(errorMessage); 28 exit(1); 29 } 30 31 void print_current_config(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { 32 int i = 0; 33 int j = 0; 34 35 for(i = 0; i < ce_info->numUtilities ; i++) { 36 cout << "Utility: " << uList[i]->name << endl; 37 cout << " Units: " << uList[i]->units << endl; 38 cout << " Goal: " << uList[i]->goal << endl; 39 cout << " Target: " << uList[i]->target << endl; 40 } 41 42 for(i = 0; i < ce_info->numParameters; i++) { 43 cout << "Parameter: " << pList[i]->name << endl; 44 printf("Cognitive Radio:: Radio Operation Profile has been sucessfully sent.\n"); 45 cout << " Units: " << pList[i]->units << endl; 46 cout << " Min: " << pList[i]->min << endl; 47 cout << " Max: " << pList[i]->max << endl; 48 cout << " Step: " << pList[i]->step << endl; 49 for(j = 0; j < pList[i]->numAffects; j++) { 50 cout << " Affect: " << pList[i]->affection_list[j].u->name << " -> " << pList[i]->affection_list[j].relation << endl; 37 #define PE_SERVER_PORT 30003 38 39 40 void 41 print_current_config(Utility* uList[], Parameter* pList[], \ 42 Observable* oList[], CE_Info* ce_info) 43 { 44 for(size_t i = 0; i < ce_info->numUtilities ; i++) { 45 LOG("Shell:: Utility: %s\n\tUnits: %s\n\tGoal: %s\n\tTarget: %s\n", \ 46 uList[i]->name, uList[i]->units, uList[i]->goal, \ 47 uList[i]->target); 48 } 49 50 for(size_t i = 0; i < ce_info->numParameters; i++) { 51 LOG("Shell:: Radio Operation Profile has been sucessfully sent.\n"); 52 LOG("Shell:: Parameter: %s\n\tUnits: %s\n\tMin: %s\n\t", \ 53 pList[i]->name, pList[i]->units, pList[i]->min); 54 LOG("\tMax: %s\n\tStep: %s\n", pList[i]->max, pList[i]->step); 55 56 for(size_t j = 0; j < pList[i]->numAffects; j++) { 57 LOG("\t\tAffect %s -> %s\n", pList[i]->affection_list[j].u->name, \ 58 pList[i]->affection_list[j].relation); 51 59 } 52 60 } 53 61 54 for(i = 0; i < ce_info->numObservables; i++) { 55 cout << "Observable: " << oList[i]->name << endl; 56 for(j = 0; j < oList[i]->numAffects; j++) { 57 cout << " Affect: " << oList[i]->affection_list[j].u->name << " -> " << oList[i]->affection_list[j].relation << endl; 62 for(size_t i = 0; i < ce_info->numObservables; i++) { 63 LOG("Observable: %s\n", oList[i]->name); 64 for(size_t j = 0; j < oList[i]->numAffects; j++) { 65 LOG("\t\tAffect %s -> %s ", oList[i]->affection_list[j].u->name, \ 66 oList[i]->affection_list[j].relation); 58 67 } 59 68 } … … 61 70 62 71 63 int parse_ce_config( TiXmlDocument * doc , Utility * u[], Parameter * p[], Observable * o[], CE_Info * ce_info) { 72 int32_t 73 parse_ce_config(TiXmlDocument* doc, Utility* u[], Parameter* p[], \ 74 Observable* o[], CE_Info* ce_info) 75 { 64 76 65 77 TiXmlElement* pElem; //!current element … … 70 82 TiXmlHandle hRoot(0); //! handle to root element 71 83 72 int count = 0;73 int i = 0;74 int j = 0;75 int k = 0;76 int match_found = 0;84 int32_t count = 0; 85 int32_t i = 0; 86 int32_t j = 0; 87 int32_t k = 0; 88 int32_t match_found = 0; 77 89 78 90 pElem = hDoc.FirstChildElement().Element(); … … 199 211 200 212 201 void error(char *msg) 202 { 203 perror(msg); 204 exit(1); 205 } 206 207 208 int ReceiveMessage(int socket,char * buffer) 209 { 210 int i,n; 213 int32_t 214 ReceiveMessage(int32_t socket, char* buffer) 215 { 216 int32_t i,n; 211 217 212 218 n = recv(socket,buffer,256,MSG_PEEK); … … 218 224 if (n < 0) 219 225 error("ERROR reading from socket"); 220 // print f("ReadMessage:%s %d\n",buffer,n);226 // print32_tf("ReadMessage:%s %d\n",buffer,n); 221 227 222 228 return n; … … 224 230 225 231 226 int SendMessage(int socketfd, string message) { 227 int n; 232 int32_t 233 SendMessage(int32_t socketfd, string message) 234 { 235 int32_t n; 228 236 229 237 message.append("\0"); … … 233 241 error("Error sending to client\n"); 234 242 if(n == 0) 235 print f("Client closed the socket.\n");236 237 //print f("SendMessage:%s %d\n",message.c_str(),n);243 print32_tf("Client closed the socket.\n"); 244 245 //print32_tf("SendMessage:%s %d\n",message.c_str(),n); 238 246 return n; 239 247 } 240 248 241 void GetEnvironment() { 242 243 } 244 245 void Policy_ValidateSettings() { 246 247 } 248 249 250 void LoadCEConfiguration(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info){ 251 int n,i,j; 249 250 void 251 LoadCEConfiguration(int32_t socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info){ 252 int32_t n,i,j; 252 253 char counter[55]; 253 254 char var[50]; 254 //int total_bytes;255 256 print f("Cognitive Radio:: Sending Radio Operating Profile to Cognitive Engine.\n\n");255 //int32_t total_bytes; 256 257 print32_tf("Cognitive Radio:: Sending Radio Operating Profile to Cognitive Engine.\n\n"); 257 258 258 259 // utilities 259 260 // Send number of utilities 260 sprint f(counter,"%d",ce_info->numUtilities);261 sprint32_tf(counter,"%d",ce_info->numUtilities); 261 262 SendMessage(socketfd,counter); 262 263 // send utility … … 265 266 SendMessage(socketfd,uList[i]->units); 266 267 SendMessage(socketfd,uList[i]->goal); 267 sprint f(var,"%f",uList[i]->target);268 sprint32_tf(var,"%f",uList[i]->target); 268 269 SendMessage(socketfd,var); 269 270 } 270 271 271 272 // parameters 272 sprint f(counter,"%i",ce_info->numParameters);273 sprint32_tf(counter,"%i",ce_info->numParameters); 273 274 SendMessage(socketfd,counter); 274 275 for(i = 0; i < ce_info->numParameters; i++) { 275 276 SendMessage(socketfd,pList[i]->name); 276 277 SendMessage(socketfd,pList[i]->units); 277 sprint f(var,"%f",pList[i]->min);278 SendMessage(socketfd,var); 279 sprint f(var,"%f",pList[i]->max);280 SendMessage(socketfd,var); 281 sprint f(var,"%f",pList[i]->step);278 sprint32_tf(var,"%f",pList[i]->min); 279 SendMessage(socketfd,var); 280 sprint32_tf(var,"%f",pList[i]->max); 281 SendMessage(socketfd,var); 282 sprint32_tf(var,"%f",pList[i]->step); 282 283 SendMessage(socketfd,var); 283 284 284 sprint f(counter,"%i",pList[i]->numAffects);285 sprint32_tf(counter,"%i",pList[i]->numAffects); 285 286 SendMessage(socketfd,counter); 286 287 for(j = 0; j < pList[i]->numAffects; j++) { … … 291 292 292 293 // observables 293 sprint f(counter,"%i",ce_info->numObservables);294 sprint32_tf(counter,"%i",ce_info->numObservables); 294 295 SendMessage(socketfd,counter); 295 296 for(i = 0; i < ce_info->numObservables; i++) { 296 297 SendMessage(socketfd,oList[i]->name); 297 298 298 sprint f(counter,"%i",oList[i]->numAffects);299 sprint32_tf(counter,"%i",oList[i]->numAffects); 299 300 SendMessage(socketfd,counter); 300 301 for(j = 0; j < oList[i]->numAffects; j++) { … … 308 309 string message; 309 310 n = ReceiveMessage(socketfd, buffer); 310 //print f("%s\n", buffer);311 //print32_tf("%s\n", buffer); 311 312 //cout << message << endl; 312 //print f("ACK received.\n");313 //print32_tf("ACK received.\n"); 313 314 314 315 } … … 322 323 } 323 324 324 void UpdateCEExperience(int socketfd, int num_rows, int num_cols,325 void UpdateCEExperience(int32_t socketfd, int32_t num_rows, int32_t num_cols, 325 326 float * past_exp[]) 326 327 { 327 int i, j;328 int32_t i, j; 328 329 char counter[55]; 329 330 char var[50]; … … 331 332 for (i = 0; i < num_rows; i++){ 332 333 for (j = 0; j< num_cols; j++){ 333 sprint f(var,"%f",past_exp[i][j]);334 //print f("%f, \n", past_exp[i][j]);335 //print f("%s, \n", var);334 sprint32_tf(var,"%f",past_exp[i][j]); 335 //print32_tf("%f, \n", past_exp[i][j]); 336 //print32_tf("%s, \n", var); 336 337 } 337 338 } 338 339 339 340 // send the number of rows to the ce first 340 sprint f(counter,"%d",num_rows);341 sprint32_tf(counter,"%d",num_rows); 341 342 SendMessage(socketfd,counter); 342 343 // send the number of columns to the ce 343 sprint f(counter,"%d",num_cols);344 sprint32_tf(counter,"%d",num_cols); 344 345 SendMessage(socketfd,counter); 345 346 // update ce with experience 346 347 for (i = 0; i < num_rows; i++){ 347 348 for (j = 0; j< num_cols; j++){ 348 sprint f(var,"%f",past_exp[i][j]);349 sprint32_tf(var,"%f",past_exp[i][j]); 349 350 SendMessage(socketfd,var); 350 351 } … … 358 359 359 360 // Update operating settings 360 // This function will int eract with the hardware "drivers"361 // This function will int32_teract with the hardware "drivers" 361 362 void UpdateRadioSettings() 362 363 { … … 364 365 365 366 366 int RequestPolicyValidation(Parameter * pList[], CE_Info *ce_info)367 int32_t RequestPolicyValidation(Parameter * pList[], CE_Info *ce_info) 367 368 { 368 369 char counter[55]; 369 370 char var[50]; 370 int i;371 int32_t i; 371 372 string control_msg; 372 373 373 int socketfd = ce_info->policy_socket;374 int32_t socketfd = ce_info->policy_socket; 374 375 375 376 // Control message that validation request is coming … … 377 378 SendMessage(socketfd,control_msg); 378 379 379 print f("Cognitive Radio:: Here. %i\n\n", socketfd);380 print32_tf("Cognitive Radio:: Here. %i\n\n", socketfd); 380 381 381 382 // Send parameter information 382 sprint f(counter,"%i",ce_info->numParameters);383 sprint32_tf(counter,"%i",ce_info->numParameters); 383 384 SendMessage(socketfd,counter); 384 385 for(i = 0; i < ce_info->numParameters; i++) { 385 386 SendMessage(socketfd,pList[i]->name); 386 387 SendMessage(socketfd,pList[i]->units); 387 sprint f(var,"%f",pList[i]->min);388 SendMessage(socketfd,var); 389 sprint f(var,"%f",pList[i]->max);390 SendMessage(socketfd,var); 391 sprint f(var,"%f",pList[i]->step);392 SendMessage(socketfd,var); 393 sprint f(var,"%f",pList[i]->value);388 sprint32_tf(var,"%f",pList[i]->min); 389 SendMessage(socketfd,var); 390 sprint32_tf(var,"%f",pList[i]->max); 391 SendMessage(socketfd,var); 392 sprint32_tf(var,"%f",pList[i]->step); 393 SendMessage(socketfd,var); 394 sprint32_tf(var,"%f",pList[i]->value); 394 395 SendMessage(socketfd,var); 395 396 … … 400 401 401 402 402 int RequestCEOptimization(int sockfd, Utility *uList[],403 int32_t RequestCEOptimization(int32_t sockfd, Utility *uList[], 403 404 Parameter *pList[], Observable *oList[], 404 405 CE_Info *ce_info) 405 406 { 406 407 char buffer[256]; 407 int i;408 int32_t i; 408 409 float var; 409 410 … … 428 429 if(ce_info->policy_engine == 1) { 429 430 430 print f("Cognitive Radio:: Found Policy Engine!\n");431 print f("Cognitive Radio:: Validating parameters with Policy Engine\n\n");431 print32_tf("Cognitive Radio:: Found Policy Engine!\n"); 432 print32_tf("Cognitive Radio:: Validating parameters with Policy Engine\n\n"); 432 433 RequestPolicyValidation(pList,ce_info); 433 print f("Cognitive Radio:: Done\n\n");434 print32_tf("Cognitive Radio:: Done\n\n"); 434 435 435 436 } … … 439 440 } 440 441 441 void RunSimulator(int socketfd, Utility * uList[],442 void RunSimulator(int32_t socketfd, Utility * uList[], 442 443 Parameter * pList[], Observable * oList[], 443 444 CE_Info * ce_info) { 444 445 445 446 float **past_exp; 446 int num_rows, num_cols;447 int32_t num_rows, num_cols; 447 448 448 449 // Set fake current environment params = current environment … … 457 458 } 458 459 459 void InitializePE(int socket, CE_Info * ce_info)460 void InitializePE(int32_t socket, CE_Info * ce_info) 460 461 { 461 462 // Policy Engine is connected … … 467 468 } 468 469 469 void InitializeCE(int socketfd, Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info)470 void InitializeCE(int32_t socketfd, Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) 470 471 { 471 472 LoadCEConfiguration(socketfd, uList, pList, oList, ce_info); … … 473 474 // cr experience 474 475 float **past_exp; 475 int num_cols;476 int32_t num_cols; 476 477 // get number of columns 477 478 num_cols = ce_info->numUtilities + ce_info->numParameters; 478 479 num_cols = num_cols + ce_info->numObservables; 479 480 num_cols = num_cols + 1; // overall utility 480 int num_rows = 2;481 int32_t num_rows = 2; 481 482 past_exp = (float **)malloc(sizeof(float)*num_rows); 482 int i;483 int32_t i; 483 484 for (i=0; i<num_rows; i++){ 484 485 past_exp[i] = (float*)malloc(sizeof(float)*num_cols); … … 502 503 503 504 // update ce with experience 504 print f("Cognitive Radio:: Sending Previous Experience to New Cognitive Engine.\n\n");505 print32_tf("Cognitive Radio:: Sending Previous Experience to New Cognitive Engine.\n\n"); 505 506 UpdateCEExperience(socketfd, num_rows, num_cols, past_exp); 506 507 … … 508 509 } 509 510 510 int AcceptTCPConnection(int servSock)511 { 512 int clntSock; /* Socket descriptor for client */511 int32_t AcceptTCPConnection(int32_t servSock) 512 { 513 int32_t clntSock; /* Socket descriptor for client */ 513 514 struct sockaddr_in echoClntAddr; 514 unsigned int clntLen;515 unsigned int32_t clntLen; 515 516 516 517 /* Set the size of the in-out parameter */ … … 525 526 /* clntSock is connected to a client! */ 526 527 527 print f("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));528 print32_tf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr)); 528 529 529 530 return clntSock; 530 531 } 531 532 532 int CreateTCPServerSocket(unsigned short port)533 { 534 int sock; /* socket to create */533 int32_t CreateTCPServerSocket(unsigned short port) 534 { 535 int32_t sock; /* socket to create */ 535 536 struct sockaddr_in echoServAddr; /* Local address */ 536 537 … … 541 542 /* Construct local address structure */ 542 543 memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ 543 echoServAddr.sin_family = AF_INET; /* Internet address family */544 echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming int erface */544 echoServAddr.sin_family = AF_INET; /* int32_ternet address family */ 545 echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming int32_terface */ 545 546 echoServAddr.sin_port = htons(port); /* Local port */ 546 547 … … 551 552 /* Mark the socket so it will listen for incoming connections */ 552 553 if (listen(sock, 5) < 0) { 553 print f("listen() failed\n");554 print32_tf("listen() failed\n"); 554 555 return 0; 555 556 } … … 558 559 } 559 560 560 void HandleTCPClient(int socketfd, Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info)561 void HandleTCPClient(int32_t socketfd, Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) 561 562 { 562 563 char buffer[256]; /* Buffer for echo string */ … … 566 567 ReceiveMessage(socketfd,buffer); 567 568 568 print f("Cognitive Radio:: Message Received - %s.\n\n", buffer);569 print32_tf("Cognitive Radio:: Message Received - %s.\n\n", buffer); 569 570 570 571 if(strcmp(buffer,"c_register") == 0) … … 580 581 } 581 582 582 int StartServers(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) {583 int * servSock;584 int running = 1;583 int32_t StartServers(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { 584 int32_t * servSock; 585 int32_t running = 1; 585 586 struct timeval selTimeout; 586 int timeout = 10;587 int cognitive_engine = 0;588 int policy_engine = 1;589 int port, rc, on = 1;590 int new_sd;591 int desc_ready = 0;587 int32_t timeout = 10; 588 int32_t cognitive_engine = 0; 589 int32_t policy_engine = 1; 590 int32_t port, rc, on = 1; 591 int32_t new_sd; 592 int32_t desc_ready = 0; 592 593 fd_set sockSet; 593 594 594 servSock = (int *) malloc(2 * sizeof(int));595 servSock = (int32_t *) malloc(2 * sizeof(int32_t)); 595 596 596 597 servSock[cognitive_engine] = CreateTCPServerSocket(CE_SERVER_PORT); 597 servSock[policy_engine] = CreateTCPServerSocket(P OLICY_SERVER_PORT);598 599 600 int maxDescriptor = servSock[cognitive_engine];598 servSock[policy_engine] = CreateTCPServerSocket(PE_SERVER_PORT); 599 600 601 int32_t maxDescriptor = servSock[cognitive_engine]; 601 602 602 603 if(servSock[cognitive_engine] < servSock[policy_engine]) … … 635 636 } 636 637 637 print f("Starting server: Hit return to shutdown\n");638 print32_tf("Starting server: Hit return to shutdown\n"); 638 639 while (running) 639 640 { … … 654 655 rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); 655 656 if (rc == 0) 656 print f("No echo requests for %i secs...Server still alive\n", timeout);657 print32_tf("No echo requests for %i secs...Server still alive\n", timeout); 657 658 else 658 659 { 659 660 if (FD_ISSET(0, &sockSet)) /* Check keyboard */ 660 661 { 661 print f("Shutting down server\n");662 print32_tf("Shutting down server\n"); 662 663 getchar(); 663 664 running = 0; … … 669 670 if (FD_ISSET(port, &sockSet)) 670 671 { 671 print f("Request on port %d: ", port);672 print32_tf("Request on port %d: ", port); 672 673 desc_ready -= 1; 673 674 … … 687 688 if(new_sd > maxDescriptor) 688 689 maxDescriptor = new_sd; 689 print f("New incoming connection - %i\n\n",new_sd);690 print32_tf("New incoming connection - %i\n\n",new_sd); 690 691 } while(new_sd != -1); 691 692 } else { 692 693 693 print f("Request on already open descriptor.\n\n");694 print32_tf("Request on already open descriptor.\n\n"); 694 695 HandleTCPClient(port, uList, pList, oList, ce_info); 695 696 … … 711 712 } 712 713 713 int main(int argc, char* argv[]) {714 715 714 int32_t 715 main(int32_t argc, char* argv[]) 716 { 716 717 // CognitiveEngine CE; 717 718 // CognitiveEngineShell Shell; 718 719 string pFilename; 719 int fd;720 int32_t fd; 720 721 721 722 Utility * uList[10]; … … 750 751 cout << "Initialize:: Configuration file parsing completed.\n" << endl; 751 752 752 //print _current_config(uList, pList, oList, &ce_info);753 //print32_t_current_config(uList, pList, oList, &ce_info); 753 754 754 755 StartServers(uList, pList, oList, ce_info);