Changeset 72 for vtcross

Show
Ignore:
Timestamp:
11/22/08 22:48:38 (16 years ago)
Author:
trnewman
Message:

Added some API function shells

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vtcross/branches/trnewman/CR_shell/src/main_cognitive_radio.cpp

    r71 r72  
    66#include "socket/ServerSocket.h" 
    77#include "socket/SocketException.h" 
    8  
    98 
    109using namespace std; 
     
    221220 
    222221        // Clear incoming data buffer 
    223     bzero(buffer,256); 
     222        bzero(buffer,256); 
    224223     
    225224        // Wait for incoming message 
     
    227226         
    228227        // Print message 
    229     if (n > 0)  
    230         printf("Here is the received meessage: %s\n",buffer); 
    231     //printf("n = %d\n",n); 
    232          
    233     return n; 
     228        if (n > 0)  
     229                printf("Here is the received meessage: %s\n",buffer); 
     230        if(n == 0) 
     231                printf("Client closed the socket.\n"); 
     232         
     233        return n; 
    234234} 
    235235 
    236236int SendMessage(int socketfd, string message) { 
    237237        int n; 
    238     int len; 
    239      
    240     char LEN[8]; 
    241     char * temp; 
    242238 
    243239        message.append("\0000");         
     
    246242        if (n<0) 
    247243                error("Error sending to client\n"); 
    248         printf("sent: %s %i\n", message.c_str(),n); 
     244        if(n == 0) 
     245                printf("Client closed the socket.\n"); 
    249246         
    250247        return n; 
    251248} 
    252249 
    253 void StartMessaging(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { 
     250void GetEnvironment() { 
     251 
     252} 
     253 
     254void Policy_ValidateSettings() { 
     255 
     256} 
     257 
     258// Update operating settings 
     259// This function will interact with the hardware "drivers" 
     260void UpdateRadioSettings() { 
     261 
     262} 
     263 
     264void LoadCEConfiguration(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info){ 
    254265        int n,i,j; 
    255266        char counter[55]; 
    256267        char var[50]; 
    257      
    258 //    while(1) { 
    259                 // Send number of utilities 
    260                 sprintf(counter,"%d",ce_info->numUtilities); 
     268        int total_bytes;    
     269 
     270        printf("Sending configuration to CE.\n"); 
     271  
     272        // Send number of utilities 
     273        sprintf(counter,"%d",ce_info->numUtilities); 
     274        SendMessage(socketfd,counter); 
     275                 
     276        // utilities 
     277        for(i = 0; i < ce_info->numUtilities; i++) { 
     278                SendMessage(socketfd,uList[i]->name); 
     279                SendMessage(socketfd,uList[i]->units); 
     280                SendMessage(socketfd,uList[i]->goal); 
     281                sprintf(var,"%f",uList[i]->target); 
     282                SendMessage(socketfd,var); 
     283        } 
     284 
     285        // parameters 
     286        sprintf(counter,"%i",ce_info->numParameters); 
     287        SendMessage(socketfd,counter); 
     288        for(i = 0; i < ce_info->numParameters; i++) { 
     289                SendMessage(socketfd,pList[i]->name); 
     290                SendMessage(socketfd,pList[i]->units); 
     291                sprintf(var,"%f",pList[i]->min); 
     292                SendMessage(socketfd,var); 
     293                sprintf(var,"%f",pList[i]->max); 
     294                SendMessage(socketfd,var); 
     295                sprintf(var,"%f",pList[i]->step); 
     296                SendMessage(socketfd,var); 
     297                 
     298                sprintf(counter,"%i",pList[i]->numAffects);  
    261299                SendMessage(socketfd,counter); 
    262                  
    263                 // utilities 
    264         for(i = 0; i < ce_info->numUtilities; i++) { 
    265         //for(i = 0; i < 1; i++) { 
    266                         SendMessage(socketfd,uList[i]->name); 
    267                         SendMessage(socketfd,uList[i]->units); 
    268                         SendMessage(socketfd,uList[i]->goal); 
    269                         sprintf(var,"%f",uList[i]->target); 
    270                         SendMessage(socketfd,var); 
    271                 } 
    272  
    273                 // parameters 
    274         sprintf(counter,"%i",ce_info->numParameters); 
     300                for(j = 0; j < pList[i]->numAffects; j++) { 
     301                        SendMessage(socketfd,pList[i]->affection_list[j].u->name); 
     302                        SendMessage(socketfd,pList[i]->affection_list[j].relation); 
     303                } 
     304        } 
     305 
     306        // observables 
     307        sprintf(counter,"%i",ce_info->numObservables); 
     308        SendMessage(socketfd,counter); 
     309        for(i = 0; i < ce_info->numObservables; i++) { 
     310                SendMessage(socketfd,oList[i]->name); 
     311                 
     312                sprintf(counter,"%i",oList[i]->numAffects);  
    275313                SendMessage(socketfd,counter); 
    276                 for(i = 0; i < ce_info->numParameters; i++) { 
    277                         SendMessage(socketfd,pList[i]->name); 
    278                         SendMessage(socketfd,pList[i]->units); 
    279                         sprintf(var,"%f",pList[i]->min); 
    280                         SendMessage(socketfd,var); 
    281                         sprintf(var,"%f",pList[i]->max); 
    282                         SendMessage(socketfd,var); 
    283                         sprintf(var,"%f",pList[i]->step); 
    284                         SendMessage(socketfd,var); 
    285                  
    286                         sprintf(counter,"%i",pList[i]->numAffects);  
    287                         SendMessage(socketfd,counter); 
    288                         for(j = 0; j < pList[i]->numAffects; j++) { 
    289                                 SendMessage(socketfd,pList[i]->affection_list[j].u->name); 
    290                                 SendMessage(socketfd,pList[i]->affection_list[j].relation); 
    291                         } 
    292                 } 
    293  
    294         // observables 
    295                 sprintf(counter,"%i",ce_info->numObservables); 
    296                 SendMessage(socketfd,counter); 
    297                 for(i = 0; i < ce_info->numObservables; i++) { 
    298                         SendMessage(socketfd,oList[i]->name); 
    299                          
    300                         sprintf(counter,"%i",oList[i]->numAffects);  
    301                         SendMessage(socketfd,counter); 
    302                         for(j = 0; j < oList[i]->numAffects; j++) { 
    303                                 SendMessage(socketfd,oList[i]->affection_list[j].u->name); 
    304                                 SendMessage(socketfd,oList[i]->affection_list[j].relation); 
    305                         } 
    306                 } 
    307          
    308                 // Receive a message 
     314                for(j = 0; j < oList[i]->numAffects; j++) { 
     315                        SendMessage(socketfd,oList[i]->affection_list[j].u->name); 
     316                        SendMessage(socketfd,oList[i]->affection_list[j].relation); 
     317                } 
     318        } 
     319         
     320        printf("Configuration sent, waiting for ACK...\n"); 
     321        // Receive ACK for utils 
    309322        string message; 
    310                 n = ReceiveMessage(socketfd, message); 
     323        n = ReceiveMessage(socketfd, message); 
    311324        cout << message << endl; 
    312  
    313  
    314         //printf("n = %d\n", n); 
    315                 /*if (n < 0) { 
    316                         printf("ERROR reading from socket\n"); 
    317                         close(socketfd); 
    318                         return; 
    319                 } 
    320                 if (n == 0) { 
    321                         printf("Client has closed the connection.\n"); 
    322                         return; 
    323                 }*/ 
    324          
    325 //    } 
     325        printf("ACK received.\n"); 
     326 
     327} 
     328 
     329void UpdateCEConfiguration() { 
     330 
     331} 
     332 
     333void ResetCEConfiguration(){ 
     334 
     335} 
     336 
     337void LoadCEExperience(){ 
     338 
     339} 
     340 
     341void UpdateCEExperience() { 
     342 
     343} 
     344 
     345void ResetCEExperience() { 
     346 
     347} 
     348 
     349void RequestCEOptimization() { 
     350 
     351} 
     352 
     353void RunSimulator(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { 
     354 
     355        // Set fake past environment 
     356        LoadCEExperience(); 
     357         
     358        // Set fake current environment 
     359        RequestCEOptimization(); 
     360 
     361        // Act like we are updating the hardware tranmission settings 
     362        UpdateRadioSettings(); 
     363 
     364        // Send back fake utility values 
     365        UpdateCEExperience();    
     366} 
     367 
     368void StartMessaging(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { 
     369 
     370        LoadCEConfiguration(socketfd,uList, pList, oList, ce_info); 
     371 
     372        RunSimulator(socketfd,uList, pList, oList, ce_info); 
    326373} 
    327374 
     
    391438        cout << "Configuration file parsing completed." << endl; 
    392439 
    393     print_current_config(uList, pList, oList, &ce_info); 
     440    //print_current_config(uList, pList, oList, &ce_info); 
    394441         
    395442    pid = fork();