Add metrics to the framework
[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         Caution:        This example code is pulled directly into one or more of the documents
39                                 (starting from the "start-example" tag below.  Use caution with
40                                 line lengths and contents because of the requirement that this
41                                 be documentation as well as working code.
42 */
43 // start-example
44 #include <stdio.h>
45
46 #include "ricxfcpp/message.hpp"
47 #include "ricxfcpp/msg_component.hpp"
48 #include <ricxfcpp/metrics.hpp>
49 #include "ricxfcpp/xapp.hpp"
50
51 // counts; not thread safe
52 long cb1_count = 0;
53 long cb2_count = 0;
54 long cbd_count = 0;
55
56 long cb1_lastts = 0;
57 long cb1_lastc = 0;
58
59 /*
60         Respond with 2 messages for each type 1 received
61         Send metrics every 1000 messages.
62 */
63 void cb1( xapp::Message& mbuf, int mtype, int subid, int len,
64                         xapp::Msg_component payload,  void* data ) {
65         long now;
66         long total_count;
67
68         // illustrate that we can use the same buffer for 2 rts calls
69         mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK1\n" );
70         mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK2\n" );
71
72         cb1_count++;
73
74     if( cb1_count % 1000 == 0 && data != NULL ) {   // send metrics every 1000 messages
75         auto x = (Xapp *) data;
76         auto msgm = std::shared_ptr<xapp::Message>( x->Alloc_msg( 4096 ) );
77
78         auto m = std::unique_ptr<xapp::Metrics>( new xapp::Metrics( msgm ) );
79         m->Push_data( "tst_cb1", (double) cb1_count );
80         m->Push_data( "tst_cb2", (double) cb2_count );
81         m->Send();
82     }
83 }
84
85 // just count messages
86 void cb2( xapp::Message& mbuf, int mtype, int subid, int len,
87                         xapp::Msg_component payload,  void* data ) {
88         cb2_count++;
89 }
90
91 // default to count all unrecognised messages
92 void cbd( xapp::Message& mbuf, int mtype, int subid, int len,
93                         xapp::Msg_component payload,  void* data ) {
94         cbd_count++;
95 }
96
97 int main( int argc, char** argv ) {
98         Xapp* x;
99         char*   port = (char *) "4560";
100         int ai = 1;                                                     // arg processing index
101         int nthreads = 1;
102
103         // very simple flag processing (no bounds/error checking)
104         while( ai < argc ) {
105                 if( argv[ai][0] != '-' )  {
106                         break;
107                 }
108
109                 switch( argv[ai][1] ) {                 // we only support -x so -xy must be -x -y
110                         case 'p':
111                                 port = argv[ai+1];
112                                 ai++;
113                                 break;
114
115                         case 't':
116                                 nthreads = atoi( argv[ai+1] );
117                                 ai++;
118                                 break;
119                 }
120
121                 ai++;
122         }
123
124         fprintf( stderr, "<XAPP> listening on port: %s\n", port );
125         fprintf( stderr, "<XAPP> starting %d threads\n", nthreads );
126
127         x = new Xapp( port, true );
128         x->Add_msg_cb( 1, cb1, x );             // register callbacks
129         x->Add_msg_cb( 2, cb2, NULL );
130         x->Add_msg_cb( x->DEFAULT_CALLBACK, cbd, NULL );
131
132         x->Run( nthreads );                             // let framework drive
133         // control should not return
134 }