gmyth/src/gmyth_jobqueue.c
branchtrunk
changeset 379 9f82d680cfe1
child 380 e0dfe4bd83a2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gmyth/src/gmyth_jobqueue.c	Wed Feb 28 14:22:25 2007 +0000
     1.3 @@ -0,0 +1,171 @@
     1.4 +/**
     1.5 + * GMyth Library
     1.6 + *
     1.7 + * @file gmyth/gmyth_jobqueue.c
     1.8 + * 
     1.9 + * @brief <p> Library to use JobQueue from mythbackend
    1.10 + *
    1.11 + * Copyright (C) 2007 INdT - Instituto Nokia de Tecnologia.
    1.12 + * @author Artur Duque de Souza <artur.souza@indt.org.br>
    1.13 + *
    1.14 + * 
    1.15 + * This program is free software; you can redistribute it and/or modify
    1.16 + * it under the terms of the GNU Lesser General Public License as published by
    1.17 + * the Free Software Foundation; either version 2 of the License, or
    1.18 + * (at your option) any later version.
    1.19 + *
    1.20 + * This program is distributed in the hope that it will be useful,
    1.21 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.22 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.23 + * GNU General Public License for more details.
    1.24 + *
    1.25 + * You should have received a copy of the GNU Lesser General Public License
    1.26 + * along with this program; if not, write to the Free Software
    1.27 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.28 + */
    1.29 + 
    1.30 +#ifdef HAVE_CONFIG_H
    1.31 +#include "config.h"
    1.32 +#endif
    1.33 +
    1.34 +#include "gmyth_jobqueue.h"
    1.35 +#include "gmyth_http.h"
    1.36 +#include "gmyth_debug.h"
    1.37 +#include "gmyth_socket.h"
    1.38 +
    1.39 +/** Function to connect
    1.40 + *
    1.41 + * @param backend_info the backendinfo
    1.42 + * @return gboolean - result of connection
    1.43 + *
    1.44 + */
    1.45 +GMythSocket*
    1.46 +backend_connect (GMythBackendInfo *backend_info)
    1.47 +{
    1.48 +    GMythSocket *socket = gmyth_socket_new ();
    1.49 +    if (gmyth_socket_connect_to_backend (socket,
    1.50 +            gmyth_backend_info_get_hostname (backend_info),
    1.51 +            gmyth_backend_info_get_port (backend_info), TRUE) == TRUE) {
    1.52 +        g_debug ("Backend socket connection success");
    1.53 +        return socket;
    1.54 +    } else {
    1.55 +        g_debug ("Connection failed");
    1.56 +        return NULL;
    1.57 +    }
    1.58 +}
    1.59 +
    1.60 +
    1.61 +/** Function to send a command to the backend
    1.62 + *
    1.63 + * @param socket pointer to a socket
    1.64 + * @param action the action itself
    1.65 + * @param job the action itself
    1.66 + * @param chanid the action itself
    1.67 + * @param starttime the action itself
    1.68 + * @param options the action itself
    1.69 + * @return the value returned by the backend
    1.70 + *
    1.71 + */
    1.72 +GString* send_command(GMythSocket *socket, gchar* action,
    1.73 +                      gchar* job, gint chanid, gchar* starttime,
    1.74 +                      gchar* options)
    1.75 +{
    1.76 +    GString* command = g_string_new ("");
    1.77 +    GMythStringList *retlist = gmyth_string_list_new();
    1.78 +
    1.79 +    g_string_printf(command, "JOBQUEUE %s %s %d %s %s", action, job,
    1.80 +                    chanid, starttime, options);
    1.81 +
    1.82 +    gmyth_string_list_append_string (retlist, command);
    1.83 +    gmyth_socket_write_stringlist (socket, retlist);
    1.84 +
    1.85 +    // receive answer
    1.86 +    gmyth_socket_read_stringlist (socket, retlist);
    1.87 +    return gmyth_string_list_get_string(retlist, 0);
    1.88 +}
    1.89 +
    1.90 +
    1.91 +/** Function to analyze the response from the backend
    1.92 + *
    1.93 + * @param ret the msg returned by the backend
    1.94 + * @param value the expected value
    1.95 + * @return 0 if success and -1 if error
    1.96 + *
    1.97 + */
    1.98 +gint test_result(gchar* ret, gchar* value)
    1.99 +{
   1.100 +        if (g_ascii_strcasecmp(ret, value))
   1.101 +            return 0;
   1.102 +        else
   1.103 +        {
   1.104 +            g_debug("JobQueue Error: %s", ret);
   1.105 +            return -1;
   1.106 +        }
   1.107 +
   1.108 +}
   1.109 +
   1.110 +/** Function to add a job inside JOBQUEUE
   1.111 + *
   1.112 + * @param transcode object holding all the info about the transcoding
   1.113 + * @param job the job you want to add the action
   1.114 + * @return the value of the key
   1.115 + *
   1.116 + */
   1.117 +gint gmyth_jobqueue_add_job (GMythTranscode* transcode, GString* job)
   1.118 +{
   1.119 +    GMythSocket *socket = backend_connect(transcode->backend_info);
   1.120 +    if (socket != NULL)
   1.121 +    {
   1.122 +        GString* options = g_string_new ("");
   1.123 +
   1.124 +        if (g_ascii_strcasecmp(job->str, "JOB_TRANSCODE"))
   1.125 +        {
   1.126 +            if (transcode->cutlist)
   1.127 +                g_string_append(options, " JOB_USE_CUTLIST");
   1.128 +
   1.129 +            if (transcode->output)
   1.130 +                g_string_append_printf(options, " JOB_OUTPUT %s",\
   1.131 +                                       transcode->output_filename->str);
   1.132 +
   1.133 +            if (transcode->profile != NULL)
   1.134 +                g_string_append_printf(options, " %s", transcode->profile->str);
   1.135 +        }
   1.136 +
   1.137 +        GString* ret = send_command(socket, "ADD", job->str, transcode->chanid,
   1.138 +                                    transcode->starttime->str, options->str);
   1.139 +
   1.140 +        return test_result(ret->str, "JOBQUEUE_OK");
   1.141 +    }
   1.142 +    else
   1.143 +    {
   1.144 +        g_debug("JobQueue Connection Failed");
   1.145 +        return -1;
   1.146 +    }
   1.147 +}
   1.148 +
   1.149 +/** Function to change a job cmd inside JOBQUEUE
   1.150 + *
   1.151 + * @param transcode object holding all the info about the transcoding
   1.152 + * @param action the action (ADD)
   1.153 + * @param job the job you want to add the action
   1.154 + * @return the value of the key
   1.155 + *
   1.156 + */
   1.157 +gint gmyth_jobqueue_change_cmd (GMythTranscode* transcode, GString* action,
   1.158 +                                GString* job)
   1.159 +{
   1.160 +    GMythSocket *socket = backend_connect(transcode->backend_info);
   1.161 +    if (socket != NULL)
   1.162 +    {
   1.163 +        GString* ret = send_command(socket, action->str, job->str, 
   1.164 +                                    transcode->chanid,
   1.165 +                                    transcode->starttime->str, "");
   1.166 +
   1.167 +        return test_result(ret->str, "JOBQUEUE_CHANGED_CMD_OK");
   1.168 +    }
   1.169 +    else
   1.170 +    {
   1.171 +        g_debug("JobQueue Connection Failed");
   1.172 +        return -1;
   1.173 +    }
   1.174 +}