dmpserverregistry.cpp
5.6 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**************************************************************************
* file: dmpserverregistry.cpp
* Author: wanzhongping
* Date: 2021-10-29 23:34:36
* Email: zhongpingw@chinadci.com
* copyright: 广州城市信息研究所有限公司
***************************************************************************/
#include "dmpserverregistry.h"
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/system/error_code.hpp>
#include "boost/typeof/typeof.hpp"
#include "boost/optional.hpp"
#include <map>
#include <string>
#include "dmpserver.h"
#include "dmpconfig.h"
using namespace std;
DmpServerRegistry::DmpServerRegistry()
{
}
DmpServerRegistry::~DmpServerRegistry()
{
cleanUp();
std::cout << "Destructing DmpServiceRegister" << std::endl;
}
void DmpServerRegistry::init(const boost::filesystem::path &module_path)
{
loader_.loadModules(module_path, *this);
}
std::shared_ptr<DmpServer> DmpServerRegistry::getServer(const string &name)
{
std::shared_ptr<DmpServer> sp_server = nullptr;
ServerMap::const_iterator iter = servers_.find(name);
if (iter != servers_.end())
{
sp_server = iter->second;
}
return sp_server;
}
void DmpServerRegistry::registerServer(std::shared_ptr<DmpServer> server)
{
string name = server->name();
if (servers_.find(name) != servers_.end()) {
return;
}
servers_[name] = server;
}
int DmpServerRegistry::unregisterServer(const string &name)
{
int removed = 0;
// VersionMap::const_iterator it = serviceVersions_.find(name);
// if (it != serviceVersions_.end())
// {
// if (version.empty())
// {
// ServiceMap::iterator iter = services_.begin();
// while (iter != services_.end())
// {
// if (iter->second->name() == name)
// {
// iter = services_.erase(iter);
// ++removed;
// }
// else
// {
// ++iter;
// }
// }
// serviceVersions_.erase(it);
// }
// else
// {
// string key = MakeServerKey(name, version);
// ServiceMap::iterator found = services_.find(key);
// if (found != services_.end())
// {
// services_.erase(found);
// removed = 1;
// //findGreaterVersion为匿名函数
// string maxVer;
// std::function<void(const ServiceMap::value_type &)>
// findGreaterVersion = [name, &maxVer](const ServiceMap::value_type &item) {
// if (item.second->name() == name &&
// (maxVer.empty() || IsVersionGreater(item.second->version(), maxVer)))
// maxVer = item.second->version();
// };
// serviceVersions_.erase(name);
// std::for_each(services_.begin(), services_.end(), findGreaterVersion);
// if (!maxVer.empty())
// {
// // Set the new default service
// string key = MakeServerKey(name, maxVer);
// serviceVersions_.insert(pair<string, pair<string, string>>(name, pair<string, string>(version, key)));
// }
// }
// }
// }
return removed;
}
void DmpServerRegistry::cleanUp()
{
servers_.clear();
loader_.unloadModules();
}
std::shared_ptr<DmpServer> DmpServerRegistry::getServerForRequest(const DmpServerRequest &request)
{
for (const auto &server : servers_)
{
if(server.second->accept(request.path()))
{
return server.second;
}
}
return nullptr;
}
void DmpServerRegistry::registerApi(std::shared_ptr<DmpServerApi> api)
{
string name = api->name();
if (apis_.find(name) != apis_.end()) {
return;
}
apis_[name] = api;
}
std::shared_ptr<DmpServerApi> DmpServerRegistry::getApiForRequest(const DmpServerRequest &request)
{
for (const auto &api : apis_)
{
if(api.second->accept(request.path()))
{
return api.second;
}
}
return nullptr;
}
std::string DmpServerRegistry::getCapabilities()
{
boost::property_tree::ptree root;
boost::property_tree::ptree ptServers;
root.put("product", "DMap Server");
root.put("version", VERSION);
for (const auto &server : servers_) {
std::shared_ptr<DmpServer> pserver = server.second;
boost::property_tree::ptree ptServer, ptVals;
std::string caps = pserver->capability();
std::vector<std::string> vec;
boost::split(vec, caps, boost::is_any_of(","), boost::token_compress_on);
for(const auto &cap : vec)
{
boost::property_tree::ptree ptValItem;
ptValItem.put_value(cap);
ptVals.push_back(std::make_pair("", ptValItem));
}
ptServer.add_child(pserver->name(), ptVals);
ptServers.push_back(std::make_pair("", ptServer));
}
root.add_child(std::string("servers"), ptServers);
std::stringstream ss;
boost::property_tree::write_json(ss, root);
return ss.str();
}