c78c710981bcc4d70a3305fff94deaa2caafd7b3
[ric-plt/lib/rmr.git] / examples / receiver.c
1 // :vim ts=4 sw=4 noet:
2 /*
3         Mnemonic:       rmr_rcvr.c
4         Abstract:       This is a very simple receiver that does nothing but listen
5                                 for messages and write stats every so often to the tty.
6
7                                 Define these environment variables to have some control:
8                                         RMR_SEED_RT -- path to the static routing table
9                                         RMR_RTG_SVC -- host:port of the route table generator
10
11         Parms:          Two positional parameters are recognised on the command line:
12                                         [port [stats-freq]]
13
14                                 where port is the port number to listen on and the stats frequency
15                                 is the number of messages received which causes stats to be 
16                                 generated.  If not supplied the listen port default is 4560
17                                 and the stats frequency is every 10 messages.
18
19         Date:           1 April 2019
20         Author:         E. Scott Daniels
21 */
22
23 #include <unistd.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28
29 #include <rmr/rmr.h>
30
31
32 int main( int argc, char** argv ) {
33     void* mrc;                                          // msg router context
34         long long total = 0;
35     rmr_mbuf_t* msg = NULL;                             // message received
36         int stat_freq = 10;                             // write stats after reciving this many messages
37         int i;
38         char*   listen_port;
39         long long count = 0;
40         long long bad = 0;
41         long long empty = 0;
42
43         if( argc > 1 ) {
44                 listen_port = argv[1];
45         }
46         if( argc > 2 ) {
47                 stat_freq = atoi( argv[2] );
48         }
49         fprintf( stderr, "<DEMO> listening on port: %s\n", listen_port );
50         fprintf( stderr, "<DEMO> stats will be reported every %d messages\n", stat_freq );
51
52     mrc = rmr_init( listen_port, RMR_MAX_RCV_BYTES, RMRFL_NONE );       // start your engines!
53         if( mrc == NULL ) {
54                 fprintf( stderr, "<DEMO> ABORT:  unable to initialise RMr\n" );
55                 exit( 1 );
56         }
57
58         while( ! rmr_ready( mrc ) ) {                                                           // wait for RMr to load a route table
59                 fprintf( stderr, "<DEMO> waiting for ready\n" );
60                 sleep( 3 );
61         }
62         fprintf( stderr, "<DEMO> rmr now shows ready\n" );
63
64     while( 1 ) {                                                                                        // forever; ctl-c, kill -15, etc to end
65                 msg = rmr_rcv_msg( mrc, msg );                                          // block until one arrives
66                 
67                 if( msg ) {
68                         if( msg->state == RMR_OK ) {
69                                 count++;                                                                        // messages received for stats output
70                         } else {
71                                 bad++;
72                         }
73                 } else {
74                         empty++;
75                 }
76
77                 if( (count % stat_freq) == 0  ) {
78                         fprintf( stderr, "<DEMO> total msg received: %lld  errors: %lld   empty: %lld\n", count, bad, empty );
79                 }
80
81     }
82 }
83