Add API allowing xAPPs to send alarm messages
[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/xapp.hpp"
49
50 // counts; not thread safe
51 long cb1_count = 0;
52 long cb2_count = 0;
53 long cbd_count = 0;
54
55 long cb1_lastts = 0;
56 long cb1_lastc = 0;
57
58 // respond with 2 messages for each type 1 received
59 void cb1( xapp::Message& mbuf, int mtype, int subid, int len,
60                         xapp::Msg_component payload,  void* data ) {
61         long now;
62         long total_count;
63
64         // illustrate that we can use the same buffer for 2 rts calls
65         mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK1\n" );
66         mbuf.Send_response( 101, -1, 5, (unsigned char *) "OK2\n" );
67
68         cb1_count++;
69 }
70
71 // just count messages
72 void cb2( xapp::Message& mbuf, int mtype, int subid, int len,
73                         xapp::Msg_component payload,  void* data ) {
74         cb2_count++;
75 }
76
77 // default to count all unrecognised messages
78 void cbd( xapp::Message& mbuf, int mtype, int subid, int len,
79                         xapp::Msg_component payload,  void* data ) {
80         cbd_count++;
81 }
82
83 int main( int argc, char** argv ) {
84         Xapp* x;
85         char*   port = (char *) "4560";
86         int ai = 1;                                                     // arg processing index
87         int nthreads = 1;
88
89         // very simple flag processing (no bounds/error checking)
90         while( ai < argc ) {
91                 if( argv[ai][0] != '-' )  {
92                         break;
93                 }
94
95                 switch( argv[ai][1] ) {                 // we only support -x so -xy must be -x -y
96                         case 'p':
97                                 port = argv[ai+1];
98                                 ai++;
99                                 break;
100
101                         case 't':
102                                 nthreads = atoi( argv[ai+1] );
103                                 ai++;
104                                 break;
105                 }
106
107                 ai++;
108         }
109
110         fprintf( stderr, "<XAPP> listening on port: %s\n", port );
111         fprintf( stderr, "<XAPP> starting %d threads\n", nthreads );
112
113         x = new Xapp( port, true );
114         x->Add_msg_cb( 1, cb1, NULL );                          // register callbacks
115         x->Add_msg_cb( 2, cb2, NULL );
116         x->Add_msg_cb( x->DEFAULT_CALLBACK, cbd, NULL );
117
118         x->Run( nthreads );                             // let framework drive
119         // control should not return
120 }