gmyth/src/gmyth_jobqueue.c
author morphbr
Thu Mar 01 17:57:42 2007 +0000 (2007-03-01)
branchtrunk
changeset 380 e0dfe4bd83a2
parent 379 9f82d680cfe1
child 385 ab93678082df
permissions -rw-r--r--
[svn r385] - Added gthread support inside "compile" script
- Updated jobqueue.* and transcode.*
- Updated gmyth_test_transcode.c
morphbr@379
     1
/**
morphbr@379
     2
 * GMyth Library
morphbr@379
     3
 *
morphbr@379
     4
 * @file gmyth/gmyth_jobqueue.c
morphbr@379
     5
 * 
morphbr@379
     6
 * @brief <p> Library to use JobQueue from mythbackend
morphbr@379
     7
 *
morphbr@379
     8
 * Copyright (C) 2007 INdT - Instituto Nokia de Tecnologia.
morphbr@379
     9
 * @author Artur Duque de Souza <artur.souza@indt.org.br>
morphbr@379
    10
 *
morphbr@379
    11
 * 
morphbr@379
    12
 * This program is free software; you can redistribute it and/or modify
morphbr@379
    13
 * it under the terms of the GNU Lesser General Public License as published by
morphbr@379
    14
 * the Free Software Foundation; either version 2 of the License, or
morphbr@379
    15
 * (at your option) any later version.
morphbr@379
    16
 *
morphbr@379
    17
 * This program is distributed in the hope that it will be useful,
morphbr@379
    18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
morphbr@379
    19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
morphbr@379
    20
 * GNU General Public License for more details.
morphbr@379
    21
 *
morphbr@379
    22
 * You should have received a copy of the GNU Lesser General Public License
morphbr@379
    23
 * along with this program; if not, write to the Free Software
morphbr@379
    24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
morphbr@379
    25
 */
morphbr@379
    26
 
morphbr@379
    27
#ifdef HAVE_CONFIG_H
morphbr@379
    28
#include "config.h"
morphbr@379
    29
#endif
morphbr@379
    30
morphbr@379
    31
#include "gmyth_jobqueue.h"
morphbr@379
    32
#include "gmyth_http.h"
morphbr@379
    33
#include "gmyth_debug.h"
morphbr@379
    34
#include "gmyth_socket.h"
morphbr@379
    35
morphbr@379
    36
/** Function to connect
morphbr@379
    37
 *
morphbr@379
    38
 * @param backend_info the backendinfo
morphbr@379
    39
 * @return gboolean - result of connection
morphbr@379
    40
 *
morphbr@379
    41
 */
morphbr@379
    42
GMythSocket*
morphbr@379
    43
backend_connect (GMythBackendInfo *backend_info)
morphbr@379
    44
{
morphbr@379
    45
    GMythSocket *socket = gmyth_socket_new ();
morphbr@379
    46
    if (gmyth_socket_connect_to_backend (socket,
morphbr@379
    47
            gmyth_backend_info_get_hostname (backend_info),
morphbr@379
    48
            gmyth_backend_info_get_port (backend_info), TRUE) == TRUE) {
morphbr@379
    49
        g_debug ("Backend socket connection success");
morphbr@379
    50
        return socket;
morphbr@379
    51
    } else {
morphbr@379
    52
        g_debug ("Connection failed");
morphbr@379
    53
        return NULL;
morphbr@379
    54
    }
morphbr@379
    55
}
morphbr@379
    56
morphbr@379
    57
morphbr@379
    58
/** Function to send a command to the backend
morphbr@379
    59
 *
morphbr@379
    60
 * @param socket pointer to a socket
morphbr@379
    61
 * @param action the action itself
morphbr@379
    62
 * @param job the action itself
morphbr@379
    63
 * @param chanid the action itself
morphbr@379
    64
 * @param starttime the action itself
morphbr@379
    65
 * @param options the action itself
morphbr@379
    66
 * @return the value returned by the backend
morphbr@379
    67
 *
morphbr@379
    68
 */
morphbr@379
    69
GString* send_command(GMythSocket *socket, gchar* action,
morphbr@379
    70
                      gchar* job, gint chanid, gchar* starttime,
morphbr@379
    71
                      gchar* options)
morphbr@379
    72
{
morphbr@379
    73
    GString* command = g_string_new ("");
morphbr@379
    74
    GMythStringList *retlist = gmyth_string_list_new();
morphbr@379
    75
morphbr@379
    76
    g_string_printf(command, "JOBQUEUE %s %s %d %s %s", action, job,
morphbr@379
    77
                    chanid, starttime, options);
morphbr@379
    78
morphbr@379
    79
    gmyth_string_list_append_string (retlist, command);
morphbr@379
    80
    gmyth_socket_write_stringlist (socket, retlist);
morphbr@379
    81
morphbr@379
    82
    // receive answer
morphbr@379
    83
    gmyth_socket_read_stringlist (socket, retlist);
morphbr@379
    84
    return gmyth_string_list_get_string(retlist, 0);
morphbr@379
    85
}
morphbr@379
    86
morphbr@379
    87
morphbr@379
    88
/** Function to analyze the response from the backend
morphbr@379
    89
 *
morphbr@379
    90
 * @param ret the msg returned by the backend
morphbr@379
    91
 * @param value the expected value
morphbr@379
    92
 * @return 0 if success and -1 if error
morphbr@379
    93
 *
morphbr@379
    94
 */
morphbr@379
    95
gint test_result(gchar* ret, gchar* value)
morphbr@379
    96
{
morphbr@380
    97
        if (g_ascii_strcasecmp(ret, value) == 0)
morphbr@379
    98
            return 0;
morphbr@379
    99
        else
morphbr@379
   100
        {
morphbr@379
   101
            g_debug("JobQueue Error: %s", ret);
morphbr@379
   102
            return -1;
morphbr@379
   103
        }
morphbr@379
   104
morphbr@379
   105
}
morphbr@379
   106
morphbr@379
   107
/** Function to add a job inside JOBQUEUE
morphbr@379
   108
 *
morphbr@379
   109
 * @param transcode object holding all the info about the transcoding
morphbr@379
   110
 * @param job the job you want to add the action
morphbr@379
   111
 * @return the value of the key
morphbr@379
   112
 *
morphbr@379
   113
 */
morphbr@380
   114
gint gmyth_jobqueue_add_job (GMythTranscode* transcode, gchar* job)
morphbr@379
   115
{
morphbr@379
   116
    GMythSocket *socket = backend_connect(transcode->backend_info);
morphbr@379
   117
    if (socket != NULL)
morphbr@379
   118
    {
morphbr@379
   119
        GString* options = g_string_new ("");
morphbr@379
   120
morphbr@380
   121
        if (g_ascii_strcasecmp(job, "JOB_TRANSCODE") == 0)
morphbr@379
   122
        {
morphbr@379
   123
            if (transcode->cutlist)
morphbr@379
   124
                g_string_append(options, " JOB_USE_CUTLIST");
morphbr@379
   125
morphbr@379
   126
            if (transcode->output)
morphbr@379
   127
                g_string_append_printf(options, " JOB_OUTPUT %s",\
morphbr@379
   128
                                       transcode->output_filename->str);
morphbr@379
   129
morphbr@379
   130
            if (transcode->profile != NULL)
morphbr@379
   131
                g_string_append_printf(options, " %s", transcode->profile->str);
morphbr@379
   132
        }
morphbr@379
   133
morphbr@380
   134
        GString* ret = send_command(socket, "ADD", job, transcode->chanid,
morphbr@379
   135
                                    transcode->starttime->str, options->str);
morphbr@379
   136
morphbr@379
   137
        return test_result(ret->str, "JOBQUEUE_OK");
morphbr@379
   138
    }
morphbr@379
   139
    else
morphbr@379
   140
    {
morphbr@379
   141
        g_debug("JobQueue Connection Failed");
morphbr@379
   142
        return -1;
morphbr@379
   143
    }
morphbr@379
   144
}
morphbr@379
   145
morphbr@379
   146
/** Function to change a job cmd inside JOBQUEUE
morphbr@379
   147
 *
morphbr@379
   148
 * @param transcode object holding all the info about the transcoding
morphbr@379
   149
 * @param action the action (ADD)
morphbr@379
   150
 * @param job the job you want to add the action
morphbr@379
   151
 * @return the value of the key
morphbr@379
   152
 *
morphbr@379
   153
 */
morphbr@380
   154
gint gmyth_jobqueue_change_cmd (GMythTranscode* transcode, gchar* action,
morphbr@380
   155
                                gchar* job)
morphbr@379
   156
{
morphbr@379
   157
    GMythSocket *socket = backend_connect(transcode->backend_info);
morphbr@379
   158
    if (socket != NULL)
morphbr@379
   159
    {
morphbr@380
   160
        GString* ret = send_command(socket, action, job,
morphbr@379
   161
                                    transcode->chanid,
morphbr@379
   162
                                    transcode->starttime->str, "");
morphbr@379
   163
morphbr@379
   164
        return test_result(ret->str, "JOBQUEUE_CHANGED_CMD_OK");
morphbr@379
   165
    }
morphbr@379
   166
    else
morphbr@379
   167
    {
morphbr@379
   168
        g_debug("JobQueue Connection Failed");
morphbr@379
   169
        return -1;
morphbr@379
   170
    }
morphbr@379
   171
}