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 {
47  setObjectName("BackendConnectionManager");
49 
50  uint reconnect_timeout = 1;
51  m_reconnectTimer = new QTimer(this);
52  m_reconnectTimer->setSingleShot(true);
53  connect(m_reconnectTimer, &QTimer::timeout,
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::MythEventMessage)
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);
118  connect(m_reconnectTimer, &QTimer::timeout,
120  }
121  m_reconnectTimer->start(reconnect_timeout);
122  }
123 }
124 
126 {
127  LOG(VB_SOCKET, LOG_INFO, "Reconnecting");
128  m_reconnecting = new Reconnect();
129  MThreadPool::globalInstance()->start(m_reconnecting, "Reconnect");
130 }
Reconnect::Reconnect
Reconnect()
Definition: backendconnectionmanager.cpp:31
MythEvent::MythEventMessage
static Type MythEventMessage
Definition: mythevent.h:79
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
MythCoreContext::IsBlockingClient
bool IsBlockingClient(void) const
is this client blocking shutdown
Definition: mythcorecontext.cpp:622
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:344
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::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:54
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:317
mythmainwindow.h
MythCoreContext::dispatch
void dispatch(const MythEvent &event)
Definition: mythcorecontext.cpp:1723
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:352