Changeset 84 in tmcsimulator


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.

Files:
16 edited
1 moved

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; 
  • branches/trunk/IDE_metadata/NetBeans/TMCSim/build/built-jar.properties

    r82 r84  
    1 #Fri, 06 Oct 2017 19:49:49 -0700 
     1#Sat, 07 Oct 2017 00:43:14 -0700 
    22 
    33 
  • branches/trunk/IDE_metadata/NetBeans/TMCSim/build/classes/tmcsim/application.properties

    r82 r84  
    1 #Fri, 06 Oct 2017 19:49:49 -0700 
     1#Sat, 07 Oct 2017 00:43:14 -0700 
    22 
    3 Application.revision=0 
     3Application.revision=83 
    44 
    5 Application.buildnumber=50 
     5Application.buildnumber=58 
  • branches/trunk/src/atmsdriver/ATMSDriver.java

    r83 r84  
    5353     
    5454    /** Sleep Time (10 seconds). **/ 
    55     private static final int SLEEP_TIME = 10000; 
     55    private static final int SLEEP_TIME = 30000; 
    5656     
    5757    /** Exchange Reader */ 
  • branches/trunk/src/atmsdriver/NetworkLoader.java

    r82 r84  
    143143    private void constructFEPLines() 
    144144    { 
    145         try { 
    146             System.out.println("Building network..."); 
    147              
     145        try {             
    148146            // Get FEPLine IDs 
    149147            ArrayList<Integer> lineNums = getLineNums(new Scanner(LDSFile)); 
  • branches/trunk/src/atmsdriver/model/HighwayStatusWriter.java

    r82 r84  
    3131        Socket sock = new Socket(host, port); 
    3232        PrintWriter out = new PrintWriter(sock.getOutputStream(), true); 
    33         out.print(xml); 
     33        out.println(xml); 
    3434        sock.close(); 
    3535    } 
  • branches/trunk/src/tmcsim/application.properties

    r82 r84  
    1 #Fri, 06 Oct 2017 19:49:49 -0700 
     1#Sat, 07 Oct 2017 00:43:14 -0700 
    22 
    3 Application.revision=0 
     3Application.revision=83 
    44 
    5 Application.buildnumber=50 
     5Application.buildnumber=58 
  • trunk/config/atms_driver_config.properties

    r79 r84  
    33NetworkFileName = networkFile.txt 
    44ExchangeFileName = exchange.xml 
     5FEPWriterHost = localhost 
     6FEPWriterPort = 8080 
  • trunk/src/atmsdriver/NetworkLoader.java

    r79 r84  
    22 
    33import atmsdriver.model.FEPLine; 
    4 import atmsdriver.model.LoopDetectorStation; 
     4import atmsdriver.model.Station; 
    55import atmsdriver.model.LoopDetector; 
    6 import atmsdriver.model.LoopDetectorStation.DIRECTION; 
     6import atmsdriver.model.Station.DIRECTION; 
    77import java.io.File; 
    88import java.io.FileNotFoundException; 
     
    1717 *  
    1818 *  The public method getFEPLines() method can be used to get all the FEPLines  
    19  * within a network. All other methods are private and are used to construct 
    20  * the FEPLines. 
    21  *  
    22  *  The "LDSFile" contains LoopDetectorStation and FEPLine information. 
    23  * ex. 
    24  *   < Insert file here > 
     19  within a network. All other methods are private and are used to construct 
     20  the FEPLines. 
     21  
     22  The "LDSFile" contains Station and FEPLine information. 
     23  ex. 
     24   < Insert file here > 
    2525 *  The "loopFile" contains individual LoopDetector information. 
    2626 *  ex. 
     
    157157                 
    158158                // Create each Station for the current FEPLine 
    159                 ArrayList<LoopDetectorStation> stns = new ArrayList<>(); 
     159                ArrayList<Station> stns = new ArrayList<>(); 
    160160                for(Integer stnNum : stnNums) 
    161161                { 
     
    165165                     
    166166                    // Create the Station and add to list for curr FEPLine 
    167                     LoopDetectorStation stn = 
     167                    Station stn = 
    168168                            createStation(stnNum, new Scanner(LDSFile), loops); 
    169169                    stns.add(stn); 
     
    181181     
    182182    // Creates  line 
    183     private FEPLine createLine(int lineNum, Scanner scLine, ArrayList<LoopDetectorStation> stns) 
     183    private FEPLine createLine(int lineNum, Scanner scLine, ArrayList<Station> stns) 
    184184    { 
    185185        FEPLine line = null; 
     
    239239     
    240240    // creates a Station 
    241     private LoopDetectorStation createStation(int stnNum, Scanner sc, ArrayList<LoopDetector> detectors) 
    242     { 
    243         LoopDetectorStation LDS = null; 
     241    private Station createStation(int stnNum, Scanner sc, ArrayList<LoopDetector> detectors) 
     242    { 
     243        Station LDS = null; 
    244244         
    245245        sc.nextLine(); 
     
    264264                double postmile = scLine.nextDouble(); 
    265265                String ldsName = getLocation(strLine); /************* DOESNT GRAB WHOLE???? *///// 
    266                 LDS = new LoopDetectorStation(lineNum, ldsID, drop, ldsName, detectors, fwy, dir, postmile); 
     266                LDS = new Station(lineNum, ldsID, drop, ldsName, detectors, fwy, dir, postmile); 
    267267                 
    268268                break; 
  • trunk/src/atmsdriver/model/FEPLine.java

    r79 r84  
    1919    /* Static FEPLine meta data */ 
    2020    final private int lineNum; 
    21     final private List<LoopDetectorStation> stations; 
     21    final private List<Station> stations; 
    2222    final private int count; 
    2323    // NOT SURE IF NEXT IS FINAL 
     
    2828    final private long scheduleSeq; 
    2929     
    30     public FEPLine(int lineNum, List<LoopDetectorStation> stations, int count, 
     30    public FEPLine(int lineNum, List<Station> stations, int count, 
    3131            int schedule, int lineInfo, long systemKey, long globalSeq, 
    3232            long scheduleSeq) 
     
    7979        Element stationsElement = theDoc.createElement(XML_TAGS.STATIONS.tag); 
    8080        lineElement.appendChild(stationsElement); 
    81         for(LoopDetectorStation station : stations) 
     81        for(Station station : stations) 
    8282        { 
    8383            station.toXML(stationsElement); 
  • trunk/src/atmsdriver/model/Station.java

    r79 r84  
    55import org.w3c.dom.Element; 
    66 
    7 /** A LoopDetectorStation is a simulation of a station in a traffic network. 
     7/** A Station is a simulation of a station in a traffic network. 
    88 *   
    9  *  A LoopDetectorStation (LDS) contains static meta data about the station, and 
    10  * two dynamic attributes, MLTotVol and OppTotVol.  
    11  *  
    12  * A single LDS contains multiple LoopDetectors, which 
    13  * contain data for a single lane on one direction of the freeway. A LDS 
    14  * is specific to a single freeway, direction, and postmile. 
     9 *  A Station (LDS) contains static meta data about the station, and 
     10  two dynamic attributes, MLTotVol and OppTotVol.  
     11  
     12  A single LDS contains multiple LoopDetectors, which 
     13  contain data for a single lane on one direction of the freeway. A LDS 
     14  is specific to a single freeway, direction, and postmile. 
    1515 * 
    1616 * @author John A. Torres 
    1717 * @version 9/10/2017 
    1818 */ 
    19 public class LoopDetectorStation { 
    20     /* Static LoopDetectorStation meta data */ 
     19public class Station implements Comparable { 
     20    /* Static Station meta data */ 
    2121    final private int lineNum; 
    2222    final private int ldsID; // double check 
     
    2828    final private DIRECTION direction; 
    2929     
    30     /* Dynamic LoopDetectorStation data */ 
     30    /* Dynamic Station data */ 
    3131    private int MLTotVol; 
    3232    private int OppTotVol; 
    3333     
    3434    /* Constructor */ 
    35     public LoopDetectorStation(int lineNum, int ldsID, int drop, 
     35    public Station(int lineNum, int ldsID, int drop, 
    3636            String location, List<LoopDetector> loops, int fwy,  
    3737            DIRECTION direction, double postmile) 
     
    4848        this.MLTotVol = 0; 
    4949        this.OppTotVol = 0; 
     50    } 
     51 
     52    @Override 
     53    public int compareTo(Object o) { 
     54         
    5055    } 
    5156     
Note: See TracChangeset for help on using the changeset viewer.