[grilo-plugins] lua-factory: set state of all operations



commit 89add4837aa4578abadaf06151e6d0ef2582e122
Author: Victor Toso <me victortoso com>
Date:   Tue Mar 8 12:32:59 2016 +0100

    lua-factory: set state of all operations
    
    This commit uses the functions introduced in previous commit in order to
    track the state of each operation and save its state
    
    https://bugzilla.gnome.org/show_bug.cgi?id=763046

 src/lua-factory/grl-lua-library-operations.c |   26 ++++++++++++++++++++++++--
 src/lua-factory/grl-lua-library.c            |   21 +++++++++++++++++++--
 2 files changed, 43 insertions(+), 4 deletions(-)
---
diff --git a/src/lua-factory/grl-lua-library-operations.c b/src/lua-factory/grl-lua-library-operations.c
index 651da0d..679c025 100644
--- a/src/lua-factory/grl-lua-library-operations.c
+++ b/src/lua-factory/grl-lua-library-operations.c
@@ -503,6 +503,27 @@ grl_lua_operations_set_proxy_table (lua_State *L,
   lua_replace (L, index - 1);
 }
 
+OperationSpec *
+grl_lua_operations_get_current_op (lua_State *L)
+{
+  OperationSpec *os;
+  LuaSourceState state;
+
+  os = priv_state_current_op_get_op_data (L);
+  g_return_val_if_fail (os != NULL, NULL);
+
+  state = priv_state_operations_source_get_state (L, os->operation_id);
+  if (state == LUA_SOURCE_FINALIZED) {
+    /* Source State is finalized. At this state it should be waiting the
+     * watchdog to free its data. Only a broken source would request
+     * OperationSpec on FINALIZED State */
+    GRL_WARNING ("operation-id: %u is on FINALIZED state and cannot be changed",
+                 os->operation_id);
+    return NULL;
+  }
+  return os;
+}
+
 /*
  * This is a wrapper to do execute the lua_pcall and all internals that might
  * be necessary to Lua-Library before calling the Lua function. The stack
@@ -515,14 +536,15 @@ grl_lua_operations_pcall (lua_State *L,
                           OperationSpec *os,
                           GError **err)
 {
+  g_assert_nonnull (os);
+
   GRL_DEBUG ("%s | %s (op-id: %u)", __func__,
              grl_source_get_id (os->source),
              os->operation_id);
 
   if (lua_pcall (L, nargs, 0, 0)) {
-    gint error_code = (os) ? os->error_code : G_IO_ERROR_CANCELLED;
     *err = g_error_new_literal (GRL_CORE_ERROR,
-                                error_code,
+                                os->error_code,
                                 lua_tolstring (L, -1, NULL));
     lua_pop (L, 1);
   }
diff --git a/src/lua-factory/grl-lua-library.c b/src/lua-factory/grl-lua-library.c
index fedbb9e..4f5dec7 100644
--- a/src/lua-factory/grl-lua-library.c
+++ b/src/lua-factory/grl-lua-library.c
@@ -53,6 +53,7 @@ typedef struct {
   guint num_urls;
   gboolean is_table;
   gchar **results;
+  OperationSpec *os;
 } FetchOperation;
 
 typedef struct {
@@ -61,6 +62,7 @@ typedef struct {
   gint lua_callback;
   gchar *url;
   gchar **filenames;
+  OperationSpec *os;
 } UnzipOperation;
 
 /* ================== Lua-Library utils/helpers ============================ */
@@ -470,6 +472,7 @@ grl_util_fetch_done (GObject *source_object,
   GError *err = NULL;
   FetchOperation *fo = (FetchOperation *) user_data;
   lua_State *L = fo->L;
+  OperationSpec *os = fo->os;
   gchar *fixed = NULL;
 
   if (!grl_net_wc_request_finish (GRL_NET_WC (source_object),
@@ -524,7 +527,7 @@ grl_util_fetch_done (GObject *source_object,
   /* get userdata from the registry */
   lua_rawgeti (L, LUA_REGISTRYINDEX, fo->lua_userdata);
 
-  if (!grl_lua_operations_pcall (L, 2, NULL, &err)) {
+  if (!grl_lua_operations_pcall (L, 2, os, &err)) {
     if (err != NULL) {
       GRL_WARNING ("calling source callback function fail: %s", err->message);
       g_error_free (err);
@@ -630,6 +633,7 @@ grl_util_unzip_done (GObject *source_object,
   GError *err = NULL;
   UnzipOperation *uo = (UnzipOperation *) user_data;
   lua_State *L = uo->L;
+  OperationSpec *os = uo->os;
   char **results;
 
   grl_net_wc_request_finish (GRL_NET_WC (source_object),
@@ -661,7 +665,7 @@ grl_util_unzip_done (GObject *source_object,
   /* get userdata from the registry */
   lua_rawgeti (L, LUA_REGISTRYINDEX, uo->lua_userdata);
 
-  if (!grl_lua_operations_pcall (L, 2, NULL, &err)) {
+  if (!grl_lua_operations_pcall (L, 2, os, &err)) {
     if (err != NULL) {
       GRL_WARNING ("calling source callback function fail: %s", err->message);
       g_error_free (err);
@@ -900,6 +904,7 @@ grl_l_fetch (lua_State *L)
   gint lua_callback;
   GrlNetWc *wc;
   gboolean is_table = FALSE;
+  OperationSpec *os;
 
   luaL_argcheck (L, (lua_isstring (L, 1) || lua_istable (L, 1)), 1,
                  "expecting url as string or an array of urls");
@@ -911,6 +916,8 @@ grl_l_fetch (lua_State *L)
                      (lua_istable (L, 2) && lua_isfunction (L, 3))), 3,
                  "expecting callback function after network parameters");
 
+  os = grl_lua_operations_get_current_op (L);
+
   /* keep arguments aligned */
   if (lua_isfunction (L, 2)) {
     lua_pushnil (L);
@@ -971,6 +978,7 @@ grl_l_fetch (lua_State *L)
 
     fo = g_new0 (FetchOperation, 1);
     fo->L = L;
+    fo->os = os;
     fo->lua_userdata = lua_userdata;
     fo->lua_callback = lua_callback;
     fo->index = i;
@@ -983,6 +991,9 @@ grl_l_fetch (lua_State *L)
   }
   g_object_unref (wc);
   g_free (urls);
+
+  /* Set the state as wating for this async operation */
+  grl_lua_operations_set_source_state (L, LUA_SOURCE_WAITING, os);
   return 0;
 }
 
@@ -1037,6 +1048,7 @@ grl_l_callback (lua_State *L)
   if (count == 0)
     os->callback_done = TRUE;
 
+  grl_lua_operations_set_source_state (L, LUA_SOURCE_FINALIZED, os);
   return 0;
 }
 
@@ -1192,6 +1204,7 @@ grl_l_unzip (lua_State *L)
   UnzipOperation *uo;
   guint num_filenames, i;
   gchar **filenames;
+  OperationSpec *os;
 
   luaL_argcheck (L, lua_isstring (L, 1), 1,
                  "expecting url as string");
@@ -1238,6 +1251,7 @@ grl_l_unzip (lua_State *L)
   GRL_DEBUG ("grl.unzip() -> '%s'", url);
 
   wc = net_wc_new_with_options (L, 3);
+  os = grl_lua_operations_get_current_op (L);
 
   uo = g_new0 (UnzipOperation, 1);
   uo->L = L;
@@ -1245,9 +1259,12 @@ grl_l_unzip (lua_State *L)
   uo->lua_callback = lua_callback;
   uo->url = g_strdup (url);
   uo->filenames = filenames;
+  uo->os = os;
 
   grl_net_wc_request_async (wc, url, NULL, grl_util_unzip_done, uo);
   g_object_unref (wc);
+
+  grl_lua_operations_set_source_state (L, LUA_SOURCE_WAITING, os);
   return 0;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]