dmpmainserver.cpp 4.0 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"
#include "dmpserverplugins.h"

using namespace std;

namespace fs = boost::filesystem;

bool DmpMainServer::initialized_ = false;
string DmpMainServer::modulePath_ = "";
string DmpMainServer::servicePath_ = "";
DmpServerManager *DmpMainServer::serverManager_ = nullptr;
DmpServerInterfaceImpl *DmpMainServer::serverInterface_ = 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--------------------");
    servicePath_ = DmpApplication::Instance()->pkgDataPath() + "services";
    serverManager_ = new DmpServerManager();
    serverInterface_ = new DmpServerInterfaceImpl();
    // Load service module
    modulePath_ = DmpApplication::Instance()->libexecPath() + "server";
    LOGGER_DEBUG("Initializing server modules from " + modulePath_);
    serverManager_->init(modulePath_);

#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(serverInterface_->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
    serverInterface_->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;
    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 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);
    }


    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(serverInterface_))
    {
        LOGGER_WARN("No server python plugins are available");
    }
    else
    {
        LOGGER_WARN("Server python plugins loaded");
    }
}
#endif