dmpserver.cpp
3.4 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
/**************************************************************************
* 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