Warning: Can't use blame annotator:
svn blame failed on branches/FEPSimulator/FEPSim.cpp: ("Can't find a temporary directory: Internal error", 20014)

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

Revision 217, 6.1 KB checked in by jtorres, 9 years ago (diff)

Implemented logging into FEPSimulator. Now logs output into FEPSimLog.txt. Error messages are still output to console so you know immediately if there is an error. The log file is cleared upon FEPSim start-up.

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