blob: 42cc017ab3c3a419c94588175bfb17c9cea75323 [file] [log] [blame]
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -04001// :vim ts=4 sw=4 noet:
E. Scott Danielsd5295492019-04-09 20:40:03 +00002/*
3==================================================================================
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 Daniels8790bf02019-04-23 12:59:28 +000011 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*/
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040020
21/*
22 Mnemonic: sender.c
23 Abstract: Very simple test sender. Sends messages with a given delay between each.
24 The sender also uses epoll_wait() to ensure that any received messages
25 don't clog the queue.
26
27 Parms: The following positional parameters are recognised on the command line:
28 [listen_port [delay [stats-freq] [msg-type]]]]
29
30 Defaults:
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040031 listen_port 43086
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040032 delay (mu-sec) 1000000 (1 sec)
33 stats-freq 10
34 msg-type 0
35
36 Date: 1 April 2019
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040037
38 CAUTION: This example is now being pulled directly into the user documentation.
39 Because of this some altered line lengths and/or parameter list breaks
40 which seem strage have been applied to ensure that it formats nicely.
41 All code following the 'start_example' tag below is included.
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040042*/
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040043// start_example
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040044
45#include <unistd.h>
46#include <errno.h>
47#include <string.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <sys/epoll.h>
51#include <time.h>
52
53#include <rmr/rmr.h>
54
55int main( int argc, char** argv ) {
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040056 void* mrc; // msg router context
57 struct epoll_event events[1]; // list of events to give to epoll
58 struct epoll_event epe; // event definition for event to listen to
59 int ep_fd = -1; // epoll's file des (given to epoll_wait)
60 int rcv_fd; // file des for epoll checks
61 int nready; // number of events ready for receive
62 rmr_mbuf_t* sbuf; // send buffer
63 rmr_mbuf_t* rbuf; // received buffer
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040064 int count = 0;
65 int rcvd_count = 0;
66 char* listen_port = "43086";
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040067 int delay = 1000000; // mu-sec delay between messages
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040068 int mtype = 0;
69 int stats_freq = 100;
70
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040071 if( argc > 1 ) { // simplistic arg picking
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040072 listen_port = argv[1];
73 }
74 if( argc > 2 ) {
75 delay = atoi( argv[2] );
76 }
77 if( argc > 3 ) {
78 mtype = atoi( argv[3] );
79 }
80
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040081 fprintf( stderr, "<DEMO> listen port: %s; mtype: %d; delay: %d\n",
82 listen_port, mtype, delay );
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040083
E. Scott Daniels8790bf02019-04-23 12:59:28 +000084 if( (mrc = rmr_init( listen_port, 1400, RMRFL_NONE )) == NULL ) {
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040085 fprintf( stderr, "<DEMO> unable to initialise RMR\n" );
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040086 exit( 1 );
87 }
88
E. Scott Danielsa3a121c2020-05-06 09:07:08 -040089 rcv_fd = rmr_get_rcvfd( mrc ); // set up epoll things, start by getting the FD from RMR
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040090 if( rcv_fd < 0 ) {
91 fprintf( stderr, "<DEMO> unable to set up polling fd\n" );
92 exit( 1 );
93 }
94 if( (ep_fd = epoll_create1( 0 )) < 0 ) {
95 fprintf( stderr, "[FAIL] unable to create epoll fd: %d\n", errno );
96 exit( 1 );
97 }
E. Scott Daniels8790bf02019-04-23 12:59:28 +000098 epe.events = EPOLLIN;
99 epe.data.fd = rcv_fd;
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400100
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000101 if( epoll_ctl( ep_fd, EPOLL_CTL_ADD, rcv_fd, &epe ) != 0 ) {
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400102 fprintf( stderr, "[FAIL] epoll_ctl status not 0 : %s\n", strerror( errno ) );
103 exit( 1 );
104 }
105
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400106 sbuf = rmr_alloc_msg( mrc, 256 ); // alloc 1st send buf; subsequent bufs alloc on send
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400107 rbuf = NULL; // don't need to alloc receive buffer
108
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400109 while( ! rmr_ready( mrc ) ) { // must have route table
110 sleep( 1 ); // wait til we get one
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400111 }
112 fprintf( stderr, "<DEMO> rmr is ready\n" );
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400113
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400114
115 while( 1 ) { // send messages until the cows come home
116 snprintf( sbuf->payload, 200,
117 "count=%d received= %d ts=%lld %d stand up and cheer!", // create the payload
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400118 count, rcvd_count, (long long) time( NULL ), rand() );
119
120 sbuf->mtype = mtype; // fill in the message bits
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400121 sbuf->len = strlen( sbuf->payload ) + 1; // send full ascii-z string
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400122 sbuf->state = 0;
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400123 sbuf = rmr_send_msg( mrc, sbuf ); // send & get next buf to fill in
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400124 while( sbuf->state == RMR_ERR_RETRY ) { // soft failure (device busy?) retry
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400125 sbuf = rmr_send_msg( mrc, sbuf ); // w/ simple spin that doesn't give up
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400126 }
127 count++;
128
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400129 // check to see if anything was received and pull all messages in
130 while( (nready = epoll_wait( ep_fd, events, 1, 0 )) > 0 ) { // 0 is non-blocking
131 if( events[0].data.fd == rcv_fd ) { // waiting on 1 thing, so [0] is ok
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400132 errno = 0;
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400133 rbuf = rmr_rcv_msg( mrc, rbuf ); // receive and ignore; just count
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400134 if( rbuf ) {
135 rcvd_count++;
136 }
137 }
138 }
139
E. Scott Danielsa3a121c2020-05-06 09:07:08 -0400140 if( (count % stats_freq) == 0 ) { // occasional stats out to tty
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400141 fprintf( stderr, "<DEMO> sent %d received %d\n", count, rcvd_count );
142 }
143
144 usleep( delay );
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000145 }
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400146}
147