dmpmainserver.cpp 4.3 KB
/**************************************************************************
* file:              dmpmainserver.cpp

* Author:            wanzhongping
* Date:              2021-07-27 21:06:07
* Email:             zhongpingw@chinadci.com
* copyright:         广州城市信息研究所有限公司
***************************************************************************/
#include <iostream>
#include "dmpconfig.h"
#include "dmpmainserver.h"
#include "dmpserver.h"
#include <boost/filesystem.hpp>
#include <vector>
#include "dmpapplication.h"
#include "dmplogger.h"
#include "dmpfilterresponsedecorator.h"
#include "dmpservercontext.h"
#include "dmpserverapicontext.h"

using namespace std;

namespace fs = boost::filesystem;

bool DmpMainServer::initialized_ = false;
string DmpMainServer::module_path_ = "";
string DmpMainServer::service_path_ = "";
DmpServerManager *DmpMainServer::serverManager_ = nullptr;
DmpServerInterfaceImpl *DmpMainServer::server_interface_ = nullptr;

DmpMainServer::DmpMainServer()
{
    Init();
}

bool DmpMainServer::Init()
{
    DmpLogger::Instance()->Init(DmpApplication::Instance()->libexecPath() + "log.config");
    if (initialized_)
    {
        return false;
    }
    LOGGER_INFO("-------------------dmap server init start--------------------");
    service_path_ = DmpApplication::Instance()->pkgDataPath() + "services";
    serverManager_ = new DmpServerManager();
    server_interface_ = new DmpServerInterfaceImpl();
    // Load service module
    module_path_ = DmpApplication::Instance()->libexecPath() + "server";
    LOGGER_DEBUG("Initializing server modules from " + module_path_);
    serverManager_->init(module_path_);

#ifdef HAVE_SERVER_PYTHON_PLUGINS
	InitPython();
#endif

    initialized_ = true;
    LOGGER_DEBUG("------------------dmap server init end-------------------");
    return true;
}

void DmpMainServer::HandleRequest(DmpServerRequest &request, DmpServerResponse &response)
{
    LOGGER_DEBUG("DmpMainServer: HandleRequest"); 
    DmpFilterResponseDecorator responseDecorator(server_interface_->Filters(), response);
    DmpRequestHandler requestHandler(request, response);
    try
    {
        requestHandler.ParseInput();
    }
    catch (std::exception &ex)
    {
        LOGGER_ERROR("Parse input exception: "); 
    }

    // Set the request handler into the interface for plugins to manipulate it
    server_interface_->SetRequestHandler(&requestHandler);

    try
    {
        responseDecorator.start();
    }
    catch (std::exception &ex)
    {
        response.sendError(500, "Internal Server Error");
        LOGGER_ERROR(ex.what());
    }
    const DmpServerParameters params = request.ServerParameters();
    std::shared_ptr<DmpServerApi> api = nullptr;
    if ( params.Service().empty())
    {
        if(api = serverManager_->apiForRequest(request)) {
            DmpServerApiContext context {&request, &responseDecorator, serverManager_};
            api->executeRequest(context);
        }
        else {
            LOGGER_DEBUG("DmpMainServer: getCapabilities"); 
            std::string capsJson = serverManager_->getCapabilities();
            response.writeJson(capsJson);  
        }
    }
    else 
    {
        std::shared_ptr<DmpServer> server = serverManager_->serverForRequest(request);
        if (server) {
            try {
                server->executeRequest(request,responseDecorator);
            }
            catch (std::exception ex) {

                response.sendError(500, "Internal Server Error");
                LOGGER_ERROR(ex.what());
            }
        }
        else {
            std::string capsJson = serverManager_->getCapabilities();
            response.writeJson(capsJson);
        }
    }

    try
    {
        responseDecorator.finish();
    }
    catch (std::exception &ex)
    {
        response.sendError(500, "Internal Server Error");
        LOGGER_CRITICAL(ex.what());
    }
}

#ifdef HAVE_SERVER_PYTHON_PLUGINS
void DmpMainServer::InitPython()
{
    // Init plugins
    if (!DmpServerPlugins::InitPlugins(server_interface_))
    {
        LOGGER_WARN("No server python plugins are available");
    }
    else
    {
        LOGGER_WARN("Server python plugins loaded");
    }
}
#endif