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
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "Service.h"
#include "addons/AddonManager.h"
#include "addons/addoninfo/AddonType.h"
#include "interfaces/generic/ScriptInvocationManager.h"
#include "utils/StringUtils.h"
#include "utils/log.h"
#include <mutex>
namespace ADDON
{
CService::CService(const AddonInfoPtr& addonInfo) : CAddon(addonInfo, AddonType::SERVICE)
{
}
CServiceAddonManager::CServiceAddonManager(CAddonMgr& addonMgr) :
m_addonMgr(addonMgr)
{
}
CServiceAddonManager::~CServiceAddonManager()
{
m_addonMgr.Events().Unsubscribe(this);
m_addonMgr.UnloadEvents().Unsubscribe(this);
}
void CServiceAddonManager::OnEvent(const ADDON::AddonEvent& event)
{
if (typeid(event) == typeid(ADDON::AddonEvents::Enabled))
{
Start(event.addonId);
}
else if (typeid(event) == typeid(ADDON::AddonEvents::ReInstalled))
{
Stop(event.addonId);
Start(event.addonId);
}
else if (typeid(event) == typeid(ADDON::AddonEvents::Disabled) ||
typeid(event) == typeid(ADDON::AddonEvents::Unload))
{
Stop(event.addonId);
}
}
void CServiceAddonManager::Start()
{
m_addonMgr.Events().Subscribe(this, &CServiceAddonManager::OnEvent);
m_addonMgr.UnloadEvents().Subscribe(this, &CServiceAddonManager::OnEvent);
VECADDONS addons;
if (m_addonMgr.GetAddons(addons, AddonType::SERVICE))
{
for (const auto& addon : addons)
{
Start(addon);
}
}
}
void CServiceAddonManager::Start(const std::string& addonId)
{
AddonPtr addon;
if (m_addonMgr.GetAddon(addonId, addon, AddonType::SERVICE, OnlyEnabled::CHOICE_YES))
{
Start(addon);
}
}
void CServiceAddonManager::Start(const AddonPtr& addon)
{
std::unique_lock<CCriticalSection> lock(m_criticalSection);
if (m_services.find(addon->ID()) != m_services.end())
{
CLog::Log(LOGDEBUG, "CServiceAddonManager: {} already started.", addon->ID());
return;
}
if (StringUtils::EndsWith(addon->LibPath(), ".py"))
{
CLog::Log(LOGDEBUG, "CServiceAddonManager: starting {}", addon->ID());
auto handle = CScriptInvocationManager::GetInstance().ExecuteAsync(addon->LibPath(), addon);
if (handle == -1)
{
CLog::Log(LOGERROR, "CServiceAddonManager: {} failed to start", addon->ID());
return;
}
m_services[addon->ID()] = handle;
}
}
void CServiceAddonManager::Stop()
{
m_addonMgr.Events().Unsubscribe(this);
m_addonMgr.UnloadEvents().Unsubscribe(this);
std::unique_lock<CCriticalSection> lock(m_criticalSection);
for (const auto& service : m_services)
{
Stop(service);
}
m_services.clear();
}
void CServiceAddonManager::Stop(const std::string& addonId)
{
std::unique_lock<CCriticalSection> lock(m_criticalSection);
auto it = m_services.find(addonId);
if (it != m_services.end())
{
Stop(*it);
m_services.erase(it);
}
}
void CServiceAddonManager::Stop(const std::map<std::string, int>::value_type& service)
{
CLog::Log(LOGDEBUG, "CServiceAddonManager: stopping {}.", service.first);
if (!CScriptInvocationManager::GetInstance().Stop(service.second))
{
CLog::Log(LOGINFO, "CServiceAddonManager: failed to stop {} (may have ended)", service.first);
}
}
}
|