source: tmcsimulator/branches/fep_rpc_client/FEPClient.cpp @ 77

Revision 77, 4.6 KB checked in by jtorres, 9 years ago (diff)

fep_rpc_client: NetworkReader? complete. All cpp code is commented and cleaned. Added tinyxml lib.

Line 
1#include "FEPClient.h"
2
3/**
4 * Constructor
5 *
6 * @param host The host rpc server ip address
7 * @param networkFile the xml network file
8 */
9FEPClient::FEPClient(char * host, char * networkFile)
10{
11        createClient(host);
12        networkReader = new NetworkReader(networkFile);
13        updateATMS();
14}
15
16/**
17 * Destructor
18 */
19FEPClient::~FEPClient()
20{
21        cout << "Destroying client..." << endl;
22        clnt_destroy(clnt);
23}
24
25/**
26 * Handler for the ATMS RPC Response (to the client RPC Call)
27 * @param response pointer to fep_reply struct
28 */
29void FEPClient::handleCallResponse(void *response)
30{
31        /* If ATMS reply call fails */
32        if (response == NULL)
33        {
34                clnt_perror(clnt, "RPC call failed");
35        }
36        /* If ATMS reply is successful */
37        else
38        {
39                cout << "Successful RPC call to ATMS..." << endl;
40        }
41}
42
43/**
44 * Sends an fep_reply for every line in the FEP.
45 */
46void FEPClient::updateATMS()
47{
48        int i, j; // i == line_index, j == lds_index
49        void *rv;
50
51    vector<FEP_LINE> lines = networkReader->getLines();
52    vector<LDS_LOOP> ldsMap = networkReader->getStations();
53
54        // Send one reply for every "line" in the FEP
55        for (i = 0; i < lines.size(); i++)
56        {
57                fep_reply  fepReply;
58                cout << "Sending fepReply for line #" << lines.at(i).lineNum << endl;
59                // populate reply
60                fepReply.reply = SHORTPOLL;
61                fepReply.schedule = lines.at(i).schedule;
62                fepReply.lineinfo = lines.at(i).lineInfo;
63                fepReply.kind = (enum polltype) 0;
64                fepReply.flag = (enum replykind) 0;
65
66                /***********************************
67                         This is an update to an extern, this should happen on the Java driver side??
68                lines.at(i).schedleSeq += 1;
69                lines.at(i).globalSeq += 51;
70                */
71
72                fepReply.schedule_sequence = lines.at(i).schedleSeq;
73                fepReply.global_sequence = lines.at(i).globalSeq;
74                /************************************
75                         Need to find out what appropriate schedule time is: look at uci_unix_simulation_time src code
76                fepReply.schedule_time =
77                        uci_unix_simulation_time(uci_simulation_time());        // GMT time
78                */
79                fepReply.schedule_time = time(NULL);
80
81                fepReply.user_info1 = lines.at(i).lineNum;
82                fepReply.user_info2 = lines.at(i).lineNum;
83                fepReply.system_key = lines.at(i).systemKey;
84
85                fepReply.answers.size = 1;
86                fepReply.answers.fep_answer_list_u.shortp.count = 1;
87
88                /* for each LDS in the Line.... (constructs the short_answer message) */
89                for (j = 0; j < lines.at(i).ldsNum; j++)
90                {
91                    cout << "Station num " << j << endl;
92                        fep_shortanswer fsa;
93                        int index = lines.at(i).ldsIndex[j];
94
95                        // msg: oa, od, ldsMap.at(index).dataPack, od, ff
96                        fsa.msg.message_len = ldsMap.at(index).length + 2;
97                        fsa.msg.message[0] = 0x0d;
98                        fsa.msg.message[1] = 0x0a;
99                        for (int k = 0; k < ldsMap.at(index).length; k++)
100                                fsa.msg.message[2 + k]  = ldsMap.at(index).dataPack[k];
101                        int aa = ldsMap.at(index).length;
102                        fsa.msg.message[2 + aa] = 0x0d;
103                        fsa.msg.message[3 + aa] = 0xff; //????????????? warning ?????
104
105                        // info
106                        fsa.info.poll_error_count = 0;
107                        /***************************************
108                                 Need to find out polltime uci time function src code
109                        fsa.info.poll_time = uci_unix_simulation_time(uci_simulation_time());
110                        */
111                        fsa.info.poll_user_info1 = ldsMap.at(index).drop;       // drop number
112                        fsa.info.poll_user_info2 = 1;   //always 1
113                        fsa.info.retries = 0;
114                        fsa.info.status = (enum replystatus) 1;
115
116                        //fepReply.answers.fep_answer_list_u.shortp.answers[j] = fsa;
117                        fepReply.answers.fep_answer_list_u.shortp.answers[0] = fsa;
118
119                        // send out data
120                        printf("Transferring line=%d, lds_drop_no=%d...\n", lines.at(i).lineNum, ldsMap.at(index).drop);
121                        rv = fep_reply_xfer_32(&fepReply, clnt);
122
123                        /* Handle ATMS response to RPC Call */
124                        printf("Handling ATMS response...\n");
125                        handleCallResponse(rv);
126                }
127        }
128}
129
130/**
131 * Creates the RPC Client. If not successful, exit with status 1.
132 * @param host rpc server ip
133 */
134void FEPClient::createClient(char * host)
135{
136        /* Create RPC Client to communicate with ATMS */
137        cout << "Creating RPC Client" << endl;
138        clnt = clnt_create(host, 100090, /*103121,*/ 32, "tcp");
139        cout << "Client created" << endl;
140        /* Check if client creation failed */
141        if (clnt == (CLIENT *) NULL)
142        {
143                cerr << "Can't create client to " << host << endl;
144                exit(1);
145        }
146}
147
148/**
149 * Main driver for ATMSCommunicator. Creates an RPC Client from command line args.
150 * @param argc
151 * @param argv
152 * @return
153 */
154int main(int argc, char *argv[]) {
155
156        char *host;
157        char *networkFile;
158
159        if (argc < 3)
160        {
161                cout << "usage:  " << argv[0] << " server_host networkFile" << endl;
162                exit(1);
163        }
164
165        /* Create RPC Client to send an fep_reply to ATMS */
166        host = argv[1];
167        networkFile = argv[2];
168
169        FEPClient *client = new FEPClient(host, networkFile);
170        delete client;
171
172        return 0;
173}
Note: See TracBrowser for help on using the repository browser.