正在显示
1 个修改的文件
包含
170 行增加
和
0 行删除
src/server/dmpserviceregistry.cpp
0 → 100644
1 | +/************************************************************************** | ||
2 | +* file: dmpserviceregistry.cpp | ||
3 | + | ||
4 | +* Author: wanzhongping | ||
5 | +* Date: 2020-12-23 15:53:11 | ||
6 | +* Email: zhongpingw@chinadci.com | ||
7 | +* copyright: 广州城市信息研究所有限公司 | ||
8 | +***************************************************************************/ | ||
9 | +#include "dmpserviceregistry.h" | ||
10 | +#include "dmpservice.h" | ||
11 | +#include <boost/format.hpp> | ||
12 | +#include <boost/algorithm/string.hpp> | ||
13 | + | ||
14 | +namespace | ||
15 | +{ | ||
16 | + | ||
17 | + string MakeServiceKey(const string &name, const string &version) | ||
18 | + { | ||
19 | + boost::format fmt = boost::format("%1%_%2%") % name % version; | ||
20 | + return fmt.str(); | ||
21 | + } | ||
22 | + | ||
23 | + bool IsVersionGreater(const string &v1, const string &v2) | ||
24 | + { | ||
25 | + vector<string> vecSeg1; | ||
26 | + boost::split(vecSeg1, v1, boost::is_any_of("."), boost::token_compress_on); | ||
27 | + vector<string> vecSeg2; | ||
28 | + boost::split(vecSeg2, v2, boost::is_any_of("."), boost::token_compress_on); | ||
29 | + | ||
30 | + vector<string>::iterator it1 = vecSeg1.begin(); | ||
31 | + vector<string>::iterator it2 = vecSeg2.begin(); | ||
32 | + bool isint; | ||
33 | + while (it1 != vecSeg1.end() && it2 != vecSeg2.end()) | ||
34 | + { | ||
35 | + if (*it1 != *it2) | ||
36 | + { | ||
37 | + // Compare as numbers | ||
38 | + int i1 = atoi(it1->c_str()); | ||
39 | + int i2 = atoi(it2->c_str()); | ||
40 | + if (i1 != i2) | ||
41 | + { | ||
42 | + return i1 > i2; | ||
43 | + } | ||
44 | + } | ||
45 | + ++it1; | ||
46 | + ++it2; | ||
47 | + } | ||
48 | + return false; | ||
49 | + } | ||
50 | +} // namespace | ||
51 | + | ||
52 | +DmpServiceRegistry::~DmpServiceRegistry() | ||
53 | +{ | ||
54 | + CleanUp(); | ||
55 | + std::cout << "Destructing DmpServiceRegistry" << std::endl; | ||
56 | +} | ||
57 | + | ||
58 | +void DmpServiceRegistry::Init(const boost::filesystem::path &module_path) | ||
59 | +{ | ||
60 | + nativeLoader_.LoadModules(module_path, *this); | ||
61 | +} | ||
62 | + | ||
63 | +std::shared_ptr<DmpService> DmpServiceRegistry::GetService(const string &name, const string &version) | ||
64 | +{ | ||
65 | + std::shared_ptr<DmpService> sp_service = nullptr; | ||
66 | + string key; | ||
67 | + string nameUpper = boost::to_upper_copy(name); | ||
68 | + VersionMap::const_iterator it = serviceVersions_.find(nameUpper); | ||
69 | + if (it != serviceVersions_.end()) | ||
70 | + { | ||
71 | + key = version.empty() ? it->second.second : MakeServiceKey(nameUpper, version); | ||
72 | + ServiceMap::const_iterator iter = services_.find(key); | ||
73 | + if (iter != services_.end()) | ||
74 | + { | ||
75 | + sp_service = iter->second; | ||
76 | + } | ||
77 | + else | ||
78 | + { | ||
79 | + // Return the default version | ||
80 | + //QgsMessageLog::logMessage(QString("Service %1 %2 not found, returning default").arg(name, version)); | ||
81 | + sp_service = services_[it->second.second]; | ||
82 | + } | ||
83 | + } | ||
84 | + else | ||
85 | + { | ||
86 | + //QgsMessageLog::logMessage(QString("Service %1 is not registered").arg(name)); | ||
87 | + } | ||
88 | + return sp_service; | ||
89 | +} | ||
90 | + | ||
91 | +void DmpServiceRegistry::RegisterService(std::shared_ptr<DmpService> service) | ||
92 | +{ | ||
93 | + string name = service->Name(); | ||
94 | + boost::to_upper(name); | ||
95 | + string version = service->Version(); | ||
96 | + | ||
97 | + string key = MakeServiceKey(name, version); | ||
98 | + if (services_.find(key) != services_.end()){ | ||
99 | + return; | ||
100 | + } | ||
101 | + services_[key] = service; | ||
102 | + if (serviceVersions_.find(name) == serviceVersions_.end()){ | ||
103 | + serviceVersions_.insert(pair<string, pair<string, string>>(name, pair<string, string>(version, key))); | ||
104 | + } | ||
105 | +} | ||
106 | + | ||
107 | +int DmpServiceRegistry::UnregisterService(const string &name, const string &version) | ||
108 | +{ | ||
109 | + int removed = 0; | ||
110 | + VersionMap::const_iterator it = serviceVersions_.find(name); | ||
111 | + if (it != serviceVersions_.end()) | ||
112 | + { | ||
113 | + if (version.empty()) | ||
114 | + { | ||
115 | + ServiceMap::iterator iter = services_.begin(); | ||
116 | + while (iter != services_.end()) | ||
117 | + { | ||
118 | + if (iter->second->Name() == name) | ||
119 | + { | ||
120 | + //QgsMessageLog::logMessage( QString( "Unregistering service %1 %2" ).arg( name, ( *it )->version() ) ); | ||
121 | + iter = services_.erase(iter); | ||
122 | + ++removed; | ||
123 | + } | ||
124 | + else | ||
125 | + { | ||
126 | + ++iter; | ||
127 | + } | ||
128 | + } | ||
129 | + serviceVersions_.erase(it); | ||
130 | + } | ||
131 | + else | ||
132 | + { | ||
133 | + string key = MakeServiceKey(name, version); | ||
134 | + ServiceMap::iterator found = services_.find(key); | ||
135 | + if (found != services_.end()) | ||
136 | + { | ||
137 | + //QgsMessageLog::logMessage( QString( "Unregistering service %1 %2" ).arg( name, version ) ); | ||
138 | + services_.erase(found); | ||
139 | + removed = 1; | ||
140 | + | ||
141 | + //findGreaterVersion为匿名函数 | ||
142 | + string maxVer; | ||
143 | + std::function<void(const ServiceMap::value_type &)> | ||
144 | + findGreaterVersion = [name, &maxVer](const ServiceMap::value_type &item) { | ||
145 | + if (item.second->Name() == name && | ||
146 | + (maxVer.empty() || IsVersionGreater(item.second->Version(), maxVer))) | ||
147 | + maxVer = item.second->Version(); | ||
148 | + }; | ||
149 | + | ||
150 | + serviceVersions_.erase(name); | ||
151 | + | ||
152 | + std::for_each(services_.begin(), services_.end(), findGreaterVersion); | ||
153 | + if (!maxVer.empty()) | ||
154 | + { | ||
155 | + // Set the new default service | ||
156 | + string key = MakeServiceKey(name, maxVer); | ||
157 | + serviceVersions_.insert(pair<string, pair<string, string>>(name, pair<string, string>(version, key))); | ||
158 | + } | ||
159 | + } | ||
160 | + } | ||
161 | + } | ||
162 | + return removed; | ||
163 | +} | ||
164 | + | ||
165 | +void DmpServiceRegistry::CleanUp() | ||
166 | +{ | ||
167 | + serviceVersions_.clear(); | ||
168 | + services_.clear(); | ||
169 | + nativeLoader_.UnloadModules(); | ||
170 | +} |
请
注册
或
登录
后发表评论