Changeset 65 for vtcross/branches

Show
Ignore:
Timestamp:
11/20/08 17:09:46 (16 years ago)
Author:
trnewman
Message:

new code

Location:
vtcross/branches/trnewman/CR_shell/src
Files:
2 modified

Legend:

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

    r64 r65  
    77#include "socket/SocketException.h" 
    88 
     9 
    910using namespace std; 
    1011 
     
    1617        int numObservables; 
    1718}; 
    18  
    19  
    2019 
    2120struct Utility { 
     
    4544}; 
    4645 
    47 void print_current_config(Utility * uList[], Parameter * pList[10], Observable * oList[], CE_Info * ce_info) { 
     46void print_current_config(Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { 
    4847        int i = 0; 
    4948        int j = 0; 
     
    216215} 
    217216 
    218 void StartMessageParser( int socketfd) { 
     217int ReceiveMessage(int socketfd) { 
     218         
    219219        char buffer[256]; 
    220220        int n; 
    221221 
    222         while(1) { 
    223                 // Clear incoming data buffer 
    224                 bzero(buffer,256); 
    225                  
    226                 // Wait for incoming message 
    227                 n = recv(socketfd,buffer,255,0); 
    228                 if (n < 0) error("ERROR reading from socket"); 
    229                 if (n == 0) { 
     222        // Clear incoming data buffer 
     223        bzero(buffer,256); 
     224     
     225        // Wait for incoming message 
     226        n = recv(socketfd,buffer,255,0); 
     227         
     228        // Print message 
     229        if (n > 0) printf("Here is the message: %s\n",buffer); 
     230 
     231        return n; 
     232} 
     233 
     234int SendMessage(int socketfd, string message) { 
     235        int n; 
     236 
     237        // Write message back to client 
     238        n = send(socketfd,message.c_str(),(message.size()+1),MSG_NOSIGNAL); 
     239        return n; 
     240 
     241} 
     242 
     243void StartMessaging(int socketfd,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { 
     244        int n,i,j; 
     245        char counter[55]; 
     246        char var[50]; 
     247        int k; 
     248 
     249 
     250//      while(1) { 
     251                // Send parameters from struct 
     252                sprintf(counter,"%d",ce_info->numUtilities); 
     253                SendMessage(socketfd,counter); 
     254                 
     255                for(i = 0; i < ce_info->numUtilities ; i++) { 
     256                        SendMessage(socketfd,uList[i]->name); 
     257                        SendMessage(socketfd,uList[i]->units); 
     258                        SendMessage(socketfd,uList[i]->goal); 
     259                        sprintf(var,"%f",uList[i]->target); 
     260                        SendMessage(socketfd,var); 
     261                } 
     262                sprintf(counter,"%i",ce_info->numParameters); 
     263                SendMessage(socketfd,counter); 
     264                for(i = 0; i < ce_info->numParameters; i++) { 
     265                        SendMessage(socketfd,pList[i]->name); 
     266                        SendMessage(socketfd,pList[i]->units); 
     267                        sprintf(var,"%f",pList[i]->min); 
     268                        SendMessage(socketfd,var); 
     269                        sprintf(var,"%f",pList[i]->max); 
     270                        SendMessage(socketfd,var); 
     271                        sprintf(var,"%f",pList[i]->step); 
     272                        SendMessage(socketfd,var); 
     273                 
     274                        sprintf(counter,"%i",pList[i]->numAffects);  
     275                        SendMessage(socketfd,counter); 
     276                        for(j = 0; j < pList[i]->numAffects; j++) { 
     277                                SendMessage(socketfd,pList[i]->affection_list[j].u->name); 
     278                                SendMessage(socketfd,pList[i]->affection_list[j].relation); 
     279                        } 
     280                } 
     281                sprintf(counter,"%i",ce_info->numObservables); 
     282                SendMessage(socketfd,counter); 
     283                for(i = 0; i < ce_info->numObservables; i++) { 
     284                        SendMessage(socketfd,oList[i]->name); 
     285                         
     286                        sprintf(counter,"%i",oList[i]->numAffects);  
     287                        SendMessage(socketfd,counter); 
     288                        for(j = 0; j < oList[i]->numAffects; j++) { 
     289                                SendMessage(socketfd,oList[i]->affection_list[j].u->name); 
     290                                SendMessage(socketfd,oList[i]->affection_list[j].relation); 
     291                        } 
     292                } 
     293/*               
     294 
     295                // Receive a message 
     296                n = ReceiveMessage(socketfd);    
     297                if (n < 0) { 
     298                        printf("ERROR reading from socket"); 
     299                        close(socketfd); 
     300                        return; 
     301                } 
     302                if (n == 0) { 
    230303                        printf("Client has closed the connection.\n"); 
    231304                        return; 
    232305                } 
    233                  
    234                 // Print message 
    235                 printf("Here is the message: %s\n",buffer); 
    236  
    237                 // Write message back to client 
    238                 n = write(socketfd,"I got your message",18); 
    239                 if (n < 0) error("ERROR writing to socket"); 
    240         } 
    241 } 
    242  
    243 int StartShell(int port) { 
     306*/       
     307//      } 
     308} 
     309 
     310int StartShell(int port,Utility * uList[], Parameter * pList[], Observable * oList[], CE_Info * ce_info) { 
    244311        // Start socket server 
    245312        int sockfd, newsockfd, clilen; 
     
    269336 
    270337                // Begin parsing the messages 
    271                 StartMessageParser(newsockfd); 
     338                StartMessaging(newsockfd,uList, pList, oList, ce_info); 
    272339        } 
    273340        return 0; 
     
    309376        if(pid == 0) { 
    310377                // In child process - open policy engine port. 
    311                 StartShell(30001); 
     378        //      StartShell(30003,uList, pList, oList, &ce_info); 
    312379        } else { 
    313380                // In parent process - open cognitive engine port. 
    314                 StartShell(30000); 
     381                StartShell(30000,uList, pList, oList, &ce_info); 
    315382        }  
    316383         
  • vtcross/branches/trnewman/CR_shell/src/socket/simple_client_main.cpp

    r35 r65  
    1313      std::string reply; 
    1414 
     15    while(1) { 
    1516      try 
    1617        { 
    17           client_socket << "Test message."; 
    1818          client_socket >> reply; 
    1919        } 
     
    2222      std::cout << "We received this response from the server:\n\"" << reply << "\"\n";; 
    2323 
     24    } 
    2425    } 
    2526  catch ( SocketException& e )