dmpservermanager.cpp 5.6 KB
/**************************************************************************
* file:              dmpservermanager.cpp

* Author:            wanzhongping
* Date:              2021-07-27 21:59:46
* Email:             zhongpingw@chinadci.com
* copyright:         广州城市信息研究所有限公司
***************************************************************************/
#include "dmpservermanager.h"
#include "dmpserver.h"
#include "dmphttputils.h"
#include "dmpserverconfig.h"
#include <memory>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <sstream>
#include <fstream>
#include <math.h>
#include "dmptilelayer.h"
#include <iostream>
#include "dmplogger.h"
#include "dmptilelayer.h"

DmpServerManager::DmpServerManager()
{
    serverRegistry_ = new DmpServerRegistry();
}

DmpServerManager::~DmpServerManager()
{
    if (serverRegistry_)
    {
        delete serverRegistry_;
        serverRegistry_ = NULL;
    }

    ProjectMap::iterator iter = projects_.begin();
    for (; iter != projects_.end(); ++iter)
    {
        DmpProject *mapProject = iter->second;
        if (mapProject)
        {
            delete mapProject;
            mapProject = nullptr;
        }
    } 
    projects_.clear();
}

void DmpServerManager::init(const boost::filesystem::path &modulePath)
{
    serverRegistry_->init(modulePath);
    if(!loadServices())
    {
        std::cout << "加载服务失败!" << std::endl;
        LOGGER_ERROR("加载服务失败!");
    }
    //LoadDmpServices();
}

std::string DmpServerManager::getCapabilities()
{
    return serverRegistry_->getCapabilities();
}

std::shared_ptr<DmpServer> DmpServerManager::serverForRequest(const DmpServerRequest &request)
{
    return serverRegistry_->getServerForRequest(request);
}

std::shared_ptr<DmpServerApi> DmpServerManager::apiForRequest(const DmpServerRequest &request)
{
    return serverRegistry_->getApiForRequest(request);
}

DmpProject *DmpServerManager::getProject(const std::string &serviceName)
{
    std::map<std::string, DmpProject *>::iterator iter = projects_.find(serviceName);
    if (iter != projects_.end())
    {
        return iter->second;
    }
    else
    {
        return nullptr;
    }
}

bool DmpServerManager::removeProject(const std::string &serviceName)
{
    try
    {
        std::map<std::string, DmpProject *>::iterator iter = projects_.find(serviceName);
        if (iter != projects_.end())
        {
            delete iter->second;
            projects_.erase(iter);
        }
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
        return false;
    }
    return true;
}

bool DmpServerManager::publish(const std::string& serverName, const std::string& serviceName, const std::string& title, int capabilities, const std::string& projectData)
{
    //project
    std::string projData;
    if (!DmpServerUtils::Base64Decode(projectData, &projData))
    {
        return false;
    }
    DmpProject *project = new DmpProject();
    if (!project->Read(projData))
    {
        delete project;
        return false;
    }

    if (!serverRegistry_->getServer(serverName)->publish(serviceName, title, capabilities, *project))
    {
        delete project;
        return false;
    }
    projects_[serviceName] = project;
    
    return true;
}

bool DmpServerManager::deleteService(const std::string &serverName, const std::string &serviceName)
{
    if (serverRegistry_->getServer(serverName)->remove(serviceName) && removeProject(serviceName))
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool DmpServerManager::startService(const std::string &serverName, const std::string &serviceName)
{
    return serverRegistry_->getServer(serverName)->start(serviceName);
}

bool DmpServerManager::stopService(const std::string &serverName, const std::string &serviceName)
{
    return serverRegistry_->getServer(serverName)->stop(serviceName);
}
bool DmpServerManager::loadServices()
{
    boost::property_tree::ptree pt,ptList;
    std::string conn = DmpServerConfig::Instance()->getMetaUrl();
    const std::string url= conn + URI_RELOAD;
    std::string strContent=DmpHttp::get(url);
    if(strContent.length()==0)
    {
        return false;
    }
    std::stringstream ssData;
    ssData<<strContent.c_str();
    boost::property_tree::read_json(ssData, pt);
    int iCount = std::atoi(pt.get<std::string>("data.count").c_str());
    if(iCount>0)
    {
        ptList=pt.get_child("data.list");
        for (auto& e : ptList) 
        {
            std::string name = e.second.get<std::string>("name");
            std::string title = e.second.get<std::string>("title");
            std::string type = e.second.get<std::string>("type");
            int capabilities =e.second.get<int>("capabilities");
            std::string project = e.second.get<std::string>("project");
            this->initServices(type,name,title,capabilities,project);
        }
    }
    return true;
}
bool DmpServerManager::initServices(const std::string& serverName, const std::string& serviceName, const std::string& title, int capabilities, const std::string& projectData)
{
    //project
    std::string projData;
    if (!DmpServerUtils::Base64Decode(projectData, &projData))
    {
        return false;
    }
    DmpProject *project = new DmpProject();
    if (!project->Read(projData))
    {
        delete project;
        return false;
    }

    if (!serverRegistry_->getServer(serverName)->publish(serviceName, title, capabilities, *project))
    {
        delete project;
        return false;
    }
    projects_[serviceName] = project;  
    return true;

}