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