blob: 6c6a0869e0eb55c43a8ac3e22644f583b99e0b51 [file] [log] [blame]
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -04001// :vim ts=4 sw=4 noet:
2/*
E. Scott Danielsd5295492019-04-09 20:40:03 +00003==================================================================================
E. Scott Danielsa3a121c2020-05-06 09:07:08 -04004 Copyright (c) 2019-2020 Nokia
5 Copyright (c) 2018-2020 AT&T Intellectual Property.
E. Scott Danielsd5295492019-04-09 20:40:03 +00006
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
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040011 http://www.apache.org/licenses/LICENSE-2.0
E. Scott Danielsd5295492019-04-09 20:40:03 +000012
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/*
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040022 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.
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040025
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040026 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
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040029
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040030 Parms: Two positional parameters are recognised on the command line:
31 [port [stats-freq]]
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040032
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040033 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.
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040037
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040038 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.
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040045*/
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040046// start_example
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040047
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
57int main( int argc, char** argv ) {
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040058 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;
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040067
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040068 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 );
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040076
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040077 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 }
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040082
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040083 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" );
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040088
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040089 while( 1 ) { // receive until killed
90 msg = rmr_rcv_msg( mrc, msg ); // block until one arrives
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040091
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040092 if( msg ) {
93 if( msg->state == RMR_OK ) {
94 count++; // nothing fancy, just count
95 } else {
96 bad++;
97 }
98 } else {
99 empty++;
100 }
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400101
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400102 if( (count % stat_freq) == 0 ) {
103 fprintf( stderr, "<DEMO> total received: %lld; errors: %lld; empty: %lld\n",
104 count, bad, empty );
105 }
106 }
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400107}
108