Example #1
0
File: main.cpp Project: 0r/MITK
int main(int /*argc*/, char** /*argv*/)
{
  char cmd[256];

  std::vector<std::string> availableModules = GetExampleModules();

  /* module path -> lib handle */
  std::map<std::string, SharedLibrary> libraryHandles;

  SharedLibrary sharedLib(LIB_PATH, "");

  std::cout << "> ";
  while(std::cin.getline(cmd, sizeof(cmd)))
  {
    std::string strCmd(cmd);
    if (strCmd == "q")
    {
      break;
    }
    else if (strCmd == "h")
    {
      std::cout << std::left << std::setw(15) << "h" << " This help text\n"
                << std::setw(15) << "l <id | name>" << " Load the module with id <id> or name <name>\n"
                << std::setw(15) << "u <id>" << " Unload the module with id <id>\n"
                << std::setw(15) << "s" << " Print status information\n"
                << std::setw(15) << "q" << " Quit\n" << std::flush;
    }
    else if (strCmd.find("l ") != std::string::npos)
    {
      std::string idOrName;
      idOrName.assign(strCmd.begin()+2, strCmd.end());
      std::stringstream ss(idOrName);

      long int id = -1;
      ss >> id;
      if (id > 0)
      {
        Module* module = ModuleRegistry::GetModule(id);
        if (!module)
        {
          std::cout << "Error: unknown id" << std::endl;
        }
        else if (module->IsLoaded())
        {
          std::cout << "Info: module already loaded" << std::endl;
        }
        else
        {
          try
          {
            std::map<std::string, SharedLibrary>::iterator libIter =
                libraryHandles.find(module->GetLocation());
            if (libIter != libraryHandles.end())
            {
              libIter->second.Load();
            }
            else
            {
              // The module has been loaded previously due to a
              // linker dependency
              SharedLibrary libHandle(module->GetLocation());
              libHandle.Load();
              libraryHandles.insert(std::make_pair(libHandle.GetFilePath(), libHandle));
            }
          }
          catch (const std::exception& e)
          {
            std::cout << e.what() << std::endl;
          }
        }
      }
      else
      {
        Module* module = ModuleRegistry::GetModule(idOrName);
        if (!module)
        {
          try
          {
            std::map<std::string, SharedLibrary>::iterator libIter =
                libraryHandles.find(sharedLib.GetFilePath(idOrName));
            if (libIter != libraryHandles.end())
            {
              libIter->second.Load();
            }
            else
            {
              bool libFound = false;
              for (std::vector<std::string>::const_iterator availableModuleIter = availableModules.begin();
                   availableModuleIter != availableModules.end(); ++availableModuleIter)
              {
                if (*availableModuleIter == idOrName)
                {
                  libFound = true;
                }
              }
              if (!libFound)
              {
                std::cout << "Error: unknown example module" << std::endl;
              }
              else
              {
                SharedLibrary libHandle(LIB_PATH, idOrName);
                libHandle.Load();
                libraryHandles.insert(std::make_pair(libHandle.GetFilePath(), libHandle));
              }
            }

            std::vector<Module*> modules = ModuleRegistry::GetModules();
            for (std::vector<Module*>::const_iterator moduleIter = modules.begin();
                 moduleIter != modules.end(); ++moduleIter)
            {
              availableModules.erase(std::remove(availableModules.begin(), availableModules.end(), (*moduleIter)->GetName()),
                                     availableModules.end());
            }

          }
          catch (const std::exception& e)
          {
            std::cout << e.what() << std::endl;
          }
        }
        else if (!module->IsLoaded())
        {
          try
          {
            const std::string modulePath = module->GetLocation();
            std::map<std::string, SharedLibrary>::iterator libIter =
                libraryHandles.find(modulePath);
            if (libIter != libraryHandles.end())
            {
              libIter->second.Load();
            }
            else
            {
              SharedLibrary libHandle(LIB_PATH, idOrName);
              libHandle.Load();
              libraryHandles.insert(std::make_pair(libHandle.GetFilePath(), libHandle));
            }
          }
          catch (const std::exception& e)
          {
            std::cout << e.what() << std::endl;
          }
        }
        else if (module)
        {
          std::cout << "Info: module already loaded" << std::endl;
        }
      }
    }
    else if (strCmd.find("u ") != std::string::npos)
Example #2
0
void PluginManager::init() {
  static const std::string plugin_config_file("./plugins.xml");
  std::string current_path = directory_conf();

  std::string full_path = current_path + "/" + plugin_config_file;

  BOOST_LOG_TRIVIAL(trace) << "Loading plugin configuration file from "
                           << full_path;
  try {
    read_xml(full_path, _pt);
  } catch (...) {
    BOOST_LOG_TRIVIAL(error) << "Can not load configuration file from "
                             << full_path;
    return;
  }

  // iterate configuration for configured plugins
  BOOST_LOG_TRIVIAL(trace) << "Target load plugins...";
  for (pt::ptree::value_type &node : _pt.get_child("plugins")) {
    if (node.first == "plugin") {
      std::string name(node.second.get<std::string>("name"));
      std::string path(current_path + "/" +
                       node.second.get<std::string>("path"));
      BOOST_LOG_TRIVIAL(trace) << "Module Name: " << name
                               << " Module Path: " << path;

      SharedLibrary libHandle(path, name);
      BOOST_LOG_TRIVIAL(trace) << "Loading library " << libHandle.GetFilePath();

      try {
        libHandle.Load();
      } catch (...) {
        BOOST_LOG_TRIVIAL(error) << "Error when loading plugin library " << path
                                 << name;
      }
    }
  }

  // iterate to every module and get service references
  std::vector<Module *> modules = ModuleRegistry::GetModules();

  BOOST_LOG_TRIVIAL(trace) << "Print existing plugins... ";
  for (Module *module : modules) {
    BOOST_LOG_TRIVIAL(trace)
        << "Module Name: " << module->GetName()
        << " Module ID: " << module->GetModuleId()
        << " Status: " << (module->IsLoaded() ? "LOADED" : "UNLOADED");

    if (module->IsLoaded()) {
      ModuleContext *context = module->GetModuleContext();
      std::vector<ServiceReference<IoTInfoProvider>> services =
          context->GetServiceReferences<IoTInfoProvider>(
              "(type=IoTInfoProvider)");

      BOOST_LOG_TRIVIAL(trace) << "Find  " << services.size()
                               << " provider references";
      for (ServiceReference<IoTInfoProvider> &value : services) {
        IoTInfoProvider *provider = context->GetService<IoTInfoProvider>(value);
        if (std::find(this->begin(), this->end(), provider) == this->end()) {
          this->push_back(provider);
        }
      }
    }
  }

  BOOST_LOG_TRIVIAL(trace) << "Total " << this->size() << " providers loaded";
}