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 @ 86

Revision 86, 5.4 KB checked in by jtorres, 9 years ago (diff)

branches/FEPSimulator: updated and improved comments, added Main.cpp which creates the FEPSimulator, minor project configurations (runtime command line arguments / tinyxml.a libosxtinyxml.a)

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