source: tmcsimulator/branches/ATMSCommunicator/FEPClient.cpp @ 76

Revision 76, 4.3 KB checked in by jtorres, 9 years ago (diff)

In network.java, LoopDetectorStation?.java and LoopDetector?.java, added network writing capabilities. The network can now be written to a specified xml file to be read by the cpp ATMS RPC program. The network reader is finished and reads in all static network data correctly

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