Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 1 | // :vim ts=4 sw=4 noet: |
| 2 | /* |
E. Scott Daniels | d529549 | 2019-04-09 20:40:03 +0000 | [diff] [blame] | 3 | ================================================================================== |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 4 | Copyright (c) 2019-2020 Nokia |
| 5 | Copyright (c) 2018-2020 AT&T Intellectual Property. |
E. Scott Daniels | d529549 | 2019-04-09 20:40:03 +0000 | [diff] [blame] | 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 | |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 11 | http://www.apache.org/licenses/LICENSE-2.0 |
E. Scott Daniels | d529549 | 2019-04-09 20:40:03 +0000 | [diff] [blame] | 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 | /* |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 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. |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 25 | |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 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 |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 29 | |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 30 | Parms: Two positional parameters are recognised on the command line: |
| 31 | [port [stats-freq]] |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 32 | |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 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. |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 37 | |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 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. |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 45 | */ |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 46 | // start_example |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 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 ) { |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 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; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 67 | |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 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 ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 76 | |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 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 | } |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 82 | |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 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" ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 88 | |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 89 | while( 1 ) { // receive until killed |
| 90 | msg = rmr_rcv_msg( mrc, msg ); // block until one arrives |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 91 | |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 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 | } |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 101 | |
E. Scott Daniels | a3a121c | 2020-05-06 09:07:08 -0400 | [diff] [blame] | 102 | if( (count % stat_freq) == 0 ) { |
| 103 | fprintf( stderr, "<DEMO> total received: %lld; errors: %lld; empty: %lld\n", |
| 104 | count, bad, empty ); |
| 105 | } |
| 106 | } |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 107 | } |
| 108 | |