[svn r362] Some fixes in the GIOWatcher clean-ups, and changed the API version number.
4 * @file gmyth/gmyth_monitor_handler.c
6 * @brief <p> GMythMonitorHandler deals with the streaming media events remote/local
7 * that are sent to the MythTV frontend.
9 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
10 * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * GStreamer MythTV plug-in properties:
29 * - location (backend server hostname/URL) [ex.: myth://192.168.1.73:28722/1000_1092091.nuv]
30 * - path (qurl - remote file to be opened)
40 #include <arpa/inet.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
48 #include "gmyth_marshal.h"
50 #include "gmyth_monitor_handler.h"
54 #define GMYTHTV_QUERY_HEADER "QUERY_FILETRANSFER "
56 #define GMYTHTV_VERSION 30
58 #define GMYTHTV_TRANSFER_MAX_WAITS 700
60 #define GMYTHTV_BUFFER_SIZE 8*1024
62 #ifdef GMYTHTV_ENABLE_DEBUG
63 #define GMYTHTV_ENABLE_DEBUG 1
65 #undef GMYTHTV_ENABLE_DEBUG
68 /* this NDEBUG is to maintain compatibility with GMyth library */
70 #define GMYTHTV_ENABLE_DEBUG 1
73 //GMainContext *io_watcher_context = NULL;
75 //GThread *monitor_th = NULL;
77 //static gboolean* myth_control_sock_listener( GIOChannel *io_channel );
78 //static gboolean gmyth_monitor_handler_listener( GIOChannel *io_channel,
79 // GIOCondition condition, gpointer data );
81 //gboolean* gmyth_monitor_handler_listener( GMythMonitorHandler *monitor );
83 void gmyth_monitor_handler_listener( GMythMonitorHandler *monitor, gpointer user_data );
85 static void gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gchar* message );
87 static GMutex* mutex = NULL;
89 //static GCond* io_watcher_cond = NULL;
91 static void gmyth_monitor_handler_class_init (GMythMonitorHandlerClass *klass);
92 static void gmyth_monitor_handler_init (GMythMonitorHandler *object);
94 static void gmyth_monitor_handler_dispose (GObject *object);
95 static void gmyth_monitor_handler_finalize (GObject *object);
97 static gboolean gmyth_connect_to_backend_monitor (GMythMonitorHandler *monitor);
99 void gmyth_monitor_handler_close( GMythMonitorHandler *monitor );
101 G_DEFINE_TYPE(GMythMonitorHandler, gmyth_monitor_handler, G_TYPE_OBJECT)
104 gmyth_monitor_handler_class_init (GMythMonitorHandlerClass *klass)
106 GObjectClass *gobject_class;
107 GMythMonitorHandlerClass *gmonitor_class;
109 gobject_class = (GObjectClass *) klass;
110 gmonitor_class = (GMythMonitorHandlerClass *) gobject_class;
112 gobject_class->dispose = gmyth_monitor_handler_dispose;
113 gobject_class->finalize = gmyth_monitor_handler_finalize;
115 gmonitor_class->backend_events_handler_signal_id =
116 g_signal_new ("backend-events-handler",
117 G_TYPE_FROM_CLASS (gmonitor_class),
118 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
122 gmyth_marshal_VOID__INT_STRING,
128 gmonitor_class->backend_events_handler = gmyth_monitor_handler_default_listener;
133 gmyth_monitor_handler_init (GMythMonitorHandler *monitor)
135 g_return_if_fail( monitor != NULL );
137 monitor->event_sock = NULL;
138 monitor->hostname = NULL;
140 monitor->actual_index = 0;
142 monitor->allow_msgs_listener = TRUE;
144 //monitor->backend_msgs = g_hash_table_new( g_int_hash, g_int_equal );
146 /* it is used for signalizing the event socket consumer thread */
147 //io_watcher_cond = g_cond_new();
149 /* mutex to control access to the event socket consumer thread */
150 //mutex = g_mutex_new();
152 monitor->monitor_th = NULL;
154 monitor->gmyth_monitor_handler_listener = gmyth_monitor_handler_listener;
158 gmyth_monitor_handler_dispose (GObject *object)
161 GMythMonitorHandler *monitor = GMYTH_MONITOR_HANDLER (object);
163 monitor->allow_msgs_listener = FALSE;
165 if ( monitor->monitor_th != NULL )
167 g_thread_pool_free( monitor->monitor_th, TRUE, FALSE );
168 //g_thread_exit( monitor->monitor_th );
169 if ( monitor->monitor_th != NULL )
170 g_object_unref( monitor->monitor_th );
171 monitor->monitor_th = NULL;
174 if ( monitor->event_sock != NULL )
176 g_object_unref( monitor->event_sock );
177 monitor->event_sock = NULL;
180 if ( monitor->hostname != NULL )
182 g_free( monitor->hostname );
183 monitor->hostname = NULL;
186 if ( monitor->backend_msgs != NULL )
188 g_hash_table_destroy ( monitor->backend_msgs );
189 monitor->backend_msgs = NULL;
194 g_mutex_free( mutex );
199 if ( io_watcher_cond != NULL )
201 g_cond_free( io_watcher_cond );
202 io_watcher_cond = NULL;
206 G_OBJECT_CLASS (gmyth_monitor_handler_parent_class)->dispose (object);
210 gmyth_monitor_handler_finalize (GObject *object)
212 g_signal_handlers_destroy (object);
214 G_OBJECT_CLASS (gmyth_monitor_handler_parent_class)->finalize (object);
217 // fixme: do we need the card_id????
219 gmyth_monitor_handler_new ( void )
221 GMythMonitorHandler *monitor = GMYTH_MONITOR_HANDLER ( g_object_new ( GMYTH_MONITOR_HANDLER_TYPE,
228 myth_control_acquire_context( gboolean do_wait )
232 //guint max_iter = 50;
234 //g_mutex_lock( mutex );
236 //while ( !has_io_access )
237 // g_cond_wait( io_watcher_cond, mutex );
239 //has_io_access = FALSE;
242 while ( --max_iter > 0 && !g_main_context_wait( io_watcher_context, io_watcher_cond, mutex ) )
244 } else if ( !g_main_context_acquire( io_watcher_context ) )
248 //g_static_mutex_lock( &st_mutex );
255 myth_control_release_context( )
260 //g_static_mutex_unlock( &st_mutex );
262 //g_main_context_release( io_watcher_context );
264 //g_main_context_wakeup( io_watcher_context );
266 //has_io_access = TRUE;
268 //g_cond_broadcast( io_watcher_cond );
270 //g_mutex_unlock( mutex );
277 gmyth_monitor_handler_open (GMythMonitorHandler *monitor, const gchar *hostname, gint port)
281 g_return_val_if_fail( hostname != NULL, FALSE );
283 if (monitor->hostname != NULL) {
284 g_free (monitor->hostname);
285 monitor->hostname = NULL;
288 monitor->hostname = g_strdup( hostname );
289 monitor->port = port;
291 gmyth_debug ("Monitor event socket --- hostname: %s, port %d\n", monitor->hostname, monitor->port);
293 /* configure the event socket */
294 if ( NULL == monitor->event_sock ) {
295 if (!gmyth_connect_to_backend_monitor (monitor)) {
296 g_printerr( "Connection to backend failed (Event Socket).\n" );
300 g_warning("Remote monitor event socket already created.\n");
308 gmyth_monitor_handler_is_backend_message( GMythMonitorHandler *monitor,
309 GMythStringList* strlist, gchar **back_msg_action )
311 gint msg_type = GMYTH_BACKEND_NO_MESSAGE;
312 GString *back_msg = NULL;
314 back_msg = gmyth_string_list_get_string( strlist, 0 );
315 if ( back_msg != NULL && back_msg->str != NULL &&
316 strstr( back_msg->str, "BACKEND" ) != NULL )
318 gmyth_debug( "MONITOR HANDLER - Received backend message = %s", back_msg->str );
319 *back_msg_action = gmyth_string_list_get_char_array( strlist, 1 );
321 if ( back_msg_action != NULL )
324 if ( g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "LIVETV_CHAIN" ) ||
325 g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "RECORDING_LIST_CHANGE" ) ||
326 g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "SCHEDULE_CHANGE" ) ||
327 g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "LIVETV_WATCH" ) )
329 gmyth_debug( "MONITOR: message type == GMYTH_BACKEND_PROGRAM_INFO_CHANGED, msg = %s", *back_msg_action );
330 msg_type = GMYTH_BACKEND_PROGRAM_INFO_CHANGED;
331 } else if ( g_strstr_len( *back_msg_action, strlen( *back_msg_action ), "DONE_RECORDING" ) ) {
332 gmyth_debug( "MONITOR: message type == GMYTH_BACKEND_DONE_RECORDING, msg = %s", *back_msg_action );
333 msg_type = GMYTH_BACKEND_DONE_RECORDING;
336 //g_hash_table_insert ( monitor->backend_msgs,
337 // &(monitor->actual_index), *back_msg_action );
342 if ( back_msg != NULL )
344 g_string_free( back_msg, TRUE );
353 gmyth_monitor_handler_default_listener( GMythMonitorHandler *monitor, gint msg_code, gchar* message )
355 //assert( message!= NULL );
356 gmyth_debug( "DEFAULT Signal handler ( msg = %s, code = %d )\n",
361 gmyth_monitor_handler_print( GString *str, gpointer ptr )
363 gmyth_debug( "Backend message event: %s --- ", str->str );
367 //gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
369 //gmyth_monitor_handler_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
370 //gboolean* gmyth_monitor_handler_listener( GMythMonitorHandler *monitor )
372 gmyth_monitor_handler_listener (GMythMonitorHandler *monitor, gpointer user_data)
374 //GMythMonitorHandler *monitor = (GMythMonitorHandler*)data;
376 GIOCondition io_cond;
378 gboolean *ret = g_new0( gboolean, 1 );
380 //gboolean ret = TRUE;
383 static guint count = 0;
385 GIOChannel *io_channel = monitor->event_sock->sd_io_ch;
386 //GIOCondition condition = g_io_channel_get_buffer_condition( io_channel );
388 GMythStringList *strlist = NULL;
390 //GMythMonitorHandler *monitor = (GMythMonitorHandler*)data;
392 myth_control_acquire_context (TRUE);
394 if ( io_channel == NULL ) {
395 g_debug ("Monitor socket is NULL!\n");
400 while (monitor->allow_msgs_listener) {
403 gmyth_debug ("%d - Listening on Monitor socket...!\n", count);
410 strlist = gmyth_string_list_new();
412 if ( monitor->event_sock != NULL )
415 len = gmyth_socket_read_stringlist( monitor->event_sock, strlist );
417 if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 )
419 bytes_sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
421 gmyth_debug ( "[%s] MONITOR: received data buffer from IO event channel... %d strings gone!\n",
426 /* debug purpose: prints out all the string list elements */
427 g_list_foreach( strlist->glist, (GFunc)gmyth_monitor_handler_print, NULL );
429 gchar *back_msg_action = g_new0( gchar, 1 );
430 gint msg_type = gmyth_monitor_handler_is_backend_message( monitor, strlist,
433 g_signal_emit ( monitor,
434 GMYTH_MONITOR_HANDLER_GET_CLASS (monitor)->backend_events_handler_signal_id,
436 msg_type, back_msg_action );
438 if (back_msg_action!= NULL)
439 g_free( back_msg_action );
446 g_object_unref( strlist );
448 io_cond = g_io_channel_get_buffer_condition( io_channel );
450 } while ( recv <= 0 && ( io_cond & G_IO_IN ) != 0 );
452 gmyth_debug ("[%s]\tMONITOR EVENT: Read %d bytes\n", __FUNCTION__, recv );
456 } /* main GThread while */
458 myth_control_release_context ();
460 if ( io_status == G_IO_STATUS_ERROR ) {
461 //gmyth_debug ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" );
462 gmyth_debug ("Error reading MONITOR event socket.\n");
470 g_object_unref( strlist );
477 gmyth_connect_to_backend_monitor (GMythMonitorHandler *monitor)
481 monitor->event_sock = gmyth_socket_new();
483 /* Connects the socket, send Mythtv ANN Monitor and verify Mythtv protocol version */
484 if (!gmyth_socket_connect_to_backend_events ( monitor->event_sock,
485 monitor->hostname, monitor->port, FALSE ) )
487 g_object_unref (monitor->event_sock);
488 monitor->event_sock = NULL;
496 gmyth_monitor_handler_setup( GMythMonitorHandler *monitor, GIOChannel *channel )
498 gboolean *ret = g_new0( gboolean, 1 );
503 //io_watcher_context = g_main_context_default();
504 //GMainLoop *loop = g_main_loop_new( io_watcher_context, TRUE );
508 if ( channel != NULL ) {
509 //source = g_io_create_watch( channel, G_IO_IN | G_IO_HUP );
510 src_id = g_io_add_watch( channel, G_IO_IN,
511 (GIOFunc)gmyth_monitor_handler_listener, monitor );
517 //g_source_set_callback ( source, (GSourceFunc)gmyth_monitor_handler_listener, NULL, NULL );
519 //g_source_attach( source, io_watcher_context );
521 //if (NULL == source){
523 gmyth_debug( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ );
528 //g_main_loop_run( loop );
531 //if ( source != NULL )
532 // g_source_unref( source );
534 //if ( io_watcher_context != NULL )
535 // g_main_context_unref( io_watcher_context );
537 //if ( loop != NULL )
538 // g_main_loop_unref( loop );
545 gmyth_monitor_handler_start (GMythMonitorHandler *monitor)
547 gboolean *ret = g_new0( gboolean, 1 );
550 /*if (!g_thread_supported () ) g_thread_init (NULL);*/
552 monitor->monitor_th = g_thread_pool_new( (GThreadFunc)gmyth_monitor_handler_listener,
553 monitor, TRUE, NULL );
555 monitor->monitor_th = g_thread_pool_new( (GFunc)gmyth_monitor_handler_listener,
556 monitor, 3, TRUE, NULL );
557 g_thread_pool_push( monitor->monitor_th, monitor, NULL );
559 //if ( ( ret = g_thread_join( monitor_th ) ) == FALSE )
560 if ( monitor->monitor_th != NULL )
561 //if ( gmyth_monitor_handler_setup( monitor, monitor->event_sock->sd_io_ch ) )
563 gmyth_debug ( "\n[%s]\tOK! Starting listener on the MONITOR event socket...[thread location = %p]\n",
564 __FUNCTION__, g_thread_self( ) );
567 gmyth_debug ( "\n[%s]\tERROR! Coudn't start listener on the MONITOR event socket...[thread location = %p]\n",
568 __FUNCTION__, g_thread_self( ) );
574 gmyth_debug( "[%s] Watch listener function over the IO control channel? %s!!!\n",
575 __FUNCTION__, ( *ret == TRUE ? "YES" : "NO" ) );
581 gmyth_monitor_handler_close( GMythMonitorHandler *monitor )
584 if (monitor->event_sock) {
585 g_object_unref( monitor->event_sock );
586 monitor->event_sock = NULL;
589 if (monitor->hostname) {
590 g_free( monitor->hostname );
591 monitor->hostname = NULL;