dmpserver.cpp 3.4 KB
/**************************************************************************
* file:              dmpserver.cpp

* Author:            wanzhongping
* Date:              2020-12-07 14:07:49
* Email:             zhongpingw@chinadci.com
* copyright:         广州城市信息研究所有限公司
***************************************************************************/
#include <iostream>
#include "dmpconfig.h"
#include "dmpserver.h"
#include "dmpservice.h"
#include <boost/filesystem.hpp>
#include <vector>
#include "dmpapplication.h"
#include "dmplogger.h"
#include "dmpfilterresponsedecorator.h"
#include "dmpserverplugins.h"

using namespace std;

namespace fs = boost::filesystem;

bool DmpServer::initialized_ = false;
DmpServiceRegistry *DmpServer::service_registry_ = nullptr;
DmpServerInterfaceImpl *DmpServer::server_interface_ = nullptr;

DmpServer::DmpServer()
{
    Init();
}

bool DmpServer::Init()
{
    if (initialized_)
    {
        return false;
    }

    LOGGER_INFO("dmap server init...");

    DmpApplication::Init();

    service_registry_ = new DmpServiceRegistry();

    server_interface_ = new DmpServerInterfaceImpl(service_registry_);
    // server_interface_ = new DmpServerInterfaceImpl();
    // Load service module
    string modulePath = DmpApplication::libexec_path() + "server";
    LOGGER_DEBUG("Initializing server modules from " + modulePath);
    service_registry_->Init(modulePath);

#ifdef HAVE_SERVER_PYTHON_PLUGINS
	InitPython();
#endif

    initialized_ = true;
    return true;
}

void DmpServer::HandleRequest(DmpServerRequest &request, DmpServerResponse &response)
{
    // Pass the filters to the requestHandler, this is needed for the following reasons:
    // Allow server request to call sendResponse plugin hook if enabled
    DmpFilterResponseDecorator responseDecorator(server_interface_->Filters(), response);

    //Request handler
    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());
    }

    DmpServerParameters params = request.ServerParameters();
    std::shared_ptr<DmpService> service = service_registry_->GetService(params.Service(), params.Version());
    if (service)
    {
        try
        {
            service->ExecuteRequest(request, response);
        }
        catch (std::exception ex)
        {
            // Internal server error
            response.SendError(500, "Internal Server Error");
            LOGGER_ERROR(ex.what());
        }
    }
    else
    {
        response.SendError(500, "erro, not found module");
    }

    try
    {
        responseDecorator.Finish();
    }
    catch (std::exception &ex)
    {
        // Internal server error
        response.SendError(500, "Internal Server Error");
        LOGGER_CRITICAL(ex.what());
    }
}

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