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

Revision 177, 6.1 KB checked in by jdalbey, 9 years ago (diff)

FEPSim.cpp Clarify comments and variable names.

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