Add unit tests and changes related
[ric-plt/xapp-frame-cpp.git] / examples / xapp_t1.cpp
1 // vi: ts=4 sw=4 noet:
2 /*
3 ==================================================================================
4         Copyright (c) 2020 Nokia
5         Copyright (c) 2020 AT&T Intellectual Property.
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
11        http://www.apache.org/licenses/LICENSE-2.0
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 /*
22         Mnemonic:       xapp_t1.cpp
23         Abstract:       This is a simple demo xapp which registers 3 callback functions
24                                 (2 for specific message types, and one default callback). It
25                                 defaults to starting 1 thread, but can cause the xapp environment
26                                 to start n listeners.  When running 1 thread it should emit 
27                                 message countes ever few seconds with a crudly computed msg/sec 
28                                 receive rate value.
29
30                                 In addition, the callback for message type 1 will send two response
31                                 messages to verify/illustrate that the same received buffer can
32                                 be used to construct multiple messages and given to the return to
33                                 sender function in RMR for multiple message reponse generation.
34
35         Date:           18 March 2020
36         Author:         E. Scott Daniels
37                 
38 */
39 #include <stdio.h>
40
41 #include "ricxfcpp/message.hpp"
42 #include "ricxfcpp/msg_component.hpp"
43 #include "ricxfcpp/xapp.hpp"
44
45 // ----------------------------------------------------------
46
47 // counts/time values for crude rate estimation; only valid when threads == 1
48 long cb1_count = 0;
49 long cb2_count = 0;
50 long cbd_count = 0;
51
52 long cb1_lastts = 0;
53 long cb1_lastc = 0;
54
55 void cb1( Message& mbuf, int mtype, int subid, int len, Msg_component payload,  void* data ) {
56         long now;
57         long total_count;
58
59         //fprintf( stderr, "callback 1 got a message type = %d len = %d\n", mtype, len );
60         mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK1\n" );    // validate that we can use the same buffer for 2 rts calls
61         mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK2\n" );
62         cb1_count++;
63         
64         now = time( NULL );
65         if( now - cb1_lastts > 5 ) {                            // crude rate estimation starting with second timer pop
66                 if( cb1_lastts ) {
67                         total_count = cb1_count + cb2_count;
68                         fprintf( stderr, "cb1: %ld  diff=%ld ~rate=%ld\n", total_count, now - cb1_lastts, (total_count-cb1_lastc)/(now - cb1_lastts) );
69                         cb1_lastc = total_count;
70                 }
71                 cb1_lastts = now;
72         }
73 }
74
75 void cb2( Message& mbuf, int mtype, int subid, int len, Msg_component payload,  void* data ) {
76         //fprintf( stderr, "callback 2 got a message type = %d len = %d\n", mtype, len );
77         //mbuf.Send_msg( 101, -1, 4, (unsigned char *) "HI\n" );                // send, including the trailing 0
78         cb2_count++;
79 }
80
81 void cbd( Message& mbuf, int mtype, int subid, int len, Msg_component payload,  void* data ) {
82         //fprintf( stderr, "default callback  got a message type = %d len = %d\n", mtype, len );
83         cbd_count++;
84 }
85
86 int main( int argc, char** argv ) {
87         Xapp* x;
88         char*   port = (char *) "4560";
89         int ai = 1;                                                     // arg processing index
90         int nthreads = 1;
91
92         ai = 1;
93         while( ai < argc ) {                            // very simple flag processing (no bounds/error checking)
94                 if( argv[ai][0] != '-' )  {
95                         break;
96                 }
97
98                 switch( argv[ai][1] ) {                 // we only support -x so -xy must be -x -y
99                         case 'p': 
100                                 port = argv[ai+1];      
101                                 ai++;
102                                 break;
103
104                         case 't':
105                                 nthreads = atoi( argv[ai+1] );
106                                 ai++;
107                                 break;
108                 }
109
110                 ai++;
111         }
112         
113         fprintf( stderr, "<XAPP> listening on port: %s\n", port );
114         fprintf( stderr, "<XAPP> starting %d threads\n", nthreads );
115
116         x = new Xapp( port, true );
117         x->Add_msg_cb( 1, cb1, NULL );
118         x->Add_msg_cb( 2, cb2, NULL );
119         x->Add_msg_cb( x->DEFAULT_CALLBACK, cbd, NULL );
120
121         x->Run( nthreads );
122 }