MythTV  master
backendconnectionmanager.cpp
Go to the documentation of this file.
1 
3 
4 #include <QCoreApplication>
5 #include <QRunnable>
6 #include <QString>
7 #include <QEvent>
8 #include <QTimer>
9 
10 #include "mythcorecontext.h"
11 #include "mythdialogbox.h"
12 #include "mythscreenstack.h"
13 #include "mythmainwindow.h"
14 #include "mthreadpool.h"
15 #include "mythlogging.h"
16 #include "exitcodes.h"
17 #include "mythtimezone.h"
18 
19 #include <chrono> // for milliseconds
20 #include <thread> // for sleep_for
21 
22 using namespace MythTZ;
23 
24 class Reconnect : public QRunnable
25 {
26  public:
28  {
29  setAutoDelete(false);
30  }
31 
32  void run(void) override // QRunnable
33  {
35  gCoreContext->dispatch(MythEvent(QString("RECONNECT_FAILURE")));
36  else
37  gCoreContext->dispatch(MythEvent(QString("RECONNECT_SUCCESS")));
38  }
39 };
40 
42 {
43  setObjectName("BackendConnectionManager");
45 
46  uint reconnect_timeout = 1;
47  m_reconnectTimer = new QTimer(this);
48  m_reconnectTimer->setSingleShot(true);
49  connect(m_reconnectTimer, SIGNAL(timeout()),
50  this, SLOT(ReconnectToBackend()));
51  m_reconnectTimer->start(reconnect_timeout);
52 }
53 
55 {
56  while (m_reconnecting)
57  std::this_thread::sleep_for(std::chrono::milliseconds(250));
59 }
60 
62 {
63  bool reconnect = false;
64  uint reconnect_timeout = 5000;
65 
66  if (event->type() == MythEvent::MythEventMessage)
67  {
68  auto *me = dynamic_cast<MythEvent *>(event);
69  if (me == nullptr)
70  return;
71 
72  const QString& message = me->Message();
73 
74  if (message == "BACKEND_SOCKETS_CLOSED")
75  {
76  LOG(VB_SOCKET, LOG_INFO, "Got BACKEND_SOCKETS_CLOSED message");
77 
78  if (!m_reconnecting)
79  {
80  LOG(VB_SOCKET, LOG_INFO, "Will reconnect");
81  reconnect = true;
82  reconnect_timeout = 500;
83  }
84  else
85  {
86  LOG(VB_SOCKET, LOG_INFO, "Already reconnecting");
87  m_reconnectAgain = true;
88  }
89  }
90  else if ((message == "RECONNECT_SUCCESS") ||
91  (message == "RECONNECT_FAILURE"))
92  {
93  LOG(VB_SOCKET, LOG_INFO, QString("Got %1 message")
94  .arg(message));
95 
96  delete m_reconnecting;
97  m_reconnecting = nullptr;
98 
99  if (!m_reconnectAgain)
100  {
101  m_reconnectAgain = message == "RECONNECT_FAILURE";
102  }
103  reconnect = m_reconnectAgain;
104  m_reconnectAgain = message == "RECONNECT_FAILURE";
105  }
106  }
107 
108  if (reconnect)
109  {
110  if (!m_reconnectTimer)
111  {
112  m_reconnectTimer = new QTimer(this);
113  m_reconnectTimer->setSingleShot(true);
114  connect(m_reconnectTimer, SIGNAL(timeout()),
115  this, SLOT(ReconnectToBackend()));
116  }
117  m_reconnectTimer->start(reconnect_timeout);
118  }
119 }
120 
122 {
123  LOG(VB_SOCKET, LOG_INFO, "Reconnecting");
124  m_reconnecting = new Reconnect();
125  MThreadPool::globalInstance()->start(m_reconnecting, "Reconnect");
126 }
Reconnect::Reconnect
Reconnect()
Definition: backendconnectionmanager.cpp:27
MythEvent::MythEventMessage
static Type MythEventMessage
Definition: mythevent.h:73
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
MythCoreContext::IsBlockingClient
bool IsBlockingClient(void) const
is this client blocking shutdown
Definition: mythcorecontext.cpp:642
mythscreenstack.h
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:17
mythdialogbox.h
arg
arg(title).arg(filename).arg(doDelete))
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
BackendConnectionManager::~BackendConnectionManager
~BackendConnectionManager() override
Definition: backendconnectionmanager.cpp:54
MythCoreContext::SafeConnectToMasterServer
bool SafeConnectToMasterServer(bool blockingClient=true, bool openEventSocket=true)
Definition: mythcorecontext.cpp:363
BackendConnectionManager::customEvent
void customEvent(QEvent *event) override
Definition: backendconnectionmanager.cpp:61
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:41
MythTZ
Definition: mythtimezone.cpp:18
uint
unsigned int uint
Definition: compat.h:140
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:57
mthreadpool.h
mythcorecontext.h
Reconnect
Definition: backendconnectionmanager.cpp:25
Reconnect::run
void run(void) override
Definition: backendconnectionmanager.cpp:32
BackendConnectionManager::ReconnectToBackend
void ReconnectToBackend(void)
Definition: backendconnectionmanager.cpp:121
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:1733
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