gmyth/tests/gmyth_test_monitor_handler.c
author melunko
Wed Feb 27 11:00:07 2008 +0000 (2008-02-27)
branchtrunk
changeset 931 e8e3219edf5f
parent 750 312d6bc514f3
permissions -rw-r--r--
[svn r940] Fixed Renato e-mail address in the copyright
     1 #include <glib-object.h>
     2 
     3 #include "common.h"
     4 
     5 #include <gmyth/gmyth_uri.h>
     6 #include <gmyth/gmyth_backendinfo.h>
     7 #include <gmyth/gmyth_livetv.h>
     8 #include <gmyth/gmyth_monitor_handler.h>
     9 #include <gmyth/gmyth_common.h>
    10 
    11 #define URI_DEFAULT	"myth://192.168.3.165:6543/livetv?channel=9"
    12 
    13 /**
    14  * The GObject signal handler function, from which all status messages 
    15  * from the Monitor Handler will be advertized, all time it receives
    16  * LiveTV status messages from the MythTV backend
    17  * 
    18  * @param monitor a GMythMonitorHandler instance
    19  * @param msg_code the MythTV's server numeric status code
    20  * @param message the message's string description
    21  * @param user_data pointer to the GMythLiveTV instance (NULL in the tests)
    22  */
    23 static void
    24 gmyth_livetv_monitor_signal_handler(GMythMonitorHandler * monitor,
    25                                     gint msg_code, gchar * message,
    26                                     gpointer user_data)
    27 {
    28     g_debug
    29         ("LIVETV Signal handler ( msg = %s, code = %d, live_tv param = %s, user_data = %s )\n",
    30          message, msg_code, "NULL", user_data != NULL ? "" : "NULL");
    31 
    32 }
    33 
    34 /**
    35  * Starts the Monitor Handler to this GMythLiveTV session, in order
    36  * to receive the status messages from the MythTV's backend server 
    37  * 
    38  * @param live_tv the GMythLiveTV instance
    39  * 
    40  * @return <code>true</code> if the Monitor Handler start-up process
    41  * 	   had been concluded succcesfully 
    42  */
    43 static          gboolean
    44 gmyth_test_monitor_handler_start(GMythBackendInfo * backend_info,
    45                                  GMythMonitorHandler * *monitor)
    46 {
    47 
    48     gboolean        res = TRUE;
    49 
    50     if (*monitor != NULL) {
    51         g_object_unref(*monitor);
    52         *monitor = NULL;
    53     }
    54 
    55     *monitor = gmyth_monitor_handler_new();
    56 
    57     res =
    58         gmyth_monitor_handler_open(*monitor,
    59                                    backend_info->hostname,
    60                                    backend_info->port);
    61 
    62     if (res == TRUE) {
    63         g_debug
    64             ("Connect MythTV Monitor event socket! Trying to start the message handler...");
    65 
    66         res = gmyth_monitor_handler_start(*monitor);
    67 
    68         if (res) {
    69             g_debug
    70                 ("MythTV Monitor event socket connected and listening!");
    71             g_signal_connect(G_OBJECT(*monitor), "backend-events-handler",
    72                              (GCallback)
    73                              gmyth_livetv_monitor_signal_handler, NULL);
    74         } else {
    75             g_debug
    76                 ("Problems when trying to start MythTV Monitor event socket!");
    77             goto error;
    78         }
    79     }
    80 
    81   error:
    82     return res;
    83 
    84 }
    85 
    86 static          gboolean
    87 test_monitor_handler_setup(gchar * uri)
    88 {
    89     gboolean        ret = TRUE;
    90     GMythMonitorHandler *monitor;
    91 
    92     if (NULL == uri)
    93         uri = g_strdup(URI_DEFAULT);
    94 
    95     GMythURI       *gmyth_uri = gmyth_uri_new_with_value(uri);
    96 
    97     GMythBackendInfo *backend_info = gmyth_backend_info_new_with_uri(uri);
    98     g_debug("uri = %s", uri);
    99 
   100     ret = gmyth_test_monitor_handler_start(backend_info, &monitor);
   101 
   102     if (ret == FALSE) {
   103         g_debug("MonitorHandler couldn't start!\n");
   104         goto init_failed;
   105     }
   106 
   107   init_failed:
   108     if (monitor != NULL)
   109         g_object_unref(monitor);
   110 
   111     if (gmyth_uri != NULL)
   112         g_object_unref(gmyth_uri);
   113 
   114     if (backend_info != NULL)
   115         g_object_unref(backend_info);
   116 
   117     return ret;
   118 
   119 }
   120 
   121 gint
   122 main(gint args, const gchar ** argv)
   123 {
   124     gboolean        ret;
   125 
   126     g_type_init();
   127 
   128     g_thread_init(NULL);
   129 
   130     fprintf(stdout, SYNC_STRING);
   131     fflush(NULL);
   132     getchar();
   133 
   134     if (args > 1)
   135         ret = test_monitor_handler_setup(argv[1]);
   136     else
   137         ret = test_monitor_handler_setup(NULL);
   138 
   139     if (!ret)
   140         g_debug("Error when running Monitor Handler setup test script!");
   141     else
   142         g_debug("MonitorHandler setup test script setup with success.");
   143 
   144     return (0);
   145 }