Changeset 86 in tmcsimulator for branches/FEPSimulator/FEPSim.cpp


Ignore:
Timestamp:
10/09/2017 11:58:19 PM (9 years ago)
Author:
jtorres
Message:

branches/FEPSimulator: updated and improved comments, added Main.cpp which creates the FEPSimulator, minor project configurations (runtime command line arguments / tinyxml.a libosxtinyxml.a)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/FEPSimulator/FEPSim.cpp

    r84 r86  
    11#include "FEPSim.h" 
    22 
    3 /** 
    4  * Constructor 
    5  *  
    6  * @param host The host rpc server ip address 
    7  * @param networkFile the xml network file 
    8  */ 
    9 FEPSim::FEPSim(char * ATMShost) { 
     3FEPSim::FEPSim(char * ATMShost, int FEP_PROG, int FEP_REV, int SOCK_PORT) { 
    104    this->ATMSHost = ATMShost; 
     5    this->FEP_PROG = FEP_PROG; 
     6    this->FEP_REV = FEP_REV; 
     7    this->SOCK_PORT = SOCK_PORT; 
    118} 
    129 
    13 /** 
    14  * Destructor 
    15  */ 
    1610FEPSim::~FEPSim() { 
    17     cout << "Destroying client..." << endl; 
    18     clnt_destroy(clnt); 
     11 
    1912} 
    2013 
    21 /** 
    22  * Handler for the ATMS RPC Response (to the client RPC Call) 
    23  * @param response pointer to fep_reply struct 
    24  */ 
    2514void FEPSim::handleCallResponse(void *response) { 
    2615    // Failed RPC Call 
     
    3423} 
    3524 
    36 void FEPSim::sendReplys(char * xml) 
    37 { 
     25void FEPSim::sendReplys(char * xml) { 
    3826    NetworkReader networkReader = NetworkReader(xml); 
    3927    vector<FEP_LINE*> lines = networkReader.getLines(); 
    4028    vector<STATION*> ldsMap = networkReader.getStations(); 
    41      
     29 
    4230    // Send one reply for every FEPLine 
    4331    for (int i = 0; i < lines.size(); i++) { 
     
    10492} 
    10593 
    106 /** 
    107  * Sends an fep_reply for every line in the FEP. 
    108  */ 
    10994void FEPSim::updateATMS(char * xml) { 
    110     if(createClient(this->ATMSHost)) 
    111     { 
     95    if (createClient()) { 
    11296        sendReplys(xml); 
     97        cout << "Destroying client..." << endl; 
     98        clnt_destroy(clnt); 
    11399    } 
    114100} 
    115101 
    116 /** 
    117  * Creates the RPC Client. If not successful, exit with status 1. 
    118  * @param host rpc server ip 
    119  */ 
    120 bool FEPSim::createClient(char * host) { 
     102bool FEPSim::createClient() { 
    121103    /* Create RPC Client to communicate with ATMS */ 
    122104    bool success = true; 
    123     cout << "Creating RPC Client" << endl; 
    124     clnt = clnt_create(host, /*100090,*/ 103121, 32, "tcp"); 
     105 
     106    clnt = clnt_create(this->ATMSHost, FEP_PROG, FEP_REV, "tcp"); 
    125107    /* Check if client creation failed */ 
    126108    if (clnt == (CLIENT *) NULL) { 
    127         cerr << "Can't create client to " << host << endl; 
     109        cerr << "Can't create client to " << this->ATMSHost << endl; 
    128110        success = false; 
    129     } 
    130     else { 
     111    } else { 
    131112        cout << "Client created" << endl; 
    132113    } 
     
    134115} 
    135116 
    136 /** 
    137  * Main driver for ATMSCommunicator. Creates an RPC Client from command line args. 
    138  * @param argc 
    139  * @param argv 
    140  * @return  
    141  */ 
    142 int main(int argc, char *argv[]) { 
    143     if(argc != 3) 
    144     { 
    145         cerr << "Usage: FEPSim <ATMS_Host> <FEP_ATMSDriver_PortNo" << endl; 
    146         exit(1); 
    147     } 
    148     char *FEPSimHost = argv[1];     
    149     FEPSim fep = FEPSim(FEPSimHost); 
    150      
     117void FEPSim::runSockServer() { 
    151118    int sockfd, newsockfd, portno, clilen; 
    152119    char buffer[BUFF_SIZE]; 
    153120    struct sockaddr_in serv_addr, cli_addr; 
    154121    int n; 
    155     portno = atoi(argv[2]); 
     122    portno = this->SOCK_PORT; 
    156123 
    157124    /* First call to socket() function */ 
     
    184151 
    185152    /* Accept actual connections from the client */ 
    186     while(1) { 
    187         newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *)&clilen); 
     153    while (1) { 
     154        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *) & clilen); 
    188155 
    189156        if (newsockfd < 0) { 
     
    195162        // zero out buffer 
    196163        bzero(buffer, BUFF_SIZE); 
    197          
     164 
    198165        // read XML from socket 
    199166        int totBytes = 0; 
    200         while ((n = recv(newsockfd, &buffer[totBytes], sizeof(buffer), 0)) > 0) { 
     167        while ((n = recv(newsockfd, &buffer[totBytes], sizeof (buffer), 0)) > 0) { 
    201168            totBytes += n; 
    202169        } 
    203          
     170 
    204171        if (n < 0) { 
    205172            perror("ERROR reading from socket"); 
    206173            exit(1); 
    207174        } 
    208          
     175 
    209176        // send data to atms 
    210         fep.updateATMS(buffer); 
     177        updateATMS(buffer); 
    211178    } 
    212  
    213     return 0; 
    214179} 
Note: See TracChangeset for help on using the changeset viewer.