24 Common::AddonFactory::SharedPtr Factory;
25 std::vector<Common::AddonId> Dependencies;
27 Common::Addon::SharedPtr Addon;
30 : Id(configuration.Id)
31 , Factory(configuration.Factory)
32 , Dependencies(configuration.Dependencies)
33 , Parameters(configuration.Parameters)
38 bool IsAddonNotStarted(
const std::pair<Common::AddonId, AddonData> & addonData)
40 return addonData.second.Addon == Common::Addon::SharedPtr();
45 typedef std::map<Common::AddonId, AddonData> AddonList;
48 AddonsManagerImpl(
const Common::Logger::SharedPtr & logger)
49 :
Common::AddonsManager(logger)
50 , ManagerStarted(false)
54 virtual ~AddonsManagerImpl()
71 LOG_ERROR(Logger,
"addons_manager | unknown exception, terminating");
79 if (ManagerStarted && !addonConfiguration.
Dependencies.empty())
84 EnsureAddonNotRegistered(addonConfiguration.
Id);
85 Addons.insert(std::make_pair(addonConfiguration.
Id, AddonData(addonConfiguration)));
96 EnsureAddonRegistered(
id);
97 AddonData & addonData = Addons.find(
id)->second;
101 addonData.Addon->Stop();
107 virtual Common::Addon::SharedPtr GetAddon(
const Common::AddonId &
id)
const 110 EnsureAddonRegistered(
id);
111 EnsureAddonInitialized(
id);
112 return Addons.find(
id)->second.Addon;
129 ManagerStarted =
true;
140 ManagerStarted =
false;
148 while (AddonData * addonData = GetNextAddonDataForStop())
153 addonData->Addon->Stop();
154 addonData->Addon.reset();
158 catch (
const std::exception & exc)
160 LOG_ERROR(Logger,
"addons_manager | failed to stop addon '{}': {}", addonData->Id, exc.what());
169 while (AddonData * addonData = GetNextAddonDataForStart())
172 Common::Addon::SharedPtr addon = addonData->Factory->CreateAddon();
177 addon->Initialize(*
this, addonData->Parameters);
181 catch (
const std::exception & exc)
183 LOG_ERROR(Logger,
"addons_manager | failed to initialize addon '{}': {}", addonData->Id, exc.what());
187 addonData->Addon = addon;
190 EnsureAllAddonsStarted();
194 AddonData * GetNextAddonDataForStart()
196 for (AddonList::iterator it = Addons.begin(); it != Addons.end(); ++it)
198 if (!IsAddonStarted(it->second) && IsAllAddonsStarted(it->second.Dependencies))
208 AddonData * GetNextAddonDataForStop()
210 for (AddonList::iterator it = Addons.begin(); it != Addons.end(); ++it)
212 if (IsAddonStarted(it->second) && IsAllDependentAddonsStopped(it->first))
221 bool IsAddonStarted(
const AddonData & addonData)
const 223 return static_cast<bool>(addonData.Addon);
226 bool IsAllAddonsStarted(
const std::vector<Common::AddonId> & ids)
const 228 for (std::vector<Common::AddonId>::const_iterator it = ids.begin(); it != ids.end(); ++it)
230 const AddonList::const_iterator addonIt = Addons.find(*it);
232 if (addonIt == Addons.end())
237 if (!IsAddonStarted(addonIt->second))
248 for (
const AddonList::value_type & addonIt : Addons)
251 if (!IsAddonStarted(addonIt.second))
257 const std::vector<Common::AddonId> & deps = addonIt.second.Dependencies;
259 if (std::find(deps.begin(), deps.end(), id) != deps.end())
270 if (!Addons.find(
id)->second.Addon)
278 if (!IsAddonRegistered(
id))
286 if (IsAddonRegistered(
id))
294 return Addons.find(
id) != Addons.end();
297 void EnsureAllAddonsStarted()
const 299 AddonList::const_iterator addonIt = std::find_if(Addons.begin(), Addons.end(), IsAddonNotStarted);
301 if (!Addons.empty() && addonIt != Addons.end())
315 return AddonsManager::UniquePtr(
new AddonsManagerImpl(logger));
std::string GetFullMessage() const
#define THROW_ERROR(data)
Addon interface definition GNU LGPL.
Common::ErrorData AddonIsNotStarted
Common::ErrorData AddonRegisteredButShouldnt
#define LOG_ERROR(__logger__,...)
Common::ErrorData AddonNotFound
Common::ErrorData AddonsManagerAlreadyStarted
AddonsManager::UniquePtr CreateAddonsManager(const Common::Logger::SharedPtr &logger)
Get instance of addons core.
Common::ErrorData UnableToRegisterAddonWhenStarted
Errors of Addons Manager GNU LGPL.
#define THROW_ERROR1(data, param1)
Common::ErrorData AddonsManagerAlreadyStopped
Common::ErrorData AddonNotRegistered
Common::ErrorData FailedToStartAddons
Common::ErrorData AddonNotInitializedYet