gmyth/tests/gmyth_test_monitor_handler.c
author renatofilho
Thu Jun 14 18:19:52 2007 +0100 (2007-06-14)
branchtrunk
changeset 750 312d6bc514f3
parent 640 d22475d73fd5
child 754 cb885ee44618
permissions -rw-r--r--
[svn r756] fixed indent using GNU Style
     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 	{
    52 	  g_object_unref(*monitor);
    53 	  *monitor = NULL;
    54 	}
    55 
    56   *monitor = gmyth_monitor_handler_new();
    57 
    58   res =
    59 	gmyth_monitor_handler_open(*monitor,
    60 							   backend_info->hostname, backend_info->port);
    61 
    62   if (res == TRUE)
    63 	{
    64 	  g_debug
    65 		("Connect MythTV Monitor event socket! Trying to start the message handler...");
    66 
    67 	  res = gmyth_monitor_handler_start(*monitor);
    68 
    69 	  if (res)
    70 		{
    71 		  g_debug("MythTV Monitor event socket connected and listening!");
    72 		  g_signal_connect(G_OBJECT(*monitor),
    73 						   "backend-events-handler",
    74 						   (GCallback) gmyth_livetv_monitor_signal_handler,
    75 						   NULL);
    76 		}
    77 	  else
    78 		{
    79 		  g_debug
    80 			("Problems when trying to start MythTV Monitor event socket!");
    81 		  goto error;
    82 		}
    83 	}
    84 
    85 error:
    86   return res;
    87 
    88 }
    89 
    90 static gboolean
    91 test_monitor_handler_setup(gchar * uri)
    92 {
    93   gboolean ret = TRUE;
    94   GMythMonitorHandler *monitor;
    95 
    96   if (NULL == uri)
    97 	uri = g_strdup(URI_DEFAULT);
    98 
    99   GMythURI *gmyth_uri = gmyth_uri_new_with_value(uri);
   100 
   101   GMythBackendInfo *backend_info = gmyth_backend_info_new_with_uri(uri);
   102   g_debug("uri = %s", uri);
   103 
   104   ret = gmyth_test_monitor_handler_start(backend_info, &monitor);
   105 
   106   if (ret == FALSE)
   107 	{
   108 	  g_debug("MonitorHandler couldn't start!\n");
   109 	  goto init_failed;
   110 	}
   111 
   112 init_failed:
   113   if (monitor != NULL)
   114 	g_object_unref(monitor);
   115 
   116   if (gmyth_uri != NULL)
   117 	g_object_unref(gmyth_uri);
   118 
   119   if (backend_info != NULL)
   120 	g_object_unref(backend_info);
   121 
   122   return ret;
   123 
   124 }
   125 
   126 gint
   127 main(gint args, const gchar ** argv)
   128 {
   129   gboolean ret;
   130 
   131   g_type_init();
   132 
   133   g_thread_init(NULL);
   134 
   135   fprintf(stdout, SYNC_STRING);
   136   fflush(NULL);
   137   getchar();
   138 
   139   if (args > 1)
   140 	ret = test_monitor_handler_setup(argv[1]);
   141   else
   142 	ret = test_monitor_handler_setup(NULL);
   143 
   144   if (!ret)
   145 	g_debug("Error when running Monitor Handler setup test script!");
   146   else
   147 	g_debug("MonitorHandler setup test script setup with success.");
   148 
   149   return (0);
   150 }