Add support for config file parsing and watching
[ric-plt/xapp-frame-cpp.git] / src / config / config.hpp
1 // vi: ts=4 sw=4 noet:
2 /*
3 ==================================================================================
4     Copyright (c) 2020 AT&T Intellectual Property.
5     Copyright (c) 2020 Nokia
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:   config.
23     Abstract:   Header for the config reader/watcher class.
24
25     Date:       27 July 2020
26     Author:     E. Scott Daniels
27 */
28
29 #ifndef XAPP_CONFIG_HPP
30 #define XAPP_CONFIG_HPP
31
32
33 #include <iostream>
34 #include <thread>
35
36 #include "jhash.hpp"
37 #include "config.hpp"
38 #include "config_cb.hpp"
39
40 namespace xapp {
41
42 #define MAX_PFNAME      (4096 + 256)            // max path name and max filname + nil for buffer allocation
43
44 class Config {
45         std::string     fname;                          // the file name that we'll listen to
46         std::thread* listener;                  // listener thread info
47
48         std::shared_ptr<Jhash>  jh;             // the currently parsed json from the config
49         std::unique_ptr<Config_cb> cb;  // info needed to drive user code when config change noticed
50         void*   user_cb_data;                   // data that the caller wants passed on notification callback
51
52         // -----------------------------------------------------------------------
53         private:
54                 std::shared_ptr<xapp::Jhash>  jparse( std::string fname );
55                 std::shared_ptr<xapp::Jhash>  jparse( );
56                 void Listener( );
57
58         public:
59                 Config();                                               // builders
60                 Config( std::string fname);
61
62                 bool Get_control_bool( std::string name, bool defval );
63                 bool Get_control_bool( std::string name );
64
65                 std::string Get_contents( );
66
67                 std::string Get_control_str( std::string name, std::string defval );
68                 std::string Get_control_str( std::string name );
69
70                 double Get_control_value( std::string name, double defval );
71                 double Get_control_value( std::string name );
72
73                 std::string Get_port( std::string name );
74
75                 void Set_callback( notify_callback usr_func, void* usr_data );
76 };
77
78
79 } // namespace
80
81
82
83 #endif