doc(code): Indent fixes and whitespace cleanup
[ric-plt/lib/rmr.git] / examples / receiver.c
1 // :vim ts=4 sw=4 noet:
2 /*
3 ==================================================================================
4         Copyright (c) 2019 Nokia
5         Copyright (c) 2018-2019 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11            http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       rmr_rcvr.c
23         Abstract:       This is a very simple receiver that does nothing but listen
24                                 for messages and write stats every so often to the tty.
25
26                                 Define these environment variables to have some control:
27                                         RMR_SEED_RT -- path to the static routing table
28                                         RMR_RTG_SVC -- host:port of the route table generator
29
30         Parms:          Two positional parameters are recognised on the command line:
31                                         [port [stats-freq]]
32
33                                 where port is the port number to listen on and the stats frequency
34                                 is the number of messages received which causes stats to be 
35                                 generated.  If not supplied the listen port default is 4560
36                                 and the stats frequency is every 10 messages.
37
38         Date:           1 April 2019
39         Author:         E. Scott Daniels
40 */
41
42 #include <unistd.h>
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <time.h>
47
48 #include <rmr/rmr.h>
49
50
51 int main( int argc, char** argv ) {
52         void* mrc;                                              // msg router context
53         long long total = 0;
54         rmr_mbuf_t* msg = NULL;                         // message received
55         int stat_freq = 10;                             // write stats after reciving this many messages
56         int i;
57         char*   listen_port;
58         long long count = 0;
59         long long bad = 0;
60         long long empty = 0;
61
62         if( argc > 1 ) {
63                 listen_port = argv[1];
64         }
65         if( argc > 2 ) {
66                 stat_freq = atoi( argv[2] );
67         }
68         fprintf( stderr, "<DEMO> listening on port: %s\n", listen_port );
69         fprintf( stderr, "<DEMO> stats will be reported every %d messages\n", stat_freq );
70
71         mrc = rmr_init( listen_port, RMR_MAX_RCV_BYTES, RMRFL_NONE );   // start your engines!
72         if( mrc == NULL ) {
73                 fprintf( stderr, "<DEMO> ABORT:  unable to initialise RMr\n" );
74                 exit( 1 );
75         }
76
77         while( ! rmr_ready( mrc ) ) {                                                           // wait for RMr to load a route table
78                 fprintf( stderr, "<DEMO> waiting for ready\n" );
79                 sleep( 3 );
80         }
81         fprintf( stderr, "<DEMO> rmr now shows ready\n" );
82
83         while( 1 ) {                                                                                    // forever; ctl-c, kill -15, etc to end
84                 msg = rmr_rcv_msg( mrc, msg );                                          // block until one arrives
85                 
86                 if( msg ) {
87                         if( msg->state == RMR_OK ) {
88                                 count++;                                                                        // messages received for stats output
89                         } else {
90                                 bad++;
91                         }
92                 } else {
93                         empty++;
94                 }
95
96                 if( (count % stat_freq) == 0  ) {
97                         fprintf( stderr, "<DEMO> total msg received: %lld  errors: %lld   empty: %lld\n", count, bad, empty );
98                 }
99
100         }
101 }
102