source: tmcsimulator/branches/LinuxBasicFEPClient/fep_client.cpp @ 154

Revision 154, 4.9 KB checked in by jtorres, 9 years ago (diff)

LinuxBasicFEPClient: files are in folder, but build script/makefile/project has not been set up. Src code only

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