ea22155102dc4585c2f446006af9d5b16b415925
[ric-app/mc.git] / sidecars / listener / src / sender.c
1 // vim: ts=4 sw=4 noet:
2 /*
3 --------------------------------------------------------------------------------
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 implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 --------------------------------------------------------------------------------
18 */
19
20 /*
21         Mnemonic:       sender.c
22         Abstract:       Very simple test sender. Sends messages with a given delay between each.
23                                 The sender also uses epoll_wait() to ensure that any received messages
24                                 don't clog the queue.
25
26         Parms:          The following positional parameters are recognised on the command line:
27                                         [listen_port [delay [stats-freq] [msg-type]]]]
28
29                                         Defaults:
30                                                 listen_port 43086
31                                                 delay (mu-sec) 1000000 (1 sec)
32                                                 stats-freq 10
33                                                 msg-type 0
34
35         Date:           1 April 2019
36 */
37
38 #include <unistd.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <sys/epoll.h>
44 #include <time.h>
45 #include <signal.h>
46
47 #include <rmr/rmr.h>
48
49 /*
50         We exit on any trapped signal so that we can kill  -15 the proecss
51         and still get gcoverage information to keep sonar happy.
52 */
53 static void sigh( int sig ) {
54         fprintf( stderr, "\n[<SNDR> exiting on signal %d\n", sig );
55         exit( 0 );
56 }
57
58 int main( int argc, char** argv ) {
59         void* mrc;                                                              //msg router context
60         struct epoll_event events[1];                   // list of events to give to epoll
61         struct epoll_event epe;                 // event definition for event to listen to
62         int     ep_fd = -1;                                             // epoll's file des (given to epoll_wait)
63         int rcv_fd;                                                             // file des that NNG tickles -- give this to epoll to listen on
64         int nready;                                                             // number of events ready for receive
65         rmr_mbuf_t*             sbuf;                                   // send buffer
66         rmr_mbuf_t*             rbuf;                                   // received buffer
67         int     count = 0;
68         int     rcvd_count = 0;
69         char*   listen_port = "43086";
70         int             delay = 1000000;                                                // mu-sec delay between messages
71         int             mtype = 0;
72         int             stats_freq = 100;
73
74         signal( SIGINT, sigh );
75         signal( SIGTERM, sigh );
76
77         if( argc > 1 ) {
78                 listen_port = argv[1];
79         }
80         if( argc > 2 ) {
81                 delay = atoi( argv[2] );
82         }
83         if( argc > 3 ) {
84                 mtype = atoi( argv[3] );
85         }
86
87         fprintf( stderr, "<DEMO> listen port: %s; mtype: %d; delay: %d\n", listen_port, mtype, delay );
88
89         if( (mrc = rmr_init( listen_port, 1400, RMRFL_NONE )) == NULL ) {
90                 fprintf( stderr, "<DEMO> unable to initialise RMr\n" );
91                 exit( 1 );
92         }
93
94         rcv_fd = rmr_get_rcvfd( mrc );                                  // set up epoll things, start by getting the FD from MRr
95         if( rcv_fd < 0 ) {
96                 fprintf( stderr, "<DEMO> unable to set up polling fd\n" );
97                 exit( 1 );
98         }
99         if( (ep_fd = epoll_create1( 0 )) < 0 ) {
100                 fprintf( stderr, "[FAIL] unable to create epoll fd: %d\n", errno );
101                 exit( 1 );
102         }
103         epe.events = EPOLLIN;
104         epe.data.fd = rcv_fd;
105
106         if( epoll_ctl( ep_fd, EPOLL_CTL_ADD, rcv_fd, &epe ) != 0 )  {
107                 fprintf( stderr, "[FAIL] epoll_ctl status not 0 : %s\n", strerror( errno ) );
108                 exit( 1 );
109         }
110
111         sbuf = rmr_alloc_msg( mrc, 256 );       // alloc first send buffer; subsequent buffers allcoated on send
112         rbuf = NULL;                                            // don't need to alloc receive buffer
113
114         while( ! rmr_ready( mrc ) ) {           // must have a route table before we can send; wait til RMr say it has one
115                 sleep( 1 );
116         }
117         fprintf( stderr, "<DEMO> rmr is ready\n" );
118
119
120         while( 1 ) {                                                                            // send messages until the cows come home
121                 snprintf( sbuf->payload, 200, "count=%d received= %d ts=%lld %d stand up and cheer!\n",         // create the payload
122                         count, rcvd_count, (long long) time( NULL ), rand() );
123
124                 sbuf->mtype = mtype;                                                    // fill in the message bits
125                 sbuf->len =  strlen( sbuf->payload ) + 1;               // our receiver likely wants a nice acsii-z string
126                 sbuf->state = 0;
127                 sbuf = rmr_send_msg( mrc, sbuf );                               // send it (send returns an empty payload on success, or the original payload on fail/retry)
128                 while( sbuf->state == RMR_ERR_RETRY ) {                 // soft failure (device busy?) retry
129                         sbuf = rmr_send_msg( mrc, sbuf );                       // retry send until it's good (simple test; real programmes should do better)
130                 }
131                 count++;
132                 fprintf( stderr, "<SNDR> sent message type=%d\n", mtype );
133
134                 usleep( delay );
135                 mtype++;
136                 if( mtype > 8 ) {                       // ensure we send a mt that doesn't have a fifo reader to ensure we don't block
137                         mtype = 1;
138                 }
139         }
140 }
141