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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
/************************************************************************
*
* Copyright (C) 2009-2024 IRCAD France
* Copyright (C) 2012-2020 IHU Strasbourg
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#include "core/runtime/detail/module.hpp"
#include "core/runtime/detail/empty_plugin.hpp"
#include "core/runtime/detail/extension_point.hpp"
#include "core/runtime/detail/profile/profile.hpp"
#include "core/runtime/detail/runtime.hpp"
#include "core/runtime/executable.hpp"
#include "core/runtime/executable_factory.hpp"
#include "core/runtime/extension.hpp"
#include "core/runtime/utils/generic_executable_factory.hpp"
#include <core/base.hpp>
#include <algorithm>
#include <cassert>
#include <exception>
#include <memory>
namespace sight::core::runtime::detail
{
//------------------------------------------------------------------------------
SPTR(module) module::s_loading_module;
//------------------------------------------------------------------------------
SPTR(module) module::get_loading_module()
{
return s_loading_module;
}
//------------------------------------------------------------------------------
module::module(
const std::filesystem::path& _location,
std::string _id,
std::string _plugin_class,
int _priority
) :
m_resources_location(std::filesystem::weakly_canonical(_location)),
m_identifier(std::move(_id)),
m_class(std::move(_plugin_class)),
m_priority(_priority)
{
// Post-condition.
SIGHT_ASSERT("Invalid module location.", m_resources_location.is_absolute() == true);
const auto location_normalized = std::filesystem::weakly_canonical(_location);
const auto str_location = std::regex_replace(
location_normalized.string(),
runtime::MATCH_MODULE_PATH,
MODULE_LIB_PREFIX
);
// This may fail if the module does not contain any library, so we ignore the returned error
m_library_location = std::filesystem::weakly_canonical(std::filesystem::path(str_location));
}
//------------------------------------------------------------------------------
void module::add_executable_factory(SPTR(executable_factory)_factory)
{
m_executable_factories.insert(_factory);
}
//------------------------------------------------------------------------------
module::executable_factory_const_iterator module::executable_factories_begin() const
{
return m_executable_factories.begin();
}
//------------------------------------------------------------------------------
module::executable_factory_const_iterator module::executable_factories_end() const
{
return m_executable_factories.end();
}
//------------------------------------------------------------------------------
SPTR(executable_factory) module::find_executable_factory(const std::string& _type) const
{
std::shared_ptr<executable_factory> res_executable_factory;
for(const auto& factory : m_executable_factories)
{
if(factory->type() == _type)
{
res_executable_factory = factory;
break;
}
}
return res_executable_factory;
}
//------------------------------------------------------------------------------
void module::add_extension(SPTR(extension)_extension)
{
m_extensions.insert(_extension);
}
//------------------------------------------------------------------------------
bool module::has_extension(const std::string& _identifier) const
{
bool has_extension = false;
for(const auto& extension : m_extensions)
{
if(extension->identifier() == _identifier)
{
has_extension = true;
break;
}
}
return has_extension;
}
//------------------------------------------------------------------------------
void module::set_enable_extension(const std::string& _identifier, const bool _enable)
{
for(const auto& extension : m_extensions)
{
if(extension->identifier() == _identifier)
{
extension->set_enable(_enable);
break; // The identifier is unique => can break the loop
}
}
}
//------------------------------------------------------------------------------
module::extension_const_iterator module::extensions_begin() const
{
return m_extensions.begin();
}
//------------------------------------------------------------------------------
module::extension_const_iterator module::extensions_end() const
{
return m_extensions.end();
}
//------------------------------------------------------------------------------
void module::add_extension_point(SPTR(extension_point)_extension_point)
{
m_extension_points.insert(_extension_point);
}
//------------------------------------------------------------------------------
SPTR(extension_point) module::find_extension_point(const std::string& _identifier) const
{
std::shared_ptr<extension_point> res_extension_point;
for(const auto& extension_point : m_extension_points)
{
if(extension_point->identifier() == _identifier && extension_point->enabled())
{
res_extension_point = extension_point;
break;
}
}
return res_extension_point;
}
//------------------------------------------------------------------------------
bool module::has_extension_point(const std::string& _identifier) const
{
bool has_extension_point = false;
for(const auto& extension_point : m_extension_points)
{
if(extension_point->identifier() == _identifier)
{
has_extension_point = true;
break;
}
}
return has_extension_point;
}
//------------------------------------------------------------------------------
void module::set_enable_extension_point(const std::string& _identifier, const bool _enable)
{
for(const auto& extension_point : m_extension_points)
{
if(extension_point->identifier() == _identifier)
{
extension_point->set_enable(_enable);
break;
}
}
}
//------------------------------------------------------------------------------
module::extension_point_const_iterator module::extension_points_begin() const
{
return m_extension_points.begin();
}
//------------------------------------------------------------------------------
module::extension_point_const_iterator module::extension_points_end() const
{
return m_extension_points.end();
}
//------------------------------------------------------------------------------
void module::set_library(SPTR(dl::library)_library)
{
_library->set_search_path(this->get_library_location());
m_library = _library;
}
//------------------------------------------------------------------------------
void module::add_requirement(const std::string& _requirement)
{
m_requirements.insert(_requirement);
}
//------------------------------------------------------------------------------
std::string module::get_class() const
{
return m_class;
}
//------------------------------------------------------------------------------
const std::string& module::identifier() const
{
return m_identifier;
}
//------------------------------------------------------------------------------
std::string module::get_library_name() const
{
return m_library ? m_library->name().string() : "";
}
//------------------------------------------------------------------------------
const std::filesystem::path& module::get_library_location() const
{
return m_library_location;
}
//------------------------------------------------------------------------------
const std::filesystem::path& module::get_resources_location() const
{
return m_resources_location;
}
//------------------------------------------------------------------------------
SPTR(plugin) module::get_plugin() const
{
return m_plugin;
}
//------------------------------------------------------------------------------
void module::load_libraries()
{
// Ensure the module is enabled.
if(!m_enabled)
{
throw runtime_exception(get_module_str(m_identifier) + ": module is not enabled.");
}
// Pre-condition
SIGHT_ASSERT("Module is already loaded", s_loading_module == nullptr);
// References the current module as the loading module.
s_loading_module = shared_from_this();
// Loads the library
if(m_library && !m_library->is_loaded())
{
try
{
m_library->load();
}
catch(std::exception& e)
{
std::string message;
message += "Unable to load module ";
message += m_library->name().string();
message += ". ";
message += e.what();
SIGHT_ERROR(message);
s_loading_module.reset();
throw runtime_exception(message);
}
}
// Unreferences the current module from the loading module.
s_loading_module.reset();
// Post-condition
assert(s_loading_module == nullptr);
}
//------------------------------------------------------------------------------
void module::load_requirements()
{
try
{
runtime& runtime = runtime::get();
requirement_container::const_iterator iter;
for(const auto& requirement : m_requirements)
{
auto module = std::dynamic_pointer_cast<detail::module>(runtime.find_module(requirement));
// Ensure that a module has been retrieved.
if(module == nullptr)
{
throw runtime_exception(requirement + ": required module not found.");
}
// Enable the required module if necessary.
if(!module->enabled())
{
module->set_enable(true);
}
// Starts the module if necessary (loads its libraries and requirements module).
module->start();
}
}
catch(const std::exception& e)
{
std::string message;
message += "Module " + get_module_str(m_identifier) + " was not able to load requirements. ";
message += e.what();
throw runtime_exception(message);
}
}
//------------------------------------------------------------------------------
void module::start()
{
if(!m_started)
{
if(!m_enabled)
{
throw runtime_exception(get_module_str(m_identifier) + ": module is not enabled.");
}
if(m_plugin == nullptr)
{
load_requirements();
load_libraries();
try
{
start_plugin();
}
catch(std::exception& e)
{
throw runtime_exception(
get_module_str(m_identifier)
+ ": start plugin error (after load requirement) " + e.what()
);
}
SIGHT_INFO("Loaded module '" + m_identifier + "' successfully");
}
}
}
//------------------------------------------------------------------------------
void module::start_plugin()
{
SIGHT_ASSERT(
"Module " + get_module_str(m_identifier) + " plugin is already started.",
!m_started
);
// Retrieves the type of the plugin.
std::string plugin_type(get_class());
// According to the presence of a class or not, build and empty
// plugin or attempt to instantiate a user defined plugin.
SPTR(plugin) plugin;
if(plugin_type.empty())
{
plugin = std::make_shared<empty_plugin>();
}
else
{
auto& runtime = runtime::get();
SPTR(executable) executable(runtime.create_executable_instance(plugin_type));
plugin = std::dynamic_pointer_cast<sight::core::runtime::plugin>(executable);
}
// Ensures that a plugin has been created.
if(plugin == nullptr)
{
throw runtime_exception(get_module_str(m_identifier) + ": unable to create a plugin instance.");
}
if(core::runtime::get_current_profile())
{
// Stores and start the plugin.
try
{
auto prof = std::dynamic_pointer_cast<detail::profile::profile>(core::runtime::get_current_profile());
prof->add_stopper(this->identifier(), this->priority());
m_plugin = plugin;
m_plugin->start();
m_started = true;
}
catch(std::exception& e)
{
throw runtime_exception(get_module_str(m_identifier) + ": start plugin error : " + e.what());
}
}
}
//------------------------------------------------------------------------------
void module::stop()
{
if(m_started)
{
SIGHT_ASSERT(get_module_str(m_identifier) + " : m_plugin not an instance.", m_plugin != nullptr);
try
{
m_plugin->stop();
m_plugin.reset();
m_started = false;
}
catch(std::exception& e)
{
throw runtime_exception(get_module_str(m_identifier) + ": stop plugin error : " + e.what());
}
//Unloads all libraries.
// LibraryContainer::iterator curEntry;
// LibraryContainer::iterator endEntry = m_libraries.end();
// for(curEntry = m_libraries.begin(); curEntry != endEntry; ++curEntry)
// {
// std::shared_ptr<dl::Library> library(*curEntry);
// if(library->isLoaded() == true )
// {
// library->unload();
// }
// }
}
}
//------------------------------------------------------------------------------
bool module::enabled() const
{
return m_enabled;
}
//------------------------------------------------------------------------------
void module::set_enable(const bool _state)
{
m_enabled = _state;
}
//------------------------------------------------------------------------------
void module::add_parameter(const std::string& _identifier, const std::string& _value)
{
m_parameters[_identifier] = _value;
}
//------------------------------------------------------------------------------
std::string module::get_parameter_value(const std::string& _identifier) const
{
auto found = m_parameters.find(_identifier);
return (found != m_parameters.end()) ? found->second : std::string();
}
//------------------------------------------------------------------------------
bool module::has_parameter(const std::string& _identifier) const
{
return m_parameters.find(_identifier) != m_parameters.end();
}
//------------------------------------------------------------------------------
core::runtime::module::extension_container core::runtime::detail::module::extensions() const
{
core::runtime::module::extension_container container;
std::ranges::transform(m_extensions, std::inserter(container, container.begin()), [](const auto& _e){return _e;});
return container;
}
//------------------------------------------------------------------------------
std::string module::get_module_str(const std::string& _identifier)
{
return _identifier;
}
//------------------------------------------------------------------------------
} // namespace sight::core::runtime::detail
|