[telepathy-gabble/telepathy-gabble-0.8] add initial implementation of CM-wide caps cache

Dafydd Harries dafydd.harries at collabora.co.uk
Wed Jan 20 15:33:44 PST 2010


This is in memory only, for now.
---
 src/Makefile.am                  |    2 +
 src/capabilities.c               |   16 +++++
 src/capabilities.h               |    2 +
 src/caps-cache.c                 |  116 ++++++++++++++++++++++++++++++++++++++
 src/caps-cache.h                 |   67 ++++++++++++++++++++++
 src/presence-cache.c             |   40 ++++++++++++-
 tests/twisted/caps/caps-cache.py |    2 +-
 7 files changed, 240 insertions(+), 5 deletions(-)
 create mode 100644 src/caps-cache.c
 create mode 100644 src/caps-cache.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 762967c..ddaa348 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -27,6 +27,8 @@ libgabble_convenience_la_SOURCES = \
     bytestream-socks5.c \
     capabilities.h \
     capabilities.c \
+    caps-cache.h \
+    caps-cache.c \
     caps-hash.h \
     caps-hash.c \
     caps-channel-manager.h \
diff --git a/src/capabilities.c b/src/capabilities.c
index 78c5760..c467960 100644
--- a/src/capabilities.c
+++ b/src/capabilities.c
@@ -192,6 +192,22 @@ capabilities_parse (LmMessageNode *query_result)
   return ret;
 }
 
+GabblePresenceCapabilities
+capabilities_from_ns (const gchar *ns)
+{
+  const Feature *i;
+
+  for (i = self_advertised_features; i->ns != NULL; i++)
+    {
+      if (0 == strcmp (ns, i->ns))
+        {
+          return i->caps;
+        }
+    }
+
+  return 0;
+}
+
 void
 capabilities_fill_cache (GabblePresenceCache *cache)
 {
diff --git a/src/capabilities.h b/src/capabilities.h
index f3ce487..d14a246 100644
--- a/src/capabilities.h
+++ b/src/capabilities.h
@@ -76,6 +76,8 @@ GabblePresenceCapabilities capabilities_get_initial_caps (void);
 
 GabblePresenceCapabilities capabilities_parse (LmMessageNode *query_result);
 
+GabblePresenceCapabilities capabilities_from_ns (const gchar *ns);
+
 typedef GabblePresenceCapabilities (*TypeFlagsToCapsFunc) (guint typeflags);
 typedef guint (*CapsToTypeFlagsFunc) (GabblePresenceCapabilities caps);
 
diff --git a/src/caps-cache.c b/src/caps-cache.c
new file mode 100644
index 0000000..1626baa
--- /dev/null
+++ b/src/caps-cache.c
@@ -0,0 +1,116 @@
+
+#include "caps-cache.h"
+
+G_DEFINE_TYPE (GabbleCapsCache, gabble_caps_cache, G_TYPE_OBJECT)
+
+#define GET_PRIVATE(o) \
+  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GABBLE_TYPE_CAPS_CACHE, GabbleCapsCachePrivate))
+
+static GabbleCapsCache *shared_cache = NULL;
+
+struct _GabbleCapsCachePrivate
+{
+  GHashTable *cache;
+};
+
+static void
+gabble_caps_cache_get_property (GObject *object, guint property_id,
+                              GValue *value, GParamSpec *pspec)
+{
+  switch (property_id)
+    {
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+    }
+}
+
+static void
+gabble_caps_cache_set_property (GObject *object, guint property_id,
+                              const GValue *value, GParamSpec *pspec)
+{
+  switch (property_id)
+    {
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+    }
+}
+
+static void
+gabble_caps_cache_dispose (GObject *object)
+{
+  G_OBJECT_CLASS (gabble_caps_cache_parent_class)->dispose (object);
+}
+
+static void
+gabble_caps_cache_finalize (GObject *object)
+{
+  G_OBJECT_CLASS (gabble_caps_cache_parent_class)->finalize (object);
+}
+
+static void
+gabble_caps_cache_class_init (GabbleCapsCacheClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  g_type_class_add_private (klass, sizeof (GabbleCapsCachePrivate));
+
+  object_class->get_property = gabble_caps_cache_get_property;
+  object_class->set_property = gabble_caps_cache_set_property;
+  object_class->dispose = gabble_caps_cache_dispose;
+  object_class->finalize = gabble_caps_cache_finalize;
+}
+
+static void
+gabble_caps_cache_init (GabbleCapsCache *self)
+{
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (
+      self, GABBLE_TYPE_CAPS_CACHE, GabbleCapsCachePrivate);
+
+  self->priv->cache = g_hash_table_new_full (
+      g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_strfreev);
+}
+
+GabbleCapsCache *
+gabble_caps_cache_new (void)
+{
+  return g_object_new (GABBLE_TYPE_CAPS_CACHE, NULL);
+}
+
+GabbleCapsCache *
+gabble_caps_cache_dup_shared (void)
+{
+  if (shared_cache == NULL)
+    {
+      shared_cache = gabble_caps_cache_new ();
+    }
+
+  g_object_ref (shared_cache);
+  return shared_cache;
+}
+
+gchar **
+gabble_caps_cache_lookup (GabbleCapsCache *self, const gchar *node)
+{
+  return g_strdupv (g_hash_table_lookup (self->priv->cache, node));
+}
+
+void
+gabble_caps_cache_insert (
+    GabbleCapsCache *self,
+    const gchar *node,
+    gchar **caps)
+{
+  GSList *old_caps;
+
+  old_caps = g_hash_table_lookup (self->priv->cache, node);
+
+  if (old_caps != NULL)
+    {
+      /* XXX: issue warning here? */
+      return;
+    }
+
+  g_hash_table_insert (
+      self->priv->cache, g_strdup (node), g_strdupv ((gchar **) caps));
+}
+
diff --git a/src/caps-cache.h b/src/caps-cache.h
new file mode 100644
index 0000000..0634c51
--- /dev/null
+++ b/src/caps-cache.h
@@ -0,0 +1,67 @@
+
+#ifndef __GABBLE_CAPS_CACHE_H
+#define __GABBLE_CAPS_CACHE_H
+
+#include <glib-object.h>
+
+#include <capabilities.h>
+
+G_BEGIN_DECLS
+
+#define GABBLE_TYPE_CAPS_CACHE gabble_caps_cache_get_type()
+
+#define GABBLE_CAPS_CACHE(obj) \
+    (G_TYPE_CHECK_INSTANCE_CAST ((obj), GABBLE_TYPE_CAPS_CACHE, \
+        GabbleCapsCache))
+
+#define GABBLE_CAPS_CACHE_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_CAST ((klass), GABBLE_TYPE_CAPS_CACHE, \
+        GabbleCapsCacheClass))
+
+#define GABBLE_IS_CAPS_CACHE(obj) \
+    (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GABBLE_TYPE_CAPS_CACHE))
+
+#define GABBLE_IS_CAPS_CACHE_CLASS(klass) \
+    (G_TYPE_CHECK_CLASS_TYPE ((klass), GABBLE_TYPE_CAPS_CACHE))
+
+#define GABBLE_CAPS_CACHE_GET_CLASS(obj) \
+    (G_TYPE_INSTANCE_GET_CLASS ((obj), GABBLE_TYPE_CAPS_CACHE, \
+        GabbleCapsCacheClass))
+
+typedef struct _GabbleCapsCachePrivate GabbleCapsCachePrivate;
+
+typedef struct
+{
+  GObject parent;
+  GabbleCapsCachePrivate *priv;
+} GabbleCapsCache;
+
+typedef struct
+{
+  GObjectClass parent_class;
+} GabbleCapsCacheClass;
+
+GType
+gabble_caps_cache_get_type (void);
+
+GabbleCapsCache *
+gabble_caps_cache_get_singleton (void);
+
+gchar **
+gabble_caps_cache_lookup (GabbleCapsCache *self, const gchar *node);
+
+void
+gabble_caps_cache_insert (
+    GabbleCapsCache *cache,
+    const gchar *node,
+    gchar **caps);
+
+GabbleCapsCache *
+gabble_caps_cache_new (void);
+
+GabbleCapsCache *
+gabble_caps_cache_dup_shared (void);
+
+G_END_DECLS
+
+#endif /* defined __GABBLE_CAPS_CACHE_H */
diff --git a/src/presence-cache.c b/src/presence-cache.c
index df24821..283bdd2 100644
--- a/src/presence-cache.c
+++ b/src/presence-cache.c
@@ -40,6 +40,7 @@
 #define DEBUG_FLAG GABBLE_DEBUG_PRESENCE
 
 #include "capabilities.h"
+#include "caps-cache.h"
 #include "caps-channel-manager.h"
 #include "caps-hash.h"
 #include "debug.h"
@@ -1233,6 +1234,13 @@ _caps_disco_cb (GabbleDisco *disco,
 
   if (trust >= CAPABILITY_BUNDLE_ENOUGH_TRUST)
     {
+      GabbleCapsCache *caps_cache;
+
+      /* Update external cache. */
+      caps_cache = gabble_caps_cache_dup_shared ();
+      gabble_caps_cache_insert (caps_cache, node, uris);
+      g_object_unref (caps_cache);
+
       /* We trust this caps node. Serve all its waiters. */
       for (i = waiters; NULL != i; i = i->next)
         {
@@ -1306,28 +1314,52 @@ _process_caps_uri (GabblePresenceCache *cache,
                    guint serial)
 {
   CapabilityInfo *info;
+  gboolean caps_in_cache = FALSE;
+  GabblePresenceCapabilities cached_caps = 0;
   GabblePresenceCachePrivate *priv;
   TpHandleRepoIface *contact_repo;
+  GabbleCapsCache *caps_cache;
+  gchar **uris, **i;
 
   priv = GABBLE_PRESENCE_CACHE_PRIV (cache);
   contact_repo = tp_base_connection_get_handles (
       (TpBaseConnection *) priv->conn, TP_HANDLE_TYPE_CONTACT);
   info = capability_info_get (cache, uri);
 
-  if (info->trust >= CAPABILITY_BUNDLE_ENOUGH_TRUST
-      || tp_intset_is_member (info->guys, handle))
+  caps_cache = gabble_caps_cache_dup_shared ();
+  uris = gabble_caps_cache_lookup (caps_cache, uri);
+  g_object_unref (caps_cache);
+
+  if (uris != NULL)
+    {
+      caps_in_cache = TRUE;
+
+      for (i = uris; *i; i++)
+        {
+          cached_caps |= capabilities_from_ns (*i);
+        }
+
+      g_strfreev (uris);
+    }
+
+  if (caps_in_cache ||
+      info->trust >= CAPABILITY_BUNDLE_ENOUGH_TRUST ||
+      tp_intset_is_member (info->guys, handle))
     {
       /* we already have enough trust for this node; apply the cached value to
        * the (handle, resource) */
 
       GabblePresence *presence = gabble_presence_cache_get (cache, handle);
+      GabblePresenceCapabilities caps =
+          caps_in_cache ? cached_caps : info->caps;
+
       DEBUG ("enough trust for URI %s, setting caps for %u (%s) to %u",
-          uri, handle, from, info->caps);
+          uri, handle, from, caps);
 
       if (presence)
         {
           gabble_presence_set_capabilities (presence, resource,
-              info->caps, info->per_channel_manager_caps, serial);
+              caps, info->per_channel_manager_caps, serial);
           DEBUG ("caps for %d (%s) now %d", handle, from, presence->caps);
         }
       else
diff --git a/tests/twisted/caps/caps-cache.py b/tests/twisted/caps/caps-cache.py
index db542f9..ec36ca5 100644
--- a/tests/twisted/caps/caps-cache.py
+++ b/tests/twisted/caps/caps-cache.py
@@ -14,7 +14,7 @@ from caps_helper import (
     compute_caps_hash, fake_client_dataforms, presence_and_disco,
     send_presence, expect_disco, send_disco_reply)
 
-client = 'http://telepathy.freedesktop.org/fake-client'
+client = 'http://telepathy.freedesktop.org/fake-client/caps-cache'
 features = [
     ns.JINGLE_015,
     ns.JINGLE_015_AUDIO,
-- 
1.5.6.5




More information about the telepathy-commits mailing list