agent: Replace closed signal with GASyncResult type function
authorOlivier Crête <olivier.crete@collabora.com>
Wed, 8 May 2019 20:40:10 +0000 (16:40 -0400)
committerOlivier Crête <olivier.crete@ocrete.ca>
Thu, 9 May 2019 01:14:37 +0000 (01:14 +0000)
This makes the API more GIO style

agent/agent.c
agent/agent.h
agent/discovery.c
agent/discovery.h
tests/test-fullmode.c
tests/test-turn.c

index dc47ba0..e653d02 100644 (file)
@@ -136,7 +136,6 @@ enum
   SIGNAL_NEW_SELECTED_PAIR_FULL,
   SIGNAL_NEW_CANDIDATE_FULL,
   SIGNAL_NEW_REMOTE_CANDIDATE_FULL,
-  SIGNAL_CLOSED,
 
   N_SIGNALS,
 };
@@ -1141,28 +1140,6 @@ nice_agent_class_init (NiceAgentClass *klass)
           NICE_TYPE_CANDIDATE,
           G_TYPE_INVALID);
 
-  /**
-   * NiceAgent::closed
-   * @agent: The #NiceAgent object
-   *
-   * This signal is fired when the agent finishes freeing resources it
-   * previously allocated on remote servers (e.g. relay ports) and is ready
-   * to be disposed.
-   *
-   * Since: 0.1.16
-   */
-  signals[SIGNAL_CLOSED] =
-      g_signal_new (
-          "closed",
-          G_OBJECT_CLASS_TYPE (klass),
-          G_SIGNAL_RUN_LAST,
-          0,
-          NULL,
-          NULL,
-          NULL,
-          G_TYPE_NONE,
-          0);
-
   /* Init debug options depending on env variables */
   nice_debug_init ();
 }
@@ -6688,19 +6665,32 @@ nice_agent_peer_candidate_gathering_done (NiceAgent *agent, guint stream_id)
 static gboolean
 on_agent_refreshes_pruned (NiceAgent *agent, gpointer user_data)
 {
-  // This is called from a timeout cb with agent lock held
+  GTask *task = user_data;
+
+  /* This is called from a timeout cb with agent lock held */
 
-  agent_queue_signal (agent, signals[SIGNAL_CLOSED]);
+  agent_unlock (agent);
+
+  g_task_return_boolean (task, TRUE);
+  g_object_unref (task);
+
+  agent_lock (agent);
 
   return G_SOURCE_REMOVE;
 }
 
 void
-nice_agent_close_async (NiceAgent *agent)
+nice_agent_close_async (NiceAgent *agent, GAsyncReadyCallback callback,
+    gpointer callback_data)
 {
+  GTask *task;
+
+  task = g_task_new (agent, NULL, callback, callback_data);
+  g_task_set_source_tag (task, nice_agent_close_async);
+
   agent_lock (agent);
 
-  refresh_prune_agent_async (agent, on_agent_refreshes_pruned);
+  refresh_prune_agent_async (agent, on_agent_refreshes_pruned, task);
 
   agent_unlock (agent);
 }
index c85ee5e..c3b77a3 100644 (file)
@@ -1662,9 +1662,15 @@ nice_agent_peer_candidate_gathering_done (
 /**
  * nice_agent_close_async:
  * @agent: The #NiceAgent object
+ * @callback: (nullable): A callback that will be called when the closing is
+ *  complete
+ * @callback_data: (nullable): A pointer that will be passed to the callback
  *
  * Asynchronously closes resources the agent has allocated on remote servers.
- * The agent will emit "closed" signal when the operation finishes.
+ *
+ * The agent will call the callback in the current #GMainContext in
+ * which this function is called. The #GAsyncResult in the callback
+ * can be ignored as this operation never fails.
  *
  * Calling this function before freeing the agent makes sure the allocated relay
  * ports aren't left behind on TURN server but properly removed.
@@ -1672,7 +1678,8 @@ nice_agent_peer_candidate_gathering_done (
  * Since: 0.1.16
  */
 void
-nice_agent_close_async (NiceAgent *agent);
+nice_agent_close_async (NiceAgent *agent, GAsyncReadyCallback callback,
+    gpointer callback_data);
 
 G_END_DECLS
 
index 189baf4..7155dc3 100644 (file)
@@ -326,9 +326,9 @@ static void refresh_prune_async (NiceAgent *agent, GSList *refreshes,
 }
 
 void refresh_prune_agent_async (NiceAgent *agent,
-    NiceTimeoutLockedCallback function)
+    NiceTimeoutLockedCallback function, gpointer user_data)
 {
-  refresh_prune_async (agent, agent->refresh_list, function, NULL);
+  refresh_prune_async (agent, agent->refresh_list, function, user_data);
 }
 
 /*
index 3e41e52..976118b 100644 (file)
@@ -86,7 +86,7 @@ typedef struct
 
 void refresh_free (NiceAgent *agent, CandidateRefresh *refresh);
 void refresh_prune_agent_async (NiceAgent *agent,
-  NiceTimeoutLockedCallback function);
+  NiceTimeoutLockedCallback function, gpointer user_data);
 void refresh_prune_stream_async (NiceAgent *agent, NiceStream *stream,
   NiceTimeoutLockedCallback function);
 void refresh_prune_candidate (NiceAgent *agent, NiceCandidate *candidate);
index d5aedb5..7c38330 100644 (file)
@@ -130,8 +130,6 @@ static gboolean global_lagent_gathering_done = FALSE;
 static gboolean global_ragent_gathering_done = FALSE;
 static gboolean global_lagent_ibr_received = FALSE;
 static gboolean global_ragent_ibr_received = FALSE;
-static gboolean global_lagent_closed = FALSE;
-static gboolean global_ragent_closed = FALSE;
 static int global_lagent_cands = 0;
 static int global_ragent_cands = 0;
 static gint global_ragent_read = 0;
@@ -298,8 +296,10 @@ static void cb_initial_binding_request_received(NiceAgent *agent, guint stream_i
   (void)agent; (void)stream_id; (void)data;
 }
 
-static void cb_closed (NiceAgent *agent, gpointer data)
+static void cb_closed (GObject *src, GAsyncResult *result, gpointer data)
 {
+  NiceAgent *agent = NICE_AGENT (src);
+
   g_debug ("test-fullmode:%s: %p", G_STRFUNC, agent);
 
   *((gboolean *)data) = TRUE;
@@ -862,6 +862,8 @@ int main (void)
   int result;
   guint timer_id;
   const char *stun_server = NULL, *stun_server_port = NULL;
+  gboolean lagent_closed = FALSE;
+  gboolean ragent_closed = FALSE;
 
 #ifdef G_OS_WIN32
   WSADATA w;
@@ -933,10 +935,6 @@ int main (void)
   g_signal_connect (G_OBJECT (ragent), "initial-binding-request-received",
       G_CALLBACK (cb_initial_binding_request_received),
       GUINT_TO_POINTER (2));
-  g_signal_connect (G_OBJECT (lagent), "closed",
-      G_CALLBACK (cb_closed), &global_lagent_closed);
-  g_signal_connect (G_OBJECT (ragent), "closed",
-      G_CALLBACK (cb_closed), &global_ragent_closed);
 
   stun_server = getenv ("NICE_STUN_SERVER");
   stun_server_port = getenv ("NICE_STUN_SERVER_PORT");
@@ -1112,15 +1110,15 @@ int main (void)
   g_assert (global_ragent_state[0] == NICE_COMPONENT_STATE_READY);
   g_assert (global_ragent_state[1] == NICE_COMPONENT_STATE_READY);
 
-  nice_agent_close_async (lagent);
-  nice_agent_close_async (ragent);
+  nice_agent_close_async (lagent, cb_closed, &lagent_closed);
+  nice_agent_close_async (ragent, cb_closed, &ragent_closed);
+  g_object_unref (lagent);
+  g_object_unref (ragent);
 
-  while (!global_lagent_closed || !global_ragent_closed) {
+  while (!ragent_closed || !ragent_closed) {
     g_main_context_iteration (NULL, TRUE);
   }
 
-  g_object_unref (lagent);
-  g_object_unref (ragent);
 
   g_main_loop_unref (global_mainloop);
   global_mainloop = NULL;
index 9a1e63f..8122d8e 100644 (file)
@@ -10,8 +10,6 @@ static NiceComponentState global_ragent_state[2] = { NICE_COMPONENT_STATE_LAST,
 static guint global_components_ready = 0;
 static gboolean global_lagent_gathering_done = FALSE;
 static gboolean global_ragent_gathering_done = FALSE;
-static gboolean global_lagent_closed = FALSE;
-static gboolean global_ragent_closed = FALSE;
 static int global_lagent_cands = 0;
 static int global_ragent_cands = 0;
 
@@ -103,8 +101,9 @@ static void cb_new_selected_pair(NiceAgent *agent, guint stream_id,
     ++global_ragent_cands;
 }
 
-static void cb_closed (NiceAgent *agent, gpointer data)
+static void cb_closed (GObject *src, GAsyncResult *res, gpointer data)
 {
+  NiceAgent *agent = NICE_AGENT (src);
   g_debug ("test-turn:%s: %p", G_STRFUNC, agent);
 
   *((gboolean *)data) = TRUE;
@@ -163,6 +162,8 @@ run_test(guint turn_port, gboolean is_ipv6,
   NiceAddress localaddr;
   guint ls_id, rs_id;
   gulong timer_id;
+  gboolean lagent_closed = FALSE;
+  gboolean ragent_closed = FALSE;
 
   if (is_ipv6)
     localhost = "::1";
@@ -173,8 +174,6 @@ run_test(guint turn_port, gboolean is_ipv6,
   global_components_ready = 0;
   global_lagent_gathering_done = FALSE;
   global_ragent_gathering_done = FALSE;
-  global_lagent_closed = FALSE;
-  global_ragent_closed = FALSE;
   global_lagent_cands = global_ragent_cands = 0;
 
   lagent = nice_agent_new (NULL, NICE_COMPATIBILITY_RFC5245);
@@ -210,10 +209,6 @@ run_test(guint turn_port, gboolean is_ipv6,
       G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(1));
   g_signal_connect (G_OBJECT (ragent), "new-selected-pair",
       G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER (2));
-  g_signal_connect (G_OBJECT (lagent), "closed",
-      G_CALLBACK (cb_closed), &global_lagent_closed);
-  g_signal_connect (G_OBJECT (ragent), "closed",
-      G_CALLBACK (cb_closed), &global_ragent_closed);
 
   g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
   g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
@@ -262,17 +257,18 @@ run_test(guint turn_port, gboolean is_ipv6,
   nice_agent_remove_stream (lagent, ls_id);
   nice_agent_remove_stream (ragent, rs_id);
 
-  nice_agent_close_async (lagent);
-  nice_agent_close_async (ragent);
+  nice_agent_close_async (lagent, cb_closed, &lagent_closed);
+  nice_agent_close_async (ragent, cb_closed, &ragent_closed);
 
-  while (!global_lagent_closed || !global_ragent_closed) {
+  g_clear_object(&lagent);
+  g_clear_object(&ragent);
+
+  while (!lagent_closed || !ragent_closed) {
     g_main_context_iteration (NULL, TRUE);
   }
 
   g_source_remove (timer_id);
 
-  g_clear_object(&lagent);
-  g_clear_object(&ragent);
 }
 
 guint global_turn_port;