Skip to content

Commit

Permalink
Add shell class for maintaining outbound connections to the master
Browse files Browse the repository at this point in the history
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
wagnerrp committed Jul 5, 2011
1 parent 541d380 commit 73786aa
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/mythversion.h
Expand Up @@ -12,7 +12,7 @@
/// Update this whenever the plug-in API changes.
/// Including changes in the libmythbase, libmyth, libmythtv, libmythav* and
/// libmythui class methods used by plug-ins.
#define MYTH_BINARY_VERSION "0.25.20110703-1"
#define MYTH_BINARY_VERSION "0.25.20110705-1"

/** \brief Increment this whenever the MythTV network protocol changes.
*
Expand Down
4 changes: 2 additions & 2 deletions mythtv/libs/libmythprotoserver/libmythprotoserver.pro
Expand Up @@ -14,13 +14,13 @@ QMAKE_CLEAN += $(TARGET)
HEADERS += mythprotoserverexp.h
HEADERS += mythsocketmanager.h socketrequesthandler.h sockethandler.h

HEADERS += requesthandler/basehandler.h
HEADERS += requesthandler/basehandler.h requesthandler/outboundhandler.h

HEADERS += requesthandler/fileserverhandler.h requesthandler/deletethread.h
HEADERS += requesthandler/fileserverutil.h sockethandler/filetransfer.h

SOURCES += mythsocketmanager.cpp sockethandler.cpp
SOURCES += requesthandler/basehandler.cpp
SOURCES += requesthandler/basehandler.cpp requesthandler/outboundhandler.cpp

SOURCES += requesthandler/fileserverhandler.cpp requesthandler/deletethread.cpp
SOURCES += requesthandler/fileserverutil.cpp sockethandler/filetransfer.cpp
Expand Down
91 changes: 91 additions & 0 deletions mythtv/libs/libmythprotoserver/requesthandler/outboundhandler.cpp
@@ -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 mythtv/libs/libmythprotoserver/requesthandler/outboundhandler.h
@@ -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;
};


0 comments on commit 73786aa

Please sign in to comment.