Added snapshot of oic-service repository
[platform/upstream/iotivity.git] / service / protocol-plugin / plugin-manager / src / PluginManager.h
1 //******************************************************************
2 //
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 /// @file PluginManager.h
22
23 /// @brief
24
25 #ifndef __PLUGINMANAGER_H
26 #define __PLUGINMANAGER_H
27 #define DLOPEN_POSIX
28
29 #include <vector>
30 #include <dirent.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <sys/inotify.h>
35 #include <unistd.h>
36 #include <boost/thread.hpp>
37 #include <boost/bind.hpp>
38 #include <internal.h>
39
40 #include "Plugin.h"
41 #include "OCPlatform.h"
42 #include "OCApi.h"
43
44 #define EVENT_SIZE  ( sizeof (struct inotify_event) )
45 #define BUF_LEN     (int)( 1024 * ( EVENT_SIZE + 16 ) )
46
47 namespace OIC
48 {
49
50
51     struct PluginManagerConfig
52     {
53         std::string rootPluinPath;
54     };
55
56     /**
57     * @brief    After installing a plug-in in a directory, each plug-ins can be managed by this class.
58     *
59     *
60     */
61     class PluginManager
62     {
63         public:
64             /**
65             * A function to register pluins in the path.
66             * This function will load plugins in plugin manager table.
67             *
68             * @param path plugin file path to be registered.
69             * @return int, 1 is success, 0 is fail.
70             *
71             * NOTE:
72             *
73             */
74             int registerPlugin(const std::string path);
75
76
77             /**
78             * A function to register pluins in the path.
79             * This function will load plugins in plugin manager table.
80             *
81             * @param path plugin file path to be registered.
82             * recursive load plugins sub folders recursively.
83             * @return int, 1 is success, 0 is fail.
84             *
85             * NOTE:
86             *
87             */
88             int registerAllPlugin(const std::string path);
89
90
91             /**
92             * Unregister plugin.
93             *
94             * @param plugin plugin object to be unregistered.
95             * @return int, 1 is success, 0 is fail.
96             */
97             int unregisterPlugin(Plugin *const plugin);
98
99
100             /**
101             * Unregister All plugin.
102             *
103             * @return int, 1 is success, 0 is fail.
104             */
105             int unregisterAllPlugin(void);
106
107
108             /**
109             * get all plugins which currently registered.
110             *
111             * @return vector of currently registered plugins
112             *
113             */
114             std::vector<Plugin> &getAllPlugins(void);
115
116
117             /**
118             * find plugins which have the key and value
119             *
120             * @return vector of currently registered plugins
121             */
122             std::vector<Plugin> *findPlugins(const std::string key, const std::string value);
123
124
125             /**
126             * Get plugin which has the id
127             *
128             * @param pluginID plugin id to find
129             * @return Plugin instance
130             */
131             Plugin *getPlugin(const std::string pluginID);
132
133             /**
134             * Start plugin
135             * This function will load dynamic plugin library on memory and call start function of plugin to be initialized.
136             *
137             * @param Plugin
138             * @param Platform pointer.
139             * @return int, 1 is success, 0 is fail.
140             */
141             int start(Plugin *const plugin, void *const arg);
142
143
144             /**
145             * Stop Plugin.
146             * This function will call stop function of plugin and unload dynamic plugin library from memory.
147             *
148             * @param Plugin
149             * @return int, 1 is success, 0 is fail.
150             */
151             int stop(Plugin *const plugin);
152
153
154             /**
155             * Check whether the plugin started or not
156             *
157             * @param Plugin to identify the Starting.
158             * @return true if started, false is stop.
159             *
160             */
161             bool isStarted(Plugin *plugin);
162
163             /**
164             * Get Plugin state.
165             *
166             * @param Plugin ID
167             * @return Plugin state.
168             */
169             const std::string getState(const std::string plugID);
170
171
172             /**
173             *
174             * new Singleton pattern instance.
175             *
176             * @return PluginManager pointer Address.
177             */
178             static PluginManager *Getinstance()
179             {
180                 if (NULL == s_pinstance)
181                 {
182                     s_pinstance = new PluginManager();
183                 }
184
185                 return s_pinstance;
186             }
187
188
189
190         private:
191
192             typedef std::map<std::string, bool> File_list;
193             std::vector<Plugin> m_plugins;
194             cp_context_t *m_context;
195             cp_status_t m_status;
196             cp_plugin_info_t **m_cp_plugins;
197             cp_plugin_info_t *m_plugin;
198             boost::thread m_file_detect_thread;
199             boost::thread_group m_thread_g;
200             std::string m_path;
201             static PluginManager *s_pinstance;
202
203             /**
204             * Constructor for PluginManager.
205             * During construction time, all plugins under the root plugin path will be loaded.
206             *
207             */
208             PluginManager();
209
210             /**
211             * Virtual destructor
212             */
213             virtual ~PluginManager(void);
214
215             /**
216             * delete Singleton pattern instance.
217             */
218             static void deleteinstance()
219             {
220                 if (NULL != s_pinstance)
221                 {
222                     delete s_pinstance;
223                     s_pinstance = NULL;
224                 }
225             }
226
227             /**
228             * detect plugins(add, delete, move)
229             *
230             * @param plugin file path.
231             * @return void
232             */
233             void observePluginPath(void *);
234
235
236             /**
237             * change Number to String.
238             *
239             * @param int.
240             * @return State String.
241             */
242             const char *state_to_string(int state);
243
244             /**
245             * Get whole "SO" file list.
246             *
247             * @param OUT, SO file list.
248             * @param Root path.
249             * @return true or false.
250             */
251             bool getFileList(File_list &list, const std::string strDir);
252
253             /**
254             * print whole plugin info.
255             *
256             * @param cpluff plugins
257             */
258             void printPluginList(cp_plugin_info_t **plugins);
259
260             /**
261             * install plugin using c-pluff.
262             *
263             * @param Root path.
264             * @return int, 1 is success, 0 is fail.
265             */
266             int installPlugin(const std::string path);
267
268
269             /**
270             * find Plugin and install plugin.(Recursive)
271             *
272             * @param Root path.
273             * @return int, 1 is success, 0 is fail.
274             */
275             int findPluginRecursive(const std::string path);
276
277             /**
278             * load Plugin information to PluginManager table.
279             *
280             * @param path to observe
281             * @return int, 1 is success, 0 is fail.
282             */
283             int loadPluginInfoToManager(const std::string path);
284     };
285 }
286
287 #endif //__PLUGINMANAGER_H