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

Revision 79, 5.2 KB checked in by jtorres, 9 years ago (diff)

new atmsdriver package in trunk. fep_rpc_client dynamic data packing added in branches. fep_rpc_client refactored data packing code into static DataPacker? class. Added vds_data folder into config/. Added new project configuration for ATMSDriver.

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