Ticket #750: zeroconfig_patch.patch

File zeroconfig_patch.patch, 14.1 KB (added by Sean.Jensen@…, 18 years ago)

The patch to add ZeroConfig? functionality tested on svn rev: 8106

  • libs/libmyth/libmyth.pro

     
    2222HEADERS += langsettings.h audiooutputnull.h
    2323HEADERS += DisplayResScreen.h util-x11.h mythdeque.h qmdcodec.h
    2424HEADERS += exitcodes.h virtualkeyboard.h
     25HEADERS += zeroconfigclient.h
    2526
    2627SOURCES += dialogbox.cpp lcddevice.cpp mythcontext.cpp mythwidgets.cpp
    2728SOURCES += oldsettings.cpp remotefile.cpp settings.cpp themedmenu.cpp
     
    3233SOURCES += volumecontrol.cpp volumebase.cpp audiooutputbase.cpp
    3334SOURCES += dbsettings.cpp screensaver.cpp screensaver-null.cpp output.cpp
    3435SOURCES += langsettings.cpp mythdbcon.cpp audiooutputnull.cpp
    35 SOURCES += DisplayResScreen.cpp util-x11.cpp qmdcodec.cpp
     36SOURCES += DisplayResScreen.cpp util-x11.cpp qmdcodec.cpp zeroconfigclient.cpp
    3637SOURCES += virtualkeyboard.cpp
    3738
    3839INCLUDEPATH += ../libmythsamplerate ../libmythsoundtouch ../..
  • libs/libmyth/mythcontext.cpp

     
    1919
    2020#include "config.h"
    2121#include "mythcontext.h"
     22#include "zeroconfigclient.h"
    2223#include "exitcodes.h"
    2324#include "oldsettings.h"
    2425#include "themedmenu.h"
     
    550551
    551552bool MythContextPrivate::FixSettingsFile(void)
    552553{
     554    DatabaseParams defaultParams;
     555
     556    /* Zero Configuration attempt */
     557    VERBOSE(VB_IMPORTANT, "Trying to find a backend on the network");
     558
     559    /* Try to connect to the backend */
     560    ZeroConfigClient *zeroconfigclient = new ZeroConfigClient(true);
     561    defaultParams = zeroconfigclient->GetDBInfo();
     562
     563    /* Lets give zeroconfig 2 seconds to find the backend */
     564    sleep(2);
     565
     566    /* We got some data from the backend */
     567    if(zeroconfigclient->FoundBackend())
     568    {
     569        VERBOSE(VB_IMPORTANT, "Found Mythtv Backend via zeroconfig");
     570        return WriteSettingsFile(defaultParams);
     571    }
     572
     573    /* Standard attempt, Zero Configuration failed */
     574    VERBOSE(VB_IMPORTANT, "ZeroConfig failed, unable to find a master backend"
     575                          " to get database details from");
    553576    VERBOSE(VB_IMPORTANT, "Trying to create a basic mysql.txt file");
    554     DatabaseParams defaultParams = parent->GetDatabaseParams();
     577    defaultParams = parent->GetDatabaseParams();
    555578   
    556579    return WriteSettingsFile(defaultParams);
    557580}
  • libs/libmyth/zeroconfigclient.cpp

     
     1#include <unistd.h>
     2#include <qsqldatabase.h>
     3#include <qsqlquery.h>
     4#include <qregexp.h>
     5#include <qstring.h>
     6#include <qdatetime.h>
     7
     8#include <sys/socket.h>
     9#include <netinet/in.h>
     10#include <arpa/inet.h>
     11#include <stdio.h>
     12
     13#include <stdlib.h>
     14#include <string.h>
     15#include <strings.h>
     16
     17using namespace std;
     18
     19#include <sys/stat.h>
     20#include <sys/time.h>
     21#include <sys/types.h>
     22#include <sys/wait.h>
     23
     24#include "mythcontext.h"
     25#include "zeroconfigclient.h"
     26
     27ZeroConfigClient::ZeroConfigClient(bool runthread)
     28{
     29    foundBackend = false;
     30
     31    threadrunning = runthread;
     32
     33    if (runthread)
     34    {
     35        pthread_create(&zeroconfigclientthread, NULL, ZeroConfigClientThread, this);
     36    }
     37}
     38 
     39ZeroConfigClient::~ZeroConfigClient()
     40{
     41    pthread_cancel(zeroconfigclientthread);
     42}
     43
     44void *ZeroConfigClient::ZeroConfigClientThread(void *param)
     45{
     46    ZeroConfigClient *zeroconfigclient = (ZeroConfigClient *)param;
     47    zeroconfigclient->GetDBInfo();
     48
     49    return NULL;
     50}
     51
     52DatabaseParams ZeroConfigClient::GetDBInfo(void)
     53{
     54    // Parse the returned data and store it in the defaultParams structure
     55    DatabaseParams dbParams;
     56
     57    int bytesRecieved;
     58    struct sockaddr_in serveraddr;
     59    char sendmsg[15] = "MYTHTVDISCOVER";
     60    char buffer[10000];
     61    QString recvmsg;
     62    int x = 1;
     63
     64    // Clear the structure
     65    bzero(&serveraddr,sizeof(serveraddr));
     66
     67    // Create the UDP socket
     68    socketfd = socket(AF_INET,SOCK_DGRAM,0);
     69
     70    // Set the socket so that we can broadcast to the entire network
     71    setsockopt(socketfd, SOL_SOCKET, SO_BROADCAST, (char*) &x, sizeof(int));
     72
     73    // Set the important information
     74    serveraddr.sin_family = AF_INET;
     75    serveraddr.sin_addr.s_addr = inet_addr("255.255.255.255");
     76    serveraddr.sin_port = htons(32000);
     77
     78    // Broadcast the message
     79    sendto(socketfd, sendmsg, strlen(sendmsg), 0, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
     80
     81    // Recieve the response from the server
     82    //   We have to get all the data in one "swoop" since UDP doesn't allow re-broadcasts,
     83    //   which means we have to have a large buffer for the largest possible response that
     84    //   can be expected
     85
     86    bytesRecieved = recvfrom(socketfd, buffer, sizeof(buffer), 0, NULL, NULL);
     87    recvmsg.append(buffer);
     88
     89#if DEBUG
     90    printf("buffer: %s\n", buffer);
     91    printf("recvmsg: %s\n", recvmsg.ascii());
     92#endif
     93    bzero(buffer, sizeof(buffer));
     94
     95    foundBackend = true;
     96
     97    QString str(recvmsg);
     98
     99#if DEBUG
     100    printf("str: %s\n", str.ascii());
     101#endif
     102    QString parameter(" ");
     103    QString value(" ");
     104
     105    int cnt = 0;
     106    QString tmp(str.section(';', cnt, cnt));
     107    parameter = tmp.section(':', 0, 0);
     108    value = tmp.section(':', 1, 1);
     109
     110    while( (!parameter.isNull() && !parameter.isEmpty()) && (!value.isNull() && !value.isEmpty()) )
     111    {
     112#if DEBUG
     113        // Print out each parameter and value
     114        printf(">>>>>parameter: [%s]\n", parameter.ascii());
     115        printf(">>>>>value: [%s]\n", value.ascii());
     116#endif
     117       
     118        if(parameter.compare("dbHostName") == 0)
     119        {
     120             dbParams.dbHostName = value;
     121        }
     122        else if(parameter.compare("dbUserName") == 0)
     123        {
     124             dbParams.dbUserName = value;
     125        }
     126        else if(parameter.compare("dbPassword") == 0)
     127        {
     128             dbParams.dbPassword = value;
     129        }
     130        else if(parameter.compare("dbName") == 0)
     131        {
     132             dbParams.dbName = value;
     133        }
     134        else if(parameter.compare("dbType") == 0)
     135        {
     136             dbParams.dbType = value;
     137        }
     138
     139        cnt++;
     140       
     141        QString tmp(str.section(';', cnt, cnt));
     142        parameter = tmp.section(':', 0, 0);
     143        value = tmp.section(':', 1, 1);
     144    }
     145
     146#if DEBUG
     147    // Print out the values we were able to parse out
     148    printf("-----------------------------\n");
     149    printf("dbHostName: %s\n",dbParams.dbHostName.ascii());
     150    printf("dbUserName: %s\n",dbParams.dbUserName.ascii());
     151    printf("dbPassword: %s\n",dbParams.dbPassword.ascii());
     152    printf("dbName: %s\n",dbParams.dbName.ascii());
     153    printf("dbType: %s\n",dbParams.dbType.ascii());
     154    printf("-----------------------------\n");
     155#endif
     156
     157    return dbParams;
     158}
     159
     160bool ZeroConfigClient::FoundBackend()
     161{
     162    return foundBackend;
     163}
  • libs/libmyth/zeroconfigclient.h

     
     1#include <qobject.h>
     2#include <sys/socket.h>
     3#include <netinet/in.h>
     4#include <stdio.h>
     5
     6#include <stdlib.h>
     7#include <string.h>
     8#include <strings.h>
     9
     10#include "mythcontext.h"
     11
     12using namespace std;
     13
     14class ZeroConfigClient : public QObject
     15{
     16  public:
     17    ZeroConfigClient(bool runthread);
     18    ~ZeroConfigClient();
     19    bool FoundBackend();
     20    DatabaseParams GetDBInfo(void);
     21
     22  protected:
     23    static void *ZeroConfigClientThread(void *param);
     24
     25  private:
     26    pthread_t zeroconfigclientthread;
     27    bool threadrunning;
     28    bool foundBackend;
     29    int socketfd;
     30};
  • programs/mythbackend/main.cpp

     
    2222#include "tv.h"
    2323#include "autoexpire.h"
    2424#include "scheduler.h"
     25#include "zeroconfigserver.h"
    2526#include "mainserver.h"
    2627#include "encoderlink.h"
    2728#include "remoteutil.h"
     
    3839AutoExpire *expirer = NULL;
    3940Scheduler *sched = NULL;
    4041JobQueue *jobqueue = NULL;
     42ZeroConfigServer *zeroconfigserver = NULL;
    4143QString pidfile;
    4244QString lockfile_location;
    4345HouseKeeper *housekeeping = NULL;
     
    233235    bool nosched = false;
    234236    bool nojobqueue = false;
    235237    bool noexpirer = false;
     238    bool usezeroconfig = false;
    236239    bool printexpire = false;
    237240    for (int argpos = 1; argpos < a.argc(); ++argpos)
    238241    {
     
    317320        {
    318321            noexpirer = true;
    319322        }
     323        else if (!strcmp(a.argv()[argpos],"--usezeroconfig"))
     324        {
     325            usezeroconfig = true;
     326        }
    320327        else if (!strcmp(a.argv()[argpos],"--printexpire"))
    321328        {
    322329            printexpire = true;
     
    349356                    "--nosched                      Do not perform any scheduling" << endl <<
    350357                    "--nojobqueue                   Do not start the JobQueue" << endl <<
    351358                    "--noautoexpire                 Do not start the AutoExpire thread" << endl <<
     359                    "--usezeroconfig                Enable the Zero Configuration thread" << endl <<
    352360                    "--version                      Version information" << endl;
    353361            return BACKEND_EXIT_INVALID_CMDLINE;
    354362        }
     
    517525    else
    518526        jobqueue = new JobQueue(ismaster);
    519527
     528    if(usezeroconfig)
     529    {
     530        cerr << "********* Using Zero Config *********\n";
     531        zeroconfigserver = new ZeroConfigServer(true);
     532    }
     533    else
     534    {
     535        cerr << "********* The Zero Config has been DISABLED by default*********\n";
     536    }
    520537    VERBOSE(VB_ALL, QString("%1 version: %2 www.mythtv.org")
    521538                            .arg(binname).arg(MYTH_BINARY_VERSION));
    522539
  • programs/mythbackend/mythbackend.pro

     
    1212
    1313# Input
    1414HEADERS += autoexpire.h encoderlink.h filetransfer.h httpstatus.h mainserver.h
    15 HEADERS += playbacksock.h scheduler.h server.h housekeeper.h
     15HEADERS += playbacksock.h scheduler.h server.h housekeeper.h zeroconfigserver.h
    1616
    1717SOURCES += autoexpire.cpp encoderlink.cpp filetransfer.cpp httpstatus.cpp
    1818SOURCES += main.cpp mainserver.cpp playbacksock.cpp scheduler.cpp server.cpp
    19 SOURCES += housekeeper.cpp
     19SOURCES += housekeeper.cpp zeroconfigserver.cpp
    2020
    2121using_oss:DEFINES += USING_OSS
    2222
  • programs/mythbackend/zeroconfigserver.cpp

     
     1#include <unistd.h>
     2#include <qsqldatabase.h>
     3#include <qsqlquery.h>
     4#include <qregexp.h>
     5#include <qstring.h>
     6#include <qdatetime.h>
     7
     8using namespace std;
     9
     10#include <sys/stat.h>
     11#include <sys/time.h>
     12#include <sys/types.h>
     13#include <sys/wait.h>
     14
     15#include <sys/socket.h>
     16#include <netinet/in.h>
     17#include <stdio.h>
     18
     19#include "libmyth/exitcodes.h"
     20#include "libmyth/mythcontext.h"
     21#include "libmyth/mythdbcon.h"
     22#include "zeroconfigserver.h"
     23
     24ZeroConfigServer::ZeroConfigServer(bool runthread)
     25{
     26    threadrunning = runthread;
     27
     28    if (runthread)
     29    {
     30        pthread_t zeroconfigserverthread;
     31        pthread_create(&zeroconfigserverthread, NULL, ZeroConfigServerThread, this);
     32    }   
     33}
     34
     35ZeroConfigServer::~ZeroConfigServer()
     36{
     37
     38}
     39
     40void *ZeroConfigServer::ZeroConfigServerThread(void *param)
     41{
     42    ZeroConfigServer *zeroconfigserver = (ZeroConfigServer *)param;
     43    zeroconfigserver->RunZeroConfigServer();
     44
     45    return NULL;
     46}
     47
     48void ZeroConfigServer::RunZeroConfigServer(void)
     49{
     50    int sockfd,n;
     51    struct sockaddr_in servaddr,cliaddr;
     52    socklen_t len;
     53    char mesg[1000];
     54
     55    sockfd=socket(AF_INET,SOCK_DGRAM,0);
     56
     57    bzero(&servaddr,sizeof(servaddr));
     58    servaddr.sin_family = AF_INET;
     59    servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
     60    servaddr.sin_port=htons(32000);
     61    bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
     62
     63    while (1)
     64    {
     65       len = sizeof(cliaddr);
     66       n = recvfrom(sockfd, mesg, 1000, 0, (struct sockaddr *)&cliaddr, &len);
     67       mesg[n] = 0;
     68#if DEBUG
     69       printf("-------------------------------------------------------\n");
     70       printf("Received the following:\n");
     71       printf("[%s]\n",mesg);
     72#endif
     73
     74       if(strcmp(mesg, "MYTHTVDISCOVER") == 0) {
     75          DatabaseParams dbParams = gContext->GetDatabaseParams();
     76          QString msg;
     77
     78          msg.sprintf("dbHostName:%s;dbUserName:%s;dbPassword:%s;dbType:%s;dbName:%s",
     79                         dbParams.dbHostName.ascii(),
     80                         dbParams.dbUserName.ascii(),
     81                         dbParams.dbPassword.ascii(),
     82                         dbParams.dbType.ascii(),
     83                         dbParams.dbName.ascii());
     84#if DEBUG
     85          printf("Sending msg: %s\n", msg.ascii());
     86#endif
     87          sendto(sockfd,msg.ascii(),msg.length(),0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
     88       }
     89       else {
     90          QString msg;
     91          msg.sprintf("ERROR: Unknown query\n");
     92          sendto(sockfd,msg.ascii(),msg.length(),0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
     93       }
     94#if DEBUG
     95       printf("-------------------------------------------------------\n");
     96#endif
     97    }
     98}
  • programs/mythbackend/zeroconfigserver.h

     
     1#include <qmutex.h>
     2#include <qobject.h>
     3
     4using namespace std;
     5
     6class ZeroConfigServer : public QObject
     7{
     8  public:
     9    ZeroConfigServer(bool runthread);
     10    ~ZeroConfigServer();
     11
     12  protected:
     13    void RunZeroConfigServer(void);
     14    static void *ZeroConfigServerThread(void *param);
     15
     16  private:
     17    bool threadrunning;
     18};