-
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathHyperionIManager.cpp
301 lines (254 loc) · 7.81 KB
/
HyperionIManager.cpp
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
#include <hyperion/HyperionIManager.h>
// hyperion
#include <hyperion/Hyperion.h>
#include <db/InstanceTable.h>
// qt
#include <QThread>
HyperionIManager* HyperionIManager::HIMinstance;
HyperionIManager::HyperionIManager(QObject* parent)
: QObject(parent)
, _log(Logger::getInstance("HYPERION-INSTMGR"))
, _instanceTable( new InstanceTable())
{
HIMinstance = this;
qRegisterMetaType<InstanceState>("InstanceState");
_instanceTable->createDefaultInstance();
}
Hyperion* HyperionIManager::getHyperionInstance(quint8 instance)
{
Hyperion* pInstance {nullptr};
if(_runningInstances.contains(instance))
return _runningInstances.value(instance);
if (!_runningInstances.isEmpty())
{
Warning(_log,"The requested instance index '%d' with name '%s' isn't running, return main instance", instance, QSTRING_CSTR(_instanceTable->getNamebyIndex(instance)));
pInstance = _runningInstances.value(0);
}
return pInstance;
}
QVector<QVariantMap> HyperionIManager::getInstanceData() const
{
QVector<QVariantMap> instances = _instanceTable->getAllInstances();
for( auto & entry : instances)
{
// add running state
entry["running"] = _runningInstances.contains(entry["instance"].toInt());
}
return instances;
}
QString HyperionIManager::getInstanceName(quint8 inst)
{
return _instanceTable->getNamebyIndex(inst);
}
QList<quint8> HyperionIManager::getRunningInstanceIdx() const
{
return _runningInstances.keys();
}
QList<quint8> HyperionIManager::getInstanceIds() const
{
return _instanceTable->getAllInstanceIDs();
}
void HyperionIManager::startAll()
{
const QVector<QVariantMap> instances = _instanceTable->getAllInstances(true);
if (instances.isEmpty())
{
Error(_log, "No enabled instances found to be started");
return;
}
for(const auto & entry : instances)
{
startInstance(entry["instance"].toInt());
}
}
void HyperionIManager::stopAll()
{
// copy the instances due to loop corruption, even with .erase() return next iter
QMap<quint8, Hyperion*> instCopy = _runningInstances;
for(auto *const instance : instCopy)
{
instance->stop();
}
}
void HyperionIManager::handleEvent(Event event)
{
Debug(_log,"%s Event [%d] received", eventToString(event), event);
switch (event) {
case Event::Suspend:
toggleSuspend(true);
break;
case Event::Resume:
toggleSuspend(false);
break;
case Event::Idle:
toggleIdle(true);
break;
case Event::ResumeIdle:
toggleIdle(false);
break;
default:
break;
}
}
void HyperionIManager::toggleSuspend(bool isSuspend)
{
Info(_log,"Put all instances in %s state", isSuspend ? "suspend" : "working");
QMap<quint8, Hyperion*> instCopy = _runningInstances;
for(const auto instance : instCopy)
{
emit instance->suspendRequest(isSuspend);
}
}
void HyperionIManager::toggleIdle(bool isIdle)
{
Info(_log,"Put all instances in %s state", isIdle ? "idle" : "working");
QMap<quint8, Hyperion*> instCopy = _runningInstances;
for(const auto instance : instCopy)
{
emit instance->idleRequest(isIdle);
}
}
void HyperionIManager::toggleStateAllInstances(bool enable)
{
// copy the instances due to loop corruption, even with .erase() return next iter
QMap<quint8, Hyperion*> instCopy = _runningInstances;
for(const auto instance : instCopy)
{
emit instance->compStateChangeRequest(hyperion::COMP_ALL, enable);
}
}
bool HyperionIManager::startInstance(quint8 inst, bool block, QObject* caller, int tan)
{
if(_instanceTable->instanceExist(inst))
{
if(!_runningInstances.contains(inst) && !_startQueue.contains(inst))
{
QThread* hyperionThread = new QThread();
hyperionThread->setObjectName("HyperionThread");
Hyperion* hyperion = new Hyperion(inst);
hyperion->moveToThread(hyperionThread);
// setup thread management
connect(hyperionThread, &QThread::started, hyperion, &Hyperion::start);
connect(hyperion, &Hyperion::started, this, &HyperionIManager::handleStarted);
connect(hyperion, &Hyperion::finished, this, &HyperionIManager::handleFinished);
connect(hyperion, &Hyperion::finished, hyperionThread, &QThread::quit, Qt::DirectConnection);
// setup further connections
// from Hyperion
connect(hyperion, &Hyperion::settingsChanged, this, &HyperionIManager::settingsChanged);
connect(hyperion, &Hyperion::videoMode, this, &HyperionIManager::requestVideoMode);
// to Hyperion
connect(this, &HyperionIManager::newVideoMode, hyperion, &Hyperion::newVideoMode);
// add to queue and start
_startQueue << inst;
hyperionThread->start();
// update db
_instanceTable->setLastUse(inst);
_instanceTable->setEnable(inst, true);
if(block)
{
while(!hyperionThread->isRunning()){}
}
if (!_pendingRequests.contains(inst) && caller != nullptr)
{
PendingRequests newDef{caller, tan};
_pendingRequests[inst] = newDef;
}
return true;
}
Debug(_log,"Can't start Hyperion instance index '%d' with name '%s' it's already running or queued for start", inst, QSTRING_CSTR(_instanceTable->getNamebyIndex(inst)));
return false;
}
Debug(_log,"Can't start Hyperion instance index '%d' it doesn't exist in DB", inst);
return false;
}
bool HyperionIManager::stopInstance(quint8 inst)
{
// inst 0 can't be stopped
if(!isInstAllowed(inst))
return false;
if(_instanceTable->instanceExist(inst))
{
if(_runningInstances.contains(inst))
{
// notify a ON_STOP rather sooner than later, queued signal listener should have some time to drop the pointer before it's deleted
emit instanceStateChanged(InstanceState::H_ON_STOP, inst);
Hyperion* hyperion = _runningInstances.value(inst);
hyperion->stop();
// update db
_instanceTable->setEnable(inst, false);
return true;
}
Debug(_log,"Can't stop Hyperion instance index '%d' with name '%s' it's not running'", inst, QSTRING_CSTR(_instanceTable->getNamebyIndex(inst)));
return false;
}
Debug(_log,"Can't stop Hyperion instance index '%d' it doesn't exist in DB", inst);
return false;
}
bool HyperionIManager::createInstance(const QString& name, bool start)
{
quint8 inst = 0;
if(_instanceTable->createInstance(name, inst))
{
Info(_log,"New Hyperion instance [%d] created with name '%s'", inst, QSTRING_CSTR(name));
emit instanceStateChanged(InstanceState::H_CREATED, inst, name);
emit change();
if(start)
startInstance(inst);
return true;
}
return false;
}
bool HyperionIManager::deleteInstance(quint8 inst)
{
// inst 0 can't be deleted
if(!isInstAllowed(inst))
{
return false;
}
// stop it if required as blocking and wait
stopInstance(inst);
if(_instanceTable->deleteInstance(inst))
{
Info(_log,"Hyperion instance with index '%d' has been deleted", inst);
emit instanceStateChanged(InstanceState::H_DELETED, inst);
emit change();
return true;
}
return false;
}
bool HyperionIManager::saveName(quint8 inst, const QString& name)
{
if(_instanceTable->saveName(inst, name))
{
emit change();
return true;
}
return false;
}
void HyperionIManager::handleFinished()
{
Hyperion* hyperion = qobject_cast<Hyperion*>(sender());
quint8 instance = hyperion->getInstanceIndex();
Info(_log,"Hyperion instance '%s' stopped", QSTRING_CSTR(_instanceTable->getNamebyIndex(instance)));
_runningInstances.remove(instance);
hyperion->thread()->deleteLater();
hyperion->deleteLater();
emit instanceStateChanged(InstanceState::H_STOPPED, instance);
emit change();
}
void HyperionIManager::handleStarted()
{
Hyperion* hyperion = qobject_cast<Hyperion*>(sender());
quint8 instance = hyperion->getInstanceIndex();
Info(_log,"Hyperion instance '%s' has been started", QSTRING_CSTR(_instanceTable->getNamebyIndex(instance)));
_startQueue.removeAll(instance);
_runningInstances.insert(instance, hyperion);
emit instanceStateChanged(InstanceState::H_STARTED, instance);
emit change();
if (_pendingRequests.contains(instance))
{
PendingRequests def = _pendingRequests.take(instance);
emit startInstanceResponse(def.caller, def.tan);
_pendingRequests.remove(instance);
}
}