Warning: Can't use blame annotator:
svn blame failed on branches/green_dot/fep_client.cpp: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator/branches/green_dot/fep_client.cpp @ 165

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

green_dot now has datapacking implemented

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