dmpserverplugins.cpp 2.5 KB
/**************************************************************************
* file:              dmpserverplugins.cpp

* Author:            wanzhongping
* Date:              2021-03-12 17:01:24
* Email:             zhongpingw@chinadci.com
* copyright:         广州城市信息研究所有限公司
***************************************************************************/

#include "dmpserverplugins.h"
#include <boost/dll/import.hpp>
#include "dmpapplication.h"
#include <dlfcn.h>
#include <boost/format.hpp>
#include <boost/make_shared.hpp>
#include "dmplogger.h"

std::shared_ptr<DmpPythonUtils> DmpServerPlugins::sp_python_utils_ = std::make_shared<DmpPythonUtils>();

std::vector<std::string> &DmpServerPlugins::ServerPlugins()
{
  static std::vector<std::string> pluginList;
  return pluginList;
}

bool DmpServerPlugins::InitPlugins(DmpServerInterface *interface)
{
    sp_python_utils_->InitServerPython(interface);
    if (sp_python_utils_->IsEnabled())
    {
        LOGGER_INFO("Python support ENABLED :-)");
    }
    else
    {
        LOGGER_WARN("Python support FAILED :-(");
        return false;
    }

    //Init plugins: loads a list of installed plugins and filter them
    //for "server" metadata
    bool at_least_one_enabled = false;
    std::vector<std::string> vec_plugins = sp_python_utils_->PluginList();
    std::vector<std::string>::iterator iter_plugin_name;
    std::string comma = ",";
    for (iter_plugin_name = vec_plugins.begin(); iter_plugin_name != vec_plugins.end(); iter_plugin_name++)
    {
        std::string plugin_service = sp_python_utils_->GetPluginMetadata(*iter_plugin_name, "server");
        if (plugin_service == "True")
        {
            if (sp_python_utils_->LoadPlugin(*iter_plugin_name))
            {
                if (sp_python_utils_->StartServerPlugin(*iter_plugin_name))
                {
                    at_least_one_enabled = true;
                    ServerPlugins().push_back(*iter_plugin_name);
                    boost::format fmt = boost::format("Server plugin %1% loaded!") % *iter_plugin_name;
                    LOGGER_INFO(fmt.str());
                }
                else
                {
                    LOGGER_CRITICAL("Error loading server plugin " + *iter_plugin_name);
                }
            }
            else
            {
                LOGGER_CRITICAL("Error starting server plugin " + *iter_plugin_name);
            }
        }
    }
    return sp_python_utils_ && sp_python_utils_->IsEnabled() && at_least_one_enabled;
}