Changeset 84 in tmcsimulator for branches/FEPSimulator


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.

Location:
branches/FEPSimulator
Files:
3 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 
  • branches/FEPSimulator/FEPSim.h

    r82 r84  
    4040const int BUFF_SIZE = 1000000; 
    4141 
    42 class FEPSim 
    43 { 
    44         public: 
    45         /* members */ 
    46                 CLIENT *clnt; // RPC Client 
     42class FEPSim { 
     43public: 
     44    /* members */ 
     45    CLIENT *clnt; // RPC Client 
    4746 
    48         /* methods */ 
    49                 FEPSim(char * host, char * networkFile); // Constructor 
    50                 ~FEPSim(); // Destructor 
     47    /* methods */ 
     48    FEPSim(char * ATMSHost); // Constructor 
     49     
     50    void updateATMS(char * xml); // updates ATMS 
    5151 
    52         private: 
    53         /* members */ 
    54         NetworkReader *networkReader; 
    55         char * ATMSHost; 
    56          
    57         /* methods */ 
    58         void handleCallResponse(void *response); // 
    59         void createClient(char *host); 
    60         void updateATMS(); // updates ATMS 
     52    ~FEPSim(); // Destructor 
     53 
     54private: 
     55    /* members */ 
     56    char * ATMSHost; 
     57 
     58    /* methods */ 
     59    void handleCallResponse(void *response); // 
     60    bool createClient(char *host); 
     61    void sendReplys(char * xml); 
    6162 
    6263}; 
  • branches/FEPSimulator/NetworkReader.cpp

    r82 r84  
    1515        TiXmlElement *subLoopElem = loopElem->FirstChildElement(); 
    1616        loop->loopID = atoi(subLoopElem->GetText()); 
    17         cout << "LOOPID: " << subLoopElem->GetText() << endl; 
    1817        subLoopElem = subLoopElem->NextSiblingElement(); 
    1918        loop->loop_loc = (char *) subLoopElem->GetText(); 
     
    4039    TiXmlElement *stationSubElem = stationElem->FirstChildElement(); 
    4140    station->lds = atol(stationSubElem->GetText()); 
     41    cout << "Station: " << station->lds << endl; 
    4242    line->lds.push_back(station->lds); 
    4343    line->ldsIndex.push_back(ldsIndex++); 
     
    6363        station->loops.push_back(loop); 
    6464    } 
    65  
     65    cout << "Number of Loops: " << station->loops.size() << endl; 
    6666    // Data pack ATMS message 
    6767    station->length = station->loops.size() * 2 + CONTROL_DATA_LEN; 
Note: See TracChangeset for help on using the changeset viewer.