00001
00002 using namespace std;
00003
00004 #include <QTimer>
00005 #include <QString>
00006 #include <QStringList>
00007
00008 #include "mythsocket.h"
00009 #include "mythsocketmanager.h"
00010 #include "socketrequesthandler.h"
00011 #include "sockethandler.h"
00012 #include "mythlogging.h"
00013 #include "mythcorecontext.h"
00014 #include "compat.h"
00015
00016 #include "requesthandler/outboundhandler.h"
00017
00018 OutboundRequestHandler::OutboundRequestHandler(void) :
00019 m_socket(NULL)
00020 {
00021 m_timer.setSingleShot(true);
00022 connect(&m_timer, SIGNAL(timeout()), this, SLOT(ConnectToMaster()));
00023 }
00024
00025 void OutboundRequestHandler::ConnectToMaster(void)
00026 {
00027 m_timer.stop();
00028 if (!DoConnectToMaster())
00029 m_timer.start(5000);
00030 }
00031
00032 bool OutboundRequestHandler::DoConnectToMaster(void)
00033 {
00034 if (m_socket)
00035 m_socket->DownRef();
00036
00037 m_socket = new MythSocket();
00038
00039 while (m_socket->state() != MythSocket::Idle)
00040 {
00041 usleep(5000);
00042 }
00043
00044 QString server = gCoreContext->GetSetting("MasterServerIP", "localhost");
00045 QString hostname = gCoreContext->GetMasterHostName();
00046 int port = gCoreContext->GetNumSetting("MasterServerPort", 6543);
00047
00048 if (!m_socket->connect(server, port))
00049 {
00050 LOG(VB_GENERAL, LOG_ERR, "Failed to connect to master backend.");
00051 m_socket->DownRef();
00052 m_socket = NULL;
00053 return false;
00054 }
00055
00056 m_socket->Lock();
00057
00058 #ifndef IGNORE_PROTO_VER_MISMATCH
00059 if (!m_socket->Validate())
00060 {
00061 LOG(VB_GENERAL, LOG_NOTICE, "Unable to confirm protocol version with backend.");
00062 m_socket->DownRef();
00063 m_socket = NULL;
00064 return false;
00065 }
00066 #endif
00067
00068 if (!AnnounceSocket())
00069 {
00070 LOG(VB_GENERAL, LOG_NOTICE, "Announcement to upstream master backend failed.");
00071 m_socket->DownRef();
00072 m_socket = NULL;
00073 return false;
00074 }
00075
00076 SocketHandler *handler = new SocketHandler(m_socket, m_parent, hostname);
00077 handler->BlockShutdown(true);
00078 handler->AllowStandardEvents(true);
00079 handler->AllowSystemEvents(true);
00080 m_parent->AddSocketHandler(handler);
00081 handler->DownRef();
00082
00083 m_socket->Unlock();
00084 m_parent->newConnection(m_socket);
00085
00086 LOG(VB_GENERAL, LOG_NOTICE, "Connected to master backend.");
00087
00088 return true;
00089 }
00090
00091 void OutboundRequestHandler::connectionClosed(MythSocket *socket)
00092 {
00093
00094 if (socket == m_socket)
00095 ConnectToMaster();
00096 }