[telepathy-gabble/master] factor out randomize_g_slist

Guillaume Desmottes guillaume.desmottes at collabora.co.uk
Tue Apr 21 03:20:50 PDT 2009


---
 src/bytestream-factory.c |   53 +++++++++++++++++++++++++--------------------
 1 files changed, 29 insertions(+), 24 deletions(-)

diff --git a/src/bytestream-factory.c b/src/bytestream-factory.c
index f9b72e0..67c262e 100644
--- a/src/bytestream-factory.c
+++ b/src/bytestream-factory.c
@@ -1953,38 +1953,43 @@ gabble_bytestream_factory_make_multi_accept_iq (const gchar *full_jid,
   return msg;
 }
 
-GSList *
-gabble_bytestream_factory_get_socks5_proxies (GabbleBytestreamFactory *self)
+static GSList *
+randomize_g_slist (GSList *list)
 {
-  GabbleBytestreamFactoryPrivate *priv = GABBLE_BYTESTREAM_FACTORY_GET_PRIVATE (
-      self);
   guint len;
+  guint i;
+  GSList *new_head, *new_tail;
 
-  len = g_slist_length (priv->socks5_fallback_proxies);
-  if (len > 1)
-    {
-      /* randomize fallback proxies to avoid to use always the same one */
-      guint i;
+  len = g_slist_length (list);
+  if (len <= 1)
+    return list;
 
-      i = g_random_int_range (0, len);
-      if (i != 0)
-        {
-          GSList *new_head, *new_tail;
+  i = g_random_int_range (0, len);
+  if (i == 0)
+    return list;
 
-          /* Cut the list at the i th position and make it the new head of the
-           * list */
-          new_tail = g_slist_nth (priv->socks5_fallback_proxies, i - 1);
-          g_assert (new_tail != NULL);
+  /* Cut the list at the i th position and make it the new head of the
+   * list */
+  new_tail = g_slist_nth (list, i - 1);
+  g_assert (new_tail != NULL);
 
-          new_head = new_tail->next;
-          g_assert (new_head != NULL);
+  new_head = new_tail->next;
+  g_assert (new_head != NULL);
 
-          new_tail->next = NULL;
+  new_tail->next = NULL;
 
-          priv->socks5_fallback_proxies = g_slist_concat (new_head,
-              priv->socks5_fallback_proxies);
-        }
-    }
+  return g_slist_concat (new_head, list);
+}
+
+GSList *
+gabble_bytestream_factory_get_socks5_proxies (GabbleBytestreamFactory *self)
+{
+  GabbleBytestreamFactoryPrivate *priv = GABBLE_BYTESTREAM_FACTORY_GET_PRIVATE (
+      self);
+
+  /* randomize fallback proxies to avoid to use always the same one */
+  priv->socks5_fallback_proxies = randomize_g_slist (
+      priv->socks5_fallback_proxies);
 
   return g_slist_concat (g_slist_copy (priv->socks5_proxies),
       g_slist_copy (priv->socks5_fallback_proxies));
-- 
1.5.6.5




More information about the telepathy-commits mailing list