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

Revision 114, 5.7 KB checked in by jtorres, 9 years ago (diff)

Updating FEPSimulator for JD's linux machine. Modified ATMS/Console Driver to show red dots.

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