source: tmcsimulator/branches/FEPSimulator/FEPSim.cpp @ 219

Revision 219, 6.1 KB checked in by jtorres, 8 years ago (diff)

FEPSim.cpp: simplified line and station variable names in sendReplys() method

Line 
1#include "FEPSim.h"
2#include <time.h>
3
4FEPSim::FEPSim(char * ATMShost, int FEP_PROG, int FEP_REV, int SOCK_PORT) {
5    this->ATMSHost = ATMShost;
6    this->FEP_PROG = FEP_PROG;
7    this->FEP_REV = FEP_REV;
8    this->SOCK_PORT = SOCK_PORT;
9   
10    // clear log file
11    FEPLogFile.open(FEPLogFileName, ios::trunc);
12    FEPLogFile.close();
13}
14
15FEPSim::~FEPSim() {
16    FEPLogFile.close();
17}
18
19void FEPSim::handleCallResponse(void *response) {
20    // Failed RPC Call
21    if (response == NULL) { 
22        clnt_perror(clnt, "RPC call failed");
23    }
24    // Successful RPC Call
25    else { 
26        FEPLogFile << "Successful RPC call to ATMS..." << endl;
27    }
28}
29
30void FEPSim::sendReplys(char * buffer) {
31    HighwaysParser highwaysParser = HighwaysParser(buffer);
32    vector<FEP_LINE*> lines = highwaysParser.lines;
33    vector<STATION*> ldsMap = highwaysParser.stations;
34
35    // Send one reply for every FEPLine
36    for (int i = 0; i < lines.size(); i++) {
37        fep_reply fepReply;
38        FEP_LINE * currLine = lines.at(i);
39        // populate reply
40        fepReply.reply = SHORTPOLL;
41        fepReply.schedule = currLine->schedule;
42        fepReply.lineinfo = currLine->lineInfo;
43        fepReply.kind = (enum polltype) 0;
44        fepReply.flag = (enum replykind) 0;
45
46        fepReply.schedule_sequence = currLine->schedleSeq;
47        fepReply.global_sequence = currLine->globalSeq;
48
49        // using current unix time, may need to look at later
50        fepReply.schedule_time = time(NULL);
51
52        fepReply.user_info1 = currLine->lineNum;
53        fepReply.user_info2 = currLine->lineNum;
54        fepReply.system_key = currLine->systemKey;
55
56        fepReply.answers.size = 1;
57        fepReply.answers.fep_answer_list_u.shortp.count = 1;
58       
59        // construct a shortanswer for each station in line
60        for (int j = 0; j < currLine->lds.size(); j++) {
61            fep_shortanswer fsa;
62            STATION * currStation = ldsMap.at(currLine->ldsIndex.at(j));
63
64            // msg: oa, od, currStation.dataPack, od, ff
65            fsa.msg.message_len = currStation->length + 2;
66            fsa.msg.message[0] = 0x0d;
67            fsa.msg.message[1] = 0x0a;
68            for (int k = 0; k < currStation->length; k++) {
69                //printf("Adding: %d %02X\n", k, currStation->dataPack[k]);     
70                fsa.msg.message[2 + k] = currStation->dataPack[k];
71            }
72            int aa = currStation->length;
73            fsa.msg.message[2 + aa] = 0x0d;
74            fsa.msg.message[3 + aa] = 0xff;
75           
76            /*for(int l = 0; l < fsa.msg.message_len + 2; l++)
77            {
78                printf("%02X", (unsigned char) fsa.msg.message[l]);
79            }
80            printf("\n");*/
81           
82            // info
83            fsa.info.poll_error_count = 0;
84            fsa.info.poll_user_info1 = currStation->drop; // drop number
85            fsa.info.poll_user_info2 = 1; //always 1
86            fsa.info.retries = 0;
87            fsa.info.status = (enum replystatus) 1;
88
89            fepReply.answers.fep_answer_list_u.shortp.answers[0] = fsa;
90            // send out data
91           
92            FEPLogFile << "Sending fepReply for line #" << currLine->lineNum
93                    << " station number #" 
94                    << ldsMap.at(currLine->ldsIndex.at(j)) << endl;
95           
96            // Transfer the station data to ATMS and listen for response
97            handleCallResponse(fep_reply_xfer_32(&fepReply, clnt));
98        }
99    }
100}
101
102void FEPSim::manageClientConnection(char * buffer) 
103{
104    // Attempt to create RPC client
105    if (createClient()) 
106    {
107        // Prepare and send the highway status as FEP replies
108        sendReplys(buffer);
109        FEPLogFile << "Destroying client..." << endl;
110        clnt_destroy(clnt);
111        int startTime = time(NULL);
112        FEPLogFile << "time is: " << startTime << endl;
113    }
114}
115
116
117bool FEPSim::createClient() {
118    /* Create RPC Client to communicate with ATMS */
119    bool success = true;
120
121    clnt = clnt_create(this->ATMSHost, FEP_PROG, FEP_REV, "tcp");
122    /* Check if client creation failed */
123    if (clnt == (CLIENT *) NULL) {
124        cerr << "Can't create client to " << this->ATMSHost << endl;
125        cerr << "Verify you are connected to ATL network." << endl;
126        success = false;
127    } else {
128        FEPLogFile << "Client created" << endl;
129    }
130    return success;
131}
132
133void FEPSim::runSockServer() {
134    int sockfd, newsockfd, portno, clilen;
135    char buffer[BUFF_SIZE];
136    struct sockaddr_in serv_addr, cli_addr;
137    int n;
138    portno = this->SOCK_PORT;
139
140    /* First call to socket() function */
141    sockfd = socket(AF_INET, SOCK_STREAM, 0);
142
143    if (sockfd < 0) {
144        perror("ERROR opening socket");
145        exit(1);
146    }
147
148    /* Initialize socket structure */
149    bzero((char *) &serv_addr, sizeof (serv_addr));
150
151    serv_addr.sin_family = AF_INET;
152    serv_addr.sin_addr.s_addr = INADDR_ANY;
153    serv_addr.sin_port = htons(portno);
154
155    /* Now bind the host address using bind() call.*/
156    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
157        perror("ERROR on binding");
158        exit(1);
159    }
160
161    /* Now start listening for the clients, here process will
162     * go in sleep mode and will wait for the incoming connection
163     */
164
165    listen(sockfd, 5);
166    clilen = sizeof (cli_addr);
167
168    /* Accept actual connections from the client */
169    while (1) {
170        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *) & clilen);
171
172        if (newsockfd < 0) {
173            perror("ERROR on accept");
174            exit(1);
175        }
176
177        /* If connection is established then start communicating */
178        // zero out buffer
179        bzero(buffer, BUFF_SIZE);
180       
181        // read XML from socket
182        int totBytes = 0;
183        while ((n = recv(newsockfd, &buffer[totBytes], sizeof (buffer), 0)) > 0) {
184            totBytes += n;
185        }
186
187        if (n < 0) {
188            perror("ERROR reading from socket");
189            exit(1);
190        }
191        // open log file
192        FEPLogFile.open(FEPLogFileName, ios::app);
193        // send data to atms
194        manageClientConnection(buffer);
195        // close log file
196        FEPLogFile.close();
197    }
198}
Note: See TracBrowser for help on using the repository browser.