Add initial codes
[it/test.git] / simulators / workload_generator / src / ric_generator.c
1 /*
2 ======================================================================
3         Copyright (c) 2019 Nokia
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
15    implied. See the License for the specific language governing 
16    permissions andlimitations under the License.
17 ======================================================================
18 */
19
20 #include <ctype.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <time.h>
27
28 #define UTA_COMPAT
29 #include "ric_wg.h"
30 #include <rmr/rmr.h>
31
32 struct timespec ts_start_tx;
33
34 int read_pdu(const char* filename, unsigned char* buffer) {
35
36   FILE* f = fopen(filename, "r");
37
38   if(f == NULL) {
39     fprintf(stderr, "abort: unable to  open PDU file: %s\n",
40             strerror(errno));
41     return -1;
42   }
43
44   return fread(buffer, sizeof(unsigned char), 2048, f);
45 }
46
47
48 int main(int argc, char** argv) {
49   void* mr;
50   char* proto_port = "tcp:4545";
51   rmr_mbuf_t* msg;
52   // data portion (payload) of the message
53   msg_t* msg_data;
54   // total count of msgs sent
55   int count = 0;
56   // cyced count used to generate type 
57   int cyc_count = 0;
58   // number of msgs per second
59   int rate = argc < 2 ? 10 : atoi(argv[1]);
60   // the number of msg types depends on the number of xAPPs
61   int type_n = 2;
62   // rate in mu-seconds/message
63   double v = (1.0/(double)rate) * SEC2MUS;
64   // delay between messages
65   int delay = (int) v;
66   // PDU buffer to be copied to each msg
67   unsigned char pdu_li[2048];
68   unsigned char pdu_rs[2048];
69   char* pdufile1 = "PDU/pdu1.per";
70   char* pdufile2 = "PDU/pdu2.per";
71   
72   struct timespec ts;
73   long long elapse;
74   double elapse_s;
75
76   int pdu_li_size = read_pdu(pdufile1, pdu_li);
77   int pdu_rs_size = read_pdu(pdufile2, pdu_rs);
78     
79   if((mr = uta_init(proto_port, 4096, UTAFL_NONE)) == NULL) {
80       fprintf(stderr, "abort: unable to initialise the lib: %s\n",
81               strerror(errno));
82       exit(1);
83   }
84   // get an initial send buffer
85   msg = uta_alloc_msg(mr, sizeof(msg_t));
86   if( msg == NULL ) {
87     fprintf(stderr, "abort: couldn't allocate initial message\n");
88     exit(1);
89   }
90   //record starting time
91   clock_gettime(CLOCK_REALTIME, &ts_start_tx);
92   while(count < MAX_TX) {
93     //sleep to have an average msg/sec send rate
94     usleep(delay);     
95     msg->mtype = cyc_count;
96     msg_data = (msg_t *) msg->payload;
97     msg_data->time_counter = count;
98     
99     snprintf(msg->xaction, UTA_MAX_XID, "x%05d", count);
100
101     if(msg->mtype == 0) {
102       msg_data->pdu_length = pdu_li_size;  
103       memcpy(msg_data->payload, pdu_li, sizeof(msg_data->payload));
104     }
105
106     if(msg->mtype == 1) {
107       msg_data->pdu_length = pdu_rs_size;
108       memcpy(msg_data->payload, pdu_rs, sizeof(msg_data->payload));
109     }
110     msg->len = sizeof(msg_t);
111     // set timestamp at the absolute last point
112     clock_gettime(CLOCK_REALTIME, &msg_data->ts);
113
114     msg = uta_send_msg(mr, msg);
115
116     count++;
117     clock_gettime(CLOCK_REALTIME, &ts);
118     elapse = (ts.tv_sec - ts_start_tx.tv_sec) * 1000000000
119           + ts.tv_nsec - ts_start_tx.tv_nsec;
120     elapse_s = (double) elapse / 1000000000;
121
122     fprintf(stderr, "\rsent %d msgs", count);
123     cyc_count = cyc_count < type_n - 1 ? cyc_count + 1 : 0;
124   }
125
126   return 0;
127 }