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 |
*/
|
rosfran@698
|
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 |
*/
|
rosfran@698
|
42 |
static GMythSocket *
|
renatofilho@750
|
43 |
backend_connect(GMythBackendInfo * backend_info)
|
morphbr@379
|
44 |
{
|
renatofilho@750
|
45 |
GMythSocket *socket = gmyth_socket_new();
|
rosfran@698
|
46 |
|
renatofilho@750
|
47 |
if (gmyth_socket_connect_to_backend(socket,
|
renatofilho@750
|
48 |
gmyth_backend_info_get_hostname
|
renatofilho@750
|
49 |
(backend_info),
|
renatofilho@750
|
50 |
gmyth_backend_info_get_port
|
renatofilho@750
|
51 |
(backend_info), TRUE) == TRUE)
|
renatofilho@750
|
52 |
{
|
renatofilho@750
|
53 |
gmyth_debug("Backend socket connection success");
|
renatofilho@750
|
54 |
return socket;
|
renatofilho@750
|
55 |
}
|
renatofilho@750
|
56 |
else
|
renatofilho@750
|
57 |
{
|
renatofilho@750
|
58 |
gmyth_debug("Connection failed");
|
renatofilho@750
|
59 |
return NULL;
|
renatofilho@750
|
60 |
}
|
morphbr@379
|
61 |
}
|
morphbr@379
|
62 |
|
morphbr@379
|
63 |
|
morphbr@379
|
64 |
/** Function to send a command to the backend
|
morphbr@379
|
65 |
*
|
morphbr@379
|
66 |
* @param socket pointer to a socket
|
morphbr@379
|
67 |
* @param action the action itself
|
morphbr@379
|
68 |
* @param job the action itself
|
morphbr@379
|
69 |
* @param chanid the action itself
|
morphbr@379
|
70 |
* @param starttime the action itself
|
morphbr@379
|
71 |
* @param options the action itself
|
morphbr@379
|
72 |
* @return the value returned by the backend
|
morphbr@379
|
73 |
*
|
morphbr@379
|
74 |
*/
|
rosfran@698
|
75 |
static gchar *
|
renatofilho@750
|
76 |
send_command(GMythSocket * socket, gchar * action,
|
renatofilho@750
|
77 |
gchar * job, gint chanid, gchar * starttime, gchar * options)
|
morphbr@379
|
78 |
{
|
renatofilho@750
|
79 |
GString *command = g_string_new("");
|
renatofilho@750
|
80 |
GString *ret_str;
|
renatofilho@750
|
81 |
gchar *ret;
|
melunko@412
|
82 |
|
renatofilho@750
|
83 |
GMythStringList *retlist = gmyth_string_list_new();
|
morphbr@379
|
84 |
|
renatofilho@750
|
85 |
g_string_printf(command, "JOBQUEUE %s %s %d %s %s", action, job,
|
renatofilho@750
|
86 |
chanid, starttime, options);
|
morphbr@379
|
87 |
|
renatofilho@750
|
88 |
gmyth_string_list_append_string(retlist, command);
|
renatofilho@750
|
89 |
gmyth_socket_write_stringlist(socket, retlist);
|
morphbr@379
|
90 |
|
renatofilho@750
|
91 |
// receive answer
|
renatofilho@750
|
92 |
gmyth_socket_read_stringlist(socket, retlist);
|
renatofilho@750
|
93 |
ret_str = gmyth_string_list_get_string(retlist, 0);
|
melunko@412
|
94 |
|
renatofilho@750
|
95 |
//ret = ret_str->str;
|
renatofilho@750
|
96 |
ret = g_string_free(ret_str, FALSE);
|
renatofilho@750
|
97 |
g_string_free(command, TRUE);
|
morphbr@426
|
98 |
|
renatofilho@750
|
99 |
gmyth_string_list_clear_all(retlist);
|
renatofilho@750
|
100 |
g_object_unref(retlist);
|
melunko@412
|
101 |
|
renatofilho@750
|
102 |
return ret;
|
morphbr@379
|
103 |
}
|
morphbr@379
|
104 |
|
morphbr@379
|
105 |
|
morphbr@379
|
106 |
/** Function to analyze the response from the backend
|
morphbr@379
|
107 |
*
|
morphbr@379
|
108 |
* @param ret the msg returned by the backend
|
morphbr@379
|
109 |
* @param value the expected value
|
morphbr@379
|
110 |
* @return 0 if success and -1 if error
|
morphbr@379
|
111 |
*
|
morphbr@379
|
112 |
*/
|
rosfran@698
|
113 |
static gboolean
|
renatofilho@750
|
114 |
test_result(gchar * ret, gchar * value)
|
morphbr@379
|
115 |
{
|
renatofilho@750
|
116 |
if (g_ascii_strcasecmp(ret, value) == 0)
|
renatofilho@750
|
117 |
{
|
renatofilho@750
|
118 |
return TRUE;
|
renatofilho@750
|
119 |
}
|
renatofilho@750
|
120 |
else
|
renatofilho@750
|
121 |
{
|
renatofilho@750
|
122 |
gmyth_debug("JobQueue Error: %s", ret);
|
renatofilho@750
|
123 |
return FALSE;
|
renatofilho@750
|
124 |
}
|
morphbr@379
|
125 |
}
|
morphbr@379
|
126 |
|
morphbr@379
|
127 |
/** Function to add a job inside JOBQUEUE
|
morphbr@379
|
128 |
*
|
morphbr@379
|
129 |
* @param transcode object holding all the info about the transcoding
|
morphbr@379
|
130 |
* @param job the job you want to add the action
|
melunko@389
|
131 |
* @return TRUE if the job was added, FALSE if not
|
morphbr@379
|
132 |
*
|
morphbr@379
|
133 |
*/
|
rosfran@698
|
134 |
gboolean
|
renatofilho@750
|
135 |
gmyth_jobqueue_add_job(GMythTranscoder * transcode, gchar * job)
|
morphbr@379
|
136 |
{
|
renatofilho@750
|
137 |
GMythSocket *socket = backend_connect(transcode->backend_info);
|
renatofilho@750
|
138 |
gboolean res = FALSE;
|
melunko@412
|
139 |
|
renatofilho@750
|
140 |
if (socket != NULL)
|
renatofilho@750
|
141 |
{
|
renatofilho@750
|
142 |
GString *options = g_string_new("");
|
renatofilho@750
|
143 |
gchar *ret = NULL;
|
morphbr@426
|
144 |
|
renatofilho@750
|
145 |
if (g_ascii_strcasecmp(job, "JOB_TRANSCODE") == 0)
|
renatofilho@750
|
146 |
{
|
renatofilho@750
|
147 |
if (transcode->cutlist)
|
renatofilho@750
|
148 |
g_string_append(options, " JOB_USE_CUTLIST");
|
morphbr@379
|
149 |
|
renatofilho@750
|
150 |
if (transcode->output)
|
renatofilho@750
|
151 |
g_string_append_printf(options, " JOB_OUTPUT %s",
|
renatofilho@750
|
152 |
transcode->output_filename);
|
morphbr@379
|
153 |
|
renatofilho@750
|
154 |
if (transcode->profile != NULL)
|
renatofilho@750
|
155 |
g_string_append_printf(options, " %s", transcode->profile);
|
renatofilho@750
|
156 |
}
|
renatofilho@750
|
157 |
ret = send_command(socket, "ADD", job, transcode->chanid,
|
renatofilho@750
|
158 |
transcode->starttime, options->str);
|
renatofilho@750
|
159 |
res = test_result(ret, "JOBQUEUE_OK");
|
renatofilho@750
|
160 |
gmyth_socket_close_connection(socket);
|
morphbr@379
|
161 |
|
renatofilho@750
|
162 |
g_object_unref(socket);
|
morphbr@426
|
163 |
|
renatofilho@750
|
164 |
g_string_free(options, TRUE);
|
morphbr@426
|
165 |
|
renatofilho@750
|
166 |
if (ret)
|
renatofilho@750
|
167 |
g_free(ret);
|
morphbr@426
|
168 |
|
renatofilho@750
|
169 |
}
|
renatofilho@750
|
170 |
else
|
renatofilho@750
|
171 |
{
|
renatofilho@750
|
172 |
gmyth_debug("JobQueue Connection Failed");
|
renatofilho@750
|
173 |
}
|
melunko@412
|
174 |
|
renatofilho@750
|
175 |
return res;
|
morphbr@379
|
176 |
}
|
morphbr@379
|
177 |
|
morphbr@379
|
178 |
/** Function to change a job cmd inside JOBQUEUE
|
morphbr@379
|
179 |
*
|
morphbr@379
|
180 |
* @param transcode object holding all the info about the transcoding
|
morphbr@379
|
181 |
* @param action the action (ADD)
|
morphbr@379
|
182 |
* @param job the job you want to add the action
|
morphbr@379
|
183 |
* @return the value of the key
|
morphbr@379
|
184 |
*
|
morphbr@379
|
185 |
*/
|
rosfran@698
|
186 |
gboolean
|
renatofilho@750
|
187 |
gmyth_jobqueue_change_cmd(GMythTranscoder * transcode, gchar * action,
|
renatofilho@750
|
188 |
gchar * job)
|
morphbr@379
|
189 |
{
|
renatofilho@750
|
190 |
GMythSocket *socket = backend_connect(transcode->backend_info);
|
renatofilho@750
|
191 |
gboolean res = FALSE;
|
melunko@412
|
192 |
|
renatofilho@750
|
193 |
if (socket != NULL)
|
renatofilho@750
|
194 |
{
|
renatofilho@750
|
195 |
gchar *ret = send_command(socket, action, job,
|
renatofilho@750
|
196 |
transcode->chanid,
|
renatofilho@750
|
197 |
transcode->starttime, "");
|
morphbr@379
|
198 |
|
renatofilho@750
|
199 |
res = test_result(ret, "JOBQUEUE_CHANGED_CMD_OK");
|
morphbr@386
|
200 |
|
renatofilho@750
|
201 |
gmyth_socket_close_connection(socket);
|
renatofilho@750
|
202 |
g_object_unref(socket);
|
melunko@412
|
203 |
|
renatofilho@750
|
204 |
g_free(ret);
|
melunko@412
|
205 |
|
renatofilho@750
|
206 |
}
|
renatofilho@750
|
207 |
else
|
renatofilho@750
|
208 |
{
|
renatofilho@750
|
209 |
gmyth_debug("JobQueue Connection Failed");
|
renatofilho@750
|
210 |
}
|
morphbr@426
|
211 |
|
renatofilho@750
|
212 |
return res;
|
morphbr@379
|
213 |
}
|