1 // vim: ts=4 sw=4 noet:
3 --------------------------------------------------------------------------------
4 Copyright (c) 2018-2019 AT&T Intellectual Property.
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 --------------------------------------------------------------------------------
22 Abstract: Very simple test sender. Sends messages with a given delay between each.
23 The sender also uses epoll_wait() to ensure that any received messages
26 Parms: The following positional parameters are recognised on the command line:
27 [listen_port [delay [stats-freq] [msg-type]]]]
31 delay (mu-sec) 1000000 (1 sec)
43 #include <sys/epoll.h>
48 int main( int argc, char** argv ) {
49 void* mrc; //msg router context
50 struct epoll_event events[1]; // list of events to give to epoll
51 struct epoll_event epe; // event definition for event to listen to
52 int ep_fd = -1; // epoll's file des (given to epoll_wait)
53 int rcv_fd; // file des that NNG tickles -- give this to epoll to listen on
54 int nready; // number of events ready for receive
55 rmr_mbuf_t* sbuf; // send buffer
56 rmr_mbuf_t* rbuf; // received buffer
59 char* listen_port = "43086";
60 int delay = 1000000; // mu-sec delay between messages
65 listen_port = argv[1];
68 delay = atoi( argv[2] );
71 mtype = atoi( argv[3] );
74 fprintf( stderr, "<DEMO> listen port: %s; mtype: %d; delay: %d\n", listen_port, mtype, delay );
76 if( (mrc = rmr_init( listen_port, 1400, RMRFL_NONE )) == NULL ) {
77 fprintf( stderr, "<DEMO> unable to initialise RMr\n" );
81 rcv_fd = rmr_get_rcvfd( mrc ); // set up epoll things, start by getting the FD from MRr
83 fprintf( stderr, "<DEMO> unable to set up polling fd\n" );
86 if( (ep_fd = epoll_create1( 0 )) < 0 ) {
87 fprintf( stderr, "[FAIL] unable to create epoll fd: %d\n", errno );
93 if( epoll_ctl( ep_fd, EPOLL_CTL_ADD, rcv_fd, &epe ) != 0 ) {
94 fprintf( stderr, "[FAIL] epoll_ctl status not 0 : %s\n", strerror( errno ) );
98 sbuf = rmr_alloc_msg( mrc, 256 ); // alloc first send buffer; subsequent buffers allcoated on send
99 rbuf = NULL; // don't need to alloc receive buffer
101 while( ! rmr_ready( mrc ) ) { // must have a route table before we can send; wait til RMr say it has one
104 fprintf( stderr, "<DEMO> rmr is ready\n" );
107 while( 1 ) { // send messages until the cows come home
108 snprintf( sbuf->payload, 200, "count=%d received= %d ts=%lld %d stand up and cheer!\n", // create the payload
109 count, rcvd_count, (long long) time( NULL ), rand() );
111 sbuf->mtype = mtype; // fill in the message bits
112 sbuf->len = strlen( sbuf->payload ) + 1; // our receiver likely wants a nice acsii-z string
114 sbuf = rmr_send_msg( mrc, sbuf ); // send it (send returns an empty payload on success, or the original payload on fail/retry)
115 while( sbuf->state == RMR_ERR_RETRY ) { // soft failure (device busy?) retry
116 sbuf = rmr_send_msg( mrc, sbuf ); // retry send until it's good (simple test; real programmes should do better)
119 fprintf( stderr, "<SNDR> sent message\n" );