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
13 #include "libmythbase/exitcodes.h"
21 
22 // MythFrontend
24 
25 
26 using namespace MythTZ;
27 
28 class 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");
128  m_reconnecting = new Reconnect();
130 }
Reconnect::Reconnect
Reconnect()
Definition: backendconnectionmanager.cpp:31
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:102
MythEvent::kMythEventMessage
static const Type kMythEventMessage
Definition: mythevent.h:79
MythCoreContext::IsBlockingClient
bool IsBlockingClient(void) const
is this client blocking shutdown
Definition: mythcorecontext.cpp:628
mythscreenstack.h
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:16
mythdialogbox.h
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
BackendConnectionManager::~BackendConnectionManager
~BackendConnectionManager() override
Definition: backendconnectionmanager.cpp:58
MythCoreContext::SafeConnectToMasterServer
bool SafeConnectToMasterServer(bool blockingClient=true, bool openEventSocket=true)
Definition: mythcorecontext.cpp:346
BackendConnectionManager::customEvent
void customEvent(QEvent *event) override
Definition: backendconnectionmanager.cpp:65
MythEvent::Message
const QString & Message() const
Definition: mythevent.h:65
MythObservable::addListener
void addListener(QObject *listener)
Add a listener to the observable.
Definition: mythobservable.cpp:38
mythlogging.h
BackendConnectionManager::m_reconnectAgain
bool m_reconnectAgain
Definition: backendconnectionmanager.h:23
BackendConnectionManager::BackendConnectionManager
BackendConnectionManager()
Definition: backendconnectionmanager.cpp:45
MythTZ
Definition: mythtimezone.cpp:17
uint
unsigned int uint
Definition: compat.h:81
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
BackendConnectionManager::m_reconnectTimer
QTimer * m_reconnectTimer
Definition: backendconnectionmanager.h:22
mthreadpool.h
mythcorecontext.h
Reconnect
Definition: backendconnectionmanager.cpp:28
Reconnect::run
void run(void) override
Definition: backendconnectionmanager.cpp:36
BackendConnectionManager::ReconnectToBackend
void ReconnectToBackend(void)
Definition: backendconnectionmanager.cpp:125
exitcodes.h
backendconnectionmanager.h
MThreadPool::globalInstance
static MThreadPool * globalInstance(void)
Definition: mthreadpool.cpp:307
mythmainwindow.h
MythCoreContext::dispatch
void dispatch(const MythEvent &event)
Definition: mythcorecontext.cpp:1727
MythObservable::removeListener
void removeListener(QObject *listener)
Remove a listener to the observable.
Definition: mythobservable.cpp:55
mythtimezone.h
MThreadPool::start
void start(QRunnable *runnable, const QString &debugName, int priority=0)
Definition: mthreadpool.cpp:342
BackendConnectionManager::m_reconnecting
Reconnect * m_reconnecting
Definition: backendconnectionmanager.h:21