KISSCPP
a C++ library for rapid application development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
configuration.hpp
Go to the documentation of this file.
1 // File : configuration.hpp
2 // Author: Dirk J. Botha <bothadj@gmail.com>
3 //
4 // This file is part of kisscpp library.
5 //
6 // The kisscpp library is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // The kisscpp library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with the kisscpp library. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef _CONFIGURATION_HPP_
20 #define _CONFIGURATION_HPP_
21 
22 #include <string>
23 #include <sstream>
24 #include <cstdlib>
25 #include <set>
26 #include <boost/filesystem.hpp>
27 #include "boost_ptree.hpp"
28 #include "logstream.hpp"
29 
30 namespace bfs = boost::filesystem;
31 
32 namespace kisscpp
33 {
34  typedef std::set<std::string> WhiteListType;
35 
36  typedef struct
37  {
40 
41  void addAppInstance (const std::string &instanceId) { instances.insert(instanceId); }
42  bool isAllowedInstance(const std::string &instanceId) { return (all_instances)?true:(instances.find(instanceId) != instances.end()); }
43  } InstanceListType;
44 
45  typedef std::map<std::string, InstanceListType> MappedWhiteListType;
46 
47  class Config
48  {
49  public:
50  static Config* instance(const std::string app_id = "kisscpp_application",
51  const std::string app_instance = "0",
52  const std::string explicit_config_path = "");
53 
54  void initiate(const std::string explicit_config_path = "");
55 
57  {
58  kisscpp::LogStream log(__PRETTY_FUNCTION__);
59  };
60 
61  //--------------------------------------------------------------------------------
62  BoostPtree get_child (const std::string &s) { return cfg_data.get_child (s); }
63  template<typename T> T get (const std::string &s) { return cfg_data.get <T>(s); }
64  template<typename T> T get (const std::string &s, T default_value) { return cfg_data.get <T>(s,default_value); }
65  template<typename T> boost::optional<T> get_optional(const std::string &s) { return cfg_data.get_optional<T>(s); }
66 
67  std::string getAppId() { return application_id; }
68  std::string getAppInstance() { return application_instance; }
69 
70  bool isAllowedIp (const std::string &ip_address);
71  bool isAllowedClient(const std::string &app_id, const std::string &app_instance);
72 
73  //--------------------------------------------------------------------------------
74  // TODO: Add method for reloading configuration.
75 
76  protected:
77  private:
78  Config () { kisscpp::LogStream log(__PRETTY_FUNCTION__); };
79  Config (Config const&) { kisscpp::LogStream log(__PRETTY_FUNCTION__); };
80  Config& operator=(Config const&) { kisscpp::LogStream log(__PRETTY_FUNCTION__); };
81 
82  Config(const std::string app_id,
83  const std::string app_instance,
84  const std::string explicit_config_path) :
85  allow_all_ip_addrs (false),
86  allow_all_applications(false)
87  {
88  kisscpp::LogStream log(__PRETTY_FUNCTION__);
89 
90  application_id = app_id;
91  application_instance = app_instance;
92 
93  initiate(explicit_config_path);
94  }
95 
96  void loadConfig ();
97  bool loadConfig (std::string &cfg_path, BoostPtree &pt);
98  void populateWhiteLists ();
99  void populateDefaultDirs();
100 
101  static Config *singleton_instance;
102 
103  std::string config_path_instance; // path to the configuration for this instance of the application
104  std::string config_path_common; // path to the common configuration for applications with this application id
105 
106  BoostPtree cfg_data;
107 
108  std::string application_id;
109  std::string application_instance;
110 
111  bool allow_all_ip_addrs;
112  bool allow_all_applications;
113 
114  WhiteListType comms_white_list_ip_addrs;
115  MappedWhiteListType comms_white_list_applications;
116  };
117 }
118 
119 #endif // _STATSKEEPER_HPP_
std::map< std::string, InstanceListType > MappedWhiteListType
Definition: configuration.hpp:45
WhiteListType instances
Definition: configuration.hpp:39
boost::property_tree::ptree BoostPtree
Definition: boost_ptree.hpp:31
BoostPtree get_child(const std::string &s)
Definition: configuration.hpp:62
std::set< std::string > WhiteListType
Definition: configuration.hpp:34
std::string getAppId()
Definition: configuration.hpp:67
Definition: logstream.hpp:145
bool isAllowedIp(const std::string &ip_address)
Definition: configuration.cpp:65
void addAppInstance(const std::string &instanceId)
Definition: configuration.hpp:41
boost::optional< T > get_optional(const std::string &s)
Definition: configuration.hpp:65
void initiate(const std::string explicit_config_path="")
Definition: configuration.cpp:38
bool all_instances
Definition: configuration.hpp:38
bool isAllowedClient(const std::string &app_id, const std::string &app_instance)
Definition: configuration.cpp:79
~Config()
Definition: configuration.hpp:56
std::string getAppInstance()
Definition: configuration.hpp:68
bool isAllowedInstance(const std::string &instanceId)
Definition: configuration.hpp:42
Definition: configuration.hpp:47
static Config * instance(const std::string app_id="kisscpp_application", const std::string app_instance="0", const std::string explicit_config_path="")
Definition: configuration.cpp:26