/*!
  Get a plugin trough  its name plugin.
  \param name The plugin name.
 */
Plugin*
PluginsManager::getPlugin (string &name)
{
  PluginInfo* info = pluginsInfos.get (name);
  if (info)
    return info->getPlugin ();
  return NULL;
}
Exemple #2
0
int main(int argc, char **argv) {
  std::vector<std::string> PluginsNameList = std::vector<std::string>();
  std::vector<PluginInfo*> PluginsInfoList;
  std::vector<BasePlugin*> PluginsList;
  std::vector<void*> PluginsHandleList;
  // Get the plugins names from the PluginsList file
  std::ifstream PluginsFile("plugins/PluginsList.txt");
  if (PluginsFile.is_open()) {
    std::string line;
    while (getline(PluginsFile, line)) {
      PluginsNameList.push_back(line);
    }
    PluginsFile.close();
  }
  // Load all the plugins in order
  for (size_t i = 0; i < PluginsNameList.size(); ++i) {
    char *error;
    std::string fileName = "plugins/" + PluginsNameList[i] + ".so";
    void *handle = dlopen(fileName.c_str(), RTLD_LAZY);
    // If no handle is present, the plugin has not been load, show the error
    if (!handle) {
      std::cerr << "Cannot open plugin " << PluginsNameList[i] << std::endl;
      std::cerr << "More info: " << dlerror() << std::endl;
      continue;
    }
    PluginsHandleList.push_back(handle);
    // Clear any existing error
    dlerror();
    PluginInfo* info = reinterpret_cast<PluginInfo*>(dlsym(handle,
                                                           "information"));
    if ((error = dlerror()) != NULL) {
      std::cerr << "Error loading plugin " << PluginsNameList[i]
                << " information" << std::endl;
      std::cerr << "More info: " << error << std::endl;
    }
    dlerror();
    PluginsInfoList.push_back(info);

    BasePlugin* plugin = reinterpret_cast<BasePlugin*>(info->getPlugin());
    PluginsList.push_back(plugin);
  }

  // Show the loaded plugins info
  for (size_t i = 0; i < PluginsInfoList.size(); ++i) {
    PluginInfo* info = PluginsInfoList[i];
    std::cout << "Plugin info" << std::endl;
    std::cout << "\tAPI version: " << info->apiVersion << std::endl;
    std::cout << "\tPlugin source file: " << info->fileName << std::endl;
    std::cout << "\tPlugin class name: " << info->className << std::endl;
    std::cout << "\tPlugin name: " << info->pluginName << std::endl;
    std::cout << "\tPlugin version: " << info->pluginVersion << std::endl;
    std::cout << "\tPlugin brief description: " << info->pluginDescription
              << std::endl;
  }

  // Execute the function of the plugins
  for (size_t i = 0; i < PluginsList.size(); ++i) {
    PluginsList[i]->textCommand("This is a test of the plugin system", 14);
  }

  // Close the dynamic library bindings
  for (size_t i = 0; i < PluginsHandleList.size(); ++i) {
    dlclose(PluginsHandleList[i]);
  }
}
/*!
  Quick load a plugin, doing all phases {pre,,post}Load.
  \param server The server object to use.
  \param plugins comma separed list of plugins to load as:
  "name1:pluginfile1.so,name2:pluginfile2.so"
*/
int
PluginsManager::quickLoad (Server *server, const string &plugins)
{
  size_t start = 0;
  int ret = 0;
  while (1)
    {
      size_t commaPos = plugins.find (",", start);

      size_t sep = plugins.find (":", start);
      if (sep > commaPos || sep == string::npos)
        {
          server->log (MYSERVER_LOG_MSG_ERROR,
                       _("Invalid plugins data specified"));
          return -1;
        }

      string name = plugins.substr (start, sep - start);
      string file = plugins.substr (sep + 1, commaPos == string::npos
                                    ? string::npos
                                    : commaPos - sep - 1);

      string dir;
      string filename;
      FilesUtility::splitPath (file, dir, filename);

      string xmlDescriptor = dir + "/plugin.xml";
      PluginInfo *pinfo = loadInfo (server, name, xmlDescriptor);

      if (! pinfo)
        {
          server->log (MYSERVER_LOG_MSG_ERROR,
                       _("Cannot find the plugin data, please check "
                         "that the specified name and path are correct"));
          return -1;
        }

      auto_ptr<PluginInfo> pinfoAutoPtr (pinfo);

      ret |= loadFile (server, name, file, pinfo);

      pinfoAutoPtr.release ();

      Plugin *plugin = pinfo->getPlugin ();
      if (! plugin)
        {
          server->log (MYSERVER_LOG_MSG_ERROR,
                       _("Cannot load plugin %s"), file.c_str ());
          return -1;
        }

      ret |= plugin->load (server);
      ret |= plugin->postLoad (server);

      if (commaPos == string::npos)
        break;

      start = commaPos + 1;
    }

  return ret;
}