MythTV master
backendconnectionmanager.cpp
Go to the documentation of this file.
1// C++
2#include <chrono> // for milliseconds
3#include <thread> // for sleep_for
4
5// Qt
6#include <QCoreApplication>
7#include <QRunnable>
8#include <QString>
9#include <QEvent>
10#include <QTimer>
11
12// MythTV
21
22// MythFrontend
24
25
26using namespace MythTZ;
27
28class Reconnect : public QRunnable
29{
30 public:
32 {
33 setAutoDelete(false);
34 }
35
36 void run(void) override // QRunnable
37 {
39 gCoreContext->dispatch(MythEvent(QString("RECONNECT_FAILURE")));
40 else
41 gCoreContext->dispatch(MythEvent(QString("RECONNECT_SUCCESS")));
42 }
43};
44
46 : m_reconnectTimer(new QTimer(this))
47{
48 setObjectName("BackendConnectionManager");
50
51 uint reconnect_timeout = 1;
52 m_reconnectTimer->setSingleShot(true);
55 m_reconnectTimer->start(reconnect_timeout);
56}
57
59{
60 while (m_reconnecting)
61 std::this_thread::sleep_for(250ms);
63}
64
66{
67 bool reconnect = false;
68 uint reconnect_timeout = 5000;
69
70 if (event->type() == MythEvent::kMythEventMessage)
71 {
72 auto *me = dynamic_cast<MythEvent *>(event);
73 if (me == nullptr)
74 return;
75
76 const QString& message = me->Message();
77
78 if (message == "BACKEND_SOCKETS_CLOSED")
79 {
80 LOG(VB_SOCKET, LOG_INFO, "Got BACKEND_SOCKETS_CLOSED message");
81
82 if (!m_reconnecting)
83 {
84 LOG(VB_SOCKET, LOG_INFO, "Will reconnect");
85 reconnect = true;
86 reconnect_timeout = 500;
87 }
88 else
89 {
90 LOG(VB_SOCKET, LOG_INFO, "Already reconnecting");
91 m_reconnectAgain = true;
92 }
93 }
94 else if ((message == "RECONNECT_SUCCESS") ||
95 (message == "RECONNECT_FAILURE"))
96 {
97 LOG(VB_SOCKET, LOG_INFO, QString("Got %1 message")
98 .arg(message));
99
100 delete m_reconnecting;
101 m_reconnecting = nullptr;
102
103 if (!m_reconnectAgain)
104 {
105 m_reconnectAgain = message == "RECONNECT_FAILURE";
106 }
107 reconnect = m_reconnectAgain;
108 m_reconnectAgain = message == "RECONNECT_FAILURE";
109 }
110 }
111
112 if (reconnect)
113 {
114 if (!m_reconnectTimer)
115 {
116 m_reconnectTimer = new QTimer(this);
117 m_reconnectTimer->setSingleShot(true);
120 }
121 m_reconnectTimer->start(reconnect_timeout);
122 }
123}
124
126{
127 LOG(VB_SOCKET, LOG_INFO, "Reconnecting");
130}
void customEvent(QEvent *event) override
static MThreadPool * globalInstance(void)
void start(QRunnable *runnable, const QString &debugName, int priority=0)
bool SafeConnectToMasterServer(bool blockingClient=true, bool openEventSocket=true)
void dispatch(const MythEvent &event)
bool IsBlockingClient(void) const
is this client blocking shutdown
This class is used as a container for messages.
Definition: mythevent.h:17
const QString & Message() const
Definition: mythevent.h:65
static const Type kMythEventMessage
Definition: mythevent.h:79
void addListener(QObject *listener)
Add a listener to the observable.
void removeListener(QObject *listener)
Remove a listener to the observable.
void run(void) override
unsigned int uint
Definition: freesurround.h:24
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39