TIVI-1924: Initial commit of IVI settings daemon.
[profile/ivi/settings-daemon.git] / lib / manager.hpp
1 /**
2  * @file manager.hpp
3  *
4  * @brief Settings manager header.
5  *
6  * @author Ossama Othman @<ossama.othman@@intel.com@>
7  *
8  * @copyright @par
9  * Copyright 2013 Intel Corporation All Rights Reserved.
10  * @par
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation;
14  * version 2.1 of the License.
15  * @par
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  * @par
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA  02110-1301  USA
25  *
26  * @note This header is internal.
27  */
28
29 #ifndef IVI_SETTINGS_MANAGER_HPP
30 #define IVI_SETTINGS_MANAGER_HPP
31
32 #include "loader.hpp"
33
34 #include <settingsd/plugin.hpp>
35
36 #include <libwebsockets.h>
37
38 #include <string>
39 #include <map>
40 #include <vector>
41 #include <memory>
42
43
44 namespace ivi
45 {
46   namespace settings
47   {
48     /**
49      * @class manager
50      *
51      * @brief The @c manager class is repository for all
52      *        settings plugins.
53      *
54      * This class exposes the basic settings interface, and hides the
55      * underlying settings plugin details from the caller.  All
56      * interaction with settings plugins is done through this class.
57      */
58     class SETTINGS_API manager
59     {
60     public:
61
62       /**
63        * Constructor.
64        *
65        * @param[in] dir The settings plugin directory path.
66        */
67       manager(std::string const & dir);
68
69       /// Destructor.
70       ~manager();
71
72       /// Register settings plugin with the settings daemon.
73       bool register_setting(std::unique_ptr<plugin> p);
74
75       /**
76        * Dispatch request to appropriate settings plugin.
77        *
78        * @param[in] request JSON formatted request string.
79        * @param[in] wsi     Websocket through which response will be
80        *                    sent.
81        */
82       void dispatch(std::string request, libwebsocket * wsi);
83
84     private:
85
86       /**
87        * @name Prevent copying
88        */
89       //@{
90       manager(manager const &) = delete;
91       manager & operator=(manager const &) = delete;
92       //@}
93
94       /// Load settings.
95       void load_settings(std::string const & dir);
96
97     private:
98
99       /**
100        * @todo A map may be overkill if only a few plugins will ever be
101        *       registered.  It may actually be more efficient from both
102        *       a space and time point of view to simply use an array or
103        *       linked list - although those alternatives would force us
104        *       to explicitly prevent registration of settings plugins
105        *       with duplicate names.
106        */
107       typedef std::map<std::string, std::unique_ptr<plugin>> map_type;
108
109       /**
110        * List of settings plugin loaders.
111        *
112        * Each plugin could potentially register multiple plugins so we
113        * keep track of the loaders independently.  We only keep these
114        * around so that we can gracefully close the plugins if
115        * necessary.
116        *
117        * @note This member must come before the settings plugin map,
118        *       @c settings_, so that the plugins are still in memory
119        *       (i.e. not @c dlclose()d) before they are destructed.
120        *       Remember that the members are destroyed in the reverse
121        *       order they are initialized.
122        */
123       std::vector<std::unique_ptr<loader>> loaders_;
124
125       /// Map of settings name to @c settings instance.
126       map_type settings_;
127
128     };
129
130   }
131 }
132
133
134 #endif /* IVI_SETTINGS_MANAGER_HPP */
135
136
137 // Local Variables:
138 // mode:c++
139 // c-basic-offset:2
140 // indent-tabs-mode: nil
141 // End: