Added API for enabling tethering
[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 #include <settingsd/send_callback.hpp>
36
37 #include <libwebsockets.h>
38
39 #include <string>
40 #include <map>
41 #include <vector>
42 #include <memory>
43 #include <mutex>
44
45
46 namespace ivi
47 {
48   namespace settings
49   {
50     /**
51      * @class manager
52      *
53      * @brief The @c manager class is repository for all
54      *        settings plugins.
55      *
56      * This class exposes the basic settings interface, and hides the
57      * underlying settings plugin details from the caller.  All
58      * interaction with settings plugins is done through this class.
59      */
60     class SETTINGS_API manager
61     {
62     public:
63
64       /**
65        * Constructor.
66        *
67        * @param[in] dir The settings plugin directory path.
68        */
69       manager(std::string const & dir);
70
71       /// Destructor.
72       ~manager();
73
74       /// Register settings plugin with the settings daemon.
75       bool register_setting(std::unique_ptr<plugin> p);
76
77       /**
78        * Dispatch request to appropriate settings plugin.
79        *
80        * @param[in] request JSON formatted request string.
81        * @param[in] wsi     Websocket through which response will be
82        *                    sent.
83        */
84       void dispatch(std::string request, libwebsocket * wsi);
85
86       /**
87        * Subscribe client on the other side of the WebSocket @a wsi to
88        * events created by settings plugins.
89        */
90       void subscribe_client(libwebsocket * wsi);
91
92       /**
93        * Unsubscribe client on the other side of the WebSocket @a wsi
94        * from receiving further settings plugin events.
95        */
96       void unsubscribe_client(libwebsocket * wsi);
97
98       /**
99        * Send JSON formatted event to all clients.
100        *
101        * @param[in] builder Object used to generate the JSON formatted
102        *                    event.
103        *
104        * @return @c true on success.
105        */
106       bool send_event(unique_ptr<JsonBuilder> const & builder) const;
107
108     private:
109
110       /**
111        * @name Prevent copying
112        */
113       //@{
114       manager(manager const &) = delete;
115       manager & operator=(manager const &) = delete;
116       //@}
117
118       /// Load settings.
119       void load_settings(std::string const & dir);
120
121     private:
122
123       /**
124        * @todo A map may be overkill if only a few plugins will ever be
125        *       registered.  It may actually be more efficient from both
126        *       a space and time point of view to simply use an array or
127        *       linked list - although those alternatives would force us
128        *       to explicitly prevent registration of settings plugins
129        *       with duplicate names.
130        */
131       typedef std::map<std::string, std::unique_ptr<plugin>> map_type;
132
133       /**
134        * List of settings plugin loaders.
135        *
136        * Each plugin could potentially register multiple plugins so we
137        * keep track of the loaders independently.  We only keep these
138        * around so that we can gracefully close the plugins if
139        * necessary.
140        *
141        * @note This member must come before the settings plugin map,
142        *       @c settings_, so that the plugins are still in memory
143        *       (i.e. not @c dlclose()d) before they are destructed.
144        *       Remember that the members are destroyed in the reverse
145        *       order they are initialized.
146        */
147       std::vector<std::unique_ptr<loader>> loaders_;
148
149       /// Map of settings name to @c settings instance.
150       map_type settings_;
151
152       /// Mutex used to synchronize access to the send_callback list.
153       mutable std::mutex mutex_;
154
155       /**
156        * List of client WebSockets to be used for sending events to
157        * the client.
158        */
159       std::vector<send_callback> send_callbacks_;
160
161     };
162
163   }
164 }
165
166
167 #endif /* IVI_SETTINGS_MANAGER_HPP */
168
169
170 // Local Variables:
171 // mode:c++
172 // c-basic-offset:2
173 // indent-tabs-mode: nil
174 // End: