1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file CpluffAdapter.cpp
25 #include "CpluffAdapter.h"
29 CpluffAdapter *CpluffAdapter::s_pinstance;
31 CpluffAdapter::CpluffAdapter()
34 m_context = cp_create_context(&m_status);
35 m_cp_plugins = nullptr;
37 registerAllPlugin("../../../plugins");
40 CpluffAdapter::~CpluffAdapter(void)
42 cp_release_info(m_context, m_cp_plugins);
43 m_thread_g.interrupt_all();
44 m_thread_g.join_all();
45 s_pinstance->deleteinstance();
46 s_pinstance = nullptr;
49 int CpluffAdapter::installPlugin(const std::string path)
51 char *argv = const_cast<char *>(path.c_str());
53 if ((m_plugin = cp_load_plugin_descriptor(m_context, argv, &m_status)) == nullptr)
55 printf("cp_load_plugin_descriptor failed\n");
58 else if ((m_status = cp_install_plugin(m_context, m_plugin)) != 0)
60 printf("cp_install_plugin failed\n");
67 int CpluffAdapter::findPluginRecursive(const std::string path)
69 ////////FILELIST////////////
71 getFileList(file_list, path);
72 File_list::iterator itr;
75 for (itr = file_list.begin(); itr != file_list.end(); itr++)
77 if (itr->second == true)
79 //printf("[Direcotry] %s\n",itr->first.c_str());
83 //printf("[FILE] = %s\n" , itr->first.c_str());
84 std::string filepath = itr->first.c_str();
86 for (unsigned int i = 0 ; i < filepath.size(); i++)
88 if (filepath.at(i) == '/')
93 std::string filename = filepath.substr(0, count );
94 //char *temp_char = const_cast<char *>(filename.c_str());
95 flag = installPlugin(filename);
96 //printf("plugin file path %s \n", plugin->plugin_path);
103 int CpluffAdapter::loadPluginInfoToManager(const std::string path)
105 //get plugins information.
106 if ((m_cp_plugins = cp_get_plugins_info(m_context, &m_status, nullptr)) == nullptr)
108 printf("cp_get_plugins_infor failed\n");
113 printPluginList(m_cp_plugins);
116 for (int i = 0 ; m_cp_plugins[i] != nullptr; i++)
118 Plugin *plugin = new Plugin;
119 plugin->setValue("Path", m_cp_plugins[i]->plugin_path);
120 plugin->setValue("Language", "CPP");
121 //printf("add filepath %s\n",m_cp_plugins[i]->plugin_path);
122 if (m_cp_plugins[i]->identifier != nullptr)
124 plugin->setValue("Id", m_cp_plugins[i]->identifier );
128 plugin->setValue("Id", "");
131 if (m_cp_plugins[i]->url != nullptr)
133 plugin->setValue("Url", m_cp_plugins[i]->url);
137 plugin->setValue("Url", "");
140 if (m_cp_plugins[i]->name != nullptr)
142 plugin->setValue("Name", m_cp_plugins[i]->name);
146 plugin->setValue("Name", "");
149 if (m_cp_plugins[i]->resourcetype != nullptr)
151 plugin->setValue("ResourceType", m_cp_plugins[i]->resourcetype);
155 plugin->setValue("ResourceType", "");
158 if (m_cp_plugins[i]->version != nullptr)
160 plugin->setValue("Version", m_cp_plugins[i]->version);
164 plugin->setValue("Version", "");
167 bool plugin_compare_flag = true;
168 for (unsigned int i = 0 ; i < m_plugins.size(); i++)
170 if (*plugin == m_plugins[i])
173 plugin_compare_flag = false;
177 if (plugin_compare_flag)
179 m_plugins.push_back(*plugin);
182 boost::thread *t = new boost::thread(boost::bind(&CpluffAdapter::observePluginPath,
183 //this, (void *)path.c_str()));
184 this, (void *)m_cp_plugins[i]->plugin_path));
185 m_thread_g.add_thread(t);
189 printf("thread throw exception\n");
193 //printf("plugin size = %d\n",m_plugins.size());
199 int CpluffAdapter::registerPlugin(const std::string path)
201 //printf("CpluffAdapter register\n");
206 printf("CpluffAdapter path == null\n");
212 flag = installPlugin(path);
214 flag = loadPluginInfoToManager(path);
219 int CpluffAdapter::registerAllPlugin(const std::string path)
221 //printf("CpluffAdapter register\n");
225 printf("CpluffAdapter path == null\n");
231 flag = findPluginRecursive(path);
233 flag = loadPluginInfoToManager(path);
238 int CpluffAdapter::unregisterPlugin(Plugin *const plugin)
241 char *argv = nullptr;
244 for (unsigned int i = 0; i < m_plugins.size(); i++)
247 if (*plugin == m_plugins[i])
249 id = m_plugins[i].getID().c_str();
250 argv = const_cast<char *>(id.c_str());
251 if ((status = cp_uninstall_plugin(m_context, argv)) != 0)
253 printf("unregister failed\n");
256 m_plugins.erase(m_plugins.begin() + i);
262 int CpluffAdapter::unregisterAllPlugin(void)
265 char *argv = nullptr;
270 id = m_plugins[0].getID().c_str();
271 argv = const_cast<char *>(id.c_str());
272 if ((status = cp_uninstall_plugin(m_context, argv)) != 0)
274 printf("unregister failed\n");
277 m_plugins.erase(m_plugins.begin());
278 if (m_plugins.size() == 0)
287 std::vector<Plugin> &CpluffAdapter::getAllPlugins(void)
292 std::vector<Plugin> *CpluffAdapter::findPlugins(const std::string key, const std::string value)
294 std::vector<Plugin> *re_plugins;
296 re_plugins = new std::vector<Plugin>;
298 for (unsigned int i = 0; i < m_plugins.size(); i++)
300 if (!m_plugins[i].getValueByAttribute(key).compare(value))
302 re_plugins->push_back(m_plugins[i]);
309 Plugin *CpluffAdapter::getPlugin(const std::string plugID)
311 for (unsigned int i = 0; i < m_plugins.size(); i++)
313 if (!(m_plugins[i].getID().compare(plugID)))
315 return &(m_plugins[i]);
322 bool CpluffAdapter::getFileList(File_list &list, const std::string strDir)
324 struct stat statinfo;
325 memset(&statinfo, 0, sizeof(statinfo));
326 std::string path = strDir;
327 if (0 != lstat(path.c_str(), &statinfo))
329 printf("OICPlugManager lstat is 0\n");
332 if (!S_ISDIR(statinfo.st_mode))
334 printf("%s is not directory", strDir.c_str());
339 struct dirent *entry;
341 if ((dir = opendir(strDir.c_str())) == nullptr)
343 printf("%s open error", strDir.c_str());
347 while ((entry = readdir(dir)) != nullptr)
349 memset(&statinfo, 0, sizeof(statinfo));
350 std::string strFilePath = strDir + "/" + entry->d_name;
351 while (strFilePath.find("//") != std::string::npos)
352 strFilePath.replace(strFilePath.find("//"), 2, "/");
354 if (0 != lstat(strFilePath.c_str(), &statinfo))
356 printf("OICPlugManager lstat is 0\n");
361 if (S_ISDIR(statinfo.st_mode))
363 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
366 list.insert(std::pair<std::string, bool>(strFilePath, true));
367 std::string strSubDir = strDir + "/" + entry->d_name;
368 getFileList(list, strSubDir);
372 int size = strFilePath.size();
373 std::string so = strFilePath.substr(size - 2, size - 1);
376 list.insert(std::pair<std::string, bool>(strFilePath, false));
385 const char *CpluffAdapter::state_to_string(int state)
406 void CpluffAdapter::printPluginList(cp_plugin_info_t **plugins)
408 const char format[] = " %-30s %-15s %-15s %-20s\n";
409 printf("\n====== Plugins List ======\n");
416 for (int i = 0; m_cp_plugins[i] != nullptr; i++)
419 m_cp_plugins[i]->identifier,
420 m_cp_plugins[i]->name != nullptr ? m_cp_plugins[i]->name : "",
421 state_to_string(cp_get_plugin_state(m_context, m_cp_plugins[i]->identifier)),
422 m_cp_plugins[i]->resourcetype != nullptr ? m_cp_plugins[i]->resourcetype : "");
427 int CpluffAdapter::start(Plugin *const plugin, void *const arg)
434 id = plugin->getID();
435 for (unsigned int i = 0 ; i < m_plugins.size(); i++)
437 if (*plugin == m_plugins[i])
439 ctx = cpi_new_context((cp_plugin_t *)hnode_get(hash_lookup(m_context->env->plugins, id.c_str())),
440 m_context->env, &status);
441 cp_define_symbol(ctx, "START_ARGUMENT", arg);
442 //printf("start ocplatform address : %x\n", arg);
444 if ((status = cp_start_plugin(m_context, (char *)id.c_str()) ) != CP_OK)
446 printf("API function CpluffAdapter::start() faild with error code.\n");
451 printf("start plug-in %s.\n", (char *)id.c_str());
460 int CpluffAdapter::stop(Plugin *const plugin)
465 id = plugin->getID();
466 printf("stop plugin id = %s\n", id.c_str());
467 if ((status = cp_stop_plugin(m_context, (char *)id.c_str())) != CP_OK)
469 printf("API function CpluffAdapter::stop() faild with error code.\n");
474 printf("Stop plug-in %s.\n", (char *)id.c_str());
480 bool CpluffAdapter::isStarted(Plugin *plugin)
483 cp_plugin_state_t state;
485 id = plugin->getID();
487 state = cp_get_plugin_state(m_context, (char *)id.c_str());
488 if (state == CP_PLUGIN_STARTING || state == CP_PLUGIN_ACTIVE)
495 void CpluffAdapter::observePluginPath(void *str)
497 //printf("start observePluginPath\n");
502 char *str1 = (char *)str;
503 std::string original_path(str1);
504 //printf("Directory is %s\n",(char*)str1));
505 char buffer[BUF_LEN];
511 printf("inotify_init\n");
514 wd = inotify_add_watch( fd, str1,
515 IN_MODIFY | IN_CREATE | IN_DELETE | IN_DELETE_SELF
516 | IN_MOVE_SELF | IN_MOVED_FROM | IN_MOVED_TO);
520 //printf("read start\n");
522 length = read( fd, buffer, BUF_LEN );
526 printf("observePluginPath read\n");
528 std::string filepath = original_path;
529 //printf("filepath = %s\n",filepath.c_str());
532 if (i < (signed)(BUF_LEN - ( sizeof( struct inotify_event) + 16)) && i > -1)
534 struct inotify_event *event = ( struct inotify_event *) &buffer[ i ];
536 if ((event->mask & IN_CREATE) || (event->mask & IN_MODIFY) || (event->mask & IN_MOVED_TO))
538 registerAllPlugin(str1);
543 //filepath += std::string(event->name);
544 std::vector<Plugin> *resource_plugin = findPlugins("Path", filepath.c_str()); //add foldername
546 //printf("plugin size is %d\n",resource_plugin->size());
547 //printf("plugin file path is %s\n",resource_plugin->());
548 if (resource_plugin->size() == 1)
550 unregisterPlugin(&(resource_plugin->at(0)));
554 registerAllPlugin(str1);
556 delete(resource_plugin);
557 resource_plugin = nullptr;
559 //printf("observePluginPath path = %s \n",str1);
560 //printf("observePluginPath directory name = %s \n",event->name);
561 i += EVENT_SIZE + event->len;
567 ( void ) inotify_rm_watch( fd, wd );
568 ( void ) close( fd );
569 //printf("observePluginPath end\n");
572 const std::string CpluffAdapter::getState(const std::string plugID)
574 return state_to_string(cp_get_plugin_state(m_context, plugID.c_str()));