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_reconnect_timer = new QTimer(this);
48  m_reconnect_timer->setSingleShot(true);
49  connect(m_reconnect_timer, SIGNAL(timeout()),
50  this, SLOT(ReconnectToBackend()));
51  m_reconnect_timer->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  MythEvent *me = static_cast<MythEvent *>(event);
69  const QString& message = me->Message();
70 
71  if (message == "BACKEND_SOCKETS_CLOSED")
72  {
73  LOG(VB_SOCKET, LOG_INFO, "Got BACKEND_SOCKETS_CLOSED message");
74 
75  if (!m_reconnecting)
76  {
77  LOG(VB_SOCKET, LOG_INFO, "Will reconnect");
78  reconnect = true;
79  reconnect_timeout = 500;
80  }
81  else
82  {
83  LOG(VB_SOCKET, LOG_INFO, "Already reconnecting");
84  m_reconnect_again = true;
85  }
86  }
87  else if ((message == "RECONNECT_SUCCESS") ||
88  (message == "RECONNECT_FAILURE"))
89  {
90  LOG(VB_SOCKET, LOG_INFO, QString("Got %1 message")
91  .arg(message));
92 
93  delete m_reconnecting;
94  m_reconnecting = nullptr;
95 
96  if (!m_reconnect_again)
97  {
98  m_reconnect_again = message == "RECONNECT_FAILURE";
99  }
100  reconnect = m_reconnect_again;
101  m_reconnect_again = message == "RECONNECT_FAILURE";
102  }
103  }
104 
105  if (reconnect)
106  {
107  if (!m_reconnect_timer)
108  {
109  m_reconnect_timer = new QTimer(this);
110  m_reconnect_timer->setSingleShot(true);
111  connect(m_reconnect_timer, SIGNAL(timeout()),
112  this, SLOT(ReconnectToBackend()));
113  }
114  m_reconnect_timer->start(reconnect_timeout);
115  }
116 }
117 
119 {
120  LOG(VB_SOCKET, LOG_INFO, "Reconnecting");
121  m_reconnecting = new Reconnect();
122  MThreadPool::globalInstance()->start(m_reconnecting, "Reconnect");
123 }
static Type MythEventMessage
Definition: mythevent.h:66
void removeListener(QObject *listener)
Remove a listener to the observable.
bool IsBlockingClient(void) const
is this client blocking shutdown
unsigned int uint
Definition: compat.h:140
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
void addListener(QObject *listener)
Add a listener to the observable.
void run(void) override
This class is used as a container for messages.
Definition: mythevent.h:16
void dispatch(const MythEvent &event)
static MThreadPool * globalInstance(void)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
void start(QRunnable *runnable, const QString &debugName, int priority=0)
bool SafeConnectToMasterServer(bool blockingClient=true, bool openEventSocket=true)
const QString & Message() const
Definition: mythevent.h:58
void customEvent(QEvent *) override