gst-plugins-mythtv/myth_file_transfer.c
author leo_sobral
Thu Sep 21 00:05:27 2006 +0100 (2006-09-21)
branchtrunk
changeset 3 265cdb1c59e3
parent 2 mythtv_plugin/myth_file_transfer.c@bd3829c2e9c9
permissions -rwxr-xr-x
[svn r4] Renamed the mythtv GStreamer module name.
leo_sobral@2
     1
/* vim: set sw=2: -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2; c-indent-level: 2-*- */
leo_sobral@2
     2
/**
leo_sobral@2
     3
 * GStreamer plug-in properties:
leo_sobral@2
     4
 * - location (backend server hostname/URL) [ex.: myth://192.168.1.73:28722/1000_1092091.nuv]
leo_sobral@2
     5
 * - path (qurl - remote file to be opened)
leo_sobral@2
     6
 * - port number
leo_sobral@2
     7
 *   @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
leo_sobral@2
     8
 */
leo_sobral@2
     9
leo_sobral@2
    10
#include "myth_file_transfer.h"
leo_sobral@2
    11
#include "myth_uri.h"
leo_sobral@2
    12
#include "myth_livetv.h"
leo_sobral@2
    13
#include <gmyth/gmyth_util.h>
leo_sobral@2
    14
#include <gmyth/gmyth_socket.h>
leo_sobral@2
    15
#include <gmyth/gmyth_stringlist.h>
leo_sobral@2
    16
leo_sobral@2
    17
#include <unistd.h>
leo_sobral@2
    18
#include <glib.h>
leo_sobral@2
    19
leo_sobral@2
    20
#include <arpa/inet.h>
leo_sobral@2
    21
#include <sys/types.h>
leo_sobral@2
    22
#include <sys/socket.h>
leo_sobral@2
    23
#include <netdb.h>
leo_sobral@2
    24
#include <errno.h>
leo_sobral@2
    25
#include <stdlib.h>
leo_sobral@2
    26
leo_sobral@2
    27
#define MYTHTV_QUERY_HEADER "QUERY_FILETRANSFER"
leo_sobral@2
    28
#define MYTHTV_RECORDER_HEADER "QUERY_RECORDER"
leo_sobral@2
    29
leo_sobral@2
    30
/* default values to the file transfer parameters */
leo_sobral@2
    31
#define MYTHTV_USER_READ_AHEAD	FALSE
leo_sobral@2
    32
#define MYTHTV_RETRIES			1
leo_sobral@2
    33
#define MYTHTV_FILE_SIZE		-1
leo_sobral@2
    34
leo_sobral@2
    35
#define MYTHTV_BUFFER_SIZE		2048
leo_sobral@2
    36
leo_sobral@2
    37
#define MYTHTV_VERSION			30
leo_sobral@2
    38
leo_sobral@2
    39
#define MYTHTV_TRANSFER_MAX_WAITS	700
leo_sobral@2
    40
leo_sobral@2
    41
#ifdef MYTHTV_ENABLE_DEBUG
leo_sobral@2
    42
#define MYTHTV_ENABLE_DEBUG	1
leo_sobral@2
    43
#else
leo_sobral@2
    44
#undef MYTHTV_ENABLE_DEBUG
leo_sobral@2
    45
#endif
leo_sobral@2
    46
leo_sobral@2
    47
/* this NDEBUG is to maintain compatibility with GMyth library */
leo_sobral@2
    48
#ifndef NDEBUG
leo_sobral@2
    49
#define MYTHTV_ENABLE_DEBUG	1
leo_sobral@2
    50
#endif
leo_sobral@2
    51
leo_sobral@2
    52
static guint wait_to_transfer = 0;
leo_sobral@2
    53
leo_sobral@2
    54
enum myth_sock_types {
leo_sobral@2
    55
  MYTH_PLAYBACK_TYPE = 0,
leo_sobral@2
    56
  MYTH_MONITOR_TYPE,
leo_sobral@2
    57
  MYTH_FILETRANSFER_TYPE,
leo_sobral@2
    58
  MYTH_RINGBUFFER_TYPE
leo_sobral@2
    59
};
leo_sobral@2
    60
leo_sobral@2
    61
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
leo_sobral@2
    62
leo_sobral@2
    63
static void myth_file_transfer_class_init          (MythFileTransferClass *klass);
leo_sobral@2
    64
static void myth_file_transfer_init                (MythFileTransfer *object);
leo_sobral@2
    65
leo_sobral@2
    66
static void myth_file_transfer_dispose  (GObject *object);
leo_sobral@2
    67
static void myth_file_transfer_finalize (GObject *object);
leo_sobral@2
    68
leo_sobral@2
    69
static GMythSocket *myth_connect_to_transfer_backend( MythFileTransfer **transfer, guint sock_type );
leo_sobral@2
    70
static void* myth_init_io_watchers( void *data );
leo_sobral@2
    71
leo_sobral@2
    72
void myth_file_transfer_close( MythFileTransfer *transfer );
leo_sobral@2
    73
leo_sobral@2
    74
G_DEFINE_TYPE(MythFileTransfer, myth_file_transfer, G_TYPE_OBJECT)
leo_sobral@2
    75
leo_sobral@2
    76
static guint64
leo_sobral@2
    77
mmyth_util_decode_long_long( GMythStringList *strlist, guint offset  )
leo_sobral@2
    78
{
leo_sobral@2
    79
leo_sobral@2
    80
  guint64 ret_value = 0LL;
leo_sobral@2
    81
leo_sobral@2
    82
  g_return_val_if_fail( strlist != NULL, ret_value );
leo_sobral@2
    83
leo_sobral@2
    84
  if ( offset < gmyth_string_list_length( strlist ))
leo_sobral@2
    85
    g_printerr( "[%s] Offset is lower than the GMythStringList (offset = %d)!\n", __FUNCTION__, offset );
leo_sobral@2
    86
  g_return_val_if_fail( offset < gmyth_string_list_length( strlist ), ret_value );
leo_sobral@2
    87
leo_sobral@2
    88
  gint l1 = gmyth_string_list_get_int( strlist, offset );
leo_sobral@2
    89
  gint l2 = gmyth_string_list_get_int( strlist, offset + 1 );
leo_sobral@2
    90
leo_sobral@2
    91
  ret_value = ((guint64)(l2) & 0xffffffffLL) | ((guint64)(l1) << 32);
leo_sobral@2
    92
leo_sobral@2
    93
  return ret_value;
leo_sobral@2
    94
leo_sobral@2
    95
}
leo_sobral@2
    96
leo_sobral@2
    97
static void
leo_sobral@2
    98
myth_file_transfer_class_init (MythFileTransferClass *klass)
leo_sobral@2
    99
{
leo_sobral@2
   100
  GObjectClass *gobject_class;
leo_sobral@2
   101
leo_sobral@2
   102
  gobject_class = (GObjectClass *) klass;
leo_sobral@2
   103
leo_sobral@2
   104
  gobject_class->dispose  = myth_file_transfer_dispose;
leo_sobral@2
   105
  gobject_class->finalize = myth_file_transfer_finalize;
leo_sobral@2
   106
}
leo_sobral@2
   107
leo_sobral@2
   108
  static void
leo_sobral@2
   109
myth_file_transfer_init (MythFileTransfer *myth_file_transfer)
leo_sobral@2
   110
{ 
leo_sobral@2
   111
  g_return_if_fail( myth_file_transfer != NULL );
leo_sobral@2
   112
  myth_file_transfer->mythtv_version = MYTHTV_VERSION;
leo_sobral@2
   113
}
leo_sobral@2
   114
leo_sobral@2
   115
static void
leo_sobral@2
   116
myth_file_transfer_dispose  (GObject *object)
leo_sobral@2
   117
{
leo_sobral@2
   118
  MythFileTransfer *myth_file_transfer = MYTH_FILE_TRANSFER(object);
leo_sobral@2
   119
leo_sobral@2
   120
  myth_file_transfer_close( myth_file_transfer );
leo_sobral@2
   121
leo_sobral@2
   122
  G_OBJECT_CLASS (myth_file_transfer_parent_class)->dispose (object);
leo_sobral@2
   123
}
leo_sobral@2
   124
leo_sobral@2
   125
  static void
leo_sobral@2
   126
myth_file_transfer_finalize (GObject *object)
leo_sobral@2
   127
{
leo_sobral@2
   128
  g_signal_handlers_destroy (object);
leo_sobral@2
   129
leo_sobral@2
   130
  G_OBJECT_CLASS (myth_file_transfer_parent_class)->finalize (object);
leo_sobral@2
   131
}
leo_sobral@2
   132
leo_sobral@2
   133
  MythFileTransfer*
leo_sobral@2
   134
myth_file_transfer_new (gint num, GString *uri_str, gshort port, gint mythtv_version)
leo_sobral@2
   135
{
leo_sobral@2
   136
  MythFileTransfer *transfer = MYTH_FILE_TRANSFER ( g_object_new (
leo_sobral@2
   137
	MYTH_FILE_TRANSFER_TYPE, FALSE ));
leo_sobral@2
   138
leo_sobral@2
   139
  if ( mythtv_version > 0 )
leo_sobral@2
   140
    transfer->mythtv_version = mythtv_version;
leo_sobral@2
   141
leo_sobral@2
   142
  transfer->card_id = num;
leo_sobral@2
   143
leo_sobral@2
   144
  transfer->rec_id = -1;
leo_sobral@2
   145
leo_sobral@2
   146
  transfer->recordernum = 0;
leo_sobral@2
   147
  transfer->uri = myth_uri_new ( uri_str->str );
leo_sobral@2
   148
leo_sobral@2
   149
  transfer->hostname = g_string_new( myth_uri_gethost(transfer->uri) );
leo_sobral@2
   150
  g_print( "\t--> transfer->hostname = %s\n", transfer->hostname->str );
leo_sobral@2
   151
leo_sobral@2
   152
  if ( port >= 0 )
leo_sobral@2
   153
    transfer->port = port;
leo_sobral@2
   154
  else
leo_sobral@2
   155
    transfer->port = myth_uri_getport( transfer->uri );
leo_sobral@2
   156
leo_sobral@2
   157
  g_print( "\t--> transfer->port = %d\n", transfer->port );
leo_sobral@2
   158
leo_sobral@2
   159
  transfer->readposition = 0;
leo_sobral@2
   160
  transfer->filesize = MYTHTV_FILE_SIZE;
leo_sobral@2
   161
  transfer->timeoutisfast = FALSE;
leo_sobral@2
   162
leo_sobral@2
   163
  transfer->userreadahead = MYTHTV_USER_READ_AHEAD;
leo_sobral@2
   164
  transfer->retries = MYTHTV_RETRIES;  
leo_sobral@2
   165
leo_sobral@2
   166
  transfer->live_tv = FALSE;
leo_sobral@2
   167
leo_sobral@2
   168
  transfer->query = g_string_new( MYTHTV_QUERY_HEADER );
leo_sobral@2
   169
  g_string_append_printf ( transfer->query, " %d", transfer->recordernum );
leo_sobral@2
   170
  g_print( "\t--> transfer->query = %s\n", transfer->query->str );
leo_sobral@2
   171
leo_sobral@2
   172
  transfer->control_sock = NULL;
leo_sobral@2
   173
  transfer->event_sock = NULL;
leo_sobral@2
   174
  transfer->sock = NULL;
leo_sobral@2
   175
leo_sobral@2
   176
  return transfer;
leo_sobral@2
   177
}
leo_sobral@2
   178
leo_sobral@2
   179
gboolean
leo_sobral@2
   180
myth_file_transfer_livetv_setup( MythFileTransfer **transfer, GMythSocket *live_socket )
leo_sobral@2
   181
{
leo_sobral@2
   182
	(*transfer)->sock = live_socket;
leo_sobral@2
   183
	g_object_ref( live_socket );
leo_sobral@2
   184
leo_sobral@2
   185
	return TRUE;
leo_sobral@2
   186
}
leo_sobral@2
   187
leo_sobral@2
   188
gboolean
leo_sobral@2
   189
myth_file_transfer_playback_setup( MythFileTransfer **transfer, gboolean live_tv )
leo_sobral@2
   190
{
leo_sobral@2
   191
leo_sobral@2
   192
  gboolean ret = TRUE;
leo_sobral@2
   193
leo_sobral@2
   194
  (*transfer)->live_tv = live_tv;
leo_sobral@2
   195
leo_sobral@2
   196
  printf("[%s] Running config to the %s...\n", __FUNCTION__, live_tv ? "LiveTV" : "FileTransfer" );
leo_sobral@2
   197
leo_sobral@2
   198
  /* configure the control socket */
leo_sobral@2
   199
  if ((*transfer)->control_sock == NULL) { 
leo_sobral@2
   200
leo_sobral@2
   201
    if ( myth_connect_to_transfer_backend ( transfer, MYTH_PLAYBACK_TYPE ) == NULL ) {
leo_sobral@2
   202
      g_printerr( "Connection to backend failed (Control Socket).\n" );
leo_sobral@2
   203
      ret = FALSE;
leo_sobral@2
   204
    }
leo_sobral@2
   205
leo_sobral@2
   206
  } else {
leo_sobral@2
   207
    g_warning("Remote transfer control socket already created.\n");
leo_sobral@2
   208
  }
leo_sobral@2
   209
leo_sobral@2
   210
  return ret;
leo_sobral@2
   211
leo_sobral@2
   212
}
leo_sobral@2
   213
leo_sobral@2
   214
gboolean
leo_sobral@2
   215
myth_file_transfer_setup( MythFileTransfer **transfer, gboolean live_tv )
leo_sobral@2
   216
{
leo_sobral@2
   217
  GMythStringList *strlist = NULL;
leo_sobral@2
   218
leo_sobral@2
   219
  gboolean ret = TRUE;
leo_sobral@2
   220
leo_sobral@2
   221
  (*transfer)->live_tv = live_tv;
leo_sobral@2
   222
leo_sobral@2
   223
  printf("[%s] Running config to the %s...\n", __FUNCTION__, live_tv ? "LiveTV" : "FileTransfer" );
leo_sobral@2
   224
leo_sobral@2
   225
#if 0
leo_sobral@2
   226
  /* configure the event socket */
leo_sobral@2
   227
  if ((*transfer)->event_sock == NULL) { 
leo_sobral@2
   228
leo_sobral@2
   229
    if ( myth_connect_to_transfer_backend ( transfer, MYTH_MONITOR_TYPE ) == NULL ) {
leo_sobral@2
   230
      g_printerr( "Connection to backend failed (Event Socket).\n" );
leo_sobral@2
   231
      ret = FALSE;
leo_sobral@2
   232
    }
leo_sobral@2
   233
leo_sobral@2
   234
  } else {
leo_sobral@2
   235
    g_warning("Remote transfer control socket already created.\n");
leo_sobral@2
   236
  }
leo_sobral@2
   237
#endif
leo_sobral@2
   238
leo_sobral@2
   239
  /* configure the socket */
leo_sobral@2
   240
  if ( (*transfer)->sock == NULL ) { 
leo_sobral@2
   241
leo_sobral@2
   242
    //if ( live_tv == FALSE ) {
leo_sobral@2
   243
leo_sobral@2
   244
    if ( myth_connect_to_transfer_backend ( transfer, MYTH_FILETRANSFER_TYPE ) == NULL ) {
leo_sobral@2
   245
      g_printerr ("Connection to backend failed (Raw Transfer Socket).\n");
leo_sobral@2
   246
      ret = FALSE;
leo_sobral@2
   247
    }
leo_sobral@2
   248
leo_sobral@2
   249
    if ( !(*transfer)->live_tv && (*transfer)->control_sock != NULL) {
leo_sobral@2
   250
      strlist = gmyth_string_list_new();
leo_sobral@2
   251
      g_string_printf ( (*transfer)->query, "%s %d", MYTHTV_QUERY_HEADER, (*transfer)->recordernum );
leo_sobral@2
   252
leo_sobral@2
   253
      gmyth_string_list_append_string( strlist, (*transfer)->query );
leo_sobral@2
   254
      gmyth_string_list_append_char_array( strlist, "IS_OPEN" );
leo_sobral@2
   255
leo_sobral@2
   256
      gmyth_socket_write_stringlist( (*transfer)->control_sock, strlist );
leo_sobral@2
   257
      gmyth_socket_read_stringlist( (*transfer)->control_sock, strlist );
leo_sobral@2
   258
leo_sobral@2
   259
      if ( strlist!=NULL && gmyth_string_list_get_int( strlist, 0 ) == 1 ) {
leo_sobral@2
   260
	g_print( "[%s] Remote Myth FileTransfer socket is open!\n", __FUNCTION__ );
leo_sobral@2
   261
      } else {
leo_sobral@2
   262
	g_print( "[%s] Remote Myth FileTransfer socket is CLOSED! See the MythTV Server Backend for configuration details...\n", __FUNCTION__ );
leo_sobral@2
   263
	ret = FALSE;
leo_sobral@2
   264
      }
leo_sobral@2
   265
    }
leo_sobral@2
   266
leo_sobral@2
   267
  } else {
leo_sobral@2
   268
    g_warning("Remote transfer (raw) socket already created.\n");
leo_sobral@2
   269
  }
leo_sobral@2
   270
leo_sobral@2
   271
  return ret;
leo_sobral@2
   272
}
leo_sobral@2
   273
leo_sobral@2
   274
static GMythSocket *
leo_sobral@2
   275
myth_connect_to_transfer_backend( MythFileTransfer **transfer, guint sock_type )
leo_sobral@2
   276
{
leo_sobral@2
   277
  GMythSocket *sock = NULL;
leo_sobral@2
   278
leo_sobral@2
   279
  g_return_val_if_fail( transfer != NULL && *transfer != NULL, NULL );
leo_sobral@2
   280
  g_return_val_if_fail( (*transfer)->uri != NULL, NULL );
leo_sobral@2
   281
leo_sobral@2
   282
  g_static_mutex_lock (&mutex);
leo_sobral@2
   283
leo_sobral@2
   284
  gchar *path_dir = myth_uri_getpath( (*transfer)->uri );
leo_sobral@2
   285
  //g_print( "\t--> %s: path_dir = %s\n", __FUNCTION__, path_dir );
leo_sobral@2
   286
leo_sobral@2
   287
  gchar *stype = g_strdup( "" );
leo_sobral@2
   288
leo_sobral@2
   289
  //  if ( (*transfer)->live_tv == FALSE ) {
leo_sobral@2
   290
leo_sobral@2
   291
  sock = gmyth_socket_new();
leo_sobral@2
   292
leo_sobral@2
   293
  gmyth_socket_connect( &sock, (*transfer)->hostname->str, (*transfer)->port );
leo_sobral@2
   294
leo_sobral@2
   295
  /*
leo_sobral@2
   296
     } else {
leo_sobral@2
   297
     sock = (*transfer)->sock;
leo_sobral@2
   298
     }
leo_sobral@2
   299
     */
leo_sobral@2
   300
#ifdef MYTHTV_ENABLE_DEBUG
leo_sobral@2
   301
leo_sobral@2
   302
  g_print( "[%s] --> Creating socket... (%s, %d)\n", __FUNCTION__, (*transfer)->hostname->str, (*transfer)->port );
leo_sobral@2
   303
#endif
leo_sobral@2
   304
leo_sobral@2
   305
  GMythStringList *strlist = NULL;
leo_sobral@2
   306
leo_sobral@2
   307
  GString *hostname = g_string_new( myth_uri_gethost( (*transfer)->uri ) );
leo_sobral@2
   308
  GString *base_str = g_string_new( "" );
leo_sobral@2
   309
leo_sobral@2
   310
  if ( gmyth_socket_check_protocol_version_number (sock, (*transfer)->mythtv_version) ) {
leo_sobral@2
   311
leo_sobral@2
   312
    if (sock == NULL) {
leo_sobral@2
   313
      stype = (sock_type==MYTH_PLAYBACK_TYPE) ? "control socket" : "file data socket";
leo_sobral@2
   314
      g_printerr( "FileTransfer, open_socket(%s): \n"
leo_sobral@2
   315
	  "\t\t\tCould not connect to server \"%s\" @ port %d\n", stype, 
leo_sobral@2
   316
	  (*transfer)->hostname->str, (*transfer)->port );
leo_sobral@2
   317
      g_object_unref(sock);
leo_sobral@2
   318
      g_static_mutex_unlock (&mutex);
leo_sobral@2
   319
      return NULL;
leo_sobral@2
   320
    }
leo_sobral@2
   321
leo_sobral@2
   322
    hostname = gmyth_socket_get_local_hostname();
leo_sobral@2
   323
leo_sobral@2
   324
    g_print( "[%s] local hostname = %s\n", __FUNCTION__, hostname->str  );
leo_sobral@2
   325
leo_sobral@2
   326
    if ( sock_type == MYTH_PLAYBACK_TYPE )
leo_sobral@2
   327
    {
leo_sobral@2
   328
      (*transfer)->control_sock = sock;
leo_sobral@2
   329
      g_string_printf( base_str, "ANN Playback %s %d", hostname->str, FALSE );
leo_sobral@2
   330
leo_sobral@2
   331
      gmyth_socket_send_command( (*transfer)->control_sock, base_str );
leo_sobral@2
   332
      GString *resp = gmyth_socket_receive_response( (*transfer)->control_sock );
leo_sobral@2
   333
      g_print( "[%s] Got Playback response from %s: %s\n", __FUNCTION__, base_str->str, resp->str );
leo_sobral@2
   334
    }
leo_sobral@2
   335
    else if ( sock_type == MYTH_MONITOR_TYPE )
leo_sobral@2
   336
    {
leo_sobral@2
   337
      (*transfer)->event_sock = sock;
leo_sobral@2
   338
      g_string_printf( base_str, "ANN Monitor %s %d", hostname->str, TRUE );
leo_sobral@2
   339
leo_sobral@2
   340
      gmyth_socket_send_command( (*transfer)->event_sock, base_str );
leo_sobral@2
   341
      GString *resp = gmyth_socket_receive_response( (*transfer)->event_sock );
leo_sobral@2
   342
      g_print( "[%s] Got Monitor response from %s: %s\n", __FUNCTION__, base_str->str, resp->str );
leo_sobral@2
   343
      g_thread_create( myth_init_io_watchers, (void*)(*transfer), FALSE, NULL );
leo_sobral@2
   344
leo_sobral@2
   345
      g_printerr( "[%s] Watch listener function to the IO control channel on thread %p.\n", __FUNCTION__, g_thread_self() );
leo_sobral@2
   346
leo_sobral@2
   347
    }
leo_sobral@2
   348
    else if ( sock_type == MYTH_FILETRANSFER_TYPE )
leo_sobral@2
   349
    {
leo_sobral@2
   350
      (*transfer)->sock = sock;
leo_sobral@2
   351
      strlist = gmyth_string_list_new();
leo_sobral@2
   352
      //g_string_printf( base_str, "ANN FileTransfer %s %d %d", hostname->str,
leo_sobral@2
   353
      //  		transfer->userreadahead, transfer->retries );
leo_sobral@2
   354
      g_string_printf( base_str, "ANN FileTransfer %s", hostname->str );
leo_sobral@2
   355
leo_sobral@2
   356
      gmyth_string_list_append_string( strlist, base_str );
leo_sobral@2
   357
      gmyth_string_list_append_char_array( strlist, path_dir );
leo_sobral@2
   358
leo_sobral@2
   359
      gmyth_socket_write_stringlist( (*transfer)->sock, strlist );
leo_sobral@2
   360
      gmyth_socket_read_stringlist( (*transfer)->sock, strlist );
leo_sobral@2
   361
leo_sobral@2
   362
      /* socket number, where all the stream data comes from - got from the MythTV remote backend */
leo_sobral@2
   363
      (*transfer)->recordernum = gmyth_string_list_get_int( strlist, 1 );
leo_sobral@2
   364
leo_sobral@2
   365
      /* Myth URI stream file size - decoded using two 8-bytes sequences (64 bits/long long types) */
leo_sobral@2
   366
      (*transfer)->filesize = mmyth_util_decode_long_long( strlist, 2 );
leo_sobral@2
   367
leo_sobral@2
   368
      printf( "[%s] ***** Received: recordernum = %d, filesize = %" G_GUINT64_FORMAT "\n", __FUNCTION__,
leo_sobral@2
   369
	  (*transfer)->recordernum, (*transfer)->filesize );
leo_sobral@2
   370
leo_sobral@2
   371
      if ( (*transfer)->filesize <= 0 ) {
leo_sobral@2
   372
	g_print( "[%s] Got filesize equals to %llu is lesser than 0 [invalid stream file]\n", __FUNCTION__, (*transfer)->filesize );
leo_sobral@2
   373
	g_object_unref(sock);
leo_sobral@2
   374
	sock = NULL; 
leo_sobral@2
   375
      }
leo_sobral@2
   376
    }
leo_sobral@2
   377
    else if ( sock_type == MYTH_RINGBUFFER_TYPE )
leo_sobral@2
   378
    {
leo_sobral@2
   379
      (*transfer)->sock = sock;
leo_sobral@2
   380
      //myth_file_transfer_spawntv( (*transfer), NULL );      
leo_sobral@2
   381
leo_sobral@2
   382
      strlist = gmyth_string_list_new();
leo_sobral@2
   383
      g_string_printf( base_str, "ANN RingBuffer %s %d", hostname->str, (*transfer)->card_id );
leo_sobral@2
   384
leo_sobral@2
   385
      gmyth_socket_send_command( (*transfer)->sock, base_str );
leo_sobral@2
   386
      GString *resp = gmyth_socket_receive_response( (*transfer)->sock );
leo_sobral@2
   387
      g_print( "[%s] Got RingBuffer response from %s: %s\n", __FUNCTION__, base_str->str, resp->str );
leo_sobral@2
   388
leo_sobral@2
   389
    }
leo_sobral@2
   390
leo_sobral@2
   391
  }
leo_sobral@2
   392
leo_sobral@2
   393
  printf("[%s] ANN %s sent: %s\n", (sock_type==MYTH_PLAYBACK_TYPE) ? "Playback" : (sock_type==MYTH_FILETRANSFER_TYPE) ? "FileTransfer" : "Monitor", __FUNCTION__, base_str->str);
leo_sobral@2
   394
leo_sobral@2
   395
  if ( strlist != NULL )
leo_sobral@2
   396
    g_object_unref( strlist );
leo_sobral@2
   397
leo_sobral@2
   398
  g_static_mutex_unlock (&mutex);
leo_sobral@2
   399
leo_sobral@2
   400
  return sock;
leo_sobral@2
   401
}    
leo_sobral@2
   402
leo_sobral@2
   403
void
leo_sobral@2
   404
myth_file_transfer_spawntv ( MythFileTransfer *file_transfer, 
leo_sobral@2
   405
    GString *tvchain_id )
leo_sobral@2
   406
{
leo_sobral@2
   407
  GMythStringList *str_list;
leo_sobral@2
   408
leo_sobral@2
   409
  g_debug ("myth_file_transfer_spawntv.\n");
leo_sobral@2
   410
leo_sobral@2
   411
  str_list = gmyth_string_list_new ();
leo_sobral@2
   412
leo_sobral@2
   413
  g_string_printf( file_transfer->query, "%s %d", MYTHTV_RECORDER_HEADER, 
leo_sobral@2
   414
      file_transfer->card_id );
leo_sobral@2
   415
  gmyth_string_list_append_string (str_list, file_transfer->query);
leo_sobral@2
   416
  gmyth_string_list_append_string (str_list, g_string_new ("SPAWN_LIVETV"));
leo_sobral@2
   417
  if (tvchain_id!=NULL) {
leo_sobral@2
   418
    gmyth_string_list_append_string (str_list, tvchain_id);
leo_sobral@2
   419
    gmyth_string_list_append_int (str_list, FALSE); // PIP = FALSE (0)
leo_sobral@2
   420
  }
leo_sobral@2
   421
leo_sobral@2
   422
  gmyth_socket_sendreceive_stringlist ( file_transfer->sock, str_list );
leo_sobral@2
   423
leo_sobral@2
   424
  //GString *str = NULL;
leo_sobral@2
   425
leo_sobral@2
   426
  //if (str_list!=NULL && (str = gmyth_string_list_get_string( str_list, 0 )) != NULL && strcasecmp( str->str, "ok" ) != 0 ) {
leo_sobral@2
   427
  //  g_print( "[%s]\t\tSpawnLiveTV is OK!\n", __FUNCTION__ );
leo_sobral@2
   428
  //}
leo_sobral@2
   429
  if (str_list!=NULL)
leo_sobral@2
   430
    g_object_unref (str_list);
leo_sobral@2
   431
leo_sobral@2
   432
}
leo_sobral@2
   433
leo_sobral@2
   434
gboolean
leo_sobral@2
   435
myth_file_transfer_is_recording ( MythFileTransfer *file_transfer )
leo_sobral@2
   436
{
leo_sobral@2
   437
  gboolean ret = TRUE;
leo_sobral@2
   438
  
leo_sobral@2
   439
  GMythStringList *str_list = gmyth_string_list_new ();
leo_sobral@2
   440
leo_sobral@2
   441
  g_debug ( "[%s]\n", __FUNCTION__ );
leo_sobral@2
   442
  g_static_mutex_lock (&mutex);
leo_sobral@2
   443
leo_sobral@2
   444
  g_string_printf( file_transfer->query, "%s %d", MYTHTV_RECORDER_HEADER, 
leo_sobral@2
   445
      file_transfer->rec_id >= 0 ? file_transfer->rec_id : file_transfer->card_id );
leo_sobral@2
   446
  gmyth_string_list_append_string (str_list, file_transfer->query);
leo_sobral@2
   447
  gmyth_string_list_append_string (str_list, g_string_new ("IS_RECORDING"));
leo_sobral@2
   448
leo_sobral@2
   449
  gmyth_socket_sendreceive_stringlist ( file_transfer->control_sock, str_list );
leo_sobral@2
   450
leo_sobral@2
   451
  if ( str_list != NULL && gmyth_string_list_length(str_list) > 0 ) 
leo_sobral@2
   452
  {
leo_sobral@2
   453
    GString *str = NULL;
leo_sobral@2
   454
    if ( ( str = gmyth_string_list_get_string( str_list, 0 ) ) != NULL && strcmp( str->str, "bad" )!= 0 ) {
leo_sobral@2
   455
      gint is_rec = gmyth_string_list_get_int( str_list, 0 );
leo_sobral@2
   456
      if ( is_rec != 0 )
leo_sobral@2
   457
	ret = TRUE;
leo_sobral@2
   458
      else
leo_sobral@2
   459
	ret = FALSE;
leo_sobral@2
   460
    }
leo_sobral@2
   461
  }  
leo_sobral@2
   462
  g_print( "[%s] %s, stream is %s being recorded!\n", __FUNCTION__, ret ? "YES" : "NO", ret ? "" : "NOT" );
leo_sobral@2
   463
  g_static_mutex_unlock (&mutex);
leo_sobral@2
   464
leo_sobral@2
   465
  if ( str_list != NULL )
leo_sobral@2
   466
    g_object_unref (str_list);
leo_sobral@2
   467
leo_sobral@2
   468
  return ret;
leo_sobral@2
   469
leo_sobral@2
   470
}
leo_sobral@2
   471
leo_sobral@2
   472
guint64
leo_sobral@2
   473
myth_file_transfer_get_file_position ( MythFileTransfer *file_transfer )
leo_sobral@2
   474
{
leo_sobral@2
   475
  guint64 pos = 0;
leo_sobral@2
   476
leo_sobral@2
   477
  GMythStringList *str_list = gmyth_string_list_new ();
leo_sobral@2
   478
leo_sobral@2
   479
  g_debug ( "[%s]\n", __FUNCTION__ );
leo_sobral@2
   480
  g_static_mutex_lock (&mutex);
leo_sobral@2
   481
leo_sobral@2
   482
  g_string_printf( file_transfer->query, "%s %d", MYTHTV_RECORDER_HEADER, 
leo_sobral@2
   483
      file_transfer->rec_id >= 0 ? file_transfer->rec_id : file_transfer->card_id );
leo_sobral@2
   484
leo_sobral@2
   485
  gmyth_string_list_append_string (str_list, file_transfer->query);
leo_sobral@2
   486
  gmyth_string_list_append_string (str_list, g_string_new ("GET_FILE_POSITION"));
leo_sobral@2
   487
leo_sobral@2
   488
  gmyth_socket_sendreceive_stringlist ( file_transfer->control_sock, str_list );
leo_sobral@2
   489
leo_sobral@2
   490
  if ( str_list != NULL && gmyth_string_list_length(str_list) > 0 ) 
leo_sobral@2
   491
  {
leo_sobral@2
   492
    GString *str = NULL;
leo_sobral@2
   493
    if ( ( str = gmyth_string_list_get_string( str_list, 0 ) ) != NULL && strcmp ( str->str, "bad" ) != 0 )
leo_sobral@2
   494
      pos = gmyth_util_decode_long_long( str_list, 0 );
leo_sobral@2
   495
  } 
leo_sobral@2
   496
  g_static_mutex_unlock (&mutex);
leo_sobral@2
   497
leo_sobral@2
   498
#ifndef MYTHTV_ENABLE_DEBUG
leo_sobral@2
   499
leo_sobral@2
   500
  g_print( "[%s] Got file position = %llu\n", __FUNCTION__, pos );
leo_sobral@2
   501
#endif
leo_sobral@2
   502
  if (str_list!=NULL)
leo_sobral@2
   503
    g_object_unref (str_list);
leo_sobral@2
   504
leo_sobral@2
   505
  return pos;
leo_sobral@2
   506
leo_sobral@2
   507
}
leo_sobral@2
   508
leo_sobral@2
   509
  glong
leo_sobral@2
   510
myth_file_transfer_get_recordernum( MythFileTransfer *transfer )
leo_sobral@2
   511
{
leo_sobral@2
   512
  return transfer->recordernum;
leo_sobral@2
   513
}
leo_sobral@2
   514
leo_sobral@2
   515
  glong
leo_sobral@2
   516
myth_file_transfer_get_filesize( MythFileTransfer *transfer )
leo_sobral@2
   517
{
leo_sobral@2
   518
  return transfer->filesize;
leo_sobral@2
   519
}
leo_sobral@2
   520
leo_sobral@2
   521
  gboolean
leo_sobral@2
   522
myth_file_transfer_isopen( MythFileTransfer *transfer )
leo_sobral@2
   523
{
leo_sobral@2
   524
  return (transfer->sock != NULL && transfer->control_sock != NULL);
leo_sobral@2
   525
}
leo_sobral@2
   526
leo_sobral@2
   527
  void
leo_sobral@2
   528
myth_file_transfer_close( MythFileTransfer *transfer )
leo_sobral@2
   529
{
leo_sobral@2
   530
  GMythStringList *strlist;
leo_sobral@2
   531
leo_sobral@2
   532
  if (transfer->control_sock == NULL)
leo_sobral@2
   533
    return;
leo_sobral@2
   534
leo_sobral@2
   535
  strlist = gmyth_string_list_new( );
leo_sobral@2
   536
leo_sobral@2
   537
  g_string_printf( transfer->query, "%s %d", MYTHTV_QUERY_HEADER, 
leo_sobral@2
   538
      transfer->recordernum );
leo_sobral@2
   539
  gmyth_string_list_append_string( strlist, transfer->query );
leo_sobral@2
   540
  gmyth_string_list_append_char_array( strlist, "DONE" );
leo_sobral@2
   541
leo_sobral@2
   542
leo_sobral@2
   543
  if ( gmyth_socket_sendreceive_stringlist(transfer->control_sock, strlist) <= 0 )
leo_sobral@2
   544
  {
leo_sobral@2
   545
    g_printerr( "Remote file timeout.\n" );
leo_sobral@2
   546
  }
leo_sobral@2
   547
leo_sobral@2
   548
  if (transfer->sock)
leo_sobral@2
   549
  {
leo_sobral@2
   550
    g_object_unref( transfer->sock );
leo_sobral@2
   551
    transfer->sock = NULL;
leo_sobral@2
   552
  }
leo_sobral@2
   553
leo_sobral@2
   554
  if (transfer->control_sock)
leo_sobral@2
   555
  {
leo_sobral@2
   556
    g_object_unref( transfer->control_sock );
leo_sobral@2
   557
    transfer->control_sock = NULL;
leo_sobral@2
   558
  } 
leo_sobral@2
   559
leo_sobral@2
   560
}
leo_sobral@2
   561
leo_sobral@2
   562
  void
leo_sobral@2
   563
myth_file_transfer_reset_controlsock( MythFileTransfer *transfer )
leo_sobral@2
   564
{
leo_sobral@2
   565
  if (transfer->control_sock == NULL)
leo_sobral@2
   566
  {
leo_sobral@2
   567
    g_printerr( "myth_file_transfer_reset_controlsock(): Called with no control socket" );
leo_sobral@2
   568
    return;
leo_sobral@2
   569
  }
leo_sobral@2
   570
leo_sobral@2
   571
  GString *str = gmyth_socket_receive_response( transfer->control_sock );
leo_sobral@2
   572
leo_sobral@2
   573
  g_string_free( str, TRUE );
leo_sobral@2
   574
}
leo_sobral@2
   575
leo_sobral@2
   576
void
leo_sobral@2
   577
myth_file_transfer_reset_sock( MythFileTransfer *transfer )
leo_sobral@2
   578
{
leo_sobral@2
   579
  if ( transfer->sock == NULL )
leo_sobral@2
   580
  {
leo_sobral@2
   581
    g_printerr( "myth_file_transfer_reset_sock(): Called with no raw socket" );
leo_sobral@2
   582
    return;
leo_sobral@2
   583
  }
leo_sobral@2
   584
leo_sobral@2
   585
  GString *str = gmyth_socket_receive_response( transfer->sock );
leo_sobral@2
   586
leo_sobral@2
   587
  g_string_free( str, TRUE );
leo_sobral@2
   588
}
leo_sobral@2
   589
leo_sobral@2
   590
void
leo_sobral@2
   591
myth_file_transfer_reset( MythFileTransfer *transfer )
leo_sobral@2
   592
{
leo_sobral@2
   593
  myth_file_transfer_reset_controlsock( transfer );
leo_sobral@2
   594
  myth_file_transfer_reset_sock( transfer );
leo_sobral@2
   595
}
leo_sobral@2
   596
leo_sobral@2
   597
guint64
leo_sobral@2
   598
myth_file_transfer_seek(MythFileTransfer *transfer, guint64 pos, gint whence)
leo_sobral@2
   599
{
leo_sobral@2
   600
  if (transfer->sock == NULL)
leo_sobral@2
   601
  {
leo_sobral@2
   602
    g_printerr( "[%s] myth_file_transfer_seek(): Called with no socket", __FUNCTION__ );
leo_sobral@2
   603
    return 0;
leo_sobral@2
   604
  }
leo_sobral@2
   605
leo_sobral@2
   606
  if (transfer->control_sock == NULL)
leo_sobral@2
   607
    return 0;
leo_sobral@2
   608
leo_sobral@2
   609
  // if (!controlSock->isOpen() || controlSock->error())
leo_sobral@2
   610
  //   return 0;
leo_sobral@2
   611
leo_sobral@2
   612
  GMythStringList *strlist = gmyth_string_list_new();
leo_sobral@2
   613
  g_string_printf (transfer->query, "%s %d", MYTHTV_QUERY_HEADER, transfer->recordernum);
leo_sobral@2
   614
  gmyth_string_list_append_string( strlist, transfer->query );  
leo_sobral@2
   615
  gmyth_string_list_append_char_array( strlist, "SEEK" );
leo_sobral@2
   616
  gmyth_string_list_append_uint64( strlist, pos );
leo_sobral@2
   617
  //  gmyth_string_list_append_int( strlist, whence );
leo_sobral@2
   618
leo_sobral@2
   619
  if (pos > 0 )
leo_sobral@2
   620
    gmyth_string_list_append_uint64( strlist, pos );
leo_sobral@2
   621
  else
leo_sobral@2
   622
    gmyth_string_list_append_uint64( strlist, transfer->readposition );
leo_sobral@2
   623
leo_sobral@2
   624
  gmyth_socket_sendreceive_stringlist( transfer->control_sock, strlist );
leo_sobral@2
   625
leo_sobral@2
   626
  guint64 retval = gmyth_string_list_get_uint64(strlist, 0);
leo_sobral@2
   627
  transfer->readposition = retval;
leo_sobral@2
   628
  g_print( "[%s] got reading position pointer from the streaming = %llu\n", 
leo_sobral@2
   629
      __FUNCTION__, retval );
leo_sobral@2
   630
leo_sobral@2
   631
  //myth_file_transfer_reset( transfer );
leo_sobral@2
   632
leo_sobral@2
   633
  return retval;
leo_sobral@2
   634
}
leo_sobral@2
   635
leo_sobral@2
   636
static gboolean
leo_sobral@2
   637
myth_control_sock_listener( GIOChannel *source, GIOCondition condition, gpointer data )
leo_sobral@2
   638
{
leo_sobral@2
   639
leo_sobral@2
   640
  GIOStatus ret;
leo_sobral@2
   641
  GError *err = NULL;
leo_sobral@2
   642
  gchar *msg = g_strdup("");
leo_sobral@2
   643
leo_sobral@2
   644
  gsize len;
leo_sobral@2
   645
  if (condition & G_IO_HUP)
leo_sobral@2
   646
    g_error ("Read end of pipe died!\n");
leo_sobral@2
   647
  ret = g_io_channel_read_line ( source, &msg, &len, NULL, &err);
leo_sobral@2
   648
  if ( ret == G_IO_STATUS_ERROR )
leo_sobral@2
   649
    g_error ("[%s] Error reading: %s\n", __FUNCTION__, err != NULL ? err->message : "" );
leo_sobral@2
   650
  g_print ("\n\n\n\n\n\n[%s]\t\tEVENT: Read %u bytes: %s\n\n\n\n\n", __FUNCTION__, len, msg != NULL ? msg : "" );
leo_sobral@2
   651
  if ( msg != NULL )
leo_sobral@2
   652
    g_free (msg);
leo_sobral@2
   653
leo_sobral@2
   654
  return TRUE;
leo_sobral@2
   655
leo_sobral@2
   656
}
leo_sobral@2
   657
leo_sobral@2
   658
static void*
leo_sobral@2
   659
myth_init_io_watchers( void *data )
leo_sobral@2
   660
{
leo_sobral@2
   661
  MythFileTransfer *transfer = (MythFileTransfer*)data;
leo_sobral@2
   662
  GMainContext *context = g_main_context_new();
leo_sobral@2
   663
  GMainLoop *loop = g_main_loop_new( NULL, FALSE );
leo_sobral@2
   664
leo_sobral@2
   665
  GSource *source = NULL;
leo_sobral@2
   666
leo_sobral@2
   667
  if ( transfer->event_sock->sd_io_ch != NULL )
leo_sobral@2
   668
    source = g_io_create_watch( transfer->event_sock->sd_io_ch, G_IO_IN | G_IO_HUP );
leo_sobral@2
   669
leo_sobral@2
   670
  g_source_set_callback ( source, (GSourceFunc)myth_control_sock_listener, NULL, NULL );
leo_sobral@2
   671
leo_sobral@2
   672
  g_source_attach( source, context );
leo_sobral@2
   673
leo_sobral@2
   674
  if (source==NULL)
leo_sobral@2
   675
    g_printerr( "[%s] Error adding watch listener function to the IO control channel!\n", __FUNCTION__ );
leo_sobral@2
   676
leo_sobral@2
   677
  g_main_loop_run( loop );
leo_sobral@2
   678
leo_sobral@2
   679
  g_source_unref( source );
leo_sobral@2
   680
leo_sobral@2
   681
  g_main_loop_unref( loop );
leo_sobral@2
   682
leo_sobral@2
   683
  g_main_context_unref( context );
leo_sobral@2
   684
leo_sobral@2
   685
  return NULL;
leo_sobral@2
   686
}
leo_sobral@2
   687
leo_sobral@2
   688
  gint 
leo_sobral@2
   689
myth_file_transfer_read(MythFileTransfer *transfer, void *data, gint size, gboolean read_unlimited)
leo_sobral@2
   690
{
leo_sobral@2
   691
  gint recv = 0;
leo_sobral@2
   692
  gsize bytes_read = 0;
leo_sobral@2
   693
leo_sobral@2
   694
  gint sent = 0;
leo_sobral@2
   695
  //guint zerocnt = 0;
leo_sobral@2
   696
  gboolean response = FALSE;
leo_sobral@2
   697
leo_sobral@2
   698
  GIOChannel *io_channel;
leo_sobral@2
   699
  GIOChannel *io_channel_control;
leo_sobral@2
   700
leo_sobral@2
   701
  GIOCondition io_cond;
leo_sobral@2
   702
  GIOCondition io_cond_control;
leo_sobral@2
   703
  GIOStatus io_status = G_IO_STATUS_NORMAL, io_status_control = G_IO_STATUS_NORMAL;   
leo_sobral@2
   704
leo_sobral@2
   705
  gint buf_len = MYTHTV_BUFFER_SIZE;
leo_sobral@2
   706
leo_sobral@2
   707
  GMythStringList *strlist = NULL;
leo_sobral@2
   708
  GError *error = NULL;
leo_sobral@2
   709
leo_sobral@2
   710
  gchar *trash = g_strdup("");
leo_sobral@2
   711
leo_sobral@2
   712
  g_return_val_if_fail ( data != NULL, -2 );
leo_sobral@2
   713
leo_sobral@2
   714
  /* gets the size of the entire file, if the size requested is lesser than 0 */
leo_sobral@2
   715
  if ( size <= 0 )
leo_sobral@2
   716
    size = transfer->filesize;
leo_sobral@2
   717
leo_sobral@2
   718
  io_channel = transfer->sock->sd_io_ch;
leo_sobral@2
   719
  io_channel_control = transfer->control_sock->sd_io_ch;
leo_sobral@2
   720
leo_sobral@2
   721
  //g_io_channel_set_flags( io_channel, G_IO_FLAG_APPEND | 
leo_sobral@2
   722
  //    G_IO_STATUS_AGAIN | G_IO_FLAG_IS_READABLE | G_IO_FLAG_IS_WRITEABLE | 
leo_sobral@2
   723
  //    G_IO_FLAG_IS_SEEKABLE, NULL );
leo_sobral@2
   724
leo_sobral@2
   725
  io_status = g_io_channel_set_encoding( io_channel, NULL, &error );
leo_sobral@2
   726
  if ( io_status == G_IO_STATUS_NORMAL )
leo_sobral@2
   727
    g_print( "[%s] Setting encoding to binary data socket).\n", __FUNCTION__ );
leo_sobral@2
   728
leo_sobral@2
   729
  io_cond = g_io_channel_get_buffer_condition( io_channel );  
leo_sobral@2
   730
leo_sobral@2
   731
  io_cond_control = g_io_channel_get_buffer_condition( io_channel );
leo_sobral@2
   732
leo_sobral@2
   733
  if ( transfer->sock == NULL || ( io_status == G_IO_STATUS_ERROR ) )
leo_sobral@2
   734
  {
leo_sobral@2
   735
    g_printerr( "myth_file_transfer_read(): Called with no raw socket.\n" );
leo_sobral@2
   736
    recv = -1;
leo_sobral@2
   737
    goto cleanup;
leo_sobral@2
   738
  }
leo_sobral@2
   739
leo_sobral@2
   740
  if ( transfer->control_sock == NULL || ( io_status_control == G_IO_STATUS_ERROR ) )
leo_sobral@2
   741
  {
leo_sobral@2
   742
    g_printerr( "myth_file_transfer_read(): Called with no control socket.\n" );
leo_sobral@2
   743
    recv = -1;
leo_sobral@2
   744
    goto cleanup;
leo_sobral@2
   745
  }
leo_sobral@2
   746
leo_sobral@2
   747
  /*
leo_sobral@2
   748
     if (!controlSock->isOpen() || controlSock->error())
leo_sobral@2
   749
     return -1;
leo_sobral@2
   750
     */
leo_sobral@2
   751
leo_sobral@2
   752
  if ( ( io_cond & G_IO_IN ) != 0 ) {
leo_sobral@2
   753
    do 
leo_sobral@2
   754
    {
leo_sobral@2
   755
leo_sobral@2
   756
      io_status = g_io_channel_read_line( io_channel, &trash, &bytes_read, NULL, &error);
leo_sobral@2
   757
leo_sobral@2
   758
      g_print( "[%s] cleaning buffer on IO binary channel... (%s)\n", __FUNCTION__, trash );
leo_sobral@2
   759
      io_cond = g_io_channel_get_buffer_condition( io_channel );
leo_sobral@2
   760
leo_sobral@2
   761
    } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) );
leo_sobral@2
   762
leo_sobral@2
   763
    if ( trash!= NULL )
leo_sobral@2
   764
      g_free( trash );
leo_sobral@2
   765
  }
leo_sobral@2
   766
leo_sobral@2
   767
  if ( ( io_cond_control & G_IO_IN ) != 0 ) {
leo_sobral@2
   768
    GMythStringList *strlist_tmp = gmyth_string_list_new();
leo_sobral@2
   769
    gmyth_socket_read_stringlist( transfer->control_sock, strlist_tmp );
leo_sobral@2
   770
    g_object_unref( strlist_tmp );
leo_sobral@2
   771
  }
leo_sobral@2
   772
leo_sobral@2
   773
  wait_to_transfer = 0;
leo_sobral@2
   774
leo_sobral@2
   775
  while ( transfer->live_tv && ( myth_file_transfer_get_file_position( transfer ) < 4096 ) &&
leo_sobral@2
   776
  		wait_to_transfer++ < MYTHTV_TRANSFER_MAX_WAITS )
leo_sobral@2
   777
  	g_usleep( 1000*50 ); /* waits just for 2/10 second */
leo_sobral@2
   778
leo_sobral@2
   779
  //g_thread_create( myth_init_io_watchers, (void*)transfer, FALSE, NULL );
leo_sobral@2
   780
  //g_printerr( "[%s] Watch listener function to the IO control channel on thread %p.\n", __FUNCTION__, g_thread_self() );
leo_sobral@2
   781
leo_sobral@2
   782
  g_static_mutex_lock (&mutex);
leo_sobral@2
   783
  strlist = gmyth_string_list_new();
leo_sobral@2
   784
leo_sobral@2
   785
  g_string_printf ( transfer->query, "%s %d", /*transfer->live_tv ? MYTHTV_RECORDER_HEADER :*/  MYTHTV_QUERY_HEADER, 
leo_sobral@2
   786
      /* transfer->live_tv ? transfer->card_id :*/ transfer->recordernum ); // transfer->recordernum
leo_sobral@2
   787
  g_print( "\t[%s] Transfer_query = %s\n", __FUNCTION__, transfer->query->str );
leo_sobral@2
   788
  strlist = gmyth_string_list_new();
leo_sobral@2
   789
leo_sobral@2
   790
  gmyth_string_list_append_string( strlist, transfer->query );
leo_sobral@2
   791
  gmyth_string_list_append_char_array( strlist, /*transfer->live_tv ? "REQUEST_BLOCK_RINGBUF" :*/ "REQUEST_BLOCK" );
leo_sobral@2
   792
  gmyth_string_list_append_int( strlist, size );
leo_sobral@2
   793
leo_sobral@2
   794
  gmyth_socket_write_stringlist( transfer->control_sock, strlist );
leo_sobral@2
   795
  sent = size;
leo_sobral@2
   796
leo_sobral@2
   797
  //data = (void*)g_new0( gchar, size );
leo_sobral@2
   798
leo_sobral@2
   799
  g_io_channel_flush( io_channel_control, NULL );
leo_sobral@2
   800
//  g_io_channel_flush( io_channel, NULL );
leo_sobral@2
   801
leo_sobral@2
   802
  io_cond = g_io_channel_get_buffer_condition( io_channel );
leo_sobral@2
   803
leo_sobral@2
   804
  while ( ( recv < sent ) )//&& ( io_cond & G_IO_IN ) != 0 )
leo_sobral@2
   805
  {
leo_sobral@2
   806
    do
leo_sobral@2
   807
    {
leo_sobral@2
   808
      //while ( ( io_cond & G_IO_IN ) == 0 ) {
leo_sobral@2
   809
      //usleep(200);
leo_sobral@2
   810
      //
leo_sobral@2
   811
      //io_cond = g_io_channel_get_buffer_condition( io_channel );
leo_sobral@2
   812
      //
leo_sobral@2
   813
leo_sobral@2
   814
      buf_len = ( sent - recv ) > MYTHTV_BUFFER_SIZE ? MYTHTV_BUFFER_SIZE : ( sent - recv );
leo_sobral@2
   815
leo_sobral@2
   816
      io_status = g_io_channel_read_chars( io_channel, data + recv, 
leo_sobral@2
   817
	  buf_len, &bytes_read, &error );
leo_sobral@2
   818
      /*
leo_sobral@2
   819
	 GString *sss = g_string_new("");
leo_sobral@2
   820
	 sss = g_string_append_len( sss, (gchar*)data+recv, bytes_read );
leo_sobral@2
   821
leo_sobral@2
   822
	 g_print( "[%s] Reading buffer (length = %d)\n", __FUNCTION__, bytes_read);
leo_sobral@2
   823
	 */
leo_sobral@2
   824
      if ( bytes_read > 0 )
leo_sobral@2
   825
      {
leo_sobral@2
   826
	if ( bytes_read <= buf_len )
leo_sobral@2
   827
	  recv += bytes_read;
leo_sobral@2
   828
      } 
leo_sobral@2
   829
leo_sobral@2
   830
      if ( io_status == G_IO_STATUS_EOF ) {
leo_sobral@2
   831
	g_printerr( "[%s] got EOS!", __FUNCTION__ );
leo_sobral@2
   832
	break;
leo_sobral@2
   833
      } else if ( io_status == G_IO_STATUS_ERROR ) {
leo_sobral@2
   834
	g_printerr( "[%s] myth_file_transfer_read(): socket error.\n", __FUNCTION__ );
leo_sobral@2
   835
	break;
leo_sobral@2
   836
      }
leo_sobral@2
   837
leo_sobral@2
   838
      /* increase buffer size, to allow get more data (do not obey to the buffer size) */
leo_sobral@2
   839
      if ( read_unlimited == TRUE ) {
leo_sobral@2
   840
	//if ( recv > buf_len )
leo_sobral@2
   841
	// sent += (bytes_read - buf_len) + 1;
leo_sobral@2
   842
      }
leo_sobral@2
   843
      if ( bytes_read == buf_len )
leo_sobral@2
   844
	break;
leo_sobral@2
   845
leo_sobral@2
   846
      /* verify if the input (read) buffer is ready to receive data */
leo_sobral@2
   847
      io_cond = g_io_channel_get_buffer_condition( io_channel );
leo_sobral@2
   848
leo_sobral@2
   849
      g_print( "[%s]\t io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
leo_sobral@2
   850
      		( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
leo_sobral@2
   851
leo_sobral@2
   852
    } while ( recv < sent && ( ( io_cond & G_IO_IN ) != 0 ) && ( io_status == G_IO_STATUS_NORMAL ) );
leo_sobral@2
   853
leo_sobral@2
   854
    io_cond_control = g_io_channel_get_buffer_condition( io_channel_control );
leo_sobral@2
   855
    if ( ( io_status == G_IO_STATUS_EOF ) || ( ( io_cond_control & G_IO_IN ) != 0 ) )
leo_sobral@2
   856
    {
leo_sobral@2
   857
      gmyth_socket_read_stringlist( transfer->control_sock, strlist );
leo_sobral@2
   858
      sent = gmyth_string_list_get_int( strlist,  0 ); // -1 on backend error      
leo_sobral@2
   859
      g_print( "[%s]\t sent = %d, io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
leo_sobral@2
   860
	  sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
leo_sobral@2
   861
      response = TRUE;
leo_sobral@2
   862
    }
leo_sobral@2
   863
  }
leo_sobral@2
   864
leo_sobral@2
   865
  if ( ( ( error == NULL ) && ( response == FALSE ) ) || 
leo_sobral@2
   866
  		( io_status == G_IO_STATUS_EOF ) || ( ( io_cond & G_IO_IN ) == 0 ) )
leo_sobral@2
   867
  {
leo_sobral@2
   868
    if ( gmyth_socket_read_stringlist( transfer->control_sock, strlist ) > 0 )
leo_sobral@2
   869
    {
leo_sobral@2
   870
      if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 ) {
leo_sobral@2
   871
	sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
leo_sobral@2
   872
	g_print( "[%s]\t sent = %d, io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, 
leo_sobral@2
   873
	    sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
leo_sobral@2
   874
      }
leo_sobral@2
   875
    }
leo_sobral@2
   876
    else
leo_sobral@2
   877
    {
leo_sobral@2
   878
      g_printerr ( "myth_file_transfer_read(): No response from control socket.");
leo_sobral@2
   879
      sent = -1;
leo_sobral@2
   880
    }
leo_sobral@2
   881
leo_sobral@2
   882
    if ( error!=NULL ) {
leo_sobral@2
   883
      g_printerr( "[%s] Error occurred: (%d, %s)\n", __FUNCTION__, error->code, error->message );
leo_sobral@2
   884
      g_error_free( error );
leo_sobral@2
   885
    }
leo_sobral@2
   886
  }
leo_sobral@2
   887
leo_sobral@2
   888
cleanup:
leo_sobral@2
   889
leo_sobral@2
   890
  if ( trash != NULL )
leo_sobral@2
   891
    g_free( trash );
leo_sobral@2
   892
leo_sobral@2
   893
  if ( strlist != NULL )
leo_sobral@2
   894
    g_object_unref( strlist );
leo_sobral@2
   895
leo_sobral@2
   896
  g_static_mutex_unlock (&mutex);
leo_sobral@2
   897
  g_print( "myth_file_transfer_read(): reqd=%d, rcvd=%d, rept=%d, "\
leo_sobral@2
   898
      "(rcvd and rept MUST be the same!)\n", size, 
leo_sobral@2
   899
      recv, sent );
leo_sobral@2
   900
leo_sobral@2
   901
  //if ( sent != recv ) {
leo_sobral@2
   902
  //  recv = -1;
leo_sobral@2
   903
  //}
leo_sobral@2
   904
leo_sobral@2
   905
  if ( error != NULL )
leo_sobral@2
   906
    g_printerr( "ERROR: %s [msg = %s, code = %d]\n", __FUNCTION__, error->message, 
leo_sobral@2
   907
	error->code );
leo_sobral@2
   908
leo_sobral@2
   909
  return recv;
leo_sobral@2
   910
}
leo_sobral@2
   911
leo_sobral@2
   912
  void 
leo_sobral@2
   913
myth_file_transfer_settimeout( MythFileTransfer *transfer, gboolean fast )
leo_sobral@2
   914
{
leo_sobral@2
   915
leo_sobral@2
   916
  GMythStringList *strlist = NULL;
leo_sobral@2
   917
leo_sobral@2
   918
  if ( transfer->timeoutisfast == fast )
leo_sobral@2
   919
    return;
leo_sobral@2
   920
leo_sobral@2
   921
  if ( transfer->sock == NULL )
leo_sobral@2
   922
  {
leo_sobral@2
   923
    g_printerr( "myth_file_transfer_settimeout(): Called with no socket" );
leo_sobral@2
   924
    return;
leo_sobral@2
   925
  }
leo_sobral@2
   926
leo_sobral@2
   927
  if ( transfer->control_sock == NULL )
leo_sobral@2
   928
    return;
leo_sobral@2
   929
leo_sobral@2
   930
  strlist = gmyth_string_list_new(); 
leo_sobral@2
   931
  gmyth_string_list_append_string( strlist, transfer->query );
leo_sobral@2
   932
  gmyth_string_list_append_char_array( strlist, "SET_TIMEOUT" );
leo_sobral@2
   933
  gmyth_string_list_append_int( strlist, fast ); 
leo_sobral@2
   934
leo_sobral@2
   935
  gmyth_socket_write_stringlist( transfer->control_sock, strlist );
leo_sobral@2
   936
  gmyth_socket_read_stringlist( transfer->control_sock, strlist );
leo_sobral@2
   937
leo_sobral@2
   938
  transfer->timeoutisfast = fast;
leo_sobral@2
   939
leo_sobral@2
   940
}
leo_sobral@2
   941
leo_sobral@2
   942
#ifdef DO_TESTING
leo_sobral@2
   943
leo_sobral@2
   944
  int
leo_sobral@2
   945
main( int argc, char *argv[] )
leo_sobral@2
   946
{
leo_sobral@2
   947
  g_type_init();
leo_sobral@2
   948
leo_sobral@2
   949
  MythFileTransfer *file_transfer = myth_file_transfer_new( 1, 
leo_sobral@2
   950
      g_string_new("myth://192.168.1.109:6543/jshks.nuv"), -1, MYTHTV_VERSION );
leo_sobral@2
   951
  myth_file_transfer_setup( &file_transfer );
leo_sobral@2
   952
  gchar *data = g_strdup("");
leo_sobral@2
   953
leo_sobral@2
   954
  gint num = myth_file_transfer_read( file_transfer, data, -1 );
leo_sobral@2
   955
leo_sobral@2
   956
  return 0;
leo_sobral@2
   957
leo_sobral@2
   958
}
leo_sobral@2
   959
leo_sobral@2
   960
#endif