dmpserverloader.cpp
2.3 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
/**************************************************************************
* file: dmpservernativeloader.cpp
* Author: wanzhongping
* Date: 2020-12-24 16:01:21
* Email: zhongpingw@chinadci.com
* copyright: 广州城市信息研究所有限公司
***************************************************************************/
#include "dmpserverloader.h"
#include "dmpservermodule.h"
#include <boost/dll/import.hpp>
#include <dlfcn.h>
#include "dmpserverutils.h"
std::shared_ptr<DmpServerModule> DmpServerLoader::loadModule(const std::string &location)
{
std::shared_ptr<DmpServerModule> sp_module = findModule(location);
if (sp_module)
{
return sp_module;
}
boost::dll::fs::error_code error;
std::shared_ptr<boost::dll::shared_library> lib = std::make_shared<boost::dll::shared_library>(location, error);
if (error)
{
return nullptr;
}
if (lib->has("server_module"))
{
sp_module = boost::move(lib)->get_alias<std::shared_ptr<DmpServerModule>()>("server_module")();
if (sp_module)
{
//保存lib的引用,否则sp_module会被释放掉
sp_module->set_lib(boost::move(lib));
modules_[location] = sp_module;
return sp_module;
}
}
return nullptr;
}
void DmpServerLoader::loadModules(const boost::filesystem::path &module_path,
DmpServerRegistry ®istry)
{
std::vector<boost::filesystem::path> locations;
DmpServerUtils::GetLibraryPaths(module_path, locations);
std::vector<boost::filesystem::path>::iterator iter;
for (iter = locations.begin(); iter != locations.end(); iter++)
{
std::shared_ptr<DmpServerModule> sp_module = loadModule(iter->string());
if (sp_module)
{
sp_module->registerSelf(registry);
}
}
}
std::shared_ptr<DmpServerModule> DmpServerLoader::findModule(const std::string &location)
{
ModuleMap::iterator item = modules_.find(location);
if (item != modules_.end())
{
return item->second;
}
return nullptr;
}
void DmpServerLoader::unloadModules()
{
modules_.clear();
}