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


Ignore:
Timestamp:
10/06/2017 06:54:21 PM (9 years ago)
Author:
jtorres
Message:

Added socket client to ATMSDriver. Added socket server to FEPSim

File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/FEPSimulator/FEPSim.cpp

    r80 r82  
    1 #include "FEPClient.h" 
     1#include "FEPSim.h" 
    22 
    33/** 
     
    77 * @param networkFile the xml network file 
    88 */ 
    9 FEPClient::FEPClient(char * host, char * networkFile) { 
    10     networkReader = new NetworkReader(networkFile); 
    11     createClient(host); 
     9FEPSim::FEPSim(char * ATMShost, char * xml) { 
     10    networkReader = new NetworkReader(xml); 
     11    this->ATMSHost = ATMShost; 
    1212    updateATMS(); 
    1313} 
     
    1616 * Destructor 
    1717 */ 
    18 FEPClient::~FEPClient() { 
     18FEPSim::~FEPSim() { 
    1919    cout << "Destroying client..." << endl; 
    2020    clnt_destroy(clnt); 
     
    2525 * @param response pointer to fep_reply struct 
    2626 */ 
    27 void FEPClient::handleCallResponse(void *response) { 
     27void FEPSim::handleCallResponse(void *response) { 
    2828    /* If ATMS reply call fails */ 
    2929    if (response == NULL) { 
    3030        clnt_perror(clnt, "RPC call failed"); 
    31     }        /* If ATMS reply is successful */ 
     31    }/* If ATMS reply is successful */ 
    3232    else { 
    3333        cout << "Successful RPC call to ATMS..." << endl; 
     
    3838 * Sends an fep_reply for every line in the FEP. 
    3939 */ 
    40 void FEPClient::updateATMS() { 
     40void FEPSim::updateATMS() { 
    4141    int i, j; // i == line_index, j == lds_index 
    4242    void *rv; 
    43  
    4443    vector<FEP_LINE*> lines = networkReader->getLines(); 
    4544    vector<STATION*> ldsMap = networkReader->getStations(); 
     
    122121 * @param host rpc server ip 
    123122 */ 
    124 void FEPClient::createClient(char * host) { 
     123void FEPSim::createClient(char * host) { 
    125124    /* Create RPC Client to communicate with ATMS */ 
    126125    cout << "Creating RPC Client" << endl; 
     
    142141int main(int argc, char *argv[]) { 
    143142 
    144     char *host; 
    145     char *networkFile; 
    146  
    147     if (argc < 3) { 
    148         cout << "usage:  " << argv[0] << " server_host networkFile" << endl; 
     143    int sockfd, newsockfd, portno, clilen; 
     144    char buffer[BUFF_SIZE]; 
     145    struct sockaddr_in serv_addr, cli_addr; 
     146    int n; 
     147     
     148    char *FEPSimHost = argv[1]; 
     149    portno = atoi(argv[2]); 
     150 
     151    /* First call to socket() function */ 
     152    sockfd = socket(AF_INET, SOCK_STREAM, 0); 
     153 
     154    if (sockfd < 0) { 
     155        perror("ERROR opening socket"); 
    149156        exit(1); 
    150157    } 
    151158 
    152     /* Create RPC Client to send an fep_reply to ATMS */ 
    153     host = argv[1]; 
    154     networkFile = argv[2]; 
    155  
    156     FEPClient *client = new FEPClient(host, networkFile); 
    157     delete client; 
     159    /* Initialize socket structure */ 
     160    bzero((char *) &serv_addr, sizeof (serv_addr)); 
     161 
     162    serv_addr.sin_family = AF_INET; 
     163    serv_addr.sin_addr.s_addr = INADDR_ANY; 
     164    serv_addr.sin_port = htons(portno); 
     165 
     166    /* Now bind the host address using bind() call.*/ 
     167    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) { 
     168        perror("ERROR on binding"); 
     169        exit(1); 
     170    } 
     171 
     172    /* Now start listening for the clients, here process will 
     173     * go in sleep mode and will wait for the incoming connection 
     174     */ 
     175 
     176    listen(sockfd, 5); 
     177    clilen = sizeof (cli_addr); 
     178 
     179    /* Accept actual connection from the client */ 
     180    while(1) { 
     181        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *)&clilen); 
     182 
     183        if (newsockfd < 0) { 
     184            perror("ERROR on accept"); 
     185            exit(1); 
     186        } 
     187 
     188        /* If connection is established then start communicating */ 
     189        bzero(buffer, BUFF_SIZE); 
     190         
     191        // NEEDS OUTER LOOP TO GET WHOLE MESSAGE FROM TCP CONN 
     192        n = read(newsockfd, buffer, sizeof(buffer)); 
     193 
     194        if (n < 0) { 
     195            perror("ERROR reading from socket"); 
     196            exit(1); 
     197        } 
     198         
     199        /* Create RPC Client to send an fep_reply to ATMS */ 
     200 
     201        FEPSim *client = new FEPSim(FEPSimHost, buffer); 
     202        delete client; 
     203    } 
    158204 
    159205    return 0; 
Note: See TracChangeset for help on using the changeset viewer.