Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / test / rmr_interface / tests / sender / rmr_sender.c
1 /*
2  *
3  * Copyright 2019 AT&T Intellectual Property
4  * Copyright 2019 Nokia
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *
18  */
19
20
21 // :vim ts=4 sw=4 noet:
22
23 /*
24         Mnemonic:       rmr_sender2.c
25         Abstract:       Very simple test sender that polls and deals with responses
26                                 in between sends (from a single process).
27
28         Date:           18 February 2018
29         Author:         E. Scott Daniels
30
31         Modified:       18 Mar 2019 - changes to support demo
32 */
33
34 #include <unistd.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/epoll.h>
40 #include <time.h>
41 #include <rmr/rmr.h>
42 #include "rmr_wrapper.h"
43
44
45 void usage( char* argv0 ) {
46         fprintf( stderr, "usage: %s [mtype-max]\n", argv0 );
47         fprintf( stderr, "Sender will send messages with rotating msg types from 0 through mtype-max (if supplied)\n" );
48         fprintf( stderr, "if not supplied, only mtype 0 is sent\n" );
49         fprintf( stderr, "The default listen port for return messages is 43086; this can be changed by setting DUMMY_SENDER_RMR_RCV_PORT  in the environment.\n" );
50         fprintf( stderr, "The sender will send forever unless DEMO_SENDER_MAX is set in the environment which causes termination after max messages.\n" );
51         fprintf( stderr, "The sender will poll for received messages after each send. The amount of time waited is controlled with DEMO_SENDER_PTO (ms) in the env. Use 0 for non-blocking poll.\n" );
52 }
53
54 int main( int argc, char** argv ) {
55         struct rmr_context *rmr_c; //obtain our enhanced rmr_context
56         int     mtype = 0;                                              // we can loop through several message types
57         long    count = 0;
58         char*   lport = "43086";                                // default listen port
59         long    rcount = 0;                                             // number of acks received
60
61         if( (eparm = getenv( "DUMMY_SENDER_RMR_RCV_PORT" )) != NULL ) {
62                 lport = strdup( eparm );
63         }
64
65         rmr_c = rmr_init_wrapper(lport);
66
67         while( ! rmr_ready( rmr_c->mrc ) ) {
68                 fprintf( stderr, "<TEST> waiting for RMr to indicate ready\n" );
69                 sleep( 1 );
70         }
71         fprintf( stderr, "[OK]   initialisation complete\n" );
72
73         while( 1 ) {
74                 usleep( 10 );                   // simulate some work being done
75                 char* message = "foo 111";
76
77                 if(rmr_send_wrapper (rmr_c, mtype, message ) == 1) {
78                         //message successfully received in the receive buffer
79                         char reply[1024];
80                         strcpy(reply,rmr_c->rbuf->payload);
81                         fprintf( stderr, "Acknowledgment received with content:%s\n",rmr_c->rbuf->payload);
82                         rcount++;
83                 }
84                 count++;
85
86                 if( (count % 5000) == 0 ) {
87                         fprintf( stdout, "[INFO] total sent: %ld total received: %ld drops=%ld\n", count, rcount, count - rcount );
88                 }
89
90         }
91
92         fprintf( stderr, "[INFO] sender is terminating having sent %ld messages\n", count );
93         rmr_close_wrapper(rmr_c);
94
95         return 0;
96 }