Changeset 389

Show
Ignore:
Timestamp:
07/28/09 23:23:14 (15 years ago)
Author:
trnewman
Message:

Added DSA CBR reference implementation.
Added simple python example application for DSA CBR.
Added GNUradio python application that uses CROSS and the DSA CBR.

Fixed several bugs in the socket interface.

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  
    2727#include "sqlite3.h" 
    2828#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 
    2936 
    3037 
     
    632639} 
    633640 
     641 
     642/* The core of the CE is this function */ 
     643 
    634644Parameter*  
    635645CognitiveEngine::GetSolution(Observable *observables, Parameter *currentParameters) 
     
    637647    LOG("Cognitive Engine:: Generating solution.\n"); 
    638648 
    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; 
    640657 
    641658    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         
    667661        if(strcmp(uList[i].goal.c_str(), "max") == 0) { 
    668662            searchOps[i] = GT; 
     
    670664            searchOps[i] = LT; 
    671665        } 
    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 */ 
    675681    uint32_t rc = cbr_search(myCBR, searchNames, searchOps, searchVals, 
    676682            radioInfo->numUtilities, returnValues); 
    677683 
    678684    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 
    683690        if(returnValues[numberColumns-1] < 0) { 
     691         
    684692            returnValues[2] = returnValues[2] - 15; 
    685693            returnValues[3] = returnValues[3] - 2; 
     694         
    686695        } else { 
     696        
    687697            returnValues[2] = returnValues[2] + 15; 
    688698            returnValues[3] = returnValues[3] + 2; 
     699         
    689700        } 
    690701    } 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  
    697716    } else { 
    698717        LOG("Cognitive Engine:: Search return an invalid value.\n"); 
    699718    } 
     719 
     720 
     721    /* Package up the new set of parameters in order to add 
     722       the new entry into the CBR database.  */ 
    700723 
    701724    size_t returnValueIndex = 0; 
     
    732755    allNames[allNameIndex] = "utility"; 
    733756 
    734     // Add row to CBR.  
     757    /* Add the new optimized set to the CBR database */ 
    735758    cbr_add_row(myCBR, allNames, returnValues, returnValueIndex+1); 
    736759 
     760    /* Return the set of new parameter values.  */ 
    737761    return pList; 
    738762} 
  • vtcross/trunk/src/cognitive_engines/DSA_CE/cbr.c

    r375 r389  
    5757    char *zErrMsg = 0; 
    5858 
    59     //printf("command: %s\n", _cbr->command); 
     59    printf("command: %s\n", _cbr->command); 
    6060    rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg); 
    6161    if( rc!=SQLITE_OK){ 
     
    212212        strcat(_cbr->command, _names[i]); 
    213213        strcat(_cbr->command, ops_str[_ops[i]]); 
    214     printf("search command: %s\n", _cbr->command); 
    215214        sprintf(str_buffer, "%E", _vals[i]); 
    216215        strcat(_cbr->command, str_buffer); 
    217  
    218216 
    219217        if (i<_n-1) 
     
    225223    //printf("search command: %s\n", _cbr->command); 
    226224 
    227     //ExecuteCommand(_cbr); 
    228225    rc = ExecuteSearchCommand(_cbr, _retvals); 
    229226     
  • vtcross/trunk/src/include/vtcross/components.h

    r285 r389  
    374374        /* Handle a message that is received from a component. 
    375375         */ 
    376         void HandleMessage(int32_t socketFD); 
     376        int HandleMessage(int32_t socketFD); 
    377377        
    378378        /* Send optimization request to primary port FD. 
  • vtcross/trunk/src/include/vtcross/socketcomm.h

    r255 r389  
    1818/* TODO 
    1919 */ 
    20 void ReadMessage(int32_t socketFD, char *msgBuffer); 
     20int32_t ReadMessage(int32_t socketFD, char *msgBuffer); 
    2121 
    2222/* TODO 
  • vtcross/trunk/src/lib/socketcomm/socketcomm.cpp

    r232 r389  
    3434// TODO also, it appears that this function can, at maximum, receive 256 bytes 
    3535// without causing a buffer overflow. can someone confirm/deny? 
    36 void 
     36int32_t 
    3737ReadMessage(int32_t socketFD, char* msgBuffer) 
    3838{ 
     
    4141    // Messages are termination with a "\0" to denote EOM. 
    4242    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    } 
    4848    size_t i; 
    4949    for(i = 0; i < 256; i++) { 
     
    5454    // Read the message into msgBuffer 
    5555    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; 
    6063} 
    6164 
     
    9396 
    9497     
    95 /* TODO I'm fairly certain this function is unnecessary, see function below for more details... 
    96 int32_t 
    97 SendMessage(int32_t socketFD, char* message)  
    98 { 
    99       
    100     // TODO explain this. What, exactly, does the below line do and how does it 
    101     // 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  
    11698// TODO this function is here to handle calls to send const char* messages. Note 
    11799// that the std::string.c_str() function auto-appends a null character at the 
     
    217199    if(clientSocket < 0) {  
    218200        //WARNING("ALERT: Could not establish connection with client socket.\n"); 
    219         return -1; 
     201        return clientSocket; 
    220202    } 
    221203    //LOG("Handling client %s\n", inet_ntoa(echoClientAddr.sin_addr)); 
  • vtcross/trunk/src/libvtcross/libvtcross.cpp

    r299 r389  
    149149    }    
    150150 
     151    close(socketFD); 
    151152    return pList; 
    152153} 
  • vtcross/trunk/src/shell/CognitiveRadioShell.cpp

    r316 r389  
    642642 
    643643 
    644 void 
     644int 
    645645CognitiveRadioShell::HandleMessage(int32_t socketFD) 
    646646{ 
    647647    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    } 
    650654 
    651655    // TODO trying to read this code lock makes my eyes bleed 
     
    685689        //GetOptimalParametersService(socketFD);   
    686690    } 
     691 
     692    return ret; 
    687693} 
    688694 
     
    699705    int32_t desc_ready = 1; 
    700706    int32_t timeout = 10; 
     707    int32_t ret = 0;; 
    701708    fd_set sockSet; 
     709    
     710 
    702711 
    703712    int32_t *servSock = new int32_t[3]; 
     
    726735        ERROR(1,"Error initializing command port\n"); 
    727736 
     737    FD_ZERO(&sockSet); 
     738     
    728739    while (running) { 
    729740        /* Zero socket descriptor vector and set for server sockets */ 
    730741        /* This must be reset every time select() is called */ 
    731         //FD_ZERO(&sockSet); 
    732742        FD_SET(servSock[primary], &sockSet); 
    733743        FD_SET(servSock[policy], &sockSet); 
     
    740750 
    741751        /* 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);  
    744753        if(rc == 0) 
    745754            LOG("No echo requests for %i secs...Server still alive\n", timeout); 
     
    754763                        do { 
    755764                            new_sd = AcceptTCPConnection(port); 
    756                             if(new_sd < 0)  
     765                            if(new_sd < 0) { 
    757766                                break; 
     767                            } 
    758768                            
    759769                            if(port == servSock[primary]) 
     
    764774                                policySocketFD = new_sd; 
    765775 
    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); 
    768784                            if(new_sd > maxDescriptor)  
    769785                                maxDescriptor = new_sd; 
    770                             //LOG("New incoming connection - %i\n\n",new_sd); 
    771786                        } while(new_sd != -1); 
    772                     }  
     787                    }  
    773788                    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                        } 
    776794                    } 
    777795                } 
     
    780798    } 
    781799 
    782  
     800    LOG("Closing it all.\n\n"); 
    783801    /* Close sockets */ 
    784802    close(servSock[primary]);