Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shell class for maintaining outbound connections to the master
server within the mechanics of libmythprotoserver. This is intended to receive inbound commands and events, used to manage slave backends, mediaservers, jobqueues, and any future application the master backend needs direct control over. In order to use, the virtual AnnounceSocket should be overridden and send the proper announce string to the master backend using the supplied socket. The ConnectToMaster() method is used to create the initial connection, and must not be called until after the request handler is registered to the socket manager. After which, the socket will automatically attempt reconnection every five seconds if it gets disconnected.
- Loading branch information
Showing
4 changed files
with
130 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
mythtv/libs/libmythprotoserver/requesthandler/outboundhandler.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
|
||
using namespace std; | ||
|
||
#include <QTimer> | ||
#include <QString> | ||
#include <QStringList> | ||
|
||
#include "mythsocket.h" | ||
#include "mythsocketmanager.h" | ||
#include "socketrequesthandler.h" | ||
#include "sockethandler.h" | ||
#include "mythlogging.h" | ||
#include "mythcorecontext.h" | ||
|
||
#include "requesthandler/outboundhandler.h" | ||
|
||
OutboundRequestHandler::OutboundRequestHandler(void) : | ||
m_socket(NULL) | ||
{ | ||
m_timer.setSingleShot(true); | ||
connect(&m_timer, SIGNAL(timeout()), this, SLOT(ConnectToMaster())); | ||
} | ||
|
||
void OutboundRequestHandler::ConnectToMaster(void) | ||
{ | ||
m_timer.stop(); | ||
if (!DoConnectToMaster()) | ||
m_timer.start(5000); | ||
} | ||
|
||
bool OutboundRequestHandler::DoConnectToMaster(void) | ||
{ | ||
if (m_socket) | ||
m_socket->DownRef(); | ||
|
||
m_socket = new MythSocket(); | ||
|
||
while (m_socket->state() != MythSocket::Idle) | ||
{ | ||
usleep(5000); | ||
} | ||
|
||
QString server = gCoreContext->GetSetting("MasterServerIP", "localhost"); | ||
int port = gCoreContext->GetNumSetting("MasterServerPort", 6543); | ||
|
||
if (!m_socket->connect(server, port)) | ||
{ | ||
LOG(VB_GENERAL, LOG_ERR, "Failed to connect to master backend."); | ||
m_socket->DownRef(); | ||
m_socket = NULL; | ||
return false; | ||
} | ||
|
||
m_socket->Lock(); | ||
|
||
if (!m_socket->Validate()) | ||
{ | ||
LOG(VB_GENERAL, LOG_NOTICE, "Unable to confirm protocol version with backend."); | ||
m_socket->DownRef(); | ||
m_socket = NULL; | ||
return false; | ||
} | ||
|
||
if (!AnnounceSocket()) | ||
{ | ||
LOG(VB_GENERAL, LOG_NOTICE, "Announcement to upstream master backend failed."); | ||
m_socket->DownRef(); | ||
m_socket = NULL; | ||
return false; | ||
} | ||
|
||
SocketHandler *handler = new SocketHandler(m_socket, m_parent, server); | ||
handler->BlockShutdown(true); | ||
handler->AllowStandardEvents(true); | ||
handler->AllowSystemEvents(true); | ||
m_parent->AddSocketHandler(handler); // register socket for reception of events | ||
|
||
m_socket->Unlock(); | ||
m_parent->newConnection(m_socket); // configure callbacks | ||
|
||
LOG(VB_GENERAL, LOG_NOTICE, "Connected to master backend."); | ||
|
||
return true; | ||
} | ||
|
||
void OutboundRequestHandler::connectionClosed(MythSocket *socket) | ||
{ | ||
// connection has closed, trigger an immediate reconnection | ||
if (socket == m_socket) | ||
ConnectToMaster(); | ||
} |
36 changes: 36 additions & 0 deletions
36
mythtv/libs/libmythprotoserver/requesthandler/outboundhandler.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
using namespace std; | ||
|
||
#include <QTimer> | ||
#include <QString> | ||
#include <QStringList> | ||
|
||
#include "mythsocket.h" | ||
#include "mythsocketmanager.h" | ||
#include "socketrequesthandler.h" | ||
#include "sockethandler.h" | ||
#include "mythprotoserverexp.h" | ||
|
||
class MainServer; | ||
|
||
class PROTOSERVER_PUBLIC OutboundRequestHandler : public SocketRequestHandler | ||
{ | ||
Q_OBJECT | ||
public: | ||
OutboundRequestHandler(void); | ||
|
||
QString GetHandlerName(void) { return "OUTBOUND"; } | ||
virtual bool AnnounceSocket(void) { return false; } | ||
void connectionClosed(MythSocket *socket); | ||
|
||
public slots: | ||
void ConnectToMaster(void); | ||
|
||
private: | ||
bool DoConnectToMaster(void); | ||
|
||
MythSocket *m_socket; | ||
QTimer m_timer; | ||
}; | ||
|
||
|