2018-08-21 00:25:44 +02:00
|
|
|
/** Loadable / plugin support.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @author Daniel Krebs <github@daniel-krebs.net>
|
2021-06-21 16:09:11 -04:00
|
|
|
* @copyright 2014-2021, Institute for Automation of Complex Power Systems, EONERC
|
2018-08-21 00:25:44 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
2018-08-21 16:54:58 +02:00
|
|
|
* VILLAScommon
|
2018-08-21 00:25:44 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-06-11 14:19:39 +02:00
|
|
|
#include <iostream>
|
2018-08-21 00:25:44 +02:00
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
#include <jansson.h>
|
2021-07-07 10:36:38 +02:00
|
|
|
#include <spdlog/fmt/ostr.h>
|
2018-08-21 00:25:44 +02:00
|
|
|
|
|
|
|
#include <villas/log.hpp>
|
2019-10-27 20:23:47 +01:00
|
|
|
#include <villas/common.hpp>
|
2018-08-21 00:25:44 +02:00
|
|
|
|
|
|
|
namespace villas {
|
2018-08-23 13:22:46 +02:00
|
|
|
namespace plugin {
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
/* Forward declarations */
|
|
|
|
class Plugin;
|
2022-02-25 09:52:09 -05:00
|
|
|
class Registry;
|
|
|
|
|
|
|
|
extern Registry *registry;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
template<typename T = Plugin>
|
|
|
|
using List = std::list<T *>;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2022-02-25 09:52:09 -05:00
|
|
|
class SubRegistry {
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual
|
|
|
|
List<> lookup() = 0;
|
|
|
|
};
|
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
class Registry {
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
protected:
|
2022-02-25 09:52:09 -05:00
|
|
|
List<> plugins;
|
|
|
|
List<SubRegistry> registries;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
public:
|
|
|
|
|
2022-02-25 09:52:09 -05:00
|
|
|
Logger getLogger()
|
2018-10-19 14:33:10 +02:00
|
|
|
{
|
|
|
|
return logging.get("plugin:registry");
|
|
|
|
}
|
2018-08-23 13:22:46 +02:00
|
|
|
|
2022-02-25 09:52:09 -05:00
|
|
|
void add(Plugin *p)
|
2018-08-23 13:22:46 +02:00
|
|
|
{
|
2022-02-25 09:52:09 -05:00
|
|
|
plugins.push_back(p);
|
2018-08-23 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:52:09 -05:00
|
|
|
void addSubRegistry(SubRegistry *sr)
|
2018-08-23 13:22:46 +02:00
|
|
|
{
|
2022-02-25 09:52:09 -05:00
|
|
|
registries.push_back(sr);
|
2018-08-23 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:52:09 -05:00
|
|
|
void remove(Plugin *p)
|
2018-08-23 13:22:46 +02:00
|
|
|
{
|
2022-02-25 09:52:09 -05:00
|
|
|
plugins.remove(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get all plugins including sub-registries
|
|
|
|
List<> lookup() {
|
|
|
|
List<> all;
|
2018-08-23 13:22:46 +02:00
|
|
|
|
2022-02-25 09:52:09 -05:00
|
|
|
all.insert(all.end(), plugins.begin(), plugins.end());
|
|
|
|
|
|
|
|
for (auto r : registries) {
|
|
|
|
auto p = r->lookup();
|
|
|
|
|
|
|
|
all.insert(all.end(), p.begin(), p.end());
|
2018-08-23 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:52:09 -05:00
|
|
|
return all;
|
2018-08-23 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:52:09 -05:00
|
|
|
/// Get all plugins of specific type
|
2018-08-23 13:22:46 +02:00
|
|
|
template<typename T = Plugin>
|
2022-02-25 09:52:09 -05:00
|
|
|
List<T> lookup()
|
2018-08-23 13:22:46 +02:00
|
|
|
{
|
|
|
|
List<T> list;
|
|
|
|
|
2022-02-25 09:52:09 -05:00
|
|
|
for (Plugin *p : lookup()) {
|
2018-08-23 13:22:46 +02:00
|
|
|
T *t = dynamic_cast<T *>(p);
|
|
|
|
if (t)
|
|
|
|
list.push_back(t);
|
|
|
|
}
|
|
|
|
|
2021-11-03 17:12:11 -04:00
|
|
|
/* Sort alphabetically */
|
|
|
|
list.sort([](const T *a, const T *b) {
|
|
|
|
return a->getName() < b->getName();
|
|
|
|
});
|
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
return list;
|
|
|
|
}
|
2020-06-11 14:19:39 +02:00
|
|
|
|
2022-02-25 09:52:09 -05:00
|
|
|
/// Get all plugins of specific type and name
|
2020-06-11 14:19:39 +02:00
|
|
|
template<typename T = Plugin>
|
2022-02-25 09:52:09 -05:00
|
|
|
T * lookup(const std::string &name)
|
|
|
|
{
|
|
|
|
for (T *p : lookup<T>()) {
|
|
|
|
if (p->getName() != name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T = Plugin>
|
|
|
|
void dump();
|
2018-08-23 13:22:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Plugin {
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
friend plugin::Registry;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2021-11-03 17:12:11 -04:00
|
|
|
protected:
|
|
|
|
Logger logger;
|
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
public:
|
2020-06-11 18:08:08 +02:00
|
|
|
Plugin();
|
2021-11-03 17:12:11 -04:00
|
|
|
|
|
|
|
virtual
|
|
|
|
~Plugin();
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2018-08-23 13:22:46 +02:00
|
|
|
// copying a plugin doesn't make sense, so explicitly deny it
|
|
|
|
Plugin(Plugin const&) = delete;
|
|
|
|
void operator=(Plugin const&) = delete;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2021-11-03 17:12:11 -04:00
|
|
|
virtual
|
|
|
|
void dump();
|
2018-08-23 13:22:46 +02:00
|
|
|
|
2020-06-11 18:08:08 +02:00
|
|
|
/// Get plugin name
|
2021-11-03 17:12:11 -04:00
|
|
|
virtual
|
|
|
|
std::string getName() const = 0;
|
2020-06-11 18:08:08 +02:00
|
|
|
|
2021-06-21 16:07:46 -04:00
|
|
|
/// Get plugin type
|
2021-11-03 17:12:11 -04:00
|
|
|
virtual
|
|
|
|
std::string getType() const = 0;
|
2021-06-21 16:07:46 -04:00
|
|
|
|
2020-06-11 18:08:08 +02:00
|
|
|
// Get plugin description
|
2021-11-03 17:12:11 -04:00
|
|
|
virtual
|
|
|
|
std::string getDescription() const = 0;
|
|
|
|
|
|
|
|
virtual
|
|
|
|
Logger getLogger()
|
|
|
|
{
|
|
|
|
if (!logger) {
|
|
|
|
auto name = fmt::format("{}:{}", getType(), getName());
|
|
|
|
logger = logging.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return logger;
|
|
|
|
}
|
2018-08-23 13:22:46 +02:00
|
|
|
|
2021-07-07 10:36:38 +02:00
|
|
|
/** Custom formatter for spdlog */
|
|
|
|
template<typename OStream>
|
|
|
|
friend OStream &operator<<(OStream &os, const class Plugin &p)
|
|
|
|
{
|
|
|
|
return os << p.getName();
|
|
|
|
}
|
2018-08-21 00:25:44 +02:00
|
|
|
};
|
|
|
|
|
2020-06-11 14:19:39 +02:00
|
|
|
template<typename T = Plugin>
|
|
|
|
void
|
2022-02-25 09:52:09 -05:00
|
|
|
Registry::dump()
|
2020-06-11 14:19:39 +02:00
|
|
|
{
|
2020-06-11 18:08:08 +02:00
|
|
|
getLogger()->info("Available plugins:");
|
|
|
|
|
2022-02-25 09:52:09 -05:00
|
|
|
for (Plugin *p : plugins) {
|
2020-06-11 14:19:39 +02:00
|
|
|
T *t = dynamic_cast<T *>(p);
|
|
|
|
if (t)
|
2021-07-07 10:36:38 +02:00
|
|
|
getLogger()->info(" - {}: {}", *p, p->getDescription());
|
2020-06-11 14:19:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 10:50:50 +02:00
|
|
|
} /* namespace plugin */
|
|
|
|
} /* namespace villas */
|