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

Revision 72, 4.8 KB checked in by jtorres, 9 years ago (diff)

added lds_data files, we are now dumping fep_reply to ATMS with real data

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
30void xfer_replys(CLIENT *clnt, FEP_LINE_LDS *lines, int lines_size, LDS_LOOP *ldsMap)
31{
32        printf("Lines_size %d\n", lines_size);
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.schedule_time = time(NULL);
61
62
63                fepReply.user_info1 = lines[i].lineNum;
64                fepReply.user_info2 = lines[i].lineNum;
65                fepReply.system_key = lines[i].systemKey;
66
67                fepReply.answers.size = 1;
68                fepReply.answers.fep_answer_list_u.shortp.count = 1;
69               
70                /* for each LDS in the Line.... (constructs the short_answer message) */
71                for (j = 0; j < lines[i].ldsNum; j++)
72                {
73                        fep_shortanswer fsa;
74                        int index = lines[i].ldsIndex[j];
75
76                        // msg: oa, od, ldsMap[index].dataPack, od, ff
77                        fsa.msg.message_len = ldsMap[index].length + 2;
78                        fsa.msg.message[0] = 0x0d;
79                        fsa.msg.message[1] = 0x0a;
80                        for (int k = 0; k < ldsMap[index].length; k++)
81                                fsa.msg.message[2 + k]  = ldsMap[index].dataPack[k];
82                        int aa = ldsMap[index].length;
83                        fsa.msg.message[2 + aa] = 0x0d;
84                        fsa.msg.message[3 + aa] = 0xff; //????????????? warning ?????
85
86                        // info
87                        fsa.info.poll_error_count = 0; 
88                        /***************************************
89                                 Need to find out polltime uci time function src code
90                        fsa.info.poll_time = uci_unix_simulation_time(uci_simulation_time());
91                        */
92                        fsa.info.poll_user_info1 = ldsMap[index].drop;  // drop number
93                        fsa.info.poll_user_info2 = 1;   //always 1
94                        fsa.info.retries = 0;
95                        fsa.info.status = (enum replystatus) 1;
96                       
97                        //fepReply.answers.fep_answer_list_u.shortp.answers[j] = fsa;   
98                        fepReply.answers.fep_answer_list_u.shortp.answers[0] = fsa;     
99                       
100                        // send out data
101                        printf("line=%d, lds=%d\n", lines[i].lineNum, ldsMap[index].drop);
102                        rv = fep_reply_xfer_32(&fepReply, clnt);
103
104                        /* Handle ATMS response to RPC Call */
105                        printf("Handling ATMS response...\n");
106                        handle_ATMS_response(clnt, rv);
107                }
108        }               
109}
110
111/* Creates an RPC Client which communicates with the ATMS through RPC Calls */
112CLIENT * create_client(char *host)
113{
114        CLIENT *clnt;
115       
116        /* Create RPC Client to communicate with ATMS */
117        printf("Preparing to create RPC client...\n");
118        clnt = clnt_create(host, /*100090,*/ 103121, 32, "tcp");
119
120        /* Check if client creation failed */ 
121        if (clnt == (CLIENT *) NULL)
122        {
123                fprintf(stderr, "Can't create client to %s\n", host);
124                exit(1);
125        }
126       
127        return clnt;
128}
129
130/* update_ATMS transfers fep_replys to the ATMS Server.
131        It creates an RPC client, loads data from line_atms.txt and lds_atms.txt,
132        updates the ATMS with the fep_reply data, and destroys the RPC client.
133 */
134void update_ATMS(char *host)
135{
136        CLIENT *clnt = create_client(host);
137       
138        /* Load data to be xfered to ATMS */
139        int lines_size = 0;
140        printf("Loading lines and ldsMap\n");
141        FEP_LINE_LDS *lines = load_lines(&lines_size, "./lines_atms.txt");
142        printf("Done loading lines\n");
143        LDS_LOOP *ldsMap = load_lds("./lds_atms.txt");
144        printf("Done loading lines and lds...\n");
145
146        /* Transfer data to ATMS */
147        xfer_replys(clnt, lines, lines_size, ldsMap);
148
149        /* Free allocated memory */
150
151        /* Destroy client */
152        printf("Destroying client...\n");
153        clnt_destroy(clnt);
154        printf("Returning to main...\n");
155}
156
157/* Main driver function to create an RPC Client, make a single RPC Call to the
158   ATMS Server */
159int main(int argc, char *argv[]) {
160        char *host;
161
162        if (argc < 2)
163        {
164                printf("usage:  %s server_host\n", argv[0]);
165                exit(1);
166        }
167
168        /* Create RPC Client to send an fep_reply to ATMS */
169        host = argv[1];
170        update_ATMS(host);
171
172        return 0;
173}
Note: See TracBrowser for help on using the repository browser.