dmpmainserver.cpp
4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**************************************************************************
* 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