MythTV master
mythcorecontext.cpp
Go to the documentation of this file.
1#include "mythcorecontext.h"
2
3// Qt
4#include <QtGlobal>
5#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
6#include <QtSystemDetection>
7#endif
8#include <QCoreApplication>
9#include <QUrl>
10#include <QDir>
11#include <QFileInfo>
12#include <QDebug>
13#include <QMutex>
14#include <QRunnable>
15#include <QWaitCondition>
16#include <QAbstractSocket>
17#include <QHostAddress>
18#include <QHostInfo>
19#include <QNetworkInterface>
20#include <QNetworkAddressEntry>
21#include <QLocale>
22#include <QPair>
23#include <QRegularExpression>
24#include <QDateTime>
25
26// Std
27#include <algorithm>
28#include <cmath>
29#include <cstdarg>
30#include <queue>
31#include <unistd.h> // for usleep()
32
33#ifdef Q_OS_WINDOWS
34#include <winsock2.h>
35#else
36#include <clocale>
37#include <utility>
38#endif
39
40// MythTV
41#include "compat.h"
42#include "mythappname.h"
43#include "mythdownloadmanager.h"
44#include "mythsocket.h"
45#include "mythsystemlegacy.h"
46#include "mthreadpool.h"
47#include "exitcodes.h"
48#include "mythlogging.h"
49#include "mythversion.h"
50#include "logging.h"
51#include "mthread.h"
52#include "serverpool.h"
53#include "mythdate.h"
54#include "mythplugin.h"
55#include "mythmiscutil.h"
56#include "mythpower.h"
57
58#define LOC QString("MythCoreContext::%1(): ").arg(__func__)
59
61
62class MythCoreContextPrivate : public QObject
63{
64 public:
65 MythCoreContextPrivate(MythCoreContext *lparent, QString binversion,
66 QObject *guicontext);
67 ~MythCoreContextPrivate() override;
68
69 bool WaitForWOL(std::chrono::milliseconds timeout = std::chrono::milliseconds::max());
70
71 public:
73 QObject *m_guiContext { nullptr };
74 QObject *m_guiObject { nullptr };
76
81 QMutex m_scopesLock;
82 QMap<QString, QString> m_scopes;
83
84 QMutex m_sockLock;
85 MythSocket *m_serverSock { nullptr };
86 MythSocket *m_eventSock { nullptr };
87
90 bool m_wolInProgress { false };
91 bool m_isWOLAllowed { true };
92
93 bool m_backend { false };
94 bool m_frontend { false };
95
96 MythDB *m_database { nullptr };
97
98 QThread *m_uiThread { nullptr };
99
100 MythLocale *m_locale { nullptr };
101 QString m_language;
103
105
106 bool m_blockingClient { true };
107
108 QMap<QObject *, MythCoreContext::PlaybackStartCb> m_playbackClients;
110 bool m_inwanting { false };
111 bool m_intvwanting { false };
112
113 bool m_announcedProtocol { false };
114
116
117 bool m_isexiting { false };
118
119 QMap<QString, QPair<int64_t, uint64_t> > m_fileswritten;
121
123
124 QList<QHostAddress> m_approvedIps;
125 QList<QHostAddress> m_deniedIps;
127
128 MythPower *m_power { nullptr };
129};
130
132 QString binversion,
133 QObject *guicontext)
134 : m_parent(lparent),
135 m_guiContext(guicontext),
136 m_appBinaryVersion(std::move(binversion)),
137 m_database(GetMythDB()),
138 m_uiThread(QThread::currentThread())
139{
140 MThread::ThreadSetup("CoreContext");
141}
142
143static void delete_sock(
144#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
145 QMutexLocker &locker,
146#else
147 QMutexLocker<QMutex> &locker,
148#endif
149 MythSocket **s)
150{
151 if (*s)
152 {
153 MythSocket *tmp = *s;
154 *s = nullptr;
155 locker.unlock();
156 tmp->DecrRef();
157 locker.relock();
158 }
159}
160
162{
163 if (m_power)
164 MythPower::AcquireRelease(this, false);
165
167
168 {
169 QMutexLocker locker(&m_sockLock);
170 delete_sock(locker, &m_serverSock);
171 delete_sock(locker, &m_eventSock);
172 }
173
174 delete m_locale;
175
176 delete m_sessionManager;
177
179
181
183
184 // This has already been run in the MythContext dtor. Do we need it here
185 // too?
186#if 0
187 logStop(); // need to shutdown db logger before we kill db
188#endif
189
191
192 GetMythDB()->GetDBManager()->CloseDatabases();
193
194 if (m_database) {
196 m_database = nullptr;
197 }
198
200}
201
205bool MythCoreContextPrivate::WaitForWOL(std::chrono::milliseconds timeout)
206{
207 std::chrono::milliseconds timeout_remaining = timeout;
208 while (m_wolInProgress && (timeout_remaining > 0ms))
209 {
210 LOG(VB_GENERAL, LOG_INFO, LOC + "Wake-On-LAN in progress, waiting...");
211
212 std::chrono::milliseconds max_wait = std::min(1000ms, timeout_remaining);
213 m_wolInProgressWaitCondition.wait(&m_wolInProgressLock, max_wait.count());
214 timeout_remaining -= max_wait;
215 }
216
217 return !m_wolInProgress;
218}
219
220MythCoreContext::MythCoreContext(const QString &binversion,
221 QObject *guiContext)
222{
223 d = new MythCoreContextPrivate(this, binversion, guiContext);
224}
225
227{
228 if (!d)
229 {
230 LOG(VB_GENERAL, LOG_EMERG, LOC + "Out-of-memory");
231 return false;
232 }
233
234 if (d->m_appBinaryVersion != MYTH_BINARY_VERSION)
235 {
236 LOG(VB_GENERAL, LOG_CRIT,
237 QString("Application binary version (%1) does not "
238 "match libraries (%2)")
239 .arg(d->m_appBinaryVersion, MYTH_BINARY_VERSION));
240
241 QString warning = tr("This application is not compatible with the "
242 "installed MythTV libraries. Please recompile "
243 "after a make distclean");
244 LOG(VB_GENERAL, LOG_WARNING, warning);
245
246 return false;
247 }
248
249#ifndef Q_OS_WINDOWS
250 static const QRegularExpression utf8
251 { "utf-?8", QRegularExpression::CaseInsensitiveOption };
252 QString lang_variables("");
253 QString lc_value = setlocale(LC_CTYPE, nullptr);
254 if (lc_value.isEmpty())
255 {
256 // try fallback to environment variables for non-glibc systems
257 // LC_ALL, then LC_CTYPE
258 lc_value = qEnvironmentVariable("LC_ALL");
259 if (lc_value.isEmpty())
260 lc_value = qEnvironmentVariable("LC_CTYPE");
261 }
262 if (!lc_value.contains(utf8))
263 lang_variables.append("LC_ALL or LC_CTYPE");
264 lc_value = qEnvironmentVariable("LANG");
265 if (!lc_value.contains(utf8))
266 {
267 if (!lang_variables.isEmpty())
268 lang_variables.append(", and ");
269 lang_variables.append("LANG");
270 }
271 LOG(VB_GENERAL, LOG_INFO, QString("Assumed character encoding: %1")
272 .arg(lc_value));
273 if (!lang_variables.isEmpty())
274 {
275 LOG(VB_GENERAL, LOG_WARNING, QString("This application expects to "
276 "be running a locale that specifies a UTF-8 codeset, and many "
277 "features may behave improperly with your current language "
278 "settings. Please set the %1 variable(s) in the environment "
279 "in which this program is executed to include a UTF-8 codeset "
280 "(such as 'en_US.UTF-8').").arg(lang_variables));
281 }
282#endif
283
284 return true;
285}
286
288{
289 delete d;
290 d = nullptr;
291}
292
293void MythCoreContext::setTestIntSettings(QMap<QString,int> &overrides)
294{
295 m_testOverrideInts = std::move(overrides);
296}
297
298void MythCoreContext::setTestFloatSettings(QMap<QString,double> &overrides)
299{
300 m_testOverrideFloats = std::move(overrides);
301}
302
303void MythCoreContext::setTestStringSettings(QMap<QString,QString> &overrides)
304{
305 m_testOverrideStrings = std::move(overrides);
306}
307
309 const QString &announcement,
310 [[maybe_unused]] std::chrono::milliseconds timeout,
311 bool &proto_mismatch)
312{
313 proto_mismatch = false;
314
315#ifndef IGNORE_PROTO_VER_MISMATCH
316 if (!CheckProtoVersion(serverSock, timeout, true))
317 {
318 proto_mismatch = true;
319 return false;
320 }
321#endif
322
323 QStringList strlist(announcement);
324
325 if (!serverSock->WriteStringList(strlist))
326 {
327 LOG(VB_GENERAL, LOG_ERR, LOC + "Connecting server socket to "
328 "master backend, socket write failed");
329 return false;
330 }
331
332 if (!serverSock->ReadStringList(strlist, MythSocket::kShortTimeout) ||
333 strlist.empty() || (strlist[0] == "ERROR"))
334 {
335 if (!strlist.empty())
336 {
337 LOG(VB_GENERAL, LOG_ERR, LOC + "Problem connecting "
338 "server socket to master backend");
339 }
340 else
341 {
342 LOG(VB_GENERAL, LOG_ERR, LOC + "Timeout connecting "
343 "server socket to master backend");
344 }
345 return false;
346 }
347
348 return true;
349}
350
351// Connects to master server safely (i.e. by taking m_sockLock)
353 bool openEventSocket)
354{
355 QMutexLocker locker(&d->m_sockLock);
356 bool success = ConnectToMasterServer(blockingClient, openEventSocket);
357
358 return success;
359}
360
361// Assumes that either m_sockLock is held, or the app is still single
362// threaded (i.e. during startup).
364 bool openEventSocket)
365{
366 if (IsMasterBackend())
367 {
368 // Should never get here unless there is a bug in the code somewhere.
369 // If this happens, it can cause endless event loops.
370 LOG(VB_GENERAL, LOG_ERR, LOC + "ERROR: Master backend tried to connect back "
371 "to itself!");
372 return false;
373 }
374 if (IsExiting())
375 return false;
376
377 QString server = GetMasterServerIP();
378 if (server.isEmpty())
379 return false;
380
381 int port = GetMasterServerPort();
382 bool proto_mismatch = false;
383
385 {
387 d->m_serverSock = nullptr;
388 }
389
390 if (!d->m_serverSock)
391 {
392 QString type { "Monitor" };
393 if (IsFrontend())
394 type = "Frontend";
395 else if (blockingClient)
396 type = "Playback";
397 QString ann = QString("ANN %1 %2 %3")
398 .arg(type, d->m_localHostname, QString::number(static_cast<int>(false)));
400 server, port, ann, &proto_mismatch);
401 }
402
403 if (!d->m_serverSock)
404 return false;
405
406 d->m_blockingClient = blockingClient;
407
408 if (!openEventSocket)
409 return true;
410
411
412 if (!IsBackend())
413 {
415 {
417 d->m_eventSock = nullptr;
418 }
419 if (!d->m_eventSock)
420 d->m_eventSock = ConnectEventSocket(server, port);
421
422 if (!d->m_eventSock)
423 {
425 d->m_serverSock = nullptr;
426
427 QCoreApplication::postEvent(
428 d->m_guiContext, new MythEvent("CONNECTION_FAILURE"));
429
430 return false;
431 }
432 }
433
434 return true;
435}
436
438 const QString &hostname, int port, const QString &announce,
439 bool *p_proto_mismatch, int maxConnTry, std::chrono::milliseconds setup_timeout)
440{
441 MythSocket *serverSock = nullptr;
442
443 {
444 QMutexLocker locker(&d->m_wolInProgressLock);
445 d->WaitForWOL();
446 }
447
448 QString WOLcmd;
449 if (IsWOLAllowed())
450 WOLcmd = GetSetting("WOLbackendCommand", "");
451
452 if (maxConnTry < 1)
453 maxConnTry = std::max(GetNumSetting("BackendConnectRetry", 1), 1);
454
455 std::chrono::seconds WOLsleepTime = 0s;
456 int WOLmaxConnTry = 0;
457 if (!WOLcmd.isEmpty())
458 {
459 WOLsleepTime = GetDurSetting<std::chrono::seconds>("WOLbackendReconnectWaitTime", 0s);
460 WOLmaxConnTry = std::max(GetNumSetting("WOLbackendConnectRetry", 1), 1);
461 maxConnTry = std::max(maxConnTry, WOLmaxConnTry);
462 }
463
464 bool we_attempted_wol = false;
465
466 if (setup_timeout <= 0ms)
467 setup_timeout = MythSocket::kShortTimeout;
468
469 bool proto_mismatch = false;
470 for (int cnt = 1; cnt <= maxConnTry; cnt++)
471 {
472 LOG(VB_GENERAL, LOG_INFO, LOC +
473 QString("Connecting to backend server: %1:%2 (try %3 of %4)")
474 .arg(hostname).arg(port).arg(cnt).arg(maxConnTry));
475
476 serverSock = new MythSocket();
477
478 std::chrono::microseconds sleepus = 0us;
479 if (serverSock->ConnectToHost(hostname, port))
480 {
482 serverSock, announce, setup_timeout, proto_mismatch))
483 {
484 break;
485 }
486
487 if (proto_mismatch)
488 {
489 if (p_proto_mismatch)
490 *p_proto_mismatch = true;
491
492 serverSock->DecrRef();
493 serverSock = nullptr;
494 break;
495 }
496
497 setup_timeout += setup_timeout / 2;
498 }
499 else if (!WOLcmd.isEmpty() && (cnt < maxConnTry))
500 {
501 if (!we_attempted_wol)
502 {
503 QMutexLocker locker(&d->m_wolInProgressLock);
504 if (d->m_wolInProgress)
505 {
506 d->WaitForWOL();
507 continue;
508 }
509
510 d->m_wolInProgress = we_attempted_wol = true;
511 }
512
515 sleepus = WOLsleepTime;
516 }
517
518 serverSock->DecrRef();
519 serverSock = nullptr;
520
521 if (cnt == 1)
522 {
523 QCoreApplication::postEvent(
524 d->m_guiContext, new MythEvent("CONNECTION_FAILURE"));
525 }
526
527 if (sleepus != 0us)
528 usleep(sleepus.count());
529 }
530
531 if (we_attempted_wol)
532 {
533 QMutexLocker locker(&d->m_wolInProgressLock);
534 d->m_wolInProgress = false;
536 }
537
538 if (!serverSock && !proto_mismatch)
539 {
540 LOG(VB_GENERAL, LOG_ERR,
541 "Connection to master server timed out.\n\t\t\t"
542 "Either the server is down or the master server settings"
543 "\n\t\t\t"
544 "in mythtv-settings does not contain the proper IP address\n");
545 }
546 else
547 {
548 QCoreApplication::postEvent(
549 d->m_guiContext, new MythEvent("CONNECTION_RESTABLISHED"));
550 }
551
552 return serverSock;
553}
554
556 int port)
557{
558 auto *eventSock = new MythSocket(-1, this);
559
560 // Assume that since we _just_ connected the command socket,
561 // this one won't need multiple retries to work...
562 if (!eventSock->ConnectToHost(hostname, port))
563 {
564 LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to connect event "
565 "socket to master backend");
566 eventSock->DecrRef();
567 return nullptr;
568 }
569
570 QString str = QString("ANN Monitor %1 %2")
571 .arg(d->m_localHostname).arg(static_cast<int>(true));
572 QStringList strlist(str);
573 eventSock->WriteStringList(strlist);
574 bool ok = true;
575 if (!eventSock->ReadStringList(strlist) || strlist.empty() ||
576 (strlist[0] == "ERROR"))
577 {
578 if (!strlist.empty())
579 {
580 LOG(VB_GENERAL, LOG_ERR, LOC +
581 "Problem connecting event socket to master backend");
582 }
583 else
584 {
585 LOG(VB_GENERAL, LOG_ERR, LOC +
586 "Timeout connecting event socket to master backend");
587 }
588 ok = false;
589 }
590
591 if (!ok)
592 {
593 eventSock->DecrRef();
594 eventSock = nullptr;
595 }
596
597 return eventSock;
598}
599
601{
602 QMutexLocker locker(&d->m_sockLock);
603 return d->m_serverSock;
604}
605
607{
608 QStringList strlist;
609
610 QMutexLocker locker(&d->m_sockLock);
611 if (d->m_serverSock == nullptr)
612 return;
613
614 strlist << "BLOCK_SHUTDOWN";
616
617 d->m_blockingClient = true;
618}
619
621{
622 QStringList strlist;
623
624 QMutexLocker locker(&d->m_sockLock);
625 if (d->m_serverSock == nullptr)
626 return;
627
628 strlist << "ALLOW_SHUTDOWN";
630
631 d->m_blockingClient = false;
632}
633
635{
636 return d->m_blockingClient;
637}
638
640{
641 d->m_isWOLAllowed = allow;
642}
643
645{
646 return d->m_isWOLAllowed;
647}
648
650{
651 d->m_backend = backend;
652}
653
655{
656 return d->m_backend;
657}
658
660{
661 d->m_frontend = frontend;
662}
663
665{
666 return d->m_frontend;
667}
668
670{
671 QString host = GetHostName();
672 return IsMasterHost(host);
673}
674
675bool MythCoreContext::IsMasterHost(const QString &host)
676{
677 // Temporary code here only to facilitate the upgrade
678 // from 1346 or earlier. The way of determining master host is
679 // changing, and the new way of determning master host
680 // will not work with earlier databases.
681 // This code can be removed when updating from prior to
682 // 1347 is no longer allowed.
683 // Note that we are deprecating some settings including
684 // MasterServerIP, and can remove them at a future time.
685 if (GetNumSetting("DBSchemaVer") < 1347)
686 {
687 // Temporary copy of code from old version of
688 // IsThisHost(Qstring&,QString&)
689 QString addr(resolveSettingAddress("MasterServerIP"));
690 if (addr.toLower() == host.toLower())
691 return true;
692 QHostAddress addrfix(addr);
693 addrfix.setScopeId(QString());
694 QString addrstr = addrfix.toString();
695 if (addrfix.isNull())
696 addrstr = resolveAddress(addr);
697 QString thisip = GetBackendServerIP4(host);
698 QString thisip6 = GetBackendServerIP6(host);
699 return !addrstr.isEmpty()
700 && ((addrstr == thisip) || (addrstr == thisip6));
701 }
702 return GetSetting("MasterServerName") == host;
703}
704
706{
707 return (IsBackend() && IsMasterHost());
708}
709
711{
712#ifdef Q_OS_BSD4
713 const char *command = "ps -axc | grep -i mythbackend | grep -v grep > /dev/null";
714#elif defined(Q_OS_WINDOWS)
715 const char *command = "%systemroot%\\system32\\tasklist.exe "
716 " | %systemroot%\\system32\\find.exe /i \"mythbackend.exe\" ";
717#else
718 const char *command = "ps ch -C mythbackend -o pid > /dev/null";
719#endif
720 uint res = myth_system(command, kMSDontBlockInputDevs |
723 return (res == GENERIC_EXIT_OK);
724}
725
726bool MythCoreContext::IsThisBackend(const QString &addr)
727{
728 return IsBackend() && IsThisHost(addr);
729}
730
731bool MythCoreContext::IsThisHost(const QString &addr)
732{
733 return IsThisHost(addr, GetHostName());
734}
735
736bool MythCoreContext::IsThisHost(const QString &addr, const QString &host)
737{
738 if (addr.toLower() == host.toLower())
739 return true;
740
741 QHostAddress addrfix(addr);
742 addrfix.setScopeId(QString());
743 QString addrstr = addrfix.toString();
744
745 if (addrfix.isNull())
746 {
747 addrstr = resolveAddress(addr);
748 }
749
750 QString thisip = GetBackendServerIP(host);
751
752 return !addrstr.isEmpty() && ((addrstr == thisip));
753}
754
756{
757 // find out if a backend runs on this host...
758 bool backendOnLocalhost = false;
759
760 QStringList strlist("QUERY_IS_ACTIVE_BACKEND");
761 strlist << GetHostName();
762
763 SendReceiveStringList(strlist);
764
765 backendOnLocalhost = strlist[0] != "FALSE";
766
767 return !backendOnLocalhost;
768}
769
770QString MythCoreContext::GenMythURL(const QString& host, int port, QString path, const QString& storageGroup)
771{
772 QUrl ret;
773
774 QString m_host;
775
776 QHostAddress addr(host);
777 if (!addr.isNull())
778 {
779 LOG(VB_GENERAL, LOG_CRIT, LOC + QString("(%1/%2): Given "
780 "IP address instead of hostname "
781 "(ID). This is invalid.").arg(host, path));
782 }
783
784 m_host = host;
785
786 // Basically if it appears to be an IPv6 IP surround the IP with [] otherwise don't bother
787 if (!addr.isNull() && addr.protocol() == QAbstractSocket::IPv6Protocol)
788 m_host = "[" + addr.toString().toLower() + "]";
789
790 ret.setScheme("myth");
791 if (!storageGroup.isEmpty())
792 ret.setUserName(storageGroup);
793 ret.setHost(m_host);
794 if (port > 0 && port != 6543)
795 ret.setPort(port);
796 if (!path.startsWith("/"))
797 path = QString("/") + path;
798 ret.setPath(path);
799
800#if 0
801 LOG(VB_GENERAL, LOG_DEBUG, LOC +
802 QString("GenMythURL returning %1").arg(ret.toString()));
803#endif
804
805 return ret.toString();
806}
807
808QString MythCoreContext::GetMasterHostPrefix(const QString &storageGroup,
809 const QString &path)
810{
813 path,
814 storageGroup);
815}
816
818{
819 QMutexLocker locker(&d->m_masterHostLock);
820
821 if (d->m_masterHostname.isEmpty())
822 {
823
824 if (IsMasterBackend())
826 else
827 {
828 QStringList strlist("QUERY_HOSTNAME");
829
830 if (SendReceiveStringList(strlist))
831 d->m_masterHostname = strlist[0];
832 }
833 }
834
835 return d->m_masterHostname;
836}
837
838void MythCoreContext::ClearSettingsCache(const QString &myKey)
839{
840 d->m_database->ClearSettingsCache(myKey);
841}
842
844{
845 d->m_database->ActivateSettingsCache(activate);
846}
847
849{
850 QMutexLocker locker(&d->m_localHostLock);
851 return d->m_localHostname;
852}
853
855{
856 return GetSetting("RecordFilePrefix");
857}
858
860 int &width, int &height,
861 double &forced_aspect,
862 double &refresh_rate,
863 int index)
864{
865 d->m_database->GetResolutionSetting(type, width, height, forced_aspect,
866 refresh_rate, index);
867}
868
869void MythCoreContext::GetResolutionSetting(const QString &t, int &w,
870 int &h, int i)
871{
872 d->m_database->GetResolutionSetting(t, w, h, i);
873}
874
876{
877 return d->m_database->GetDBManager();
878}
879
887{
888 return d->m_database->IsDatabaseIgnored();
889}
890
891void MythCoreContext::SaveSetting(const QString &key, int newValue)
892{
893 d->m_database->SaveSetting(key, newValue);
894}
895
896void MythCoreContext::SaveSetting(const QString &key, const QString &newValue)
897{
898 d->m_database->SaveSetting(key, newValue);
899}
900
902 const QString &newValue,
903 const QString &host)
904{
905 return d->m_database->SaveSettingOnHost(key, newValue, host);
906}
907
908QString MythCoreContext::GetSetting(const QString &key,
909 const QString &defaultval)
910{
911 if (!m_testOverrideStrings.empty())
912 return m_testOverrideStrings[key];
913 return d->m_database->GetSetting(key, defaultval);
914}
915
916bool MythCoreContext::GetBoolSetting(const QString &key, bool defaultval)
917{
918 int result = GetNumSetting(key, static_cast<int>(defaultval));
919 return result > 0;
920}
921
922int MythCoreContext::GetNumSetting(const QString &key, int defaultval)
923{
924 if (!m_testOverrideInts.empty())
925 return m_testOverrideInts[key];
926 return d->m_database->GetNumSetting(key, defaultval);
927}
928
929double MythCoreContext::GetFloatSetting(const QString &key, double defaultval)
930{
931 if (!m_testOverrideFloats.empty())
932 return m_testOverrideFloats[key];
933 return d->m_database->GetFloatSetting(key, defaultval);
934}
935
936QString MythCoreContext::GetSettingOnHost(const QString &key,
937 const QString &host,
938 const QString &defaultval)
939{
940 if (!m_testOverrideStrings.empty())
941 return m_testOverrideStrings[key];
942 return d->m_database->GetSettingOnHost(key, host, defaultval);
943}
944
946 const QString &host,
947 bool defaultval)
948{
949 int result = GetNumSettingOnHost(key, host, static_cast<int>(defaultval));
950 return result > 0;
951}
952
954 const QString &host,
955 int defaultval)
956{
957 if (!m_testOverrideInts.empty())
958 return m_testOverrideInts[key];
959 return d->m_database->GetNumSettingOnHost(key, host, defaultval);
960}
961
963 const QString &host,
964 double defaultval)
965{
966 if (!m_testOverrideFloats.empty())
967 return m_testOverrideFloats[key];
968 return d->m_database->GetFloatSettingOnHost(key, host, defaultval);
969}
970
977{
978 QString masterserver = gCoreContext->GetSetting("MasterServerName");
979 QString masterip = resolveSettingAddress("BackendServerAddr",masterserver);
980 // Even if empty, return it here if we were to assume that localhost
981 // should be used it just causes a lot of unnecessary error messages.
982 return masterip;
983}
984
991{
992 QString masterserver = gCoreContext->GetSetting
993 ("MasterServerName");
995 ("BackendServerPort", masterserver, 6543);
996}
997
1004{
1005 QString masterhost = GetMasterHostName();
1006
1007 return GetBackendStatusPort(masterhost);
1008}
1009
1015{
1017}
1018
1028QString MythCoreContext::GetBackendServerIP(const QString &host)
1029{
1030 return resolveSettingAddress("BackendServerAddr",host);
1031}
1032
1038{
1040}
1041
1047QString MythCoreContext::GetBackendServerIP4(const QString &host)
1048{
1049 return resolveSettingAddress("BackendServerIP", host, ResolveIPv4);
1050}
1051
1057{
1059}
1060
1066QString MythCoreContext::GetBackendServerIP6(const QString &host)
1067{
1068 return resolveSettingAddress("BackendServerIP6", host, ResolveIPv6);
1069}
1070
1075{
1077}
1078
1079QHash<QString,int> MythCoreContext::s_serverPortCache;
1080
1082{
1083 s_serverPortCache.clear();
1084}
1085
1090{
1091 int port = s_serverPortCache.value(host, -1);
1092 if (port != -1)
1093 return port;
1094 port = GetNumSettingOnHost("BackendServerPort", host, 6543);
1095 s_serverPortCache[host] = port;
1096 return port;
1097}
1098
1103{
1105}
1106
1111{
1112 return GetNumSettingOnHost("BackendStatusPort", host, 6544);
1113}
1114
1119bool MythCoreContext::GetScopeForAddress(QHostAddress &addr) const
1120{
1121 QHostAddress addr1 = addr;
1122 addr1.setScopeId(QString());
1123 QString addrstr = addr1.toString();
1124 QMutexLocker lock(&d->m_scopesLock);
1125
1126 if (!d->m_scopes.contains(addrstr))
1127 return false;
1128
1129 addr.setScopeId(d->m_scopes[addrstr]);
1130 return true;
1131}
1132
1139void MythCoreContext::SetScopeForAddress(const QHostAddress &addr)
1140{
1141 QHostAddress addr1 = addr;
1142 addr1.setScopeId(QString());
1143 QMutexLocker lock(&d->m_scopesLock);
1144
1145 d->m_scopes.insert(addr1.toString(), addr.scopeId());
1146}
1147
1153void MythCoreContext::SetScopeForAddress(const QHostAddress &addr, int scope)
1154{
1155 QHostAddress addr1 = addr;
1156 addr1.setScopeId(QString());
1157 QMutexLocker lock(&d->m_scopesLock);
1158
1159 d->m_scopes.insert(addr1.toString(), QString::number(scope));
1160}
1161
1173QString MythCoreContext::resolveSettingAddress(const QString &name,
1174 const QString &host,
1175 ResolveType type, bool keepscope)
1176{
1177 QString value;
1178
1179 if (host.isEmpty())
1180 {
1181 value = GetSetting(name);
1182 }
1183 else
1184 {
1185 value = GetSettingOnHost(name, host);
1186 }
1187
1188 return resolveAddress(value, type, keepscope);
1189}
1190
1203 bool keepscope)
1204{
1205 QHostAddress addr(host);
1206
1207 if (!host.isEmpty() && addr.isNull())
1208 {
1209 // address is likely a hostname, attempts to resolve it
1210 QHostInfo info = QHostInfo::fromName(host);
1211 QList<QHostAddress> list = info.addresses();
1212
1213 if (list.isEmpty())
1214 {
1215 LOG(VB_GENERAL, LOG_WARNING, LOC +
1216 QString("Can't resolve hostname:'%1', using localhost").arg(host));
1217 return type == ResolveIPv4 ? "127.0.0.1" : "::1";
1218 }
1219 QHostAddress v4;
1220 QHostAddress v6;
1221
1222 // Return the first address fitting the type critera
1223 for (const auto& item : std::as_const(list))
1224 {
1225 addr = item;
1226 QAbstractSocket::NetworkLayerProtocol prot = addr.protocol();
1227
1228 if (prot == QAbstractSocket::IPv4Protocol)
1229 {
1230 v4 = addr;
1231 if (type == 0)
1232 break;
1233 }
1234 else if (prot == QAbstractSocket::IPv6Protocol)
1235 {
1236 v6 = addr;
1237 if (type != 0)
1238 break;
1239 }
1240 }
1241 switch (type)
1242 {
1243 case ResolveIPv4:
1244 addr = v4.isNull() ? QHostAddress::LocalHost : v4;
1245 break;
1246 case ResolveIPv6:
1247 addr = v6.isNull() ? QHostAddress::LocalHostIPv6 : v6;
1248 break;
1249 default:
1250 if (!v6.isNull())
1251 addr = v6;
1252 else if (!v4.isNull())
1253 addr = v4;
1254 else
1255 addr = QHostAddress::LocalHostIPv6;
1256 break;
1257 }
1258 }
1259 else if (host.isEmpty())
1260 {
1261 return {};
1262 }
1263
1264 if (!keepscope)
1265 {
1266 addr.setScopeId(QString());
1267 }
1268 return addr.toString();
1269}
1270
1282bool MythCoreContext::CheckSubnet(const QAbstractSocket *socket)
1283{
1284 QHostAddress peer = socket->peerAddress();
1285 return CheckSubnet(peer);
1286}
1287
1299bool MythCoreContext::CheckSubnet(const QHostAddress &peer)
1300{
1301 if (GetBoolSetting("AllowConnFromAll",false))
1302 return true;
1303 return IsLocalSubnet(peer, true);
1304}
1305
1314bool MythCoreContext::IsLocalSubnet(const QHostAddress &peer, bool log)
1315{
1316 static const QHostAddress kLinkLocal("fe80::");
1317 // Ensure m_approvedIps and m_deniedIps are single threaded
1318 QMutexLocker lock(&d->m_listMutex);
1319 if (d->m_approvedIps.contains(peer))
1320 return true;
1321 if (d->m_deniedIps.contains(peer))
1322 {
1323 if (log)
1324 LOG(VB_GENERAL, LOG_WARNING, LOC +
1325 QString("Repeat denied connection from ip address: %1")
1326 .arg(peer.toString()));
1327 return false;
1328 }
1329
1330 // allow all link-local
1331 if (peer.isInSubnet(kLinkLocal,10))
1332 {
1333 d->m_approvedIps.append(peer);
1334 return true;
1335 }
1336
1337 // loop through all available interfaces
1338 QList<QNetworkInterface> IFs = QNetworkInterface::allInterfaces();
1339 for (const auto & qni : std::as_const(IFs))
1340 {
1341 if ((qni.flags() & QNetworkInterface::IsRunning) == 0)
1342 continue;
1343
1344 QList<QNetworkAddressEntry> IPs = qni.addressEntries();
1345 for (const auto & qnai : std::as_const(IPs))
1346 {
1347 int pfxlen = qnai.prefixLength();
1348 // Set this to test rejection without having an extra
1349 // network.
1350 if (GetBoolSetting("DebugSubnet"))
1351 pfxlen += 4;
1352 if (peer.isInSubnet(qnai.ip(),pfxlen))
1353 {
1354 d->m_approvedIps.append(peer);
1355 return true;
1356 }
1357 }
1358 }
1359 d->m_deniedIps.append(peer);
1360 if (log)
1361 LOG(VB_GENERAL, LOG_WARNING, LOC +
1362 QString("Denied connection from ip address: %1")
1363 .arg(peer.toString()));
1364 return false;
1365}
1366
1367
1369 const QString &value)
1370{
1371 d->m_database->OverrideSettingForSession(key, value);
1372}
1373
1375{
1376 d->m_database->ClearOverrideSettingForSession(key);
1377}
1378
1380{
1382}
1383
1403 QStringList &strlist, bool quickTimeout, bool block)
1404{
1405 QString msg;
1406 if (HasGUI() && IsUIThread())
1407 {
1408 msg = "SendReceiveStringList(";
1409 for (uint i=0; i<(uint)strlist.size() && i<2; i++)
1410 msg += (i?",":"") + strlist[i];
1411 msg += (strlist.size() > 2) ? "...)" : ")";
1412 LOG(VB_GENERAL, LOG_DEBUG, LOC + msg + " called from UI thread");
1413 }
1414
1415 QString query_type = "UNKNOWN";
1416
1417 if (!strlist.isEmpty())
1418 query_type = strlist[0];
1419
1420 QMutexLocker locker(&d->m_sockLock);
1421 if (!d->m_serverSock)
1422 {
1423 bool blockingClient = d->m_blockingClient &&
1424 (GetNumSetting("idleTimeoutSecs",0) > 0);
1425 ConnectToMasterServer(blockingClient);
1426 }
1427
1428 bool ok = false;
1429
1430 if (d->m_serverSock)
1431 {
1432 std::chrono::milliseconds timeout = quickTimeout ?
1434 ok = d->m_serverSock->SendReceiveStringList(strlist, 0, timeout);
1435
1436 if (!ok)
1437 {
1438 LOG(VB_GENERAL, LOG_NOTICE, LOC +
1439 QString("Connection to backend server lost"));
1441 d->m_serverSock = nullptr;
1442
1443 if (d->m_eventSock)
1444 {
1445 d->m_eventSock->DecrRef();
1446 d->m_eventSock = nullptr;
1447 }
1448
1449 if (block)
1450 {
1452
1453 if (d->m_serverSock)
1454 {
1456 strlist, 0, timeout);
1457 }
1458 }
1459 }
1460
1461 // this should not happen
1462 while (ok && strlist[0] == "BACKEND_MESSAGE")
1463 {
1464 // oops, not for us
1465 LOG(VB_GENERAL, LOG_EMERG, LOC + "SRSL you shouldn't see this!!");
1466 QString message = strlist[1];
1467 strlist.pop_front(); strlist.pop_front();
1468
1469 MythEvent me(message, strlist);
1470 dispatch(me);
1471
1472 ok = d->m_serverSock->ReadStringList(strlist, timeout);
1473 }
1474
1475 if (!ok)
1476 {
1477 if (d->m_serverSock)
1478 {
1480 d->m_serverSock = nullptr;
1481 }
1482
1483 LOG(VB_GENERAL, LOG_CRIT, LOC +
1484 QString("Reconnection to backend server failed"));
1485
1486 QCoreApplication::postEvent(d->m_guiContext,
1487 new MythEvent("PERSISTENT_CONNECTION_FAILURE"));
1488 }
1489 }
1490
1491 if (ok)
1492 {
1493 if (strlist.isEmpty())
1494 ok = false;
1495 else if (strlist[0] == "ERROR")
1496 {
1497 if (strlist.size() == 2)
1498 {
1499 LOG(VB_GENERAL, LOG_INFO, LOC +
1500 QString("Protocol query '%1' responded with the error '%2'")
1501 .arg(query_type, strlist[1]));
1502 }
1503 else
1504 {
1505 LOG(VB_GENERAL, LOG_INFO, LOC +
1506 QString("Protocol query '%1' responded with an error, but "
1507 "no error message.") .arg(query_type));
1508 }
1509
1510 ok = false;
1511 }
1512 else if (strlist[0] == "UNKNOWN_COMMAND")
1513 {
1514 LOG(VB_GENERAL, LOG_ERR, LOC +
1515 QString("Protocol query '%1' responded with the error 'UNKNOWN_COMMAND'")
1516 .arg(query_type));
1517
1518 ok = false;
1519 }
1520 }
1521
1522 return ok;
1523}
1524
1525class SendAsyncMessage : public QRunnable
1526{
1527 public:
1528 SendAsyncMessage(QString msg, QStringList extra) :
1529 m_message(std::move(msg)), m_extraData(std::move(extra))
1530 {
1531 }
1532
1533 explicit SendAsyncMessage(QString msg) : m_message(std::move(msg)) { }
1534
1535 void run(void) override // QRunnable
1536 {
1537 QStringList strlist("MESSAGE");
1538 strlist << m_message;
1539 strlist << m_extraData;
1541 }
1542
1543 private:
1544 QString m_message;
1545 QStringList m_extraData;
1546};
1547
1548void MythCoreContext::SendMessage(const QString &message)
1549{
1550 if (IsBackend())
1551 {
1552 dispatch(MythEvent(message));
1553 }
1554 else
1555 {
1557 new SendAsyncMessage(message), "SendMessage");
1558 }
1559}
1560
1562{
1563 if (IsBackend())
1564 {
1565 dispatch(event);
1566 }
1567 else
1568 {
1570 new SendAsyncMessage(event.Message(), event.ExtraDataList()),
1571 "SendEvent");
1572 }
1573}
1574
1575void MythCoreContext::SendSystemEvent(const QString &msg)
1576{
1577 if (QCoreApplication::applicationName() == MYTH_APPNAME_MYTHTV_SETUP)
1578 return;
1579
1580 SendMessage(QString("SYSTEM_EVENT %1 SENDER %2")
1581 .arg(msg, GetHostName()));
1582}
1583
1585 const QString &hostname,
1586 const QString &args)
1587{
1588 SendSystemEvent(QString("%1 HOST %2 %3").arg(msg, hostname, args));
1589}
1590
1591
1593{
1594 while (sock->IsDataAvailable())
1595 {
1596 QStringList strlist;
1597 if (!sock->ReadStringList(strlist))
1598 continue;
1599
1600 if (strlist.size() < 2)
1601 continue;
1602
1603 QString prefix = strlist[0];
1604 QString message = strlist[1];
1605 QStringList tokens = message.split(" ", Qt::SkipEmptyParts);
1606
1607 if (prefix == "OK")
1608 {
1609 }
1610 else if (prefix != "BACKEND_MESSAGE")
1611 {
1612 LOG(VB_NETWORK, LOG_ERR, LOC +
1613 QString("Received a: %1 message from the backend "
1614 "but I don't know what to do with it.")
1615 .arg(prefix));
1616 }
1617 else if (message == "CLEAR_SETTINGS_CACHE")
1618 {
1619 // No need to dispatch this message to ourself, so handle it
1620 LOG(VB_NETWORK, LOG_INFO, LOC + "Received remote 'Clear Cache' request");
1622 }
1623 else if (message.startsWith("FILE_WRITTEN"))
1624 {
1625 QString file;
1626 uint64_t size = 0;
1627 int NUMTOKENS = 3; // Number of tokens expected
1628
1629 if (tokens.size() == NUMTOKENS)
1630 {
1631 file = tokens[1];
1632 size = tokens[2].toULongLong();
1633 }
1634 else
1635 {
1636 LOG(VB_NETWORK, LOG_ERR, LOC +
1637 QString("FILE_WRITTEN event received "
1638 "with invalid number of arguments, "
1639 "%1 expected, %2 actual")
1640 .arg(NUMTOKENS-1)
1641 .arg(tokens.size()-1));
1642 return;
1643 }
1644 // No need to dispatch this message to ourself, so handle it
1645 LOG(VB_NETWORK, LOG_INFO, LOC +
1646 QString("Received remote 'FILE_WRITTEN %1' request").arg(file));
1648 }
1649 else if (message.startsWith("FILE_CLOSED"))
1650 {
1651 QString file;
1652 int NUMTOKENS = 2; // Number of tokens expected
1653
1654 if (tokens.size() == NUMTOKENS)
1655 {
1656 file = tokens[1];
1657 }
1658 else
1659 {
1660 LOG(VB_NETWORK, LOG_ERR, LOC +
1661 QString("FILE_CLOSED event received "
1662 "with invalid number of arguments, "
1663 "%1 expected, %2 actual")
1664 .arg(NUMTOKENS-1)
1665 .arg(tokens.size()-1));
1666 return;
1667 }
1668 // No need to dispatch this message to ourself, so handle it
1669 LOG(VB_NETWORK, LOG_INFO, LOC +
1670 QString("Received remote 'FILE_CLOSED %1' request").arg(file));
1672 }
1673 else
1674 {
1675 strlist.pop_front();
1676 strlist.pop_front();
1677 MythEvent me(message, strlist);
1678 dispatch(me);
1679 }
1680 }
1681}
1682
1684{
1685 LOG(VB_GENERAL, LOG_NOTICE, LOC +
1686 "Event socket closed. No connection to the backend.");
1687
1688 dispatch(MythEvent("BACKEND_SOCKETS_CLOSED"));
1689}
1690
1692 std::chrono::milliseconds timeout,
1693 bool error_dialog_desired)
1694{
1695 if (!socket)
1696 return false;
1697
1698 QStringList strlist(QString("MYTH_PROTO_VERSION %1 %2")
1699 .arg(MYTH_PROTO_VERSION,
1700 QString::fromUtf8(MYTH_PROTO_TOKEN)));
1701 socket->WriteStringList(strlist);
1702
1703 if (!socket->ReadStringList(strlist, timeout) || strlist.empty())
1704 {
1705 LOG(VB_GENERAL, LOG_CRIT, "Protocol version check failure.\n\t\t\t"
1706 "The response to MYTH_PROTO_VERSION was empty.\n\t\t\t"
1707 "This happens when the backend is too busy to respond,\n\t\t\t"
1708 "or has deadlocked due to bugs or hardware failure.");
1709
1710 return false;
1711 }
1712 if (strlist[0] == "REJECT" && strlist.size() >= 2)
1713 {
1714 LOG(VB_GENERAL, LOG_CRIT, LOC + QString("Protocol version or token mismatch "
1715 "(frontend=%1/%2,backend=%3/\?\?)\n")
1716 .arg(MYTH_PROTO_VERSION,
1717 QString::fromUtf8(MYTH_PROTO_TOKEN),
1718 strlist[1]));
1719
1720 if (error_dialog_desired && d->m_guiContext)
1721 {
1722 QStringList list(strlist[1]);
1723 QCoreApplication::postEvent(
1724 d->m_guiContext, new MythEvent("VERSION_MISMATCH", list));
1725 }
1726
1727 return false;
1728 }
1729 if (strlist[0] == "ACCEPT")
1730 {
1731 if (!d->m_announcedProtocol)
1732 {
1733 d->m_announcedProtocol = true;
1734 LOG(VB_GENERAL, LOG_INFO, LOC +
1735 QString("Using protocol version %1 %2")
1736 .arg(MYTH_PROTO_VERSION,
1737 QString::fromUtf8(MYTH_PROTO_TOKEN)));
1738 }
1739
1740 return true;
1741 }
1742
1743 LOG(VB_GENERAL, LOG_ERR, LOC +
1744 QString("Unexpected response to MYTH_PROTO_VERSION: %1")
1745 .arg(strlist[0]));
1746 return false;
1747}
1748
1750{
1751 LOG(VB_NETWORK, LOG_INFO, LOC + QString("MythEvent: %1").arg(event.Message()));
1752
1754}
1755
1757{
1758 QMutexLocker locker(&d->m_localHostLock);
1760 d->m_database->SetLocalHostname(hostname);
1761}
1762
1764{
1765 d->m_guiObject = gui;
1766}
1767
1769{
1770 return d->m_guiObject;
1771}
1772
1774{
1775 return d->m_guiObject;
1776}
1777
1779{
1780 return d->m_guiContext;
1781}
1782
1784{
1785 return d->m_database;
1786}
1787
1789{
1790 return d->m_locale;
1791}
1792
1798{
1799 return GetLanguageAndVariant().left(2);
1800}
1801
1810{
1811 if (d->m_language.isEmpty())
1812 d->m_language = GetSetting("Language", "en_US").toLower();
1813
1814 return d->m_language;
1815}
1816
1818{
1819 d->m_language.clear();
1820}
1821
1827{
1828 return GetAudioLanguageAndVariant().left(2);
1829}
1830
1839{
1840 if (d->m_audioLanguage.isEmpty())
1841 {
1842 auto menuLanguage = GetLanguageAndVariant();
1843 d->m_audioLanguage = GetSetting("AudioLanguage", menuLanguage).toLower();
1844
1845 LOG(VB_AUDIO, LOG_DEBUG, LOC + QString("audio language:%1 menu language:%2")
1846 .arg(d->m_audioLanguage, menuLanguage));
1847 }
1848 return d->m_audioLanguage;
1849}
1850
1852{
1853 d->m_audioLanguage.clear();
1854}
1855
1857{
1858 QMutexLocker locker(&d->m_sockLock);
1859 LOG(VB_GENERAL, LOG_INFO, "Restarting Backend Connections");
1860 if (d->m_serverSock)
1862 if (d->m_eventSock)
1864 dispatch(MythEvent("BACKEND_SOCKETS_CLOSED"));
1865}
1866
1868{
1869 if (Create && !d->m_power)
1870 {
1872 }
1873 else if (!Create && d->m_power)
1874 {
1876 d->m_power = nullptr;
1877 }
1878}
1879
1881{
1882 if (!d->m_locale)
1883 d->m_locale = new MythLocale();
1884
1885 QString localeCode = d->m_locale->GetLocaleCode();
1886 LOG(VB_GENERAL, LOG_NOTICE, QString("Setting QT default locale to %1")
1887 .arg(localeCode));
1888 QLocale::setDefault(d->m_locale->ToQLocale());
1889}
1890
1892{
1893 if (!d->m_locale)
1894 d->m_locale = new MythLocale();
1895 else
1896 d->m_locale->ReInit();
1897
1898 QString localeCode = d->m_locale->GetLocaleCode();
1899 LOG(VB_GENERAL, LOG_NOTICE, QString("Setting QT default locale to %1")
1900 .arg(localeCode));
1901 QLocale::setDefault(d->m_locale->ToQLocale());
1902}
1903
1905{
1906 if (!d->m_locale)
1907 InitLocale();
1908
1909 return d->m_locale->ToQLocale();
1910}
1911
1913{
1914 if (!d->m_locale)
1915 InitLocale();
1916
1917 if (!d->m_locale->GetLocaleCode().isEmpty())
1918 {
1919 LOG(VB_GENERAL, LOG_INFO,
1920 QString("Current locale %1") .arg(d->m_locale->GetLocaleCode()));
1921
1923 return;
1924 }
1925
1926 LOG(VB_GENERAL, LOG_ERR, LOC +
1927 "No locale defined! We weren't able to set locale defaults.");
1928}
1929
1931{
1932 d->m_scheduler = sched;
1933}
1934
1936{
1937 return d->m_scheduler;
1938}
1939
1944void MythCoreContext::WaitUntilSignals(std::vector<CoreWaitInfo> & sigs) const
1945{
1946 if (sigs.empty())
1947 return;
1948
1949 QEventLoop eventLoop;
1950 for (const auto& s : sigs)
1951 {
1952 LOG(VB_GENERAL, LOG_DEBUG, LOC +
1953 QString("Waiting for signal %1")
1954 .arg(s.name));
1955 connect(this, s.fn, &eventLoop, &QEventLoop::quit);// clazy:exclude=connect-non-signal
1956 }
1957
1958 eventLoop.exec(QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers);
1959}
1960
1968{
1969 if (!sender || !method)
1970 return;
1971
1972 QMutexLocker lock(&d->m_playbackLock);
1973
1974 if (!d->m_playbackClients.contains(sender))
1975 {
1976 d->m_playbackClients.insert(sender, method);
1978 sender, method,
1979 Qt::BlockingQueuedConnection);
1980 }
1981}
1982
1989{
1990 QMutexLocker lock(&d->m_playbackLock);
1991
1992 if (d->m_playbackClients.contains(sender))
1993 {
1994 PlaybackStartCb method = d->m_playbackClients.value(sender);
1996 sender, method);
1997 d->m_playbackClients.remove(sender);
1998 }
1999}
2000
2008{
2009 QMutexLocker lock(&d->m_playbackLock);
2010 PlaybackStartCb method { nullptr };
2011 d->m_inwanting = true;
2012
2013 // If any registered client are in the same thread, they will deadlock, so rebuild
2014 // connections for any clients in the same thread as non-blocking connection
2015 QThread *currentThread = QThread::currentThread();
2016
2017 QMap<QObject *, PlaybackStartCb>::iterator it = d->m_playbackClients.begin();
2018 for (; it != d->m_playbackClients.end(); ++it)
2019 {
2020 if (it.key() == sender)
2021 continue; // will be done separately, no need to do it again
2022
2023 QThread *thread = it.key()->thread();
2024
2025 if (thread != currentThread)
2026 continue;
2027
2028 disconnect(this, &MythCoreContext::TVPlaybackAboutToStart, it.key(), it.value());
2029 connect(this, &MythCoreContext::TVPlaybackAboutToStart, it.key(), it.value());
2030 }
2031
2032 // disconnect sender so it won't receive the message
2033 if (d->m_playbackClients.contains(sender))
2034 {
2035 method = d->m_playbackClients.value(sender);
2036 disconnect(this, &MythCoreContext::TVPlaybackAboutToStart, sender, method);
2037 }
2038
2039 // emit signal
2041
2042 // reconnect sender
2043 if (method)
2044 {
2046 sender, method,
2047 Qt::BlockingQueuedConnection);
2048 }
2049 // Restore blocking connections
2050 for (; it != d->m_playbackClients.end(); ++it)
2051 {
2052 if (it.key() == sender)
2053 continue; // already done above, no need to do it again
2054
2055 QThread *thread = it.key()->thread();
2056
2057 if (thread != currentThread)
2058 continue;
2059
2061 it.key(), it.value());
2063 it.key(), it.value(), Qt::BlockingQueuedConnection);
2064 }
2065 d->m_inwanting = false;
2066}
2067
2074{
2075 // when called, it will be while the m_playbackLock is held
2076 // following a call to WantingPlayback
2077 d->m_intvwanting = b;
2078}
2079
2086{
2087 bool locked = d->m_playbackLock.tryLock();
2088 bool intvplayback = d->m_intvwanting;
2089
2090 if (!locked && d->m_inwanting)
2091 return true; // we're in the middle of WantingPlayback
2092
2093 if (!locked)
2094 return false;
2095
2096 d->m_playbackLock.unlock();
2097
2098 return intvplayback;
2099}
2100
2102{
2103 if (!d->m_sessionManager)
2104 {
2106 if (!d->m_sessionManager)
2109 }
2110 return d->m_sessionManager;
2111}
2112
2113bool MythCoreContext::TestPluginVersion(const QString &name,
2114 const QString &libversion,
2115 const QString &pluginversion)
2116{
2117 if (libversion == pluginversion)
2118 return true;
2119
2120 LOG(VB_GENERAL, LOG_EMERG, LOC +
2121 QString("Plugin %1 (%2) binary version does not "
2122 "match libraries (%3)")
2123 .arg(name, pluginversion, libversion));
2124 return false;
2125}
2126
2128{
2129 if (d->m_pluginmanager == pmanager)
2130 return;
2131
2132 if (d->m_pluginmanager)
2133 {
2134 delete d->m_pluginmanager;
2135 d->m_pluginmanager = nullptr;
2136 }
2137
2138 d->m_pluginmanager = pmanager;
2139}
2140
2142{
2143 return d->m_pluginmanager;
2144}
2145
2147{
2148 d->m_isexiting = exiting;
2149}
2150
2152{
2153 return d->m_isexiting;
2154}
2155
2156void MythCoreContext::RegisterFileForWrite(const QString& file, uint64_t size)
2157{
2158 QMutexLocker lock(&d->m_fileslock);
2159
2160 QPair<int64_t, uint64_t> pair(QDateTime::currentMSecsSinceEpoch(), size);
2161 d->m_fileswritten.insert(file, pair);
2162
2163 if (IsBackend())
2164 {
2165 QString message = QString("FILE_WRITTEN %1 %2").arg(file).arg(size);
2166 MythEvent me(message);
2167 dispatch(me);
2168 }
2169
2170 LOG(VB_FILE, LOG_DEBUG, LOC +
2171 QString("%1").arg(file));
2172}
2173
2175{
2176 QMutexLocker lock(&d->m_fileslock);
2177
2178 d->m_fileswritten.remove(file);
2179
2180 if (IsBackend())
2181 {
2182 QString message = QString("FILE_CLOSED %1").arg(file);
2183 MythEvent me(message);
2184 dispatch(me);
2185 }
2186
2187 LOG(VB_FILE, LOG_DEBUG, LOC +
2188 QString("%1").arg(file));
2189}
2190
2192{
2193 QMutexLocker lock(&d->m_fileslock);
2194
2195 return d->m_fileswritten.contains(file);
2196}
2197
2198/* vim: set expandtab tabstop=4 shiftwidth=4: */
DB connection pool, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:55
static void StopAllPools(void)
static MThreadPool * globalInstance(void)
void start(QRunnable *runnable, const QString &debugName, int priority=0)
static void ShutdownAllPools(void)
static void Cleanup(void)
This will print out all the running threads, call exit(1) on each and then wait up to 5 seconds total...
Definition: mthread.cpp:142
static void ThreadSetup(const QString &name)
This is to be called on startup in those few threads that haven't been ported to MThread.
Definition: mthread.cpp:221
MythSocket * m_eventSock
socket events arrive on
MythCoreContextPrivate(MythCoreContext *lparent, QString binversion, QObject *guicontext)
MythCoreContext * m_parent
QMutex m_sockLock
protects both m_serverSock and m_eventSock
MythSocket * m_serverSock
socket for sending MythProto requests
QMap< QObject *, MythCoreContext::PlaybackStartCb > m_playbackClients
QString m_masterHostname
master backend hostname
QString m_localHostname
hostname from config.xml or gethostname()
QWaitCondition m_wolInProgressWaitCondition
MythSessionManager * m_sessionManager
MythPluginManager * m_pluginmanager
bool WaitForWOL(std::chrono::milliseconds timeout=std::chrono::milliseconds::max())
If another thread has already started WOL process, wait on them...
QMutex m_masterHostLock
Locking for m_masterHostname.
MythScheduler * m_scheduler
QMutex m_scopesLock
Locking for m_masterHostname.
QMutex m_localHostLock
Locking for m_localHostname.
QMap< QString, QPair< int64_t, uint64_t > > m_fileswritten
QList< QHostAddress > m_deniedIps
QMap< QString, QString > m_scopes
Scope Id cache for Link-Local addresses.
QList< QHostAddress > m_approvedIps
This class contains the runtime context for MythTV.
void TVPlaybackAboutToStart(void)
bool IsFrontend(void) const
is this process a frontend process
void ResetAudioLanguage(void)
void UnregisterForPlayback(QObject *sender)
Unregister sender from being called when TVPlaybackAboutToStart signal is emitted.
bool IsConnectedToMaster(void)
MythDB * GetDB(void)
void setTestIntSettings(QMap< QString, int > &overrides)
void ClearSettingsCache(const QString &myKey=QString(""))
bool SafeConnectToMasterServer(bool blockingClient=true, bool openEventSocket=true)
QString resolveSettingAddress(const QString &name, const QString &host=QString(), ResolveType type=ResolveAny, bool keepscope=false)
Retrieve IP setting "name" for "host".
QString GetMasterServerIP(void)
Returns the Master Backend IP address If the address is an IPv6 address, the scope Id is removed.
int GetNumSettingOnHost(const QString &key, const QString &host, int defaultval=0)
QMap< QString, QString > m_testOverrideStrings
void readyRead(MythSocket *sock) override
QMap< QString, double > m_testOverrideFloats
void ActivateSettingsCache(bool activate=true)
bool IsLocalSubnet(const QHostAddress &peer, bool log)
Check if peer is on local subnet.
void SetWOLAllowed(bool allow)
void SendHostSystemEvent(const QString &msg, const QString &hostname, const QString &args)
bool IsDatabaseIgnored(void) const
/brief Returns true if database is being ignored.
QString GetHostName(void)
void SetScopeForAddress(const QHostAddress &addr)
Record the scope Id of the given IP address.
MythCoreContextPrivate * d
static bool BackendIsRunning(void)
a backend process is running on this host
bool GetScopeForAddress(QHostAddress &addr) const
Return the cached scope Id for the given address.
void ResetLanguage(void)
void ClearOverrideSettingForSession(const QString &key)
static QHash< QString, int > s_serverPortCache
void TVInWantingPlayback(bool b)
Let the TV class tell us if we was interrupted following a call to WantingPlayback().
void connectionClosed(MythSocket *sock) override
QString GetBackendServerIP4(void)
Returns the IPv4 address defined for the current host see GetBackendServerIP4(host)
void SaveSetting(const QString &key, int newValue)
MythSocket * ConnectCommandSocket(const QString &hostname, int port, const QString &announcement, bool *proto_mismatch=nullptr, int maxConnTry=-1, std::chrono::milliseconds setup_timeout=-1ms)
void setTestFloatSettings(QMap< QString, double > &overrides)
void SetLocalHostname(const QString &hostname)
void SetExiting(bool exiting=true)
static QString resolveAddress(const QString &host, ResolveType type=ResolveAny, bool keepscope=false)
if host is an IP address, it will be returned or resolved otherwise.
void SetAsBackend(bool backend)
QLocale GetQLocale(void)
void RegisterForPlayback(QObject *sender, PlaybackStartCb method)
Register sender for TVPlaybackAboutToStart signal.
bool IsThisHost(const QString &addr)
is this address mapped to this host
MythScheduler * GetScheduler(void)
MythCoreContext(const QString &binversion, QObject *guiContext)
QString GetMasterHostPrefix(const QString &storageGroup=QString(), const QString &path=QString())
MythSessionManager * GetSessionManager(void)
QString GetSetting(const QString &key, const QString &defaultval="")
void SendSystemEvent(const QString &msg)
QString GetAudioLanguageAndVariant(void)
Returns the user-set audio language and variant.
bool SaveSettingOnHost(const QString &key, const QString &newValue, const QString &host)
static int GetMasterServerPort(void)
Returns the Master Backend control port If no master server port has been defined in the database,...
bool IsRegisteredFileForWrite(const QString &file)
bool CheckProtoVersion(MythSocket *socket, std::chrono::milliseconds timeout=kMythSocketLongTimeout, bool error_dialog_desired=false)
QString GetAudioLanguage(void)
Returns two character ISO-639 language descriptor for audio language.
QObject * GetGUIContext(void)
bool CheckSubnet(const QAbstractSocket *socket)
Check if a socket is connected to an approved peer.
void setTestStringSettings(QMap< QString, QString > &overrides)
MythPluginManager * GetPluginManager(void)
bool InWantingPlayback(void)
Returns true if a client has requested playback.
int GetBackendServerPort(void)
Returns the locally defined backend control port.
void OverrideSettingForSession(const QString &key, const QString &value)
void RegisterFileForWrite(const QString &file, uint64_t size=0LL)
QString GetSettingOnHost(const QString &key, const QString &host, const QString &defaultval="")
QObject * GetGUIObject(void)
bool IsFrontendOnly(void)
is there a frontend, but no backend, running on this host
void BlockShutdown(void)
void WantingPlayback(QObject *sender)
All the objects that have registered using MythCoreContext::RegisterForPlayback but sender will be ca...
int GetBackendStatusPort(void)
Returns the locally defined backend status port.
bool IsBackend(void) const
is this process a backend process
double GetFloatSetting(const QString &key, double defaultval=0.0)
bool IsThisBackend(const QString &addr)
is this address mapped to this backend host
int GetMasterServerStatusPort(void)
Returns the Master Backend status port If no master server status port has been defined in the databa...
~MythCoreContext() override
QString GetBackendServerIP6(void)
Returns the IPv6 address defined for the current host see GetBackendServerIP6(host)
void SetScheduler(MythScheduler *sched)
QString GetFilePrefix(void)
void dispatch(const MythEvent &event)
bool ConnectToMasterServer(bool blockingClient=true, bool openEventSocket=true)
void SendMessage(const QString &message)
bool SetupCommandSocket(MythSocket *serverSock, const QString &announcement, std::chrono::milliseconds timeout, bool &proto_mismatch)
bool IsMasterHost(void)
is this the same host as the master
bool SendReceiveStringList(QStringList &strlist, bool quickTimeout=false, bool block=true)
Send a message to the backend and wait for a response.
static QString GenMythURL(const QString &host=QString(), int port=0, QString path=QString(), const QString &storageGroup=QString())
QString GetLanguage(void)
Returns two character ISO-639 language descriptor for UI language.
void SetPluginManager(MythPluginManager *pmanager)
bool IsWOLAllowed() const
void SetGUIObject(QObject *gui)
void SaveLocaleDefaults(void)
int GetNumSetting(const QString &key, int defaultval=0)
static bool TestPluginVersion(const QString &name, const QString &libversion, const QString &pluginversion)
QString GetBackendServerIP(void)
Returns the IP address of the locally defined backend IP.
void(QObject::*)(void) PlaybackStartCb
double GetFloatSettingOnHost(const QString &key, const QString &host, double defaultval=0.0)
void SetAsFrontend(bool frontend)
QString GetMasterHostName(void)
QMap< QString, int > m_testOverrideInts
bool IsMasterBackend(void)
is this the actual MBE process
MythSocket * ConnectEventSocket(const QString &hostname, int port)
static void ClearBackendServerPortCache()
void UnregisterFileForWrite(const QString &file)
bool IsBlockingClient(void) const
is this client blocking shutdown
MDBManager * GetDBManager(void)
void AllowShutdown(void)
void InitPower(bool Create=true)
bool GetBoolSettingOnHost(const QString &key, const QString &host, bool defaultval=false)
void WaitUntilSignals(std::vector< CoreWaitInfo > &sigs) const
Wait until any of the provided signals have been received.
void SendEvent(const MythEvent &event)
QString GetLanguageAndVariant(void)
Returns the user-set language and variant.
bool GetBoolSetting(const QString &key, bool defaultval=false)
MythLocale * GetLocale(void) const
void GetResolutionSetting(const QString &type, int &width, int &height, double &forced_aspect, double &refresh_rate, int index=-1)
bool HasGUI(void) const
This class is used as a container for messages.
Definition: mythevent.h:17
const QString & Message() const
Definition: mythevent.h:65
const QStringList & ExtraDataList() const
Definition: mythevent.h:67
void ReInit()
Definition: mythlocale.cpp:54
QLocale ToQLocale() const
Definition: mythlocale.h:29
QString GetLocaleCode() const
Name of language in that language.
Definition: mythlocale.h:27
void SaveLocaleDefaults(bool overwrite=false)
Definition: mythlocale.cpp:173
void dispatch(const MythEvent &event)
Dispatch an event to all listeners.
static MythPower * AcquireRelease(void *Reference, bool Acquire, std::chrono::seconds MinimumDelay=0s)
Definition: mythpower.cpp:74
This is an generic interface to a program scheduler.
Definition: mythscheduler.h:18
We use digest authentication because it protects the password over unprotected networks.
Definition: mythsession.h:106
static void UnlockSessions()
static void LockSessions()
Class for communcating between myth backends and frontends.
Definition: mythsocket.h:26
static constexpr std::chrono::milliseconds kLongTimeout
Definition: mythsocket.h:71
bool SendReceiveStringList(QStringList &list, uint min_reply_length=0, std::chrono::milliseconds timeoutMS=kLongTimeout)
Definition: mythsocket.cpp:330
bool ReadStringList(QStringList &list, std::chrono::milliseconds timeoutMS=kShortTimeout)
Definition: mythsocket.cpp:317
bool IsConnected(void) const
Definition: mythsocket.cpp:555
bool IsDataAvailable(void)
Definition: mythsocket.cpp:561
static constexpr std::chrono::milliseconds kShortTimeout
Definition: mythsocket.h:70
void DisconnectFromHost(void)
Definition: mythsocket.cpp:502
bool WriteStringList(const QStringList &list)
Definition: mythsocket.cpp:305
bool ConnectToHost(const QString &hostname, quint16 port)
connect to host
Definition: mythsocket.cpp:378
virtual int DecrRef(void)
Decrements reference count and deletes on 0.
SendAsyncMessage(QString msg)
void run(void) override
SendAsyncMessage(QString msg, QStringList extra)
unsigned int uint
Definition: compat.h:68
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:13
static uint32_t * tmp
Definition: goom_core.cpp:28
@ quit
Definition: lirc_client.h:30
void loggingDeregisterThread(void)
Deregister the current thread's name.
Definition: logging.cpp:726
void logStop(void)
Entry point for stopping logging for an application.
Definition: logging.cpp:688
bool is_current_thread(MThread *thread)
Use this to determine if you are in the named thread.
Definition: mthread.cpp:40
static constexpr const char * MYTH_APPNAME_MYTHTV_SETUP
Definition: mythappname.h:7
static void delete_sock(QMutexLocker< QMutex > &locker, MythSocket **s)
#define LOC
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythDB * GetMythDB(void)
Definition: mythdb.cpp:51
void DestroyMythDB(void)
Definition: mythdb.cpp:56
void ShutdownMythDownloadManager(void)
Deletes the running MythDownloadManager at program exit.
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
bool MythWakeup(const QString &wakeUpCommand, uint flags, std::chrono::seconds timeout)
@ kMSDontBlockInputDevs
avoid blocking LIRC & Joystick Menu
Definition: mythsystem.h:36
@ kMSProcessEvents
process events while waiting
Definition: mythsystem.h:39
@ kMSDontDisableDrawing
avoid disabling UI drawing
Definition: mythsystem.h:37
uint myth_system(const QString &command, uint flags, std::chrono::seconds timeout)
void MBASE_PUBLIC ShutdownMythSystemLegacy(void)
dictionary info
Definition: azlyrics.py:7
string hostname
Definition: caa.py:17
STL namespace.
None log(str msg, int level=LOGDEBUG)
Definition: xbmc.py:9
static QPair< QHostAddress, int > kLinkLocal
Definition: serverpool.cpp:27
Scheduler * sched