# HG changeset patch # User morphbr # Date 1188285373 -3600 # Node ID 30368d31696efa409fc520c94997feb82bfbb4f5 # Parent e0152712fd4f1af0b27f6806537a10604f69b238 [svn r833] - Restored working version of gmyth_monitor_handler.c - Patch on gmyth-cat by Renato Filho diff -r e0152712fd4f -r 30368d31696e gmyth/samples/gmyth_cat.c --- a/gmyth/samples/gmyth_cat.c Thu Aug 23 22:45:15 2007 +0100 +++ b/gmyth/samples/gmyth_cat.c Tue Aug 28 08:16:13 2007 +0100 @@ -200,7 +200,7 @@ (GByteArray *) array, 64000, TRUE)) == GMYTH_FILE_READ_OK) || file_transf_ret == GMYTH_FILE_READ_NEXT_PROG_CHAIN) { - fwrite(array->data, array->len, 1, stdout); + fwrite(array->data, sizeof(gpointer), array->len, stdout); fflush(stdout); g_array_remove_range(array, 0, array->len); diff -r e0152712fd4f -r 30368d31696e gmyth/src/gmyth_monitor_handler.c --- a/gmyth/src/gmyth_monitor_handler.c Thu Aug 23 22:45:15 2007 +0100 +++ b/gmyth/src/gmyth_monitor_handler.c Tue Aug 28 08:16:13 2007 +0100 @@ -70,9 +70,7 @@ #define GMYTHTV_ENABLE_DEBUG 1 #endif -gboolean gmyth_monitor_handler_listener (GIOChannel *io_channel, - GIOCondition condition, - gpointer data); +gpointer gmyth_monitor_handler_listener(gpointer data); static void gmyth_monitor_handler_default_listener(GMythMonitorHandler * monitor, @@ -94,10 +92,51 @@ void gmyth_monitor_handler_close(GMythMonitorHandler * monitor); -G_DEFINE_TYPE(GMythMonitorHandler, gmyth_monitor_handler, G_TYPE_OBJECT); +G_DEFINE_TYPE(GMythMonitorHandler, gmyth_monitor_handler, G_TYPE_OBJECT) + static void -static void -gmyth_monitor_handler_class_init(GMythMonitorHandlerClass * klass) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + gmyth_monitor_handler_class_init(GMythMonitorHandlerClass * klass) { GObjectClass *gobject_class; GMythMonitorHandlerClass *gmonitor_class; @@ -130,11 +169,23 @@ monitor->hostname = NULL; monitor->port = 0; monitor->actual_index = 0; + monitor->allow_msgs_listener = FALSE; + + /* + * monitor->backend_msgs = g_hash_table_new( g_int_hash, g_int_equal + * ); + */ + /* * it is used for signalizing the event socket consumer thread */ monitor->mutex = g_mutex_new(); + + monitor->th = NULL; + + monitor->gmyth_monitor_handler_listener = + gmyth_monitor_handler_listener; } static void @@ -146,9 +197,14 @@ monitor->allow_msgs_listener = FALSE; - if (monitor->io_source != 0) { - g_source_remove (monitor->io_source); - monitor->io_source = 0; + if (monitor->th != NULL) { + gboolean *ret = (gboolean *) g_thread_join(monitor->th); + + if (*ret == FALSE) + gmyth_debug("Error closing GThread listener socket!"); + else + gmyth_debug("Closed GThread listener socket."); + // g_object_unref( monitor->th ); } /* @@ -170,7 +226,6 @@ monitor->hostname = NULL; } - if (monitor->backend_msgs != NULL) { g_hash_table_destroy(monitor->backend_msgs); monitor->backend_msgs = NULL; @@ -181,7 +236,6 @@ * io_watcher_cond = NULL; } */ - G_OBJECT_CLASS(gmyth_monitor_handler_parent_class)->dispose(object); } @@ -299,22 +353,30 @@ gmyth_debug("Monitor event socket --- hostname: %s, port %d\n", monitor->hostname, monitor->port); - if (monitor->event_sock != NULL) { + if (NULL != monitor->event_sock) { g_object_unref(monitor->event_sock); monitor->event_sock = NULL; } /* - * configure the event socket + * configure the event socket */ - if (!gmyth_connect_to_backend_monitor(monitor)) { - gmyth_debug("Connection to backend failed (Event Socket)!"); - ret = FALSE; + if (NULL == monitor->event_sock) { + if (!gmyth_connect_to_backend_monitor(monitor)) { + gmyth_debug("Connection to backend failed (Event Socket)!"); + ret = FALSE; + } else { + gmyth_debug + ("Remote monitor event socket had been succesfully created. (io_fd == %d)\n", + g_io_channel_unix_get_fd(monitor->event_sock->sd_io_ch)); + } } else { - gmyth_debug ("Remote monitor event socket had been succesfully create"); + gmyth_debug + ("ASSERT ERROR: Remote monitor event socket is not NULL at the setup...\n"); } return ret; + } /** @@ -431,73 +493,122 @@ * @return Pointer to a gboolean true value, if the data was * successfully read. */ -gboolean -gmyth_monitor_handler_listener (GIOChannel *io_channel, - GIOCondition io_cond, - gpointer data) +gpointer +gmyth_monitor_handler_listener(gpointer data) { - GMythMonitorHandler *monitor; + GMythMonitorHandler *monitor = (GMythMonitorHandler *) data; guint recv = 0; + gboolean *ret = g_new0(gboolean, 1); gsize len = 0; - GMythStringList *strlist = NULL; - gint bytes_sent = 0; + GIOChannel *io_channel = monitor->event_sock->sd_io_ch; + GIOCondition io_cond = + g_io_channel_get_buffer_condition(io_channel); + static guint count = 0; - monitor = (GMythMonitorHandler *) data; + *ret = TRUE; gmyth_debug("Entering MONITOR handler listener..."); myth_control_acquire_context(monitor, TRUE); - if (((io_cond & G_IO_HUP) != 0) || - ((io_cond & G_IO_ERR) != 0)) { + if ((io_cond & G_IO_HUP) != 0) { + *ret = FALSE; goto clean_up; } + GMythStringList *strlist = NULL; - gmyth_debug("%d - Listening on Monitor socket...!\n", count); - strlist = gmyth_string_list_new(); - - len = gmyth_socket_read_stringlist(monitor->event_sock, strlist); - if ((len > 0) && strlist != NULL && gmyth_string_list_length(strlist) > 0) { - gchar *back_msg_action; - gint msg_type; - - bytes_sent = gmyth_string_list_get_int(strlist, 0); - // on backend error - gmyth_debug ("received data buffer from IO event channel... %d strings gone!\n", len); - recv += len; - - /* - * debug purpose: prints out all the string list - * elements - */ - g_list_foreach(strlist->glist, - (GFunc) gmyth_monitor_handler_print, - NULL); - - back_msg_action = g_new0(gchar, 1); - msg_type = gmyth_monitor_handler_is_backend_message(monitor, - strlist, - &back_msg_action); - - if (msg_type != GMYTH_BACKEND_NO_MESSAGE) { - g_signal_emit(monitor, - GMYTH_MONITOR_HANDLER_GET_CLASS(monitor)->backend_events_handler_signal_id, - 0, msg_type, back_msg_action); - } - - if (back_msg_action != NULL) - g_free(back_msg_action); - - g_object_unref(strlist); + if (NULL == io_channel) { + gmyth_debug("Monitor socket is NULL! (GIOChannel)"); + *ret = FALSE; + goto clean_up; } -clean_up: + while (monitor->allow_msgs_listener) { + ++count; + + gmyth_debug("%d - Listening on Monitor socket...!\n", count); + + do { + + gint bytes_sent = 0; + + strlist = gmyth_string_list_new(); + + if (monitor->event_sock != NULL) { + + len = + gmyth_socket_read_stringlist(monitor->event_sock, + strlist); + + if ((len > 0) && strlist != NULL + && gmyth_string_list_length(strlist) > 0) { + bytes_sent = gmyth_string_list_get_int(strlist, 0); // -1 + // + // + // on + // backend + // error + + gmyth_debug + ("[%s] MONITOR: received data buffer from IO event channel... %d strings gone!\n", + __FUNCTION__, len); + + recv += len; + + /* + * debug purpose: prints out all the string list + * elements + */ + g_list_foreach(strlist->glist, + (GFunc) gmyth_monitor_handler_print, + NULL); + + gchar *back_msg_action = g_new0(gchar, 1); + gint msg_type = + gmyth_monitor_handler_is_backend_message(monitor, + strlist, + &back_msg_action); + + if (monitor != NULL + && msg_type != GMYTH_BACKEND_NO_MESSAGE) + g_signal_emit(monitor, GMYTH_MONITOR_HANDLER_GET_CLASS(monitor)->backend_events_handler_signal_id, 0, /* details + */ + msg_type, back_msg_action); + + if (back_msg_action != NULL) + g_free(back_msg_action); + + } + + } + + if (strlist != NULL) { + g_object_unref(strlist); + strlist = NULL; + } + + io_cond = g_io_channel_get_buffer_condition(io_channel); + + g_usleep(500); + + } + while (recv <= 0 && ((io_cond & G_IO_HUP) == 0)); + + gmyth_debug("\tMONITOR EVENT: Read %d bytes\n", recv); + + } /* main GThread while */ + + clean_up: myth_control_release_context(monitor); - return TRUE; + + g_thread_exit(ret); + + return (gpointer) ret; + } -/** +/** * Opens connection events' socket the the Monitor socket on * MythTV backend server. * @@ -505,7 +616,7 @@ * * @return true, if the socket was successfully opened. */ -static gboolean +static gboolean gmyth_connect_to_backend_monitor(GMythMonitorHandler * monitor) { gboolean ret = TRUE; @@ -514,7 +625,7 @@ /* * Connects the socket, send Mythtv ANN Monitor and verify Mythtv - * protocol version + * protocol version */ if (!gmyth_socket_connect_to_backend_events(monitor->event_sock, monitor->hostname, @@ -527,38 +638,52 @@ return ret; } -/** +/** * Opens connection the the Monitor socket on MythTV backend server, * where all status messages are notified to the client. - * + * * @param monitor The GMythMonitorHandler instance. * @param channel The GIOChannel instance to the Monitor socket. - * + * * @return Pointer to the boolean value, and it is true only if the - * GMythMonitorHandler could be configured. + * GMythMonitorHandler could be configured. */ -static gboolean +static gboolean gmyth_monitor_handler_setup(GMythMonitorHandler * monitor, GIOChannel * channel) { - gboolean ret = TRUE; + gboolean ret = TRUE; if (channel != NULL) { monitor->allow_msgs_listener = TRUE; - monitor->io_source = g_io_add_watch (channel, G_IO_IN | G_IO_ERR | G_IO_HUP, - gmyth_monitor_handler_listener, - monitor); + + monitor->th = + g_thread_create((GThreadFunc) gmyth_monitor_handler_listener, + monitor, TRUE, NULL); + gmyth_debug("MONITOR GThread created!"); } else { ret = FALSE; + goto cleanup; } + + if (NULL == monitor->th) { + gmyth_debug + ("[%s] Error adding GThread listener function to the IO control channel!\n", + __FUNCTION__); + ret = FALSE; + goto cleanup; + } + + cleanup: + return ret; } -/** +/** * Starts the MonitorHandler thread to the GIOWatcher. - * + * * @param monitor The GMythMonitorHandler instance. - * + * * @return true, if the MonitorHandler was started. */ gboolean diff -r e0152712fd4f -r 30368d31696e gmyth/src/gmyth_monitor_handler.h --- a/gmyth/src/gmyth_monitor_handler.h Thu Aug 23 22:45:15 2007 +0100 +++ b/gmyth/src/gmyth_monitor_handler.h Tue Aug 28 08:16:13 2007 +0100 @@ -63,12 +63,12 @@ GObjectClass parent_class; /* - * callbacks + * callbacks */ guint backend_events_handler_signal_id; /* - * signal default handlers + * signal default handlers */ void (*backend_events_handler) (GMythMonitorHandler * monitor, gint msg_code, @@ -88,7 +88,13 @@ */ GMythSocket *event_sock; - //gpointer(*gmyth_monitor_handler_listener) (gpointer data); + + + + + + + gpointer(*gmyth_monitor_handler_listener) (gpointer data); gchar *hostname; gint port; @@ -103,7 +109,9 @@ GHashTable *backend_msgs; GMutex *mutex; - guint io_source; + + GThread *th; + }; GType gmyth_monitor_handler_get_type(void);