-
Notifications
You must be signed in to change notification settings - Fork 342
/
remoteutil.cpp
598 lines (487 loc) · 15.2 KB
/
remoteutil.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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#include <unistd.h>
#include <QFileInfo>
#include <QFile>
#include <QDir>
#include <QList>
#include "compat.h"
#include "remoteutil.h"
#include "programinfo.h"
#include "mythcorecontext.h"
#include "storagegroup.h"
#include "mythevent.h"
#include "mythsocket.h"
vector<ProgramInfo *> *RemoteGetRecordedList(int sort)
{
QString str = "QUERY_RECORDINGS ";
if (sort < 0)
str += "Descending";
else if (sort > 0)
str += "Ascending";
else
str += "Unsorted";
QStringList strlist(str);
vector<ProgramInfo *> *info = new vector<ProgramInfo *>;
if (!RemoteGetRecordingList(*info, strlist))
{
delete info;
return NULL;
}
return info;
}
bool RemoteGetLoad(float load[3])
{
QStringList strlist(QString("QUERY_LOAD"));
if (gCoreContext->SendReceiveStringList(strlist))
{
load[0] = strlist[0].toFloat();
load[1] = strlist[1].toFloat();
load[2] = strlist[2].toFloat();
return true;
}
return false;
}
bool RemoteGetUptime(time_t &uptime)
{
QStringList strlist(QString("QUERY_UPTIME"));
if (!gCoreContext->SendReceiveStringList(strlist))
return false;
if (!strlist[0].at(0).isNumber())
return false;
if (sizeof(time_t) == sizeof(int))
uptime = strlist[0].toUInt();
else if (sizeof(time_t) == sizeof(long))
uptime = strlist[0].toULong();
else if (sizeof(time_t) == sizeof(long long))
uptime = strlist[0].toULongLong();
return true;
}
bool RemoteGetMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
{
QStringList strlist(QString("QUERY_MEMSTATS"));
if (gCoreContext->SendReceiveStringList(strlist))
{
totalMB = strlist[0].toInt();
freeMB = strlist[1].toInt();
totalVM = strlist[2].toInt();
freeVM = strlist[3].toInt();
return true;
}
return false;
}
bool RemoteCheckFile(const ProgramInfo *pginfo, bool checkSlaves)
{
QStringList strlist("QUERY_CHECKFILE");
strlist << QString::number((int)checkSlaves);
pginfo->ToStringList(strlist);
if ((!gCoreContext->SendReceiveStringList(strlist)) ||
(!strlist[0].toInt()))
return false;
// Only modify the pathname if the recording file is available locally on
// this host
QString localpath = strlist[1];
QFile checkFile(localpath);
if (checkFile.exists())
pginfo->SetPathname(localpath);
return true;
}
bool RemoteDeleteRecording(
uint chanid, const QDateTime &recstartts, bool forceMetadataDelete,
bool forgetHistory)
{
bool result = true;
QString cmd =
QString("DELETE_RECORDING %1 %2 %3 %4")
.arg(chanid)
.arg(recstartts.toString(Qt::ISODate))
.arg(forceMetadataDelete ? "FORCE" : "NO_FORCE")
.arg(forgetHistory ? "FORGET" : "NO_FORGET");
QStringList strlist(cmd);
if (!gCoreContext->SendReceiveStringList(strlist) || strlist.empty())
result = false;
else if (strlist[0].toInt() == -2)
result = false;
if (!result)
{
LOG(VB_GENERAL, LOG_ALERT,
QString("Failed to delete recording %1:%2")
.arg(chanid).arg(recstartts.toString(Qt::ISODate)));
}
return result;
}
bool RemoteUndeleteRecording(uint chanid, const QDateTime &recstartts)
{
bool result = false;
#if 0
if (!gCoreContext->GetNumSetting("AutoExpireInsteadOfDelete", 0))
return result;
#endif
QStringList strlist(QString("UNDELETE_RECORDING"));
strlist.push_back(QString::number(chanid));
strlist.push_back(recstartts.toString(Qt::ISODate));
gCoreContext->SendReceiveStringList(strlist);
if (strlist[0].toInt() == 0)
result = true;
return result;
}
void RemoteGetAllScheduledRecordings(vector<ProgramInfo *> &scheduledlist)
{
QStringList strList(QString("QUERY_GETALLSCHEDULED"));
RemoteGetRecordingList(scheduledlist, strList);
}
void RemoteGetAllExpiringRecordings(vector<ProgramInfo *> &expiringlist)
{
QStringList strList(QString("QUERY_GETEXPIRING"));
RemoteGetRecordingList(expiringlist, strList);
}
uint RemoteGetRecordingList(
vector<ProgramInfo *> &reclist, QStringList &strList)
{
if (!gCoreContext->SendReceiveStringList(strList))
return 0;
int numrecordings = strList[0].toInt();
if (numrecordings <= 0)
return 0;
if (numrecordings * NUMPROGRAMLINES + 1 > (int)strList.size())
{
LOG(VB_GENERAL, LOG_ERR,
"RemoteGetRecordingList() list size appears to be incorrect.");
return 0;
}
uint reclist_initial_size = (uint) reclist.size();
QStringList::const_iterator it = strList.begin() + 1;
for (int i = 0; i < numrecordings; i++)
{
ProgramInfo *pginfo = new ProgramInfo(it, strList.end());
reclist.push_back(pginfo);
}
return ((uint) reclist.size()) - reclist_initial_size;
}
vector<ProgramInfo *> *RemoteGetConflictList(const ProgramInfo *pginfo)
{
QString cmd = QString("QUERY_GETCONFLICTING");
QStringList strlist( cmd );
pginfo->ToStringList(strlist);
vector<ProgramInfo *> *retlist = new vector<ProgramInfo *>;
RemoteGetRecordingList(*retlist, strlist);
return retlist;
}
QDateTime RemoteGetPreviewLastModified(const ProgramInfo *pginfo)
{
QStringList strlist( "QUERY_PIXMAP_LASTMODIFIED" );
pginfo->ToStringList(strlist);
if (!gCoreContext->SendReceiveStringList(strlist))
return QDateTime();
if (!strlist.empty() && strlist[0] != "BAD")
{
uint timet = strlist[0].toUInt();
return MythDate::fromTime_t(timet);
}
return QDateTime();
}
/// Download preview & get timestamp if newer than cachefile's
/// last modified time, otherwise just get the timestamp
QDateTime RemoteGetPreviewIfModified(
const ProgramInfo &pginfo, const QString &cachefile)
{
QString loc("RemoteGetPreviewIfModified: ");
QDateTime cacheLastModified;
QFileInfo cachefileinfo(cachefile);
if (cachefileinfo.exists())
cacheLastModified = cachefileinfo.lastModified();
QStringList strlist("QUERY_PIXMAP_GET_IF_MODIFIED");
strlist << ((cacheLastModified.isValid()) ? // unix secs, UTC
QString::number(cacheLastModified.toTime_t()) : QString("-1"));
strlist << QString::number(200 * 1024); // max size of preview file
pginfo.ToStringList(strlist);
if (!gCoreContext->SendReceiveStringList(strlist) ||
strlist.empty() || strlist[0] == "ERROR")
{
LOG(VB_GENERAL, LOG_ERR, loc + "Remote error" +
((strlist.size() >= 2) ? (":\n\t\t\t" + strlist[1]) : ""));
return QDateTime();
}
if (strlist[0] == "WARNING")
{
LOG(VB_NETWORK, LOG_WARNING, loc + "Remote warning" +
((strlist.size() >= 2) ? (":\n\t\t\t" + strlist[1]) : ""));
return QDateTime();
}
QDateTime retdatetime;
qlonglong timet = strlist[0].toLongLong();
if (timet >= 0)
retdatetime = MythDate::fromTime_t(timet);
if (strlist.size() < 4)
{
return retdatetime;
}
size_t length = strlist[1].toULongLong();
quint16 checksum16 = strlist[2].toUInt();
QByteArray data = QByteArray::fromBase64(strlist[3].toLatin1());
if ((size_t) data.size() < length)
{ // (note data.size() may be up to 3 bytes longer after decoding
LOG(VB_GENERAL, LOG_ERR, loc +
QString("Preview size check failed %1 < %2")
.arg(data.size()).arg(length));
return QDateTime();
}
data.resize(length);
if (checksum16 != qChecksum(data.constData(), data.size()))
{
LOG(VB_GENERAL, LOG_ERR, loc + "Preview checksum failed");
return QDateTime();
}
QString pdir(cachefile.section("/", 0, -2));
QDir cfd(pdir);
if (!cfd.exists() && !cfd.mkdir(pdir))
{
LOG(VB_GENERAL, LOG_ERR, loc +
QString("Unable to create remote cache directory '%1'")
.arg(pdir));
return QDateTime();
}
QFile file(cachefile);
if (!file.open(QIODevice::WriteOnly|QIODevice::Truncate))
{
LOG(VB_GENERAL, LOG_ERR, loc +
QString("Unable to open cached preview file for writing '%1'")
.arg(cachefile));
return QDateTime();
}
off_t offset = 0;
size_t remaining = length;
uint failure_cnt = 0;
while ((remaining > 0) && (failure_cnt < 5))
{
ssize_t written = file.write(data.data() + offset, remaining);
if (written < 0)
{
failure_cnt++;
usleep(50000);
continue;
}
failure_cnt = 0;
offset += written;
remaining -= written;
}
if (remaining)
{
LOG(VB_GENERAL, LOG_ERR, loc +
QString("Failed to write cached preview file '%1'")
.arg(cachefile));
file.resize(0); // in case unlink fails..
file.remove(); // closes fd
return QDateTime();
}
file.close();
return retdatetime;
}
bool RemoteFillProgramInfo(ProgramInfo &pginfo, const QString &playbackhost)
{
QStringList strlist( "FILL_PROGRAM_INFO" );
strlist << playbackhost;
pginfo.ToStringList(strlist);
if (gCoreContext->SendReceiveStringList(strlist))
{
ProgramInfo tmp(strlist);
if (tmp.HasPathname() || tmp.GetChanID())
{
pginfo = tmp;
return true;
}
}
return false;
}
QStringList RemoteRecordings(void)
{
QStringList strlist("QUERY_ISRECORDING");
if (!gCoreContext->SendReceiveStringList(strlist, false, false))
{
QStringList empty;
empty << "0" << "0";
return empty;
}
return strlist;
}
int RemoteGetRecordingMask(void)
{
int mask = 0;
QString cmd = "QUERY_ISRECORDING";
QStringList strlist( cmd );
if (!gCoreContext->SendReceiveStringList(strlist))
return mask;
if (strlist.empty())
return 0;
int recCount = strlist[0].toInt();
for (int i = 0, j = 0; j < recCount; i++)
{
cmd = QString("QUERY_RECORDER %1").arg(i + 1);
strlist = QStringList( cmd );
strlist << "IS_RECORDING";
if (gCoreContext->SendReceiveStringList(strlist) && !strlist.empty())
{
if (strlist[0].toInt())
{
mask |= 1<<i;
j++; // count active recorder
}
}
else
{
break;
}
}
return mask;
}
int RemoteGetFreeRecorderCount(void)
{
QStringList strlist( "GET_FREE_RECORDER_COUNT" );
if (!gCoreContext->SendReceiveStringList(strlist, true))
return 0;
if (strlist.empty())
return 0;
if (strlist[0] == "UNKNOWN_COMMAND")
{
LOG(VB_GENERAL, LOG_EMERG,
"Unknown command GET_FREE_RECORDER_COUNT, upgrade your "
"backend version.");
return 0;
}
return strlist[0].toInt();
}
bool RemoteGetFileList(QString host, QString path, QStringList* list,
QString sgroup, bool fileNamesOnly)
{
// Make sure the list is empty when we get started
list->clear();
if (sgroup.isEmpty())
sgroup = "Videos";
*list << "QUERY_SG_GETFILELIST";
*list << host;
*list << StorageGroup::GetGroupToUse(host, sgroup);
*list << path;
*list << QString::number(fileNamesOnly);
bool ok = false;
if (gCoreContext->IsMasterBackend())
{
// since the master backend cannot connect back around to
// itself, and the libraries do not have access to the list
// of connected slave backends to query an existing connection
// start up a new temporary connection directly to the slave
// backend to query the file list
QString ann = QString("ANN Playback %1 0")
.arg(gCoreContext->GetHostName());
QString addr = gCoreContext->GetBackendServerIP(host);
int port = gCoreContext->GetNumSettingOnHost("BackendServerPort",
host, 6543);
bool mismatch = false;
MythSocket *sock = gCoreContext->ConnectCommandSocket(
addr, port, ann, &mismatch);
if (sock)
{
ok = sock->SendReceiveStringList(*list);
sock->DecrRef();
}
else
list->clear();
}
else
ok = gCoreContext->SendReceiveStringList(*list);
// Should the SLAVE UNREACH test be here ?
return ok;
}
/**
* Get recorder for a programme.
*
* \return recordernum if pginfo recording in progress, else 0
*/
int RemoteCheckForRecording(const ProgramInfo *pginfo)
{
QStringList strlist( QString("CHECK_RECORDING") );
pginfo->ToStringList(strlist);
if (gCoreContext->SendReceiveStringList(strlist) && !strlist.empty())
return strlist[0].toInt();
return 0;
}
/**
* Get status of an individual programme (with pre-post roll?).
*
* \retval 0 Not Recording
* \retval 1 Recording
* \retval 2 Under-Record
* \retval 3 Over-Record
*/
int RemoteGetRecordingStatus(
const ProgramInfo *pginfo, int overrecsecs, int underrecsecs)
{
QDateTime curtime = MythDate::current();
int retval = 0;
if (pginfo)
{
if (curtime >= pginfo->GetScheduledStartTime().addSecs(-underrecsecs) &&
curtime < pginfo->GetScheduledEndTime().addSecs(overrecsecs))
{
if (curtime >= pginfo->GetScheduledStartTime() &&
curtime < pginfo->GetScheduledEndTime())
retval = 1;
else if (curtime < pginfo->GetScheduledStartTime() &&
RemoteCheckForRecording(pginfo) > 0)
retval = 2;
else if (curtime > pginfo->GetScheduledEndTime() &&
RemoteCheckForRecording(pginfo) > 0)
retval = 3;
}
}
return retval;
}
/**
* \brief return list of currently recording shows
*/
vector<ProgramInfo *> *RemoteGetCurrentlyRecordingList(void)
{
QString str = "QUERY_RECORDINGS ";
str += "Recording";
QStringList strlist( str );
vector<ProgramInfo *> *reclist = new vector<ProgramInfo *>;
vector<ProgramInfo *> *info = new vector<ProgramInfo *>;
if (!RemoteGetRecordingList(*info, strlist))
{
if (info)
delete info;
return reclist;
}
ProgramInfo *p = NULL;
vector<ProgramInfo *>::iterator it = info->begin();
// make sure whatever RemoteGetRecordingList() returned
// only has rsRecording shows
for ( ; it != info->end(); ++it)
{
p = *it;
if (p->GetRecordingStatus() == rsRecording ||
(p->GetRecordingStatus() == rsRecorded &&
p->GetRecordingGroup() == "LiveTV"))
{
reclist->push_back(new ProgramInfo(*p));
}
}
while (!info->empty())
{
delete info->back();
info->pop_back();
}
if (info)
delete info;
return reclist;
}
/**
* \brief return list of backends currently connected to the master
*/
bool RemoteGetActiveBackends(QStringList *list)
{
list->clear();
*list << "QUERY_ACTIVE_BACKENDS";
if (!gCoreContext->SendReceiveStringList(*list))
return false;
list->removeFirst();
return true;
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */