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

Revision 141, 5.8 KB checked in by jdalbey, 9 years ago (diff)

FEPSim.cpp Removed 'Adding' msg. Added 'Verify network connection' in client failed error msg.

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        cerr << "Verify you are connected to ATL network." << endl;
115        success = false;
116    } else {
117        cout << "Client created" << endl;
118    }
119    return success;
120}
121
122void FEPSim::runSockServer() {
123    int sockfd, newsockfd, portno, clilen;
124    char buffer[BUFF_SIZE];
125    struct sockaddr_in serv_addr, cli_addr;
126    int n;
127    portno = this->SOCK_PORT;
128
129    /* First call to socket() function */
130    sockfd = socket(AF_INET, SOCK_STREAM, 0);
131
132    if (sockfd < 0) {
133        perror("ERROR opening socket");
134        exit(1);
135    }
136
137    /* Initialize socket structure */
138    bzero((char *) &serv_addr, sizeof (serv_addr));
139
140    serv_addr.sin_family = AF_INET;
141    serv_addr.sin_addr.s_addr = INADDR_ANY;
142    serv_addr.sin_port = htons(portno);
143
144    /* Now bind the host address using bind() call.*/
145    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
146        perror("ERROR on binding");
147        exit(1);
148    }
149
150    /* Now start listening for the clients, here process will
151     * go in sleep mode and will wait for the incoming connection
152     */
153
154    listen(sockfd, 5);
155    clilen = sizeof (cli_addr);
156
157    /* Accept actual connections from the client */
158    while (1) {
159        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *) & clilen);
160
161        if (newsockfd < 0) {
162            perror("ERROR on accept");
163            exit(1);
164        }
165
166        /* If connection is established then start communicating */
167        // zero out buffer
168        bzero(buffer, BUFF_SIZE);
169
170        // read XML from socket
171        int totBytes = 0;
172        while ((n = recv(newsockfd, &buffer[totBytes], sizeof (buffer), 0)) > 0) {
173            totBytes += n;
174        }
175
176        if (n < 0) {
177            perror("ERROR reading from socket");
178            exit(1);
179        }
180
181        // send data to atms
182        updateATMS(buffer);
183    }
184}
Note: See TracBrowser for help on using the repository browser.