Add support for config file parsing and watching
[ric-plt/xapp-frame-cpp.git] / examples / xapp_t3.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_t3.cpp
23         Abstract:       This is a simple xAPP which includes using the config support
24                                 in the framework.  It is based on the xapp_t1 example and doesn't
25                                 do much more than that.
26
27         Date:           30 July 2020
28         Author:         E. Scott Daniels
29
30         Caution:        This example code is pulled directly into one or more of the documents
31                                 (starting from the "start-example" tag below.  Use caution with
32                                 line lengths and contents because of the requirement that this
33                                 be documentation as well as working code.
34 */
35 // start-example
36 #include <stdio.h>
37
38 #include "ricxfcpp/config.hpp"
39 #include "ricxfcpp/message.hpp"
40 #include "ricxfcpp/msg_component.hpp"
41 #include <ricxfcpp/metrics.hpp>
42 #include "ricxfcpp/xapp.hpp"
43
44 int vlevel = 0;                                 // verbose mode set via config
45
46 /*
47         Just print something to the tty when we receive a message
48         and are in verbose mode.
49 */
50 void cb1( xapp::Message& mbuf, int mtype, int subid, int len,
51                         xapp::Msg_component payload,  void* data ) {
52         if( vlevel > 0 ) {
53                 fprintf( stdout, "message received is %d bytes long\n", len );
54         }
55 }
56
57 /*
58         Driven when the configuration changes. We snarf the verbose
59         level from the new config and update it. If it changed to
60         >0, incoming messages should be recorded with a tty message.
61         If set to 0, then tty output will be disabled.
62 */
63 void config_cb( xapp::Config& c, void* data ) {
64         int* vp;
65
66         if( (vp = (int *) data) != NULL ) {
67                 *vp = c.Get_control_value( "verbose_level", *vp );
68         }
69 }
70
71 int main( int argc, char** argv ) {
72         Xapp*   x;
73         int             nthreads = 1;
74         std::unique_ptr<xapp::Config> cfg;
75
76         // only parameter recognised is the config file name
77         if( argc > 1 ) {
78                 cfg = std::unique_ptr<xapp::Config>( new xapp::Config( std::string( argv[1] ) ) );
79         } else {
80                 cfg = std::unique_ptr<xapp::Config>( new xapp::Config( ) );
81         }
82
83         // must get a port from the config; no default
84         auto port = cfg->Get_port( "rmr-data" );
85         if( port.empty() ) {
86                 fprintf( stderr, "<FAIL> no port in config file\n" );
87                 exit( 1 );
88         }
89
90         // dig other data from the config
91         vlevel = cfg->Get_control_value( "verbose_level", 0 );
92         nthreads = cfg->Get_control_value( "thread_count", 1 );
93         // very simple flag processing (no bounds/error checking)
94
95         if( vlevel > 0 ) {
96                 fprintf( stderr, "<XAPP> listening on port: %s\n", port.c_str() );
97                 fprintf( stderr, "<XAPP> starting %d threads\n", nthreads );
98         }
99
100         // register the config change notification callback
101         cfg->Set_callback( config_cb, (void *) &vlevel );
102
103         x = new Xapp( port.c_str(), true );
104         x->Add_msg_cb( 1, cb1, x );             // register message callback
105
106         x->Run( nthreads );                             // let framework drive
107         // control should not return
108 }