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


Ignore:
Timestamp:
10/06/2017 11:44:52 PM (9 years ago)
Author:
jtorres
Message:

FEPSimulator.cpp: FEPSimulator socket server is now receiving full messages, no more partial messages over the TCP socket. General housekeeping on FEPSim.cpp, NetworkReader?.cpp, ATMSDriver.java, NetworkLoader?.java.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/FEPSimulator/FEPSim.cpp

    r82 r84  
    77 * @param networkFile the xml network file 
    88 */ 
    9 FEPSim::FEPSim(char * ATMShost, char * xml) { 
    10     networkReader = new NetworkReader(xml); 
     9FEPSim::FEPSim(char * ATMShost) { 
    1110    this->ATMSHost = ATMShost; 
    12     updateATMS(); 
    1311} 
    1412 
     
    2624 */ 
    2725void FEPSim::handleCallResponse(void *response) { 
    28     /* If ATMS reply call fails */ 
     26    // Failed RPC Call 
    2927    if (response == NULL) { 
    3028        clnt_perror(clnt, "RPC call failed"); 
    31     }/* If ATMS reply is successful */ 
     29    } 
     30    // Successful RPC Call 
    3231    else { 
    3332        cout << "Successful RPC call to ATMS..." << endl; 
     
    3534} 
    3635 
    37 /** 
    38  * Sends an fep_reply for every line in the FEP. 
    39  */ 
    40 void FEPSim::updateATMS() { 
    41     int i, j; // i == line_index, j == lds_index 
    42     void *rv; 
    43     vector<FEP_LINE*> lines = networkReader->getLines(); 
    44     vector<STATION*> ldsMap = networkReader->getStations(); 
    45  
    46     // Send one reply for every "line" in the FEP 
    47     for (i = 0; i < lines.size(); i++) { 
     36void FEPSim::sendReplys(char * xml) 
     37{ 
     38    NetworkReader networkReader = NetworkReader(xml); 
     39    vector<FEP_LINE*> lines = networkReader.getLines(); 
     40    vector<STATION*> ldsMap = networkReader.getStations(); 
     41     
     42    // Send one reply for every FEPLine 
     43    for (int i = 0; i < lines.size(); i++) { 
    4844        fep_reply fepReply; 
    4945        cout << "Sending fepReply for line #" << lines.at(i)->lineNum << endl; 
     
    5652 
    5753        /*********************************** 
    58                  This is an update to an extern, this should happen on the Java driver side?? 
     54         * We might need to handle this on the java side if its an 
     55         * issue 
    5956        lines.at(i).schedleSeq += 1; 
    6057        lines.at(i).globalSeq += 51; 
     
    6360        fepReply.schedule_sequence = lines.at(i)->schedleSeq; 
    6461        fepReply.global_sequence = lines.at(i)->globalSeq; 
    65         /************************************ 
    66                  Need to find out what appropriate schedule time is: look at uci_unix_simulation_time src code 
    67         fepReply.schedule_time = 
    68                 uci_unix_simulation_time(uci_simulation_time());        // GMT time 
    69          */ 
     62 
     63        // using current unix time, may need to look at later 
    7064        fepReply.schedule_time = time(NULL); 
    7165 
     
    7771        fepReply.answers.fep_answer_list_u.shortp.count = 1; 
    7872 
    79         /* for each LDS in the Line.... (constructs the short_answer message) */ 
    80         for (j = 0; j < lines.at(i)->lds.size(); j++) { 
     73        // construct a shortanswer for each station in line 
     74        for (int j = 0; j < lines.at(i)->lds.size(); j++) { 
    8175            fep_shortanswer fsa; 
    8276            int index = lines.at(i)->ldsIndex.at(j); 
     
    9084            int aa = ldsMap.at(index)->length; 
    9185            fsa.msg.message[2 + aa] = 0x0d; 
    92             fsa.msg.message[3 + aa] = 0xff; //????????????? warning ????? 
     86            fsa.msg.message[3 + aa] = 0xff; 
    9387 
    9488            // info 
    9589            fsa.info.poll_error_count = 0; 
    96             /*************************************** 
    97                      Need to find out polltime uci time function src code 
    98             fsa.info.poll_time = uci_unix_simulation_time(uci_simulation_time()); 
    99              */ 
    10090            fsa.info.poll_user_info1 = ldsMap.at(index)->drop; // drop number 
    10191            fsa.info.poll_user_info2 = 1; //always 1 
     
    10393            fsa.info.status = (enum replystatus) 1; 
    10494 
    105             //fepReply.answers.fep_answer_list_u.shortp.answers[j +1] = fsa; 
    10695            fepReply.answers.fep_answer_list_u.shortp.answers[0] = fsa; 
    10796 
    10897            // send out data 
    10998            printf("Transferring line=%d, lds_drop_no=%d...\n", lines.at(i)->lineNum, ldsMap.at(index)->drop); 
    110             rv = fep_reply_xfer_32(&fepReply, clnt); 
    111  
    112             /* Handle ATMS response to RPC Call */ 
     99            // Make RPC Call and handle response 
    113100            printf("Handling ATMS response...\n"); 
    114             handleCallResponse(rv); 
    115         } 
     101            handleCallResponse(fep_reply_xfer_32(&fepReply, clnt)); 
     102        } 
     103    } 
     104} 
     105 
     106/** 
     107 * Sends an fep_reply for every line in the FEP. 
     108 */ 
     109void FEPSim::updateATMS(char * xml) { 
     110    if(createClient(this->ATMSHost)) 
     111    { 
     112        sendReplys(xml); 
    116113    } 
    117114} 
     
    121118 * @param host rpc server ip 
    122119 */ 
    123 void FEPSim::createClient(char * host) { 
     120bool FEPSim::createClient(char * host) { 
    124121    /* Create RPC Client to communicate with ATMS */ 
     122    bool success = true; 
    125123    cout << "Creating RPC Client" << endl; 
    126124    clnt = clnt_create(host, /*100090,*/ 103121, 32, "tcp"); 
    127     cout << "Client created" << endl; 
    128125    /* Check if client creation failed */ 
    129126    if (clnt == (CLIENT *) NULL) { 
    130127        cerr << "Can't create client to " << host << endl; 
    131         exit(1); 
    132     } 
     128        success = false; 
     129    } 
     130    else { 
     131        cout << "Client created" << endl; 
     132    } 
     133    return success; 
    133134} 
    134135 
     
    140141 */ 
    141142int main(int argc, char *argv[]) { 
    142  
     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     
    143151    int sockfd, newsockfd, portno, clilen; 
    144152    char buffer[BUFF_SIZE]; 
    145153    struct sockaddr_in serv_addr, cli_addr; 
    146154    int n; 
    147      
    148     char *FEPSimHost = argv[1]; 
    149155    portno = atoi(argv[2]); 
    150156 
     
    177183    clilen = sizeof (cli_addr); 
    178184 
    179     /* Accept actual connection from the client */ 
     185    /* Accept actual connections from the client */ 
    180186    while(1) { 
    181187        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *)&clilen); 
     
    187193 
    188194        /* If connection is established then start communicating */ 
     195        // zero out buffer 
    189196        bzero(buffer, BUFF_SIZE); 
    190197         
    191         // NEEDS OUTER LOOP TO GET WHOLE MESSAGE FROM TCP CONN 
    192         n = read(newsockfd, buffer, sizeof(buffer)); 
    193  
     198        // read XML from socket 
     199        int totBytes = 0; 
     200        while ((n = recv(newsockfd, &buffer[totBytes], sizeof(buffer), 0)) > 0) { 
     201            totBytes += n; 
     202        } 
     203         
    194204        if (n < 0) { 
    195205            perror("ERROR reading from socket"); 
     
    197207        } 
    198208         
    199         /* Create RPC Client to send an fep_reply to ATMS */ 
    200  
    201         FEPSim *client = new FEPSim(FEPSimHost, buffer); 
    202         delete client; 
     209        // send data to atms 
     210        fep.updateATMS(buffer); 
    203211    } 
    204212 
Note: See TracChangeset for help on using the changeset viewer.