Changeset 389 for vtcross/trunk
- Timestamp:
- 07/28/09 23:23:14 (15 years ago)
- Location:
- vtcross/trunk/src
- Files:
-
- 9 added
- 7 modified
Legend:
- Unmodified
- Added
- Removed
-
vtcross/trunk/src/cognitive_engines/DSA_CE/DSA_CognitiveEngine.cpp
r375 r389 27 27 #include "sqlite3.h" 28 28 #include "sqlite3ext.h" 29 30 #define INTERFERENCE 0 31 #define COMMUNICATION_TIME 1 32 #define CHANNEL 2 33 #define ENERGY 3 34 #define FITNESS 4 35 29 36 30 37 … … 632 639 } 633 640 641 642 /* The core of the CE is this function */ 643 634 644 Parameter* 635 645 CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters) … … 637 647 LOG("Cognitive Engine:: Generating solution.\n"); 638 648 639 char *searchNames[radioInfo->numUtilities]; 649 /* Put together the CBR search array */ 650 651 char *searchNames[radioInfo->numUtilities + 1]; 652 float searchVals[radioInfo->numUtilities + 1]; 653 int searchOps[radioInfo->numUtilities + 1]; 654 uint32_t numberColumns = radioInfo->numUtilities + 655 radioInfo->numParameters + 656 radioInfo->numObservables + 1; 640 657 641 658 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 642 searchNames[i] = (char*)observables[i].name.c_str(); 643 } 644 645 float searchVals[radioInfo->numUtilities]; 646 647 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 648 searchVals[i] = uList[i].target; 649 } 650 651 uint32_t numberColumns = 652 radioInfo->numUtilities + 653 radioInfo->numParameters + 654 radioInfo->numObservables + 1; 655 656 float returnValues[numberColumns]; 657 658 int searchOps[radioInfo->numUtilities]; 659 for(size_t i = 0; i < radioInfo->numUtilities; i++) { 660 661 /* If the goal is to maximum, set the search operation to 662 * return values greater than the target. 663 * 664 * If the goal is to minimize, set the search operation to 665 * return values less than the target. 666 */ 659 searchNames[i] = (char*)uList[i].name.c_str(); 660 667 661 if(strcmp(uList[i].goal.c_str(), "max") == 0) { 668 662 searchOps[i] = GT; … … 670 664 searchOps[i] = LT; 671 665 } 672 } 673 674 /* CBR specific call */ 666 667 /* Set search target to utility target */ 668 /* This may be bad if no target is set */ 669 searchVals[i] = uList[i].target; 670 } 671 672 673 /* Make sure we do not return any entries for the current channel */ 674 searchNames[radioInfo->numUtilities+1] = "channel"; 675 searchOps[radioInfo->numUtilities+1] = NE; 676 searchVals[radioInfo->numUtilities+1] = currentParameters[0].value; 677 678 float returnValues[numberColumns]; 679 680 /* Execute CBR search and put output into returnValues */ 675 681 uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 676 682 radioInfo->numUtilities, returnValues); 677 683 678 684 if(rc == 0){ 679 /* Adapt the returned parameters to meet the objective */ 680 LOG("Cognitive Engine:: Found\n"); 681 682 /* Should do a random adaptation.. */ 685 /* Similiar environment was found. Adapt the returned parameters to 686 meet the objective */ 687 688 LOG("Cognitive Engine:: Previous similar environment found\n"); 689 683 690 if(returnValues[numberColumns-1] < 0) { 691 684 692 returnValues[2] = returnValues[2] - 15; 685 693 returnValues[3] = returnValues[3] - 2; 694 686 695 } else { 696 687 697 returnValues[2] = returnValues[2] + 15; 688 698 returnValues[3] = returnValues[3] + 2; 699 689 700 } 690 701 } else if(rc == 31337) { 691 LOG("Cognitive Engine:: Not Found.\n"); 692 /* No rows in the CBR, pick default parameters */ 693 /* Currently this is hard coded and implementation specific! */ 694 returnValues[2] = currentParameters[0].value + 5; 695 returnValues[3] = currentParameters[1].value + 10; 696 702 /* No previous similar solution was found, use default. */ 703 LOG("Cognitive Engine:: No previous similar environment found.\n"); 704 705 /* In the DSA case, the default is to select a random channel */ 706 707 /* initialize random seed: */ 708 srand ( time(NULL) ); 709 710 /* generate random channel */ 711 //returnValues[CHANNEL] = rand() % currentParameters[0].max + currentParameters[0].min; 712 returnValues[CHANNEL] = rand() % 5; 713 714 printf("One: %i Two: %i\n",(int)currentParameters[0].max,(int)currentParameters[0].min); 715 697 716 } else { 698 717 LOG("Cognitive Engine:: Search return an invalid value.\n"); 699 718 } 719 720 721 /* Package up the new set of parameters in order to add 722 the new entry into the CBR database. */ 700 723 701 724 size_t returnValueIndex = 0; … … 732 755 allNames[allNameIndex] = "utility"; 733 756 734 / / Add row to CBR.757 /* Add the new optimized set to the CBR database */ 735 758 cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1); 736 759 760 /* Return the set of new parameter values. */ 737 761 return pList; 738 762 } -
vtcross/trunk/src/cognitive_engines/DSA_CE/cbr.c
r375 r389 57 57 char *zErrMsg = 0; 58 58 59 //printf("command: %s\n", _cbr->command);59 printf("command: %s\n", _cbr->command); 60 60 rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg); 61 61 if( rc!=SQLITE_OK){ … … 212 212 strcat(_cbr->command, _names[i]); 213 213 strcat(_cbr->command, ops_str[_ops[i]]); 214 printf("search command: %s\n", _cbr->command);215 214 sprintf(str_buffer, "%E", _vals[i]); 216 215 strcat(_cbr->command, str_buffer); 217 218 216 219 217 if (i<_n-1) … … 225 223 //printf("search command: %s\n", _cbr->command); 226 224 227 //ExecuteCommand(_cbr);228 225 rc = ExecuteSearchCommand(_cbr, _retvals); 229 226 -
vtcross/trunk/src/include/vtcross/components.h
r285 r389 374 374 /* Handle a message that is received from a component. 375 375 */ 376 voidHandleMessage(int32_t socketFD);376 int HandleMessage(int32_t socketFD); 377 377 378 378 /* Send optimization request to primary port FD. -
vtcross/trunk/src/include/vtcross/socketcomm.h
r255 r389 18 18 /* TODO 19 19 */ 20 voidReadMessage(int32_t socketFD, char *msgBuffer);20 int32_t ReadMessage(int32_t socketFD, char *msgBuffer); 21 21 22 22 /* TODO -
vtcross/trunk/src/lib/socketcomm/socketcomm.cpp
r232 r389 34 34 // TODO also, it appears that this function can, at maximum, receive 256 bytes 35 35 // without causing a buffer overflow. can someone confirm/deny? 36 void 36 int32_t 37 37 ReadMessage(int32_t socketFD, char* msgBuffer) 38 38 { … … 41 41 // Messages are termination with a "\0" to denote EOM. 42 42 ssize_t msgLength = recv(socketFD, msgBuffer, 256, MSG_PEEK); 43 if(msgLength < 0)44 ERROR(1, "Error reading from socket.\n");45 if(msgLength == 0)46 ERROR(1, "Remote component closed connection. 1\n");47 43 if(msgLength <= 0) { 44 //ERROR(1, "Remote component closed connection. 1\n"); 45 //LOG("Client closed connection.\n"); 46 return -1; 47 } 48 48 size_t i; 49 49 for(i = 0; i < 256; i++) { … … 54 54 // Read the message into msgBuffer 55 55 msgLength = recv(socketFD, msgBuffer, i + 1, 0); 56 if(msgLength < 0) 57 ERROR(1, "Error reading from socket.\n"); 58 if(msgLength == 0) 59 ERROR(1, "Remote component closed connection. 2\n"); 56 if(msgLength <= 0) { 57 //ERROR(1, "Remote component closed connection. 1\n"); 58 //LOG("Client closed connection.\n"); 59 return -1; 60 } 61 62 return 1; 60 63 } 61 64 … … 93 96 94 97 95 /* TODO I'm fairly certain this function is unnecessary, see function below for more details...96 int32_t97 SendMessage(int32_t socketFD, char* message)98 {99 100 // TODO explain this. What, exactly, does the below line do and how does it101 // affect the rest of the function?102 strcat(message, "\0000");103 104 ssize_t numSentBytes = send(socketFD, message, (strlen(message) + 1), 0);105 if(numSentBytes < 0) {106 ERROR(1, "Error sending to server.");107 }108 else if(numSentBytes == 0) {109 LOG("socket_comm::SendMessage - Server closed the socket.\n");110 }111 112 return numSentBytes;113 }114 */115 116 98 // TODO this function is here to handle calls to send const char* messages. Note 117 99 // that the std::string.c_str() function auto-appends a null character at the … … 217 199 if(clientSocket < 0) { 218 200 //WARNING("ALERT: Could not establish connection with client socket.\n"); 219 return -1;201 return clientSocket; 220 202 } 221 203 //LOG("Handling client %s\n", inet_ntoa(echoClientAddr.sin_addr)); -
vtcross/trunk/src/libvtcross/libvtcross.cpp
r299 r389 149 149 } 150 150 151 close(socketFD); 151 152 return pList; 152 153 } -
vtcross/trunk/src/shell/CognitiveRadioShell.cpp
r316 r389 642 642 643 643 644 void 644 int 645 645 CognitiveRadioShell::HandleMessage(int32_t socketFD) 646 646 { 647 647 char buffer[256]; 648 //printf("here\n"); 649 ReadMessage(socketFD, buffer); 648 int ret = 0; 649 650 ret = ReadMessage(socketFD, buffer); 651 if(ret == -1) { 652 return ret; 653 } 650 654 651 655 // TODO trying to read this code lock makes my eyes bleed … … 685 689 //GetOptimalParametersService(socketFD); 686 690 } 691 692 return ret; 687 693 } 688 694 … … 699 705 int32_t desc_ready = 1; 700 706 int32_t timeout = 10; 707 int32_t ret = 0;; 701 708 fd_set sockSet; 709 710 702 711 703 712 int32_t *servSock = new int32_t[3]; … … 726 735 ERROR(1,"Error initializing command port\n"); 727 736 737 FD_ZERO(&sockSet); 738 728 739 while (running) { 729 740 /* Zero socket descriptor vector and set for server sockets */ 730 741 /* This must be reset every time select() is called */ 731 //FD_ZERO(&sockSet);732 742 FD_SET(servSock[primary], &sockSet); 733 743 FD_SET(servSock[policy], &sockSet); … … 740 750 741 751 /* Suspend program until descriptor is ready or timeout */ 742 //printf("Waiting for signal...\n"); 743 rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, NULL); 752 rc = select(maxDescriptor + 1, &sockSet, NULL, NULL, &selTimeout); 744 753 if(rc == 0) 745 754 LOG("No echo requests for %i secs...Server still alive\n", timeout); … … 754 763 do { 755 764 new_sd = AcceptTCPConnection(port); 756 if(new_sd < 0) 765 if(new_sd < 0) { 757 766 break; 767 } 758 768 759 769 if(port == servSock[primary]) … … 764 774 policySocketFD = new_sd; 765 775 766 HandleMessage(new_sd); 767 FD_SET(new_sd,&sockSet); 776 ret = HandleMessage(new_sd); 777 778 if(ret == -1) { 779 FD_CLR(new_sd,&sockSet); 780 close(new_sd); 781 } 782 783 FD_SET(new_sd,&sockSet); 768 784 if(new_sd > maxDescriptor) 769 785 maxDescriptor = new_sd; 770 //LOG("New incoming connection - %i\n\n",new_sd);771 786 } while(new_sd != -1); 772 787 } 773 788 else { 774 //LOG("Request on already open descriptor.\n\n"); 775 HandleMessage(port); 789 ret = HandleMessage(port); 790 if(ret == -1) { 791 FD_CLR(port,&sockSet); 792 close(port); 793 } 776 794 } 777 795 } … … 780 798 } 781 799 782 800 LOG("Closing it all.\n\n"); 783 801 /* Close sockets */ 784 802 close(servSock[primary]);