提交 21c518598bfb39e91785ff2f8ae8d53a50f6face

作者 LJH 李佳桓
1 个父辈 6194c243

add

正在显示 1 个修改的文件 包含 136 行增加0 行删除
  1 +/**************************************************************************
  2 +* file: dmpserver.cpp
  3 +
  4 +* Author: wanzhongping
  5 +* Date: 2020-12-07 14:07:49
  6 +* Email: zhongpingw@chinadci.com
  7 +* copyright: 广州城市信息研究所有限公司
  8 +***************************************************************************/
  9 +#include <iostream>
  10 +#include "dmpconfig.h"
  11 +#include "dmpserver.h"
  12 +#include "dmpservice.h"
  13 +#include <boost/filesystem.hpp>
  14 +#include <vector>
  15 +#include "dmpapplication.h"
  16 +#include "dmplogger.h"
  17 +#include "dmpfilterresponsedecorator.h"
  18 +#include "dmpserverplugins.h"
  19 +
  20 +using namespace std;
  21 +
  22 +namespace fs = boost::filesystem;
  23 +
  24 +bool DmpServer::initialized_ = false;
  25 +DmpServiceRegistry *DmpServer::service_registry_ = nullptr;
  26 +DmpServerInterfaceImpl *DmpServer::server_interface_ = nullptr;
  27 +
  28 +DmpServer::DmpServer()
  29 +{
  30 + Init();
  31 +}
  32 +
  33 +bool DmpServer::Init()
  34 +{
  35 + if (initialized_)
  36 + {
  37 + return false;
  38 + }
  39 +
  40 + LOGGER_INFO("dmap server init...");
  41 +
  42 + DmpApplication::Init();
  43 +
  44 + service_registry_ = new DmpServiceRegistry();
  45 +
  46 + server_interface_ = new DmpServerInterfaceImpl(service_registry_);
  47 + // server_interface_ = new DmpServerInterfaceImpl();
  48 + // Load service module
  49 + string modulePath = DmpApplication::libexec_path() + "server";
  50 + LOGGER_DEBUG("Initializing server modules from " + modulePath);
  51 + service_registry_->Init(modulePath);
  52 +
  53 +#ifdef HAVE_SERVER_PYTHON_PLUGINS
  54 + InitPython();
  55 +#endif
  56 +
  57 + initialized_ = true;
  58 + return true;
  59 +}
  60 +
  61 +void DmpServer::HandleRequest(DmpServerRequest &request, DmpServerResponse &response)
  62 +{
  63 + // Pass the filters to the requestHandler, this is needed for the following reasons:
  64 + // Allow server request to call sendResponse plugin hook if enabled
  65 + DmpFilterResponseDecorator responseDecorator(server_interface_->Filters(), response);
  66 +
  67 + //Request handler
  68 + DmpRequestHandler requestHandler(request, response);
  69 + try
  70 + {
  71 + requestHandler.ParseInput();
  72 + }
  73 + catch (std::exception &ex)
  74 + {
  75 + LOGGER_ERROR("Parse input exception: ");
  76 + }
  77 +
  78 + // Set the request handler into the interface for plugins to manipulate it
  79 + server_interface_->SetRequestHandler(&requestHandler);
  80 +
  81 + try
  82 + {
  83 + responseDecorator.Start();
  84 + }
  85 + catch (std::exception &ex)
  86 + {
  87 + response.SendError(500, "Internal Server Error");
  88 + LOGGER_ERROR(ex.what());
  89 + }
  90 +
  91 + DmpServerParameters params = request.ServerParameters();
  92 + std::shared_ptr<DmpService> service = service_registry_->GetService(params.Service(), params.Version());
  93 + if (service)
  94 + {
  95 + try
  96 + {
  97 + service->ExecuteRequest(request, response);
  98 + }
  99 + catch (std::exception ex)
  100 + {
  101 + // Internal server error
  102 + response.SendError(500, "Internal Server Error");
  103 + LOGGER_ERROR(ex.what());
  104 + }
  105 + }
  106 + else
  107 + {
  108 + response.SendError(500, "erro, not found module");
  109 + }
  110 +
  111 + try
  112 + {
  113 + responseDecorator.Finish();
  114 + }
  115 + catch (std::exception &ex)
  116 + {
  117 + // Internal server error
  118 + response.SendError(500, "Internal Server Error");
  119 + LOGGER_CRITICAL(ex.what());
  120 + }
  121 +}
  122 +
  123 +#ifdef HAVE_SERVER_PYTHON_PLUGINS
  124 +void DmpServer::InitPython()
  125 +{
  126 + // Init plugins
  127 + if (!DmpServerPlugins::InitPlugins(server_interface_))
  128 + {
  129 + LOGGER_WARN("No server python plugins are available");
  130 + }
  131 + else
  132 + {
  133 + LOGGER_WARN("Server python plugins loaded");
  134 + }
  135 +}
  136 +#endif
注册登录 后发表评论