[svn r176] Added the Monitor socket handler to receive backend event messages.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth/src/gmyth_monitor_handler.c Sat Dec 02 03:40:20 2006 +0000
1.3 @@ -0,0 +1,429 @@
1.4 +/* vim: set sw=2: -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2; c-indent-level: 2-*- */
1.5 +/**
1.6 + * GMyth Library
1.7 + *
1.8 + * @file gmyth/gmyth_monitor_handler.c
1.9 + *
1.10 + * @brief <p> GMythMonitorHandler deals with the streaming media events remote/local
1.11 + * that are sent to the MythTV frontend.
1.12 + *
1.13 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
1.14 + * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
1.15 + *
1.16 + *//*
1.17 + *
1.18 + * This program is free software; you can redistribute it and/or modify
1.19 + * it under the terms of the GNU Lesser General Public License as published by
1.20 + * the Free Software Foundation; either version 2 of the License, or
1.21 + * (at your option) any later version.
1.22 + *
1.23 + * This program is distributed in the hope that it will be useful,
1.24 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.25 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.26 + * GNU General Public License for more details.
1.27 + *
1.28 + * You should have received a copy of the GNU Lesser General Public License
1.29 + * along with this program; if not, write to the Free Software
1.30 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.31 + *
1.32 + * GStreamer MythTV plug-in properties:
1.33 + * - location (backend server hostname/URL) [ex.: myth://192.168.1.73:28722/1000_1092091.nuv]
1.34 + * - path (qurl - remote file to be opened)
1.35 + * - port number *
1.36 + */
1.37 +
1.38 +#include "gmyth_uri.h"
1.39 +#include "gmyth_livetv.h"
1.40 +#include "gmyth_util.h"
1.41 +#include "gmyth_socket.h"
1.42 +#include "gmyth_stringlist.h"
1.43 +#include "gmyth_monitor_handler.h"
1.44 +#include "gmyth_debug.h"
1.45 +
1.46 +#include <unistd.h>
1.47 +#include <glib.h>
1.48 +
1.49 +#include <arpa/inet.h>
1.50 +#include <sys/types.h>
1.51 +#include <sys/socket.h>
1.52 +#include <netdb.h>
1.53 +#include <errno.h>
1.54 +#include <stdlib.h>
1.55 +#include <assert.h>
1.56 +
1.57 +#define GMYTHTV_QUERY_HEADER "QUERY_FILETRANSFER "
1.58 +
1.59 +#define GMYTHTV_VERSION 30
1.60 +
1.61 +#define GMYTHTV_TRANSFER_MAX_WAITS 700
1.62 +
1.63 +#define GMYTHTV_BUFFER_SIZE 8*1024
1.64 +
1.65 +#ifdef GMYTHTV_ENABLE_DEBUG
1.66 +#define GMYTHTV_ENABLE_DEBUG 1
1.67 +#else
1.68 +#undef GMYTHTV_ENABLE_DEBUG
1.69 +#endif
1.70 +
1.71 +/* this NDEBUG is to maintain compatibility with GMyth library */
1.72 +#ifndef NDEBUG
1.73 +#define GMYTHTV_ENABLE_DEBUG 1
1.74 +#endif
1.75 +
1.76 +GThread *monitor_th = NULL;
1.77 +
1.78 +enum myth_sock_types {
1.79 + GMYTH_PLAYBACK_TYPE = 0,
1.80 + GMYTH_MONITOR_TYPE,
1.81 + GMYTH_FILETRANSFER_TYPE,
1.82 + GMYTH_RINGBUFFER_TYPE
1.83 +};
1.84 +
1.85 +static GStaticMutex st_mutex = G_STATIC_MUTEX_INIT;
1.86 +
1.87 +static gboolean* myth_control_sock_listener( GIOChannel *io_channel );
1.88 +//static gboolean myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition,
1.89 +// gpointer data );
1.90 +
1.91 +static GMutex* mutex = NULL;
1.92 +
1.93 +static GCond* io_watcher_cond = NULL;
1.94 +
1.95 +static GMainContext* io_watcher_context = NULL;
1.96 +
1.97 +static void gmyth_monitor_handler_class_init (GMythMonitorHandlerClass *klass);
1.98 +static void gmyth_monitor_handler_init (GMythMonitorHandler *object);
1.99 +
1.100 +static void gmyth_monitor_handler_dispose (GObject *object);
1.101 +static void gmyth_monitor_handler_finalize (GObject *object);
1.102 +
1.103 +static gboolean gmyth_connect_to_backend_monitor (GMythMonitorHandler *monitor);
1.104 +
1.105 +void gmyth_monitor_handler_close( GMythMonitorHandler *monitor );
1.106 +
1.107 +static gboolean myth_control_acquire_context( gboolean do_wait );
1.108 +
1.109 +static gboolean myth_control_release_context( );
1.110 +
1.111 +G_DEFINE_TYPE(GMythMonitorHandler, gmyth_monitor_handler, G_TYPE_OBJECT)
1.112 +
1.113 +static void
1.114 +gmyth_monitor_handler_class_init (GMythMonitorHandlerClass *klass)
1.115 +{
1.116 + GObjectClass *gobject_class;
1.117 +
1.118 + gobject_class = (GObjectClass *) klass;
1.119 +
1.120 + gobject_class->dispose = gmyth_monitor_handler_dispose;
1.121 + gobject_class->finalize = gmyth_monitor_handler_finalize;
1.122 +}
1.123 +
1.124 +static void
1.125 +gmyth_monitor_handler_init (GMythMonitorHandler *monitor)
1.126 +{
1.127 + g_return_if_fail( monitor != NULL );
1.128 +
1.129 + monitor->event_sock = NULL;
1.130 + monitor->hostname = NULL;
1.131 + monitor->port = 0;
1.132 + monitor->actual_index = 0;
1.133 +
1.134 + monitor->backend_msgs = g_hash_table_new( g_int_hash, g_int_equal );
1.135 +
1.136 + /* it is used for signalizing the event socket consumer thread */
1.137 + io_watcher_cond = g_cond_new();
1.138 +
1.139 + /* mutex to control access to the event socket consumer thread */
1.140 + mutex = g_mutex_new();
1.141 +}
1.142 +
1.143 +static void
1.144 +gmyth_monitor_handler_dispose (GObject *object)
1.145 +{
1.146 + G_OBJECT_CLASS (gmyth_monitor_handler_parent_class)->dispose (object);
1.147 +}
1.148 +
1.149 +static void
1.150 +gmyth_monitor_handler_finalize (GObject *object)
1.151 +{
1.152 + g_signal_handlers_destroy (object);
1.153 +
1.154 + G_OBJECT_CLASS (gmyth_monitor_handler_parent_class)->finalize (object);
1.155 +}
1.156 +
1.157 +// fixme: do we need the card_id????
1.158 +GMythMonitorHandler*
1.159 +gmyth_monitor_handler_new (void)
1.160 +{
1.161 + GMythMonitorHandler *monitor = GMYTH_MONITOR_HANDLER ( g_object_new (
1.162 + GMYTH_MONITOR_HANDLER_TYPE, FALSE ));
1.163 +
1.164 + return monitor;
1.165 +}
1.166 +
1.167 +gboolean
1.168 +gmyth_monitor_handler_open (GMythMonitorHandler *monitor, gchar *hostname, gint port)
1.169 +{
1.170 + gboolean ret = TRUE;
1.171 +
1.172 + if (monitor->hostname != NULL) {
1.173 + g_free (monitor->hostname);
1.174 + monitor->hostname = NULL;
1.175 + }
1.176 +
1.177 + monitor->hostname = g_strdup( hostname );
1.178 + monitor->port = port;
1.179 +
1.180 + gmyth_debug ("Monitor event socket --- hostname: %s, port %d\n", monitor->hostname, monitor->port);
1.181 +
1.182 + /* configure the event socket */
1.183 + if (monitor->event_sock == NULL) {
1.184 + if (!gmyth_connect_to_backend_monitor (monitor)) {
1.185 + g_printerr( "Connection to backend failed (Event Socket).\n" );
1.186 + ret = FALSE;
1.187 + }
1.188 + } else {
1.189 + g_warning("Remote monitor event socket already created.\n");
1.190 + }
1.191 +
1.192 + return ret;
1.193 +
1.194 +}
1.195 +
1.196 +static gboolean*
1.197 +//myth_control_sock_listener( GIOChannel *io_channel, GIOCondition condition, gpointer data )
1.198 +myth_control_sock_listener( GIOChannel *io_channel )
1.199 +{
1.200 +
1.201 + GIOStatus io_status;
1.202 + GError *error = NULL;
1.203 + GIOCondition io_cond;
1.204 + GIOCondition condition;
1.205 + gchar *trash = NULL;
1.206 + GByteArray *byte_array = NULL;
1.207 + guint recv = 0;
1.208 + gboolean* ret = g_new0( gboolean, 1 );
1.209 + //gboolean ret = TRUE;
1.210 + gsize len = 0;
1.211 +
1.212 + //GMythMonitorHandler *monitor = (GMythMonitorHandler*)data;
1.213 +
1.214 + //*ret = TRUE;
1.215 + //myth_control_acquire_context (TRUE);
1.216 +
1.217 + if ( io_channel == NULL ) {
1.218 + g_debug ("Monitor socket is NULL!\n");
1.219 + *ret = FALSE;
1.220 + goto clean_up;
1.221 + }
1.222 + gmyth_debug ("Listening on Monitor socket...!\n");
1.223 +
1.224 + while (TRUE) {
1.225 +
1.226 + //condition = g_io_channel_get_buffer_condition( io_channel );
1.227 +
1.228 + //myth_control_acquire_context (TRUE);
1.229 +
1.230 + //while ( !has_io_access )
1.231 + // g_cond_wait( io_watcher_cond, mutex );
1.232 +
1.233 + //myth_control_acquire_context (TRUE);
1.234 +
1.235 + if (condition & G_IO_HUP) {
1.236 + gmyth_debug ("Read end of pipe died!\n");
1.237 + *ret = FALSE;
1.238 + goto clean_up;
1.239 + }
1.240 +
1.241 + if ( ( condition & G_IO_IN ) != 0 ) {
1.242 + byte_array = g_byte_array_new();
1.243 + io_status = g_io_channel_set_encoding( io_channel, NULL, &error );
1.244 + guint buffer_size = GMYTHTV_BUFFER_SIZE;
1.245 + do
1.246 + {
1.247 + trash = g_new0( gchar, buffer_size );
1.248 +
1.249 + io_status = g_io_channel_read_chars( io_channel, trash,
1.250 + buffer_size, &len, &error);
1.251 +
1.252 + gmyth_debug ( "[%s] Received data buffer from IO binary channel... %d bytes gone!\n",
1.253 + __FUNCTION__, len );
1.254 +
1.255 + recv += len;
1.256 +
1.257 + byte_array = g_byte_array_append( byte_array, (const guint8*)trash, len );
1.258 +
1.259 + if ( trash != NULL )
1.260 + g_free( trash );
1.261 +
1.262 + io_cond = g_io_channel_get_buffer_condition( io_channel );
1.263 +
1.264 + } while ( ( io_cond & G_IO_IN ) != 0 );
1.265 + }
1.266 +
1.267 + g_usleep( 300 );
1.268 +
1.269 + }
1.270 + //ret = g_io_channel_read_chars ( source, &msg, &len, NULL, &err);
1.271 + if ( io_status == G_IO_STATUS_ERROR ) {
1.272 + gmyth_debug ("[%s] Error reading: %s\n", __FUNCTION__, error != NULL ? error->message : "" );
1.273 + *ret = FALSE;
1.274 + goto clean_up;
1.275 + }
1.276 + gmyth_debug ("\n[%s]\tEVENT: Read %d bytes: %s\n\n", __FUNCTION__, recv, byte_array->data != NULL ? (gchar*)byte_array->data : "[no event data]" );
1.277 +
1.278 + //g_hash_table_insert( monitor->backend_msgs, (gpointer)monitor->actual_index,
1.279 + // byte_array->data );
1.280 +
1.281 + //monitor->actual_index += recv;
1.282 +
1.283 + if ( byte_array != NULL ) {
1.284 + g_byte_array_free( byte_array, TRUE );
1.285 + byte_array = NULL;
1.286 + }
1.287 +
1.288 +clean_up:
1.289 +
1.290 + return ret;
1.291 +
1.292 +}
1.293 +
1.294 +static gboolean
1.295 +gmyth_connect_to_backend_monitor (GMythMonitorHandler *monitor)
1.296 +{
1.297 + gboolean ret = TRUE;
1.298 +
1.299 + /* Creates the event socket */
1.300 + if (monitor->event_sock != NULL) {
1.301 + g_object_unref (monitor->event_sock);
1.302 + monitor->event_sock = NULL;
1.303 + }
1.304 +
1.305 + monitor->event_sock = gmyth_socket_new();
1.306 +
1.307 + /* Connects the socket, send Mythtv ANN Monitor and verify Mythtv protocol version */
1.308 + if (!gmyth_socket_connect_to_backend_events ( monitor->event_sock,
1.309 + monitor->hostname, monitor->port, FALSE ) )
1.310 + {
1.311 + g_object_unref (monitor->event_sock);
1.312 + monitor->event_sock = NULL;
1.313 + ret = FALSE;
1.314 + }
1.315 +
1.316 + return ret;
1.317 +}
1.318 +
1.319 +gboolean
1.320 +gmyth_monitor_handler_start (GMythMonitorHandler *monitor)
1.321 +{
1.322 + gboolean ret = TRUE;
1.323 +
1.324 + monitor_th = g_thread_create( (GThreadFunc)myth_control_sock_listener,
1.325 + monitor->event_sock->sd_io_ch, TRUE, NULL );
1.326 +
1.327 +/*
1.328 + io_watcher_context = g_main_context_default();
1.329 + //GMainLoop *loop = g_main_loop_new( NULL, TRUE );
1.330 +
1.331 + GSource *source;
1.332 +
1.333 + if ( monitor->event_sock->sd_io_ch != NULL ) {
1.334 + source = g_io_create_watch( monitor->event_sock->sd_io_ch, G_IO_IN );
1.335 + //monitor->event_sock->sd_io_ch
1.336 + } else {
1.337 + ret = FALSE;
1.338 + goto cleanup;
1.339 + }
1.340 +
1.341 + g_source_set_callback ( source, (GSourceFunc)myth_control_sock_listener, NULL, NULL );
1.342 +
1.343 + g_source_attach( source, io_watcher_context );
1.344 +
1.345 + if (source==NULL) {
1.346 + gmyth_debug( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ );
1.347 + goto cleanup;
1.348 + }
1.349 + */
1.350 +
1.351 + gmyth_debug ( "[%s]\tOK! Starting listener on the MONITOR event socket...\n", __FUNCTION__ );
1.352 +
1.353 + //g_main_loop_run( loop );
1.354 +
1.355 +cleanup:
1.356 + //if ( source != NULL )
1.357 + // g_source_unref( source );
1.358 +
1.359 + gmyth_debug( "[%s] Watch listener function over the IO control channel? %s!!!\n",
1.360 + __FUNCTION__, ( ret == TRUE ? "YES" : "NO" ) );
1.361 +
1.362 + return ret;
1.363 +}
1.364 +
1.365 +void
1.366 +gmyth_monitor_handler_close( GMythMonitorHandler *monitor )
1.367 +{
1.368 +
1.369 + if (monitor->event_sock) {
1.370 + g_object_unref( monitor->event_sock );
1.371 + monitor->event_sock = NULL;
1.372 + }
1.373 +
1.374 + if (monitor->hostname) {
1.375 + g_free( monitor->hostname );
1.376 + monitor->hostname = NULL;
1.377 + }
1.378 +
1.379 +}
1.380 +
1.381 +static gboolean
1.382 +myth_control_acquire_context( gboolean do_wait )
1.383 +{
1.384 +
1.385 + gboolean ret = TRUE;
1.386 + //guint max_iter = 50;
1.387 +
1.388 + //g_mutex_lock( mutex );
1.389 +
1.390 + //while ( !has_io_access )
1.391 + // g_cond_wait( io_watcher_cond, mutex );
1.392 +
1.393 + //has_io_access = FALSE;
1.394 +
1.395 + //myth_control_acquire_context (FALSE);
1.396 +
1.397 + /*
1.398 + if ( do_wait ) {
1.399 + while ( --max_iter > 0 && !g_main_context_wait( io_watcher_context, io_watcher_cond, mutex ) )
1.400 + ret = FALSE;
1.401 + } else if ( !g_main_context_acquire( io_watcher_context ) )
1.402 + ret = FALSE;
1.403 + */
1.404 +
1.405 + //g_static_mutex_lock( &st_mutex );
1.406 +
1.407 + return ret;
1.408 +
1.409 +}
1.410 +
1.411 +static gboolean
1.412 +myth_control_release_context( )
1.413 +{
1.414 +
1.415 + gboolean ret = TRUE;
1.416 +
1.417 + //g_static_mutex_unlock( &st_mutex );
1.418 +
1.419 + //g_main_context_release( io_watcher_context );
1.420 +
1.421 + //g_main_context_wakeup( io_watcher_context );
1.422 +
1.423 + //has_io_access = TRUE;
1.424 +
1.425 + //g_cond_broadcast( io_watcher_cond );
1.426 +
1.427 + //g_mutex_unlock( mutex );
1.428 +
1.429 + return ret;
1.430 +
1.431 +}
1.432 +
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/gmyth/src/gmyth_monitor_handler.h Sat Dec 02 03:40:20 2006 +0000
2.3 @@ -0,0 +1,100 @@
2.4 +/* vim: set sw=2: -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2; c-indent-level: 2-*- */
2.5 +/**
2.6 + * GMyth Library
2.7 + *
2.8 + * @file gmyth/gmyth_monitor_handler.h
2.9 + *
2.10 + * @brief <p> GMythMonitorHandler deals with the streaming media events remote/local
2.11 + * that are sent to the MythTV frontend.
2.12 + *
2.13 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
2.14 + * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
2.15 + *
2.16 + *//*
2.17 + *
2.18 + * This program is free software; you can redistribute it and/or modify
2.19 + * it under the terms of the GNU Lesser General Public License as published by
2.20 + * the Free Software Foundation; either version 2 of the License, or
2.21 + * (at your option) any later version.
2.22 + *
2.23 + * This program is distributed in the hope that it will be useful,
2.24 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.25 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.26 + * GNU General Public License for more details.
2.27 + *
2.28 + * You should have received a copy of the GNU Lesser General Public License
2.29 + * along with this program; if not, write to the Free Software
2.30 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2.31 + */
2.32 +
2.33 +#ifndef __GMYTH_MONITOR_HANDLER_H__
2.34 +#define __GMYTH_MONITOR_HANDLER_H__
2.35 +
2.36 +#include <glib-object.h>
2.37 +#include <glib.h>
2.38 +
2.39 +#include "gmyth_socket.h"
2.40 +#include "gmyth_uri.h"
2.41 +#include "gmyth_livetv.h"
2.42 +
2.43 +#include <stdio.h>
2.44 +#include <stdlib.h>
2.45 +#include <string.h>
2.46 +#include <netdb.h>
2.47 +#include <sys/socket.h>
2.48 +#include <unistd.h>
2.49 +
2.50 +#define G_BEGIN_DECLS
2.51 +
2.52 +#define GMYTH_MONITOR_HANDLER_TYPE (gmyth_monitor_handler_get_type ())
2.53 +#define GMYTH_MONITOR_HANDLER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMYTH_MONITOR_HANDLER_TYPE, GMythMonitorHandler))
2.54 +#define GMYTH_MONITOR_HANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GMYTH_MONITOR_HANDLER_TYPE, GMythMonitorHandlerClass))
2.55 +#define IS_GMYTH_MONITOR_HANDLER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMYTH_MONITOR_HANDLER_TYPE))
2.56 +#define IS_GMYTH_MONITOR_HANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GMYTH_MONITOR_HANDLER_TYPE))
2.57 +#define GMYTH_MONITOR_HANDLER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GMYTH_MONITOR_HANDLER_TYPE, GMythMonitorHandlerClass))
2.58 +
2.59 +#define GMYTHTV_MONITOR_HANDLER_READ_ERROR -314
2.60 +
2.61 +typedef struct _GMythMonitorHandler GMythMonitorHandler;
2.62 +typedef struct _GMythMonitorHandlerClass GMythMonitorHandlerClass;
2.63 +
2.64 +struct _GMythMonitorHandlerClass
2.65 +{
2.66 + GObjectClass parent_class;
2.67 +
2.68 + /* callbacks */
2.69 + /* no one for now */
2.70 +};
2.71 +
2.72 +struct _GMythMonitorHandler
2.73 +{
2.74 + GObject parent;
2.75 +
2.76 + /* MythTV version number */
2.77 + gint mythtv_version;
2.78 +
2.79 + /* socket descriptors */
2.80 + GMythSocket *event_sock;
2.81 +
2.82 + gchar *hostname;
2.83 + gint port;
2.84 +
2.85 + gint64 actual_index;
2.86 +
2.87 + /* stores the messages coming from the backend */
2.88 + GHashTable *backend_msgs;
2.89 +};
2.90 +
2.91 +GType gmyth_monitor_handler_get_type (void);
2.92 +
2.93 +GMythMonitorHandler* gmyth_monitor_handler_new (void);
2.94 +
2.95 +gboolean gmyth_monitor_handler_open (GMythMonitorHandler *monitor, gchar *hostname, gint port);
2.96 +
2.97 +gboolean gmyth_monitor_handler_start (GMythMonitorHandler *monitor);
2.98 +
2.99 +void gmyth_monitor_handler_close (GMythMonitorHandler *monitor);
2.100 +
2.101 +#define G_END_DECLS
2.102 +
2.103 +#endif /* __GMYTH_MONITOR_HANDLER_H__ */