-
Notifications
You must be signed in to change notification settings - Fork 343
/
backendconnectionmanager.cpp
137 lines (126 loc) · 4.34 KB
/
backendconnectionmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <QCoreApplication>
#include <QThreadPool>
#include <QRunnable>
#include <QString>
#include <QEvent>
#include <QTimer>
#include "mythcorecontext.h"
#include "mythdialogbox.h"
#include "mythscreenstack.h"
#include "mythmainwindow.h"
#include "exitcodes.h"
#include "util.h" // for checkTimeZone()
#include "backendconnectionmanager.h"
#include "mythlogging.h"
class Reconnect : public QRunnable
{
public:
Reconnect()
{
setAutoDelete(false);
}
virtual void run(void)
{
threadRegister("Reconnect");
if (gCoreContext->GetMasterHostPrefix().isEmpty())
gCoreContext->dispatch(MythEvent(QString("RECONNECT_FAILURE")));
else
gCoreContext->dispatch(MythEvent(QString("RECONNECT_SUCCESS")));
threadDeregister();
}
};
BackendConnectionManager::BackendConnectionManager() :
QObject(), m_reconnecting(NULL), m_reconnect_timer(NULL), m_first_time(true)
{
gCoreContext->addListener(this);
uint reconnect_timeout = 1;
m_reconnect_timer = new QTimer(this);
m_reconnect_timer->setSingleShot(true);
connect(m_reconnect_timer, SIGNAL(timeout()),
this, SLOT(ReconnectToBackend()));
m_reconnect_timer->start(reconnect_timeout);
}
BackendConnectionManager::~BackendConnectionManager()
{
while (m_reconnecting)
usleep(250*1000);
gCoreContext->removeListener(this);
}
void BackendConnectionManager::customEvent(QEvent *event)
{
bool reconnect = false;
uint reconnect_timeout = 5000;
if ((MythEvent::Type)(event->type()) == MythEvent::MythEventMessage)
{
MythEvent *me = (MythEvent *)event;
QString message = me->Message();
if (message == "BACKEND_SOCKETS_CLOSED")
{
if (!m_reconnecting)
{
reconnect = true;
reconnect_timeout = 500;
}
}
else if (message == "RECONNECT_SUCCESS")
{
delete m_reconnecting;
m_reconnecting = NULL;
if (m_first_time && !checkTimeZone())
{
// Check for different time zones,
// different offsets, different times
LOG(VB_GENERAL, LOG_ERR,
"The time and/or time zone settings on this "
"system do not match those in use on the master "
"backend. Please ensure all frontend and backend "
"systems are configured to use the same time "
"zone and have the current time properly set.");
LOG(VB_GENERAL, LOG_ERR,
"Unable to run with invalid time settings. Exiting.");
MythScreenStack *popupStack = GetMythMainWindow()->
GetStack("popup stack");
QString message = tr("Your frontend and backend are configured "
"in different timezones. You must "
"correct this mismatch to continue.");
MythConfirmationDialog *error = new MythConfirmationDialog(
popupStack, message, false);
if (error->Create())
{
QObject::connect(error, SIGNAL(haveResult(bool)),
qApp, SLOT(quit()));
popupStack->AddScreen(error);
}
else
{
delete error;
delete popupStack;
qApp->exit(GENERIC_EXIT_INVALID_TIMEZONE);
}
}
m_first_time = false;
}
else if (message == "RECONNECT_FAILURE")
{
delete m_reconnecting;
m_reconnecting = NULL;
reconnect = true;
}
}
if (reconnect)
{
if (!m_reconnect_timer)
{
m_reconnect_timer = new QTimer(this);
m_reconnect_timer->setSingleShot(true);
connect(m_reconnect_timer, SIGNAL(timeout()),
this, SLOT(ReconnectToBackend()));
}
m_reconnect_timer->start(reconnect_timeout);
}
}
void BackendConnectionManager::ReconnectToBackend(void)
{
m_reconnecting = new Reconnect();
QThreadPool::globalInstance()->start(m_reconnecting);
}