2
* @file loadComponents.cpp
4
* This file loadComponents.cpp is created at Almende B.V. It is open-source software and part
5
* of the Common Hybrid Agent Platform (CHAP). A toolbox with a lot of open-source tools.
6
* Ranging from thread pools, and TCP/IP components to control architectures and learning
7
* algorithms. This software is published under the GNU Lesser General Public license,
9
* It is not possible to add usage restrictions to an open-source license. Nevertheless,
10
* we personally strongly object against this software used by the military, in the
11
* bio-industry, for animal experimentation, or anything that violates the Universal
12
* Declaration of Human Rights.
14
* @author Anne C. van Rossum
16
* @project Replicator FP7
17
* @company Almende B.V.
27
#include <dtUtil/log.h>
30
#include <srCore/loadComponents.h>
31
#include <srCore/pluginEntryPointBase.h>
35
/* **************************************************************************************
36
* Implementation of LoadComponents
37
* **************************************************************************************/
39
LoadComponents::LoadComponents() {
45
LoadComponents::~LoadComponents() {
46
LOG_INFO("Close all dynamically linked libraries/components (symbols become unavailable)");
47
std::vector<void *>::iterator i;
48
for (i = handles.begin(); i != handles.end(); i++) {
53
void LoadComponents::SetLibPath(const std::string &libpath) {
55
std::ostringstream msg; msg.clear(); msg.str("");
56
msg << "Set library path for additional plugins to " << libpath;
58
assert (boost::filesystem::is_directory(path));
62
* Get all the shared libraries (.so files) in the previously assigned path.
63
* Do not call this function before you have set SetLibPath.
65
void LoadComponents::GetLibraries() {
66
assert (!path.empty());
67
LOG_INFO("Try to find libraries in given library path");
68
std::ostringstream msg;
69
std::string file, extension;
70
boost::filesystem::directory_iterator i(path), dir_end;
71
for(; i != dir_end; ++i) {
73
extension = boost::filesystem::extension(*i);
74
if (std::string(extension).find(".so") != std::string::npos) {
75
msg.clear(); msg.str("");
76
msg << "Found file " << file;
78
file = path.string() + '/' + file;
79
boost::filesystem::complete(file);
80
library.push_back(file);
87
* Load additional components or more general, libraries, by searching through a given path that
88
* comes from the \<scenario.xml\> file.
89
* The plugins are loaded by dlopen and subsequently dlsym. The latter searches for a
90
* CreatePluginEntryPoint. Check if that field indeed exist in unmangeled state in the library
91
* to be loaded (by for example nm).
95
void LoadComponents::LoadLibs(dtGame::GameApplication &application) {
97
LOG_INFO("Load plugins");
98
std::ostringstream msg;
100
std::vector<std::string>::iterator i;
101
for (i = library.begin(); i < library.end(); i++) {
102
std::string lib = *i;
103
//open the plugin library
104
LOG_DEBUG("Open the library by \"dlopen\"");
105
void *handle = dlopen(lib.c_str(), RTLD_NOW);// RTLD_LAZY);
106
handles.push_back(handle);
108
msg.clear(); msg.str("");
109
msg << "Library " << dlerror();
110
LOG_ERROR(msg.str());
114
//load "CreatePluginEntryPoint" in the plugin library
115
LOG_DEBUG("Get entry point \"CreatePluginEntryPoint\"");
116
PluginEntryPointBase* (*entrypoint)();
117
entrypoint = (PluginEntryPointBase* (*)())dlsym(handle, "CreatePluginEntryPoint");
119
//tell that everything went well
121
if ((error = dlerror()) != NULL) {
122
msg.clear(); msg.str("");
123
msg << "No CreatePluginEntryPoint method found! (" << error << ")";
124
LOG_ERROR(msg.str());
128
//execute PluginEntryPointBase->StartPlugin
129
msg.clear(); msg.str("");
130
msg << "Run plugin " << lib;
132
(*entrypoint)()->StartPlugin(application);
135
std::vector<dtGame::GMComponent*> components;
136
application.GetGameManager()->GetAllComponents(components);
139
for(unsigned int i=0; i < components.size(); i++) {
140
msg.clear(); msg.str("");
141
msg << "Loaded component ";
142
msg << components[i]->GetName();
148
} // end of namespace srAlmende