source: tmcsimulator/branches/fep_client_cpp/fep_client.cpp @ 71

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

corrected compile errors, minor housekeeping

Line 
1#include "fep.h"
2#include <stdio.h>
3#include <stdlib.h>
4#include "network.h"
5#include "network_factory.cpp"
6#include "time.h"
7
8/* Author: John A. Torres
9   The fep_program_32 function creates a new RPC Client which
10   sends an fep_reply structure to the ATMS Server. After the
11   reply is sent, the ATMS Sends a response and the RPC Client
12   is destroyed.
13 */
14
15/* Handles the ATMS's response to RPC Call */
16void handle_ATMS_response(CLIENT *clnt, void *response)
17{
18        /* If ATMS reply call fails */
19        if (response == NULL)
20        {
21                clnt_perror(clnt, "RPC call failed");
22        }
23        /* If ATMS reply is successful */
24        else
25        {
26                printf("Successful RPC call to ATMS...\n");
27        }
28}
29
30
31void xfer_replys(CLIENT *clnt, FEP_LINE_LDS *lines, int lines_size, LDS_LOOP *ldsMap)
32{
33        int i, j; // i == line_index, j == lds_index
34        void *rv;
35        // Send one reply for every "line" in the FEP
36        for (i = 0; i < lines_size; i++)
37        {
38                fep_reply  fepReply;
39               
40                // populate reply
41                fepReply.reply = SHORTPOLL;
42                fepReply.schedule = lines[i].schedule;
43                fepReply.lineinfo = lines[i].lineInfo;
44                fepReply.kind = (enum polltype) 0;
45                fepReply.flag = (enum replykind) 0;
46               
47                /***********************************
48                         This is an update to an extern, this should happen on the Java driver side??
49                lines[i].schedleSeq += 1;
50                lines[i].globalSeq += 51;
51                */
52
53                fepReply.schedule_sequence = lines[i].schedleSeq;
54                fepReply.global_sequence = lines[i].globalSeq;
55                /************************************
56                         Need to find out what appropriate schedule time is: look at uci_unix_simulation_time src code         
57                fepReply.schedule_time =
58                        uci_unix_simulation_time(uci_simulation_time());        // GMT time
59                */
60                fepReply.user_info1 = lines[i].lineNum;
61                fepReply.user_info2 = lines[i].lineNum;
62                fepReply.system_key = lines[i].systemKey;
63
64                fepReply.answers.size = 1;
65                fepReply.answers.fep_answer_list_u.shortp.count = 1;
66               
67                /* for each LDS in the Line.... (constructs the short_answer message) */
68                for (j = 0; j < lines[i].ldsNum; j++)
69                {
70                        fep_shortanswer fsa;
71                        int index = lines[i].ldsIndex[j];
72
73                        // msg: oa, od, ldsMap[index].dataPack, od, ff
74                        fsa.msg.message_len = ldsMap[index].length + 2;
75                        fsa.msg.message[0] = 0x0d;
76                        fsa.msg.message[1] = 0x0a;
77                        for (int k = 0; k < ldsMap[index].length; k++)
78                                fsa.msg.message[2 + k]  = ldsMap[index].dataPack[k];
79                        int aa = ldsMap[index].length;
80                        fsa.msg.message[2 + aa] = 0x0d;
81                        fsa.msg.message[3 + aa] = 0xff; //????????????? warning ?????
82
83                        // info
84                        fsa.info.poll_error_count = 0; 
85                        /***************************************
86                                 Need to find out polltime uci time function src code
87                        fsa.info.poll_time = uci_unix_simulation_time(uci_simulation_time());
88                        */
89                        fsa.info.poll_user_info1 = ldsMap[index].drop;  // drop number
90                        fsa.info.poll_user_info2 = 1;   //always 1
91                        fsa.info.retries = 0;
92                        fsa.info.status = (enum replystatus) 1;
93                       
94                        //fepReply.answers.fep_answer_list_u.shortp.answers[j] = fsa;   
95                        fepReply.answers.fep_answer_list_u.shortp.answers[0] = fsa;     
96                       
97                        // send out data
98                        printf("line=%d, lds=%d\n", lines[i].lineNum, ldsMap[index].drop);
99                        rv = fep_reply_xfer_32(&fepReply, clnt);
100
101                        /* Handle ATMS response to RPC Call */
102                        printf("Handling ATMS response...\n");
103                        handle_ATMS_response(clnt, rv);
104                }
105        }               
106}
107
108/* Creates an RPC Client which communicates with the ATMS through RPC Calls */
109CLIENT * create_client(char *host)
110{
111        CLIENT *clnt;
112       
113        /* Create RPC Client to communicate with ATMS */
114        printf("Preparing to create RPC client...\n");
115        clnt = clnt_create(host, /*100090,*/ 103121, 32, "tcp");
116
117        /* Check if client creation failed */ 
118        if (clnt == (CLIENT *) NULL)
119        {
120                fprintf(stderr, "Can't create client to %s\n", host);
121                exit(1);
122        }
123       
124        return clnt;
125}
126
127/* update_ATMS transfers fep_replys to the ATMS Server.
128        It creates an RPC client, loads data from line_atms.txt and lds_atms.txt,
129        updates the ATMS with the fep_reply data, and destroys the RPC client.
130 */
131void update_ATMS(char *host)
132{
133        CLIENT *clnt = create_client(host);
134       
135        /* Load data to be xfered to ATMS */
136        int lines_size;
137        FEP_LINE_LDS *lines = load_lines(&lines_size, "./lines_atms.txt");
138        LDS_LOOP *ldsMap = load_lds("./lds_atms.txt");
139
140        /* Transfer data to ATMS */
141        xfer_replys(clnt, lines, lines_size, ldsMap);
142
143        /* Free allocated memory */
144        free(lines);
145        free(ldsMap);
146
147        /* Destroy client */
148        printf("Destroying client...\n");
149        clnt_destroy(clnt);
150        printf("Returning to main...\n");
151}
152
153/* Main driver function to create an RPC Client, make a single RPC Call to the
154   ATMS Server */
155int main(int argc, char *argv[]) {
156        char *host;
157
158        if (argc < 2)
159        {
160                printf("usage:  %s server_host\n", argv[0]);
161                exit(1);
162        }
163
164        /* Create RPC Client to send an fep_reply to ATMS */
165        host = argv[1];
166        update_ATMS(host);
167
168        return 0;
169}
Note: See TracBrowser for help on using the repository browser.