Blue Brain BioExplorer
PluginManager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  * All rights reserved. Do not distribute without permission.
4  * Responsible Author: Juan Hernando <juan.hernando@epfl.ch>
5  *
6  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 3.0 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include "PluginManager.h"
23 
24 #include <Defines.h>
25 
29 
31 #ifdef USE_NETWORKING
33 #endif
34 
35 namespace
36 {
37 bool containsString(const int length, const char** input, const char* toFind)
38 {
39  return std::count_if(input, input + length, [toFind](const char* arg) { return std::strcmp(arg, toFind) == 0; }) >
40  0;
41 }
42 } // namespace
43 
44 namespace core
45 {
46 typedef ExtensionPlugin* (*CreateFuncType)(int, const char**);
47 
48 PluginManager::PluginManager(int argc, const char** argv)
49 {
50  const bool help = containsString(argc, argv, "--help");
51 
52  for (int i = 0; i < argc; ++i)
53  {
54  if (std::strcmp(argv[i], "--plugin") != 0)
55  continue;
56  if (++i == argc || argv[i][0] == '\0' || argv[i][0] == '-')
57  {
58  // Do not print anything here, errors will be reported later
59  // during option parsing
60  continue;
61  }
62 
63  std::string str(argv[i]);
64  string_utils::trim(str);
65  auto words = string_utils::split(str, ' ');
66 
67  if (help)
68  words.push_back("--help");
69 
70  const char* name = words.front().c_str();
71  std::vector<const char*> args;
72  for (const auto& w : words)
73  args.push_back(w.c_str());
74 
75  _loadPlugin(name, args.size(), args.data());
76  }
77 }
78 
80 {
81  // Rockets plugin cannot be initialized until we have the command line
82  // parameters
83  auto& parameters = api->getParametersManager();
84  auto& appParameters = parameters.getApplicationParameters();
85 
86  const bool haveHttpServerURI = !appParameters.getHttpServerURI().empty();
87 
88  if (haveHttpServerURI)
89 #ifdef USE_NETWORKING
90  // Since the Rockets plugin provides the ActionInterface, it must be
91  // initialized before anything else
92  _extensions.insert(_extensions.begin(), std::make_unique<RocketsPlugin>());
93 #else
94  throw std::runtime_error(
95  "CORE_NETWORKING_ENABLED was not set, but HTTP server URI "
96  "was specified");
97 #endif
98 
99  for (const auto& extension : _extensions)
100  {
101  extension->_api = api;
102  extension->init();
103  }
104 }
105 
107 {
108  _extensions.clear();
109  _libs.clear();
110 }
111 
113 {
114  for (const auto& extension : _extensions)
115  extension->preRender();
116 }
117 
119 {
120  for (const auto& extension : _extensions)
121  extension->postRender();
122 }
123 
124 void PluginManager::_loadPlugin(const char* name, int argc, const char* argv[])
125 {
126  try
127  {
128  DynamicLib library(name);
129  auto createSym = library.getSymbolAddress("core_plugin_create");
130  if (!createSym)
131  {
132  throw std::runtime_error(std::string("Plugin '") + name + "' is not a valid Core plugin; missing " +
133  "core_plugin_create()");
134  }
135 
136  CreateFuncType createFunc = (CreateFuncType)createSym;
137  if (auto plugin = createFunc(argc, argv))
138  {
139  _extensions.emplace_back(plugin);
140  _libs.push_back(std::move(library));
141  CORE_INFO("Loaded plugin '" << name << "'");
142  }
143  }
144  catch (const std::runtime_error& exc)
145  {
146  CORE_ERROR(exc.what());
147  }
148 }
149 } // namespace core
const std::string & getHttpServerURI() const
PLATFORM_API ApplicationParameters & getApplicationParameters()
virtual ParametersManager & getParametersManager()=0
PluginManager(int argc, const char **argv)
Constructor.
void initPlugins(PluginAPI *api)
void trim(std::string &s)
std::vector< std::string > split(const std::string &s, char delim)
Engine *(* CreateFuncType)(int, const char **, ParametersManager &)
#define CORE_INFO(__msg)
Definition: Logs.h:33
#define CORE_ERROR(__msg)
Definition: Logs.h:31