MythTV  master
outboundhandler.cpp
Go to the documentation of this file.
1 #include <chrono>
2 
3 #include <QTimer>
4 #include <QString>
5 #include <QStringList>
6 
7 #include "mythsocket.h"
8 #include "mythsocketmanager.h"
9 #include "socketrequesthandler.h"
10 #include "sockethandler.h"
11 #include "mythlogging.h"
12 #include "mythcorecontext.h"
13 #include "compat.h"
14 
16 
17 using namespace std::chrono_literals;
18 
20 {
21  m_timer.setSingleShot(true);
22  connect(&m_timer, &QTimer::timeout, this, &OutboundRequestHandler::ConnectToMaster);
23 }
24 
26 {
27  m_timer.stop();
28  if (!DoConnectToMaster())
29  m_timer.start(5s);
30 }
31 
33 {
34  if (m_socket)
35  m_socket->DecrRef();
36 
37  m_socket = new MythSocket(-1, m_parent);
38 
39  QString server = gCoreContext->GetMasterServerIP();
42 
43  if (!m_socket->ConnectToHost(server, port))
44  {
45  LOG(VB_GENERAL, LOG_ERR, "Failed to connect to master backend.");
46  m_socket->DecrRef();
47  m_socket = nullptr;
48  return false;
49  }
50 
51 #ifndef IGNORE_PROTO_VER_MISMATCH
52  if (!m_socket->Validate())
53  {
54  LOG(VB_GENERAL, LOG_NOTICE, "Unable to confirm protocol version with backend.");
55  m_socket->DecrRef();
56  m_socket = nullptr;
57  return false;
58  }
59 #endif
60 
61  if (!AnnounceSocket())
62  {
63  LOG(VB_GENERAL, LOG_NOTICE, "Announcement to upstream master backend failed.");
64  m_socket->DecrRef();
65  m_socket = nullptr;
66  return false;
67  }
68 
69  auto *handler = new SocketHandler(m_socket, m_parent, hostname);
70  handler->BlockShutdown(true);
71  handler->AllowStandardEvents(true);
72  handler->AllowSystemEvents(true);
73  m_parent->AddSocketHandler(handler); // register socket for reception of events
74  handler->DecrRef(); // drop local instance in counter
75  handler = nullptr;
76 
77  LOG(VB_GENERAL, LOG_NOTICE, "Connected to master backend.");
78 
79  return true;
80 }
81 
83 {
84  // connection has closed, trigger an immediate reconnection
85  if (socket == m_socket)
86  ConnectToMaster();
87 }
outboundhandler.h
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:103
MythCoreContext::GetMasterHostName
QString GetMasterHostName(void)
Definition: mythcorecontext.cpp:828
OutboundRequestHandler::ConnectToMaster
void ConnectToMaster(void)
Definition: outboundhandler.cpp:25
mythsocketmanager.h
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
socketrequesthandler.h
MythSocket
Class for communcating between myth backends and frontends.
Definition: mythsocket.h:27
OutboundRequestHandler::OutboundRequestHandler
OutboundRequestHandler(void)
Definition: outboundhandler.cpp:19
mythlogging.h
MythCoreContext::GetMasterServerIP
QString GetMasterServerIP(void)
Returns the Master Backend IP address If the address is an IPv6 address, the scope Id is removed.
Definition: mythcorecontext.cpp:987
compat.h
sockethandler.h
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
MythCoreContext::GetMasterServerPort
static int GetMasterServerPort(void)
Returns the Master Backend control port If no master server port has been defined in the database,...
Definition: mythcorecontext.cpp:1001
mythcorecontext.h
OutboundRequestHandler::connectionClosed
void connectionClosed(MythSocket *socket) override
Definition: outboundhandler.cpp:82
SocketHandler
Definition: sockethandler.h:17
musicbrainzngs.caa.hostname
string hostname
Definition: caa.py:17
OutboundRequestHandler::DoConnectToMaster
bool DoConnectToMaster(void)
Definition: outboundhandler.cpp:32
mythsocket.h