1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth/src/libgmyth/gmyth_socket.c Wed Sep 20 23:45:35 2006 +0100
1.3 @@ -0,0 +1,708 @@
1.4 +/**
1.5 + * GMyth Library
1.6 + *
1.7 + * @file gmyth/gmyth_socket.c
1.8 + *
1.9 + * @brief <p> MythTV socket implementation, according to the MythTV Project
1.10 + * (www.mythtv.org).
1.11 + *
1.12 + * This component provides basic socket functionalities to interact with
1.13 + * the Mythtv backend.
1.14 + * <p>
1.15 + *
1.16 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
1.17 + * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
1.18 + *
1.19 + *//*
1.20 + *
1.21 + * This program is free software; you can redistribute it and/or modify
1.22 + * it under the terms of the GNU Lesser General Public License as published by
1.23 + * the Free Software Foundation; either version 2 of the License, or
1.24 + * (at your option) any later version.
1.25 + *
1.26 + * This program is distributed in the hope that it will be useful,
1.27 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.28 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.29 + * GNU General Public License for more details.
1.30 + *
1.31 + * You should have received a copy of the GNU Lesser General Public License
1.32 + * along with this program; if not, write to the Free Software
1.33 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1.34 + */
1.35 +
1.36 +#include <glib.h>
1.37 +#include <glib/gprintf.h>
1.38 +
1.39 +#include <arpa/inet.h>
1.40 +#include <sys/types.h>
1.41 +#include <sys/socket.h>
1.42 +#include <netdb.h>
1.43 +#include <errno.h>
1.44 +#include <stdlib.h>
1.45 +
1.46 +#include "gmyth_socket.h"
1.47 +#include "gmyth_stringlist.h"
1.48 +#include "gmyth_context.h"
1.49 +
1.50 +#define BUFLEN 512
1.51 +#define MYTH_SEPARATOR "[]:[]"
1.52 +#define MYTH_PROTOCOL_FIELD_SIZE 8
1.53 +
1.54 +static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
1.55 +
1.56 +static void gmyth_socket_class_init (GMythSocketClass *klass);
1.57 +static void gmyth_socket_init (GMythSocket *object);
1.58 +
1.59 +static void gmyth_socket_dispose (GObject *object);
1.60 +static void gmyth_socket_finalize (GObject *object);
1.61 +
1.62 +G_DEFINE_TYPE(GMythSocket, gmyth_socket, G_TYPE_OBJECT)
1.63 +
1.64 +static void
1.65 +gmyth_socket_class_init (GMythSocketClass *klass)
1.66 +{
1.67 + GObjectClass *gobject_class;
1.68 +
1.69 + gobject_class = (GObjectClass *) klass;
1.70 +
1.71 + gobject_class->dispose = gmyth_socket_dispose;
1.72 + gobject_class->finalize = gmyth_socket_finalize;
1.73 +}
1.74 +
1.75 +static void
1.76 +gmyth_socket_init (GMythSocket *gmyth_socket)
1.77 +{
1.78 +}
1.79 +
1.80 +/** Gets the some important address translation info, from the client socket
1.81 + * that will open a connection.
1.82 + *
1.83 + * @return gint that represents the error number from getaddrinfo().
1.84 + */
1.85 +static gint
1.86 +gmyth_socket_toaddrinfo( gchar *addr, gint port, struct addrinfo **addrInfo )
1.87 +{
1.88 + struct addrinfo hints;
1.89 + gchar *portStr = g_strnfill( 32, ' ' );
1.90 + gint errorn = EADDRNOTAVAIL;
1.91 +
1.92 + memset( &hints, 0, sizeof(hints) );
1.93 + hints.ai_family = AF_INET;
1.94 + hints.ai_socktype = SOCK_STREAM;
1.95 + /* hints.ai_flags = AI_NUMERICHOST; */
1.96 + if ( port != -1 )
1.97 + sprintf(portStr, "%d", port);
1.98 + else
1.99 + portStr = NULL;
1.100 +
1.101 + g_debug( "[%s] Address: %s, port: %s\n", __FUNCTION__, addr, portStr );
1.102 + if ( ( errorn = getaddrinfo(addr, portStr, &hints, addrInfo) ) != 0 ) {
1.103 + g_printerr( "[%s] Socket ERROR: %s\n", __FUNCTION__, gai_strerror(errorn) );
1.104 + }
1.105 +
1.106 + return errorn;
1.107 +}
1.108 +
1.109 +/** This function retrieves the local hostname of the
1.110 + * client machine.
1.111 + *
1.112 + * @return GString* get local hostname.
1.113 + */
1.114 +GString *
1.115 +gmyth_socket_get_local_hostname( )
1.116 +{
1.117 + GString *str = g_string_new("");
1.118 +
1.119 + gchar *localhostname = g_strnfill( 1024, ' ' );
1.120 + gchar *localaddr = g_strdup( "127.0.0.1" );
1.121 +
1.122 + gboolean found_addr = FALSE;
1.123 +
1.124 + struct addrinfo* addr_info_data = NULL, *addr_info0 = NULL;
1.125 +
1.126 + struct sockaddr_in* sa = NULL;
1.127 +
1.128 + g_static_mutex_lock( &mutex );
1.129 +
1.130 + gethostname(localhostname, 1024);
1.131 +
1.132 + gint err = gmyth_socket_toaddrinfo( localhostname, -1, &addr_info_data );
1.133 +
1.134 + addr_info0 = addr_info_data;
1.135 +
1.136 + while( addr_info0 != NULL && addr_info0->ai_addr != NULL &&
1.137 + ( sa = (struct sockaddr_in*)addr_info0->ai_addr ) != NULL && !found_addr ) {
1.138 + localaddr = inet_ntoa( sa->sin_addr );
1.139 + if ( localaddr != NULL )
1.140 + g_print( "[%s] localaddr = %s\n", __FUNCTION__, localaddr );
1.141 +
1.142 + if ( localaddr != NULL && ( g_strrstr( localaddr, "127" ) == NULL ) ) {
1.143 + g_print( "[%s] Trying the address %s (err = %d).\n",
1.144 + __FUNCTION__, localaddr, err );
1.145 + g_print( "[%s] Found local address %s!\n", __FUNCTION__, localaddr );
1.146 + str = g_string_assign( str, g_strdup( localaddr ) );
1.147 + found_addr = TRUE;
1.148 + break;
1.149 + }
1.150 + addr_info0 = addr_info0->ai_next;
1.151 + };
1.152 +
1.153 + if ( found_addr == FALSE ) {
1.154 + g_warning("[%s] Could not determine the local hostname address. Setting to %s\n",
1.155 + __FUNCTION__, localaddr );
1.156 + if ( localaddr != NULL )
1.157 + str = g_string_assign( str, localaddr );
1.158 + else
1.159 + str = g_string_assign( str, "127.0.0.1" );
1.160 + }
1.161 +
1.162 + g_static_mutex_unlock( &mutex );
1.163 +
1.164 + if (localhostname!=NULL)
1.165 + g_free( localhostname );
1.166 +
1.167 + return str;
1.168 +}
1.169 +
1.170 +static void
1.171 +gmyth_socket_dispose (GObject *object)
1.172 +{
1.173 + GMythSocket *gmyth_socket = GMYTH_SOCKET(object);
1.174 +
1.175 + gmyth_socket_close_connection (gmyth_socket);
1.176 + /* disconnect socket */
1.177 + G_OBJECT_CLASS (gmyth_socket_parent_class)->dispose (object);
1.178 +}
1.179 +
1.180 +static void
1.181 +gmyth_socket_finalize (GObject *object)
1.182 +{
1.183 + g_signal_handlers_destroy (object);
1.184 +
1.185 + G_OBJECT_CLASS (gmyth_socket_parent_class)->finalize (object);
1.186 +}
1.187 +
1.188 +/** Creates a new instance of GMythSocket.
1.189 + *
1.190 + * @return a new instance of GMythSocket.
1.191 + */
1.192 +GMythSocket*
1.193 +gmyth_socket_new ()
1.194 +{
1.195 + GMythSocket *gmyth_socket = GMYTH_SOCKET (g_object_new(GMYTH_SOCKET_TYPE, NULL));
1.196 +
1.197 + gmyth_socket->sd_io_ch = NULL;
1.198 +
1.199 + gmyth_socket->hostname = g_strdup("");
1.200 +
1.201 + gmyth_socket->port = 6543;
1.202 +
1.203 + return gmyth_socket;
1.204 +}
1.205 +
1.206 +/** Connects to the backend.
1.207 + *
1.208 + * @param gmyth_socket The GMythSocket instance.
1.209 + * @param hostname The backend hostname or IP address.
1.210 + * @param port The backend port.
1.211 + * @return TRUE if success, FALSE if error.
1.212 + */
1.213 +gboolean
1.214 +gmyth_socket_connect (GMythSocket **gmyth_socket,
1.215 + gchar *hostname, gint port)
1.216 +{
1.217 + struct addrinfo *addr_info_data = NULL, *addr_info0 = NULL;
1.218 + gint ret_code = -1;
1.219 + gint errno;
1.220 + gboolean ret = TRUE;
1.221 +
1.222 + if ( hostname == NULL )
1.223 + g_printerr ( "[%s] Invalid hostname parameter!\n", __FUNCTION__ );
1.224 +
1.225 + errno = gmyth_socket_toaddrinfo( hostname, port, &addr_info_data );
1.226 +
1.227 + g_return_val_if_fail( addr_info_data != NULL, FALSE );
1.228 +
1.229 + /* store hostname and port number */
1.230 + (*gmyth_socket)->hostname = g_strdup( hostname );
1.231 + (*gmyth_socket)->port = port;
1.232 +
1.233 + for ( addr_info0 = addr_info_data; addr_info0; addr_info0 = addr_info_data->ai_next ) {
1.234 +
1.235 + struct sockaddr_in *sa = (struct sockaddr_in*)addr_info0->ai_addr;
1.236 + /* init socket descriptor */
1.237 + (*gmyth_socket)->sd = socket( addr_info0->ai_family, addr_info0->ai_socktype,
1.238 + addr_info0->ai_protocol );
1.239 +
1.240 + if ( (*gmyth_socket)->sd < 0 )
1.241 + continue;
1.242 +
1.243 + g_debug( "[%s] hostname = %s, sock_fd = %d, addr = %s, addr_len = %d, \
1.244 + ai_family = %d, ai_protocol = %d\n",
1.245 + __FUNCTION__, hostname, (*gmyth_socket)->sd, inet_ntoa( sa->sin_addr ),
1.246 + addr_info0->ai_addrlen, addr_info0->ai_family, addr_info0->ai_protocol );
1.247 +
1.248 + if ( ( ret_code = connect( (*gmyth_socket)->sd, (struct sockaddr *)addr_info0->ai_addr,
1.249 + addr_info0->ai_addrlen ) ) < 0 )
1.250 + {
1.251 + g_printerr( "[%s] Error connecting to backend!\n", __FUNCTION__ );
1.252 + if ( ret_code == ETIMEDOUT )
1.253 + g_printerr( "[%s]\tBackend host unreachable!\n", __FUNCTION__ );
1.254 +
1.255 + g_printerr( "ERROR: %s\n", gai_strerror(ret_code) );
1.256 + continue;
1.257 + }
1.258 +
1.259 + /* only will be reached if none of the error above occurred */
1.260 + break;
1.261 +
1.262 + }
1.263 +
1.264 + (*gmyth_socket)->sd_io_ch = g_io_channel_unix_new( (*gmyth_socket)->sd );
1.265 +
1.266 + //if (addr_info_data != NULL )
1.267 + //freeaddrinfo( addr_info_data );
1.268 +
1.269 + ret = ( ret_code == 0 ) ? TRUE : FALSE ;
1.270 +
1.271 + return ret;
1.272 +
1.273 +}
1.274 +
1.275 +/** Gets the GIOChannel associated to the given GMythSocket.
1.276 + *
1.277 + * @param gmyth_socket The GMythSocket instance.
1.278 + */
1.279 +GIOChannel *
1.280 +gmyth_socket_get_io_channel( GMythSocket *gmyth_socket )
1.281 +{
1.282 + g_return_val_if_fail( gmyth_socket != NULL, NULL );
1.283 +
1.284 + return gmyth_socket->sd_io_ch;
1.285 +}
1.286 +
1.287 +/** Verifies if the socket is able to read.
1.288 + *
1.289 + * @param gmyth_socket The GMythSocket instance.
1.290 + * @return TRUE if the socket is able to read, FALSE if not.
1.291 + */
1.292 +gboolean
1.293 +gmyth_socket_is_able_to_read( GMythSocket *gmyth_socket )
1.294 +{
1.295 + gboolean ret = TRUE;
1.296 +
1.297 + /* verify if the input (read) buffer is ready to receive data */
1.298 + GIOCondition io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch );
1.299 +
1.300 + if ( ( io_cond & G_IO_IN ) == 0 ) {
1.301 + g_warning ("[%s] IO channel is not able to send data!\n", __FUNCTION__);
1.302 + ret = FALSE;
1.303 + }
1.304 +
1.305 + return ret;
1.306 +
1.307 +}
1.308 +
1.309 +/** Verifies if the socket is able to write.
1.310 + *
1.311 + * @param gmyth_socket The GMythSocket instance.
1.312 + * @return TRUE if the socket is able to write, FALSE if not.
1.313 + */
1.314 +gboolean
1.315 +gmyth_socket_is_able_to_write( GMythSocket *gmyth_socket )
1.316 +{
1.317 + gboolean ret = TRUE;
1.318 +
1.319 + /* verify if the input (read) buffer is ready to receive data */
1.320 + GIOCondition io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch );
1.321 +
1.322 + if ( ( ( io_cond & G_IO_OUT ) == 0 ) || ( ( io_cond & G_IO_HUP ) == 0 ) ) {
1.323 + g_warning ("[%s] IO channel is not able to send data!\n", __FUNCTION__);
1.324 + ret = FALSE;
1.325 + }
1.326 +
1.327 + return ret;
1.328 +
1.329 +}
1.330 +
1.331 +/** Sends a command to the backend.
1.332 + *
1.333 + * @param gmyth_socket the GMythSocket instance.
1.334 + * @param command The string command to be sent.
1.335 + */
1.336 +gboolean
1.337 +gmyth_socket_send_command(GMythSocket *gmyth_socket, GString *command)
1.338 +{
1.339 + gboolean ret = TRUE;
1.340 +
1.341 + GIOStatus io_status = G_IO_STATUS_NORMAL;
1.342 + //GIOCondition io_cond;
1.343 + GError* error = NULL;
1.344 + gchar *buffer = NULL;
1.345 +
1.346 + gsize bytes_written = 0;
1.347 +
1.348 + if( command == NULL || ( command->len <= 0 ) ) {
1.349 + g_warning ("[%s] Invalid NULL command parameter!\n", __FUNCTION__);
1.350 + ret = FALSE;
1.351 + goto done;
1.352 + }
1.353 +
1.354 + g_static_mutex_lock( &mutex );
1.355 + g_debug ("[%s] Sending command to backend: %s\n", __FUNCTION__, command->str);
1.356 +
1.357 + /*
1.358 + io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch );
1.359 +
1.360 + if ( ( io_cond & G_IO_IN ) == 0 ) {
1.361 + g_warning ("[%s] IO channel is not able to send data!\n", __FUNCTION__);
1.362 + ret = FALSE;
1.363 + goto done;
1.364 + }
1.365 + */
1.366 +
1.367 + buffer = g_strnfill( BUFLEN, ' ' );
1.368 + snprintf( buffer, MYTH_PROTOCOL_FIELD_SIZE+1, "%-8d", command->len);
1.369 + g_print( "[%s] buffer = [%s]\n", __FUNCTION__, buffer );
1.370 +
1.371 + command = g_string_prepend(command, buffer);
1.372 +
1.373 + g_print( "[%s] command = [%s]\n", __FUNCTION__, command->str );
1.374 +
1.375 + /* write bytes to socket */
1.376 + io_status = g_io_channel_write_chars( gmyth_socket->sd_io_ch, command->str,
1.377 + command->len, &bytes_written, &error );
1.378 +
1.379 +
1.380 + if( (io_status == G_IO_STATUS_ERROR) || ( bytes_written <= 0 ) ) {
1.381 + g_warning ("[%s] Error while writing to socket", __FUNCTION__);
1.382 + ret = FALSE;
1.383 + } else if ( bytes_written < command->len ) {
1.384 + g_warning ("[%s] Not all data was written socket", __FUNCTION__);
1.385 + ret = FALSE;
1.386 + }
1.387 +
1.388 + io_status = g_io_channel_flush( gmyth_socket->sd_io_ch, &error );
1.389 +
1.390 + if ( ( bytes_written != command->len ) || ( io_status == G_IO_STATUS_ERROR ) )
1.391 + {
1.392 + g_warning ("[%s] Some problem occurred when sending data to the socket\n", __FUNCTION__);
1.393 +
1.394 + ret = TRUE;
1.395 + }
1.396 +
1.397 + g_static_mutex_unlock( &mutex );
1.398 +done:
1.399 + if ( error != NULL ) {
1.400 + g_printerr( "[%s] Error found reading data from IO channel: (%d, %s)\n", __FUNCTION__, error->code, error->message );
1.401 + ret = FALSE;
1.402 + g_error_free( error );
1.403 + }
1.404 +
1.405 + if ( buffer!= NULL )
1.406 + g_free( buffer );
1.407 +
1.408 + return ret;
1.409 +}
1.410 +
1.411 +/** Starts Mythtv protocol level connection. Checks Mythtv protocol version
1.412 + * supported by the backend and send the "ANN" command.
1.413 + *
1.414 + * @param gmyth_socket the GMythSocket instance.
1.415 + * @param hostname_backend The backend hostname or IP address.
1.416 + * @param port The backend port to connect.
1.417 + * @param blocking_client A flag to choose between blocking and non-blocking
1.418 + * backend connection.
1.419 + */
1.420 +gboolean
1.421 +gmyth_socket_connect_to_backend (GMythSocket *gmyth_socket,
1.422 + gchar *hostname_backend, int port, gboolean blocking_client)
1.423 +{
1.424 + if (!gmyth_socket_connect (&gmyth_socket, hostname_backend, port)) {
1.425 + g_warning ("[%s] Could not open socket to backend machine", __FUNCTION__);
1.426 + return FALSE;
1.427 + }
1.428 +
1.429 + if (gmyth_socket_check_protocol_version (gmyth_socket)) {
1.430 +
1.431 + GString *result;
1.432 + GString *base_str = g_string_new("");
1.433 + GString *hostname = NULL;
1.434 +
1.435 + hostname = gmyth_socket_get_local_hostname();
1.436 +
1.437 + g_string_printf(base_str, "ANN %s %s 0",
1.438 + (blocking_client ? "Playback" : "Monitor"),
1.439 + hostname->str);
1.440 +
1.441 + g_debug ("[%s] Connection command sent to backend: %s", __FUNCTION__, base_str->str);
1.442 +
1.443 + gmyth_socket_send_command (gmyth_socket, base_str);
1.444 + result = gmyth_socket_receive_response(gmyth_socket);
1.445 +
1.446 + if (result != NULL) {
1.447 + g_debug ("[%s] Response received from backend: %s", __FUNCTION__, result->str);
1.448 + g_string_free (result, TRUE);
1.449 + }
1.450 +
1.451 + g_string_free (hostname, TRUE);
1.452 + g_string_free (base_str, TRUE);
1.453 +
1.454 + return TRUE;
1.455 + } else {
1.456 + g_warning ("[%s] GMythSocket could not connect to the backend", __FUNCTION__);
1.457 + return FALSE;
1.458 + }
1.459 +
1.460 +}
1.461 +
1.462 +/** Closes the socket connection to the backend.
1.463 + *
1.464 + * @param gmyth_socket The GMythSocket instance.
1.465 + */
1.466 +void
1.467 +gmyth_socket_close_connection (GMythSocket *gmyth_socket)
1.468 +{
1.469 + close (gmyth_socket->sd);
1.470 +}
1.471 +
1.472 +
1.473 +/** Try the MythTV version numbers, and get the version returned by
1.474 + * the possible REJECT message, in order to contruct a new
1.475 + * MythTV version request.
1.476 + *
1.477 + * @param gmyth_socket The GMythSocket instance.
1.478 + * @param mythtv_version The Mythtv protocol version to be tested
1.479 + */
1.480 +gboolean
1.481 +gmyth_socket_check_protocol_version_number (GMythSocket *gmyth_socket, gint mythtv_version)
1.482 +{
1.483 + GString *response;
1.484 + GString *payload;
1.485 + gchar *new_version = g_strdup("");
1.486 + gboolean res = TRUE;
1.487 +
1.488 +try_new_version:
1.489 + payload = g_string_new ("MYTH_PROTO_VERSION");
1.490 + g_string_append_printf( payload, " %d", mythtv_version );
1.491 +
1.492 + gmyth_socket_send_command(gmyth_socket, payload);
1.493 + response = gmyth_socket_receive_response(gmyth_socket);
1.494 +
1.495 + if (response == NULL) {
1.496 + g_warning ("[%s] Check protocol version error! Not answered!", __FUNCTION__);
1.497 + res = FALSE;
1.498 + goto done;
1.499 + }
1.500 +
1.501 + res = g_str_has_prefix (response->str, "ACCEPT");
1.502 + if (!res) {
1.503 + g_warning ("[%s] Protocol version request error: %s", __FUNCTION__, response->str);
1.504 + /* get the version number returned by the REJECT message */
1.505 + if ( ( res = g_str_has_prefix (response->str, "REJECT") ) == TRUE ) {
1.506 + new_version = g_strrstr( response->str, "]" );
1.507 + if (new_version!=NULL) {
1.508 + ++new_version; /* skip ']' character */
1.509 + if ( new_version != NULL ) {
1.510 + g_print( "[%s] got MythTV version = %s\n", __FUNCTION__, new_version );
1.511 + mythtv_version = g_ascii_strtoull( g_strdup( new_version ), NULL, 10 );
1.512 + /* do reconnection to the socket (socket is closed if the MythTV version was wrong) */
1.513 + gmyth_socket_connect( &gmyth_socket, gmyth_socket->hostname, gmyth_socket->port );
1.514 + /* g_free( new_version ); */
1.515 + goto try_new_version;
1.516 + }
1.517 + }
1.518 + }
1.519 + }
1.520 +
1.521 +done:
1.522 + if ( payload != NULL )
1.523 + g_string_free (payload, TRUE);
1.524 + if ( response != NULL )
1.525 + g_string_free (response, TRUE);
1.526 +// if (new_version!=NULL)
1.527 +// g_free( new_version );
1.528 +
1.529 + return res;
1.530 +}
1.531 +
1.532 +/** Verifies if the Mythtv backend supported the GMyth supported version.
1.533 + *
1.534 + * @param gmyth_socket The GMythSocket instance.
1.535 + * @return TRUE if supports, FALSE if not.
1.536 + */
1.537 +gboolean
1.538 +gmyth_socket_check_protocol_version (GMythSocket *gmyth_socket)
1.539 +{
1.540 + return gmyth_socket_check_protocol_version_number( gmyth_socket, MYTHTV_VERSION_DEFAULT );
1.541 +}
1.542 +
1.543 +/** Receives a backend answer after a gmyth_socket_send_command_call ().
1.544 + *
1.545 + * @param gmyth_socket The GMythSocket instance.
1.546 + * @return The response received, or NULL if error or nothing was received.
1.547 + */
1.548 +GString*
1.549 +gmyth_socket_receive_response(GMythSocket *gmyth_socket)
1.550 +{
1.551 + GIOStatus io_status = G_IO_STATUS_NORMAL;
1.552 + GError* error = NULL;
1.553 + gchar *buffer = NULL;
1.554 +
1.555 + GString *str = NULL;
1.556 +
1.557 + gsize bytes_read = 0;
1.558 + gint len = 0;
1.559 + GIOCondition io_cond;
1.560 +
1.561 + g_return_val_if_fail( gmyth_socket != NULL, NULL );
1.562 +
1.563 + /* verify if the input (read) buffer is ready to receive data */
1.564 +
1.565 + buffer = g_strnfill( BUFLEN, ' ' );
1.566 +
1.567 + g_static_mutex_lock( &mutex );
1.568 +
1.569 + io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, MYTH_PROTOCOL_FIELD_SIZE, &bytes_read, &error );
1.570 +
1.571 +
1.572 + /* verify if the input (read) buffer is ready to receive data */
1.573 + io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch );
1.574 +
1.575 + g_print ( "[%s] Bytes read = %d\n", __FUNCTION__, bytes_read );
1.576 +
1.577 + if( (io_status == G_IO_STATUS_ERROR) || (bytes_read <= 0) ) {
1.578 + g_warning ("[%s] Error in mythprotocol response from backend\n", __FUNCTION__);
1.579 + str = NULL;
1.580 + //return NULL;
1.581 + } else {
1.582 +
1.583 + io_status = g_io_channel_flush( gmyth_socket->sd_io_ch, &error );
1.584 + /* verify if the input (read) buffer is ready to receive data */
1.585 + io_cond = g_io_channel_get_buffer_condition( gmyth_socket->sd_io_ch );
1.586 +
1.587 + if ( ( io_cond & G_IO_IN ) != 0 ) {
1.588 +
1.589 + snprintf( buffer, MYTH_PROTOCOL_FIELD_SIZE+1, "%-8s", g_strdup(buffer));
1.590 + g_print( "[%s] buffer = [%s]\n", __FUNCTION__, buffer );
1.591 +
1.592 + /* removes trailing whitespace */
1.593 + buffer = g_strstrip( buffer );
1.594 +
1.595 + len = (gint)strtoull ( buffer, NULL, 10 );
1.596 +
1.597 + bytes_read = 0;
1.598 + io_status = g_io_channel_read_chars( gmyth_socket->sd_io_ch, buffer, len, &bytes_read, &error );
1.599 + buffer[bytes_read] = '\0';
1.600 + }
1.601 + }
1.602 +
1.603 + g_static_mutex_unlock( &mutex );
1.604 +
1.605 + g_debug ("[%s] Response received from backend: {%s}\n", __FUNCTION__, buffer);
1.606 +
1.607 + if ( ( bytes_read != len ) || ( io_status == G_IO_STATUS_ERROR ) )
1.608 + str = NULL;
1.609 + else
1.610 + str = g_string_new( buffer );
1.611 +
1.612 + if ( buffer != NULL )
1.613 + g_free( buffer );
1.614 +
1.615 + if ( error != NULL ) {
1.616 + g_printerr( "[%s] Error found receiving response from the IO channel: (%d, %s)\n", __FUNCTION__, error->code, error->message );
1.617 + str = NULL;
1.618 + g_error_free( error );
1.619 + }
1.620 +
1.621 + return str;
1.622 +}
1.623 +
1.624 +/** Format a Mythtv command from the str_list entries and send it to backend.
1.625 + *
1.626 + * @param gmyth_socket The GMythSocket instance.
1.627 + * @param str_list The string list to form the command
1.628 + * @return TRUE if command was sent, FALSE if any error happens.
1.629 + */
1.630 +gboolean
1.631 +gmyth_socket_write_stringlist(GMythSocket *gmyth_socket, GMythStringList* str_list)
1.632 +{
1.633 +
1.634 + GList *tmp_list;
1.635 + GPtrArray *ptr_array;
1.636 + gchar *str_array;
1.637 +
1.638 + g_static_mutex_lock( &mutex );
1.639 +
1.640 + ptr_array = g_ptr_array_sized_new(g_list_length(str_list->glist));
1.641 +
1.642 + g_print( "[%s] Number of parameters = %d\n", __FUNCTION__, g_list_length(str_list->glist) );
1.643 +
1.644 + // FIXME: change this implementation!
1.645 + tmp_list = str_list->glist;
1.646 + for(; tmp_list; tmp_list = tmp_list->next) {
1.647 + if ( tmp_list->data != NULL )
1.648 + g_ptr_array_add(ptr_array, ((GString*)tmp_list->data)->str);
1.649 + }
1.650 + g_ptr_array_add(ptr_array, NULL); // g_str_joinv() needs a NULL terminated string
1.651 +
1.652 + str_array = g_strjoinv (MYTH_SEPARATOR, (gchar **) (ptr_array->pdata));
1.653 +
1.654 + g_static_mutex_unlock( &mutex );
1.655 +
1.656 + // Sends message to backend
1.657 + // TODO: implement looping to send remaining data, and add timeout testing!
1.658 + gmyth_socket_send_command(gmyth_socket, g_string_new(str_array));
1.659 +
1.660 + g_free (str_array);
1.661 + g_ptr_array_free (ptr_array, TRUE);
1.662 +
1.663 + return TRUE;
1.664 +}
1.665 +
1.666 +/* Receives a backend command response and split it into the given string list.
1.667 + *
1.668 + * @param gmyth_socket The GMythSocket instance.
1.669 + * @param str_list the string list to be filled.
1.670 + * @return The number of received strings.
1.671 + */
1.672 +gint
1.673 +gmyth_socket_read_stringlist (GMythSocket *gmyth_socket, GMythStringList* str_list)
1.674 +{
1.675 + GString *response;
1.676 + gchar **str_array;
1.677 + gint i;
1.678 +
1.679 + response = gmyth_socket_receive_response(gmyth_socket);
1.680 + g_static_mutex_lock( &mutex );
1.681 +
1.682 + gmyth_string_list_clear_all (str_list);
1.683 + str_array = g_strsplit (response->str, MYTH_SEPARATOR, -1);
1.684 +
1.685 + for (i=0; i< g_strv_length (str_array); i++) {
1.686 + gmyth_string_list_append_string (str_list, g_string_new (str_array[i]));
1.687 + }
1.688 + g_static_mutex_unlock( &mutex );
1.689 +
1.690 + g_string_free (response, TRUE);
1.691 + g_strfreev (str_array);
1.692 +
1.693 + return gmyth_string_list_length (str_list);
1.694 +}
1.695 +
1.696 +/** Formats a Mythtv protocol command based on str_list and sends it to
1.697 + * the connected backend. The backend response is overwritten into str_list.
1.698 + *
1.699 + * @param gmyth_socket The GMythSocket instance.
1.700 + * @param str_list The string list to be sent, and on which the answer
1.701 + * will be written.
1.702 + * @return TRUE if command was sent and an answer was received, FALSE if any
1.703 + * error happens.
1.704 + */
1.705 +gint
1.706 +gmyth_socket_sendreceive_stringlist (GMythSocket *gmyth_socket, GMythStringList *str_list)
1.707 +{
1.708 + gmyth_socket_write_stringlist (gmyth_socket, str_list);
1.709 +
1.710 + return gmyth_socket_read_stringlist (gmyth_socket, str_list);
1.711 +}