RSC  0.19.0
Plugin.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project.
4  *
5  * Copyright (C) 2012-2018 Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "Plugin.h"
28 
29 #include <errno.h>
30 #include <string.h>
31 
32 #if defined(_WIN32)
33 #include <windows.h>
34 #endif
35 
36 #if defined(__linux__) or defined(__APPLE__)
37 #include <dlfcn.h>
38 #endif
39 
40 #include <stdexcept>
41 
42 #include <boost/format.hpp>
43 
44 #include "../logging/Logger.h"
45 #include "../logging/LoggerFactory.h"
46 
47 using namespace std;
48 
49 using namespace boost;
50 
51 namespace rsc {
52 namespace plugins {
53 
54 const std::string PLUGIN_INIT_SYMBOL = "rsc_plugin_init";
55 const std::string PLUGIN_SHUTDOWN_SYMBOL = "rsc_plugin_shutdown";
56 
57 class Impl {
58 public:
59  Impl(const std::string& name,
60  const std::string& library)
61  : logger(logging::LoggerFactory::getInstance()
62  .getLogger(str((format("rsc.plugins.Plugin[%1%]")
63  % name)))),
64  name(name), library(library),
65  loaded(false), handle(NULL),
66  init(NULL), shutdown(NULL) {
67  }
68 
69  const string& getName() const {
70  return this->name;
71  }
72 
73  const string& getLibrary() const {
74  return this->library;
75  }
76 
77  void load(bool wrapExceptions) {
78 
79  if (this->loaded) {
80  throw runtime_error(
81  boost::str(
82  boost::format(
83  "Plugin %1% is already loaded. Cannot load it again,")
84  % this->name));
85  }
86 
87  RSCINFO(this->logger, "Trying to load library `" << this->library << "'");
88 
89  // Load the library containing the plugin.
90  loadLibrary();
91 
92  // Lookup init and shutdown functions in the plugin library.
93  this->init
94  = reinterpret_cast<InitFunction>(resolveSymbol(PLUGIN_INIT_SYMBOL));
95  this->shutdown
96  = reinterpret_cast<ShutdownFunction>(resolveSymbol(PLUGIN_SHUTDOWN_SYMBOL));
97 
98  // Initialize the plugin.
99  RSCINFO(this->logger, "Initializing");
100  if (wrapExceptions) {
101  try {
102  this->init();
103  this->loaded = true;
104  } catch (const std::exception& e) {
105  throw runtime_error(str(format("Plugin `%1%' failed to initialize: %2%")
106  % this->name
107  % e.what()));
108  } catch (...) {
109  throw runtime_error(str(format("Plugin `%1%' failed to initialize due to unknown error.")
110  % this->name));
111  }
112  } else {
113  this->init();
114  this->loaded = true;
115  }
116  }
117 
118  void unload(bool wrapExceptions) {
119 
120  if (!loaded) {
121  throw runtime_error(
122  str(format("Plugin `%1%' failed cannot be unloaded because it has not been loaded correctly.")
123  % this->name));
124  }
125 
126  // Shut the plugin down.
127  RSCINFO(this->logger, "Shutting down");
128  this->loaded = false;
129 
130  if (wrapExceptions) {
131  try {
132  this->shutdown();
133  } catch (const std::exception& e) {
134  throw runtime_error(str(format("Plugin `%1%' failed to shutdown: %2%")
135  % this->name
136  % e.what()));
137  } catch (...) {
138  throw runtime_error(str(format("Plugin `%1%' failed to shutdown due to unknown error.")
139  % this->name));
140  }
141  } else {
142  this->shutdown();
143  }
144  }
145 
146  bool ensureLoaded(bool wrapExceptions) {
147  if (this->loaded) {
148  return false;
149  }
150  load(wrapExceptions);
151  return true;
152  }
153 private:
154  typedef void (*InitFunction)();
155  typedef void (*ShutdownFunction)();
156 
158 
159  string name;
160  string library;
161 
162  bool loaded;
163 
164 #if defined(_WIN32)
165  HMODULE handle;
166 #else
167  void* handle;
168 #endif
169  InitFunction init;
170  ShutdownFunction shutdown;
171 
172  void loadLibrary() {
173 #if defined(__linux__) || defined(__APPLE__)
174  if (!(this->handle = dlopen(this->library.c_str(), RTLD_NOW))) {
175  const char* result = dlerror();
176  throw runtime_error(str(format("Failed to load plugin `%1%' from shared object `%2%': %3%.")
177  % this->name % this->library
178  % (result ? result : "<unknown error>")));
179  }
180 #elif defined(_WIN32)
181  if (!(this->handle= LoadLibrary(this->library.c_str()))) {
182  throw runtime_error(str(format("Failed to load plugin `%1%' from shared object `%2%': %3%.")
183  % this->name % this->library % GetLastError()));
184  }
185 #else
186  throw runtime_error("Plugins are not implemented for this platform.");
187 #endif
188  }
189 
190  void* resolveSymbol(const string& name) {
191  RSCINFO(this->logger, "Resolving symbol `"
192  << name << "' in library `" << this->library << "'");
193 
194  assert(this->handle);
195 
196  void *address;
197 #if defined(__linux__) || defined(__APPLE__)
198  if (!(address = dlsym(this->handle, name.c_str()))) {
199  const char* result = dlerror();
200  throw runtime_error(str(format("Plugin `%1%' failed to define function `%2%': %3%")
201  % this->name % name
202  % (result ? result : "<unknown error>")));
203  }
204 #elif defined(_WIN32)
205  if (!(address = reinterpret_cast<void*>(GetProcAddress(this->handle, name.c_str())))) {
206  throw runtime_error(str(format("Plugin `%1%' failed to define function `%2%': %3%")
207  % this->name % name % GetLastError()));
208  }
209 #else
210  throw runtime_error("Plugins are not implemented for this platform.");
211 #endif
212  return address;
213  }
214 };
215 
216 Plugin::Plugin(Impl* impl)
217  : impl(impl) {
218 }
219 
221 }
222 
223 const string& Plugin::getName() const {
224  return this->impl->getName();
225 }
226 
227 void Plugin::load(bool wrapExceptions) {
228  this->impl->load(wrapExceptions);
229 }
230 
231 void Plugin::unload(bool wrapExceptions) {
232  this->impl->unload(wrapExceptions);
233 }
234 
235 bool Plugin::ensureLoaded(bool wrapExceptions) {
236  return this->impl->ensureLoaded(wrapExceptions);
237 }
238 
239 string Plugin::getLibrary() const {
240  return this->impl->getLibrary();
241 }
242 
243 PluginPtr Plugin::create(const std::string& name, const std::string& library) {
244  return PluginPtr(new Plugin(new Impl(name, library)));
245 }
246 
247 }
248 }
InitFunction init
Definition: Plugin.cpp:169
void unload(bool wrapExceptions)
Definition: Plugin.cpp:118
STL namespace.
const std::string & getName() const
Returns the name of the plugin.
Definition: Plugin.cpp:223
const string & getName() const
Definition: Plugin.cpp:69
virtual ~Plugin()
Definition: Plugin.cpp:220
Impl(const std::string &name, const std::string &library)
Definition: Plugin.cpp:59
const std::string PLUGIN_INIT_SYMBOL
Definition: Plugin.cpp:54
void load(bool wrapExceptions)
Definition: Plugin.cpp:77
void * resolveSymbol(const string &name)
Definition: Plugin.cpp:190
std::string getLibrary() const
Returns the path to the library implementing this plugin.
Definition: Plugin.cpp:239
rsc::logging::LoggerPtr logger
Definition: Plugin.cpp:157
bool ensureLoaded(bool wrapExceptions)
Definition: Plugin.cpp:146
static boost::shared_ptr< Plugin > create(const std::string &name, const std::string &library)
Definition: Plugin.cpp:243
bool ensureLoaded(bool wrapExceptions=true)
Tries to load the plugin unless it is already loaded.
Definition: Plugin.cpp:235
Plugin(Impl *impl)
Definition: Plugin.cpp:216
LoggerPtr getLogger()
#define RSCINFO(logger, msg)
Definition: Logger.h:224
const std::string PLUGIN_SHUTDOWN_SYMBOL
Definition: Plugin.cpp:55
void load(bool wrapExceptions=true)
Tries to load the functionality of the plugin into the current process.
Definition: Plugin.cpp:227
ShutdownFunction shutdown
Definition: Plugin.cpp:170
boost::shared_ptr< Plugin > PluginPtr
Definition: Plugin.h:119
void loadLibrary()
Definition: Plugin.cpp:172
void unload(bool wrapExceptions=true)
Tries to unload the functionality of the plugin.
Definition: Plugin.cpp:231
boost::scoped_ptr< Impl > impl
Definition: Plugin.h:111
const string & getLibrary() const
Definition: Plugin.cpp:73
boost::shared_ptr< Logger > LoggerPtr
Definition: Logger.h:41