CI: Add silent cmake SonarCloud scan
[ric-plt/lib/rmr.git] / examples / receiver.c
1 // :vim ts=4 sw=4 noet:
2 /*
3 ==================================================================================
4     Copyright (c) 2019-2020 Nokia
5     Copyright (c) 2018-2020 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     CAUTION:    This example is now being pulled directly into the user documentation.
42                 Because of this some altered line lengths and/or parameter list breaks
43                 which seem strage have been applied to ensure that it formats nicely.
44                 All code following the 'start_example' tag below is included.
45 */
46 // start_example
47
48 #include <unistd.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <time.h>
53
54 #include <rmr/rmr.h>
55
56
57 int main( int argc, char** argv ) {
58     void* mrc;                     // msg router context
59     long long total = 0;
60     rmr_mbuf_t* msg = NULL;        // message received
61     int stat_freq = 10;            // write stats after reciving this many messages
62     int i;
63     char*    listen_port = "4560"; // default to what has become the standard RMR port
64     long long count = 0;
65     long long bad = 0;
66     long long empty = 0;
67
68     if( argc > 1 ) {
69         listen_port = argv[1];
70     }
71     if( argc > 2 ) {
72         stat_freq = atoi( argv[2] );
73     }
74     fprintf( stderr, "<DEMO> listening on port: %s\n", listen_port );
75     fprintf( stderr, "<DEMO> stats will be reported every %d messages\n", stat_freq );
76
77     mrc = rmr_init( listen_port, RMR_MAX_RCV_BYTES, RMRFL_NONE );
78     if( mrc == NULL ) {
79         fprintf( stderr, "<DEMO> ABORT:  unable to initialise RMr\n" );
80         exit( 1 );
81     }
82
83     while( ! rmr_ready( mrc ) ) {       // wait for RMR to get a route table
84         fprintf( stderr, "<DEMO> waiting for ready\n" );
85         sleep( 3 );
86     }
87     fprintf( stderr, "<DEMO> rmr now shows ready\n" );
88
89     while( 1 ) {                              // receive until killed
90         msg = rmr_rcv_msg( mrc, msg );        // block until one arrives
91
92         if( msg ) {
93             if( msg->state == RMR_OK ) {
94                 count++;                      // nothing fancy, just count
95             } else {
96                 bad++;
97             }
98         } else {
99             empty++;
100         }
101
102         if( (count % stat_freq) == 0  ) {
103             fprintf( stderr, "<DEMO> total received: %lld; errors: %lld; empty: %lld\n",
104                 count, bad, empty );
105         }
106     }
107 }
108