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

Revision 113, 5.5 KB checked in by jtorres, 9 years ago (diff)

LinuxVM non-segfaulting FEPSimulator - checkpoint

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   // info
75            fsa.info.poll_error_count = 0;
76            fsa.info.poll_user_info1 = ldsMap.at(index)->drop; // drop number
77            fsa.info.poll_user_info2 = 1; //always 1
78            fsa.info.retries = 0;
79            fsa.info.status = (enum replystatus) 1;
80
81            fepReply.answers.fep_answer_list_u.shortp.answers[0] = fsa;
82            // send out data
83            printf("Transferring line=%d, lds_drop_no=%d...\n", lines.at(i)->lineNum, ldsMap.at(index)->drop);
84            // Make RPC Call and handle response
85            printf("Handling ATMS response...\n");
86            handleCallResponse(fep_reply_xfer_32(&fepReply, clnt));
87        }
88    }
89}
90
91void FEPSim::updateATMS(char * xml) {
92    if (createClient()) {
93        sendReplys(xml);
94        cout << "Destroying client..." << endl;
95        clnt_destroy(clnt);
96    }
97}
98
99bool FEPSim::createClient() {
100    /* Create RPC Client to communicate with ATMS */
101    bool success = true;
102
103    clnt = clnt_create(this->ATMSHost, FEP_PROG, FEP_REV, "tcp");
104    /* Check if client creation failed */
105    if (clnt == (CLIENT *) NULL) {
106        cerr << "Can't create client to " << this->ATMSHost << endl;
107        success = false;
108    } else {
109        cout << "Client created" << endl;
110    }
111    return success;
112}
113
114void FEPSim::runSockServer() {
115    int sockfd, newsockfd, portno, clilen;
116    char buffer[BUFF_SIZE];
117    struct sockaddr_in serv_addr, cli_addr;
118    int n;
119    portno = this->SOCK_PORT;
120
121    /* First call to socket() function */
122    sockfd = socket(AF_INET, SOCK_STREAM, 0);
123
124    if (sockfd < 0) {
125        perror("ERROR opening socket");
126        exit(1);
127    }
128
129    /* Initialize socket structure */
130    bzero((char *) &serv_addr, sizeof (serv_addr));
131
132    serv_addr.sin_family = AF_INET;
133    serv_addr.sin_addr.s_addr = INADDR_ANY;
134    serv_addr.sin_port = htons(portno);
135
136    /* Now bind the host address using bind() call.*/
137    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
138        perror("ERROR on binding");
139        exit(1);
140    }
141
142    /* Now start listening for the clients, here process will
143     * go in sleep mode and will wait for the incoming connection
144     */
145
146    listen(sockfd, 5);
147    clilen = sizeof (cli_addr);
148
149    /* Accept actual connections from the client */
150    while (1) {
151        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *) & clilen);
152
153        if (newsockfd < 0) {
154            perror("ERROR on accept");
155            exit(1);
156        }
157
158        /* If connection is established then start communicating */
159        // zero out buffer
160        bzero(buffer, BUFF_SIZE);
161
162        // read XML from socket
163        int totBytes = 0;
164        while ((n = recv(newsockfd, &buffer[totBytes], sizeof (buffer), 0)) > 0) {
165            totBytes += n;
166        }
167
168        if (n < 0) {
169            perror("ERROR reading from socket");
170            exit(1);
171        }
172
173        // send data to atms
174        updateATMS(buffer);
175    }
176}
Note: See TracBrowser for help on using the repository browser.