[gssdp/wip/client-cache: 29/31] Add user-agent cache to GSSDP client



commit a1d2e647183711029f848690779df55f4ec011b1
Author: Jens Georg <mail jensge org>
Date:   Fri Dec 30 18:38:28 2011 +0100

    Add user-agent cache to GSSDP client
    
    https://bugzilla.gnome.org/show_bug.cgi?id=653894

 libgssdp/gssdp-client.c           |   76 +++++++++++++++++++++++++++++++++++++
 libgssdp/gssdp-client.h           |    9 ++++
 libgssdp/gssdp-socket-functions.c |    1 -
 libgssdp/gssdp-socket-functions.h |    1 +
 4 files changed, 86 insertions(+), 1 deletions(-)
---
diff --git a/libgssdp/gssdp-client.c b/libgssdp/gssdp-client.c
index c4d745b..43d108c 100644
--- a/libgssdp/gssdp-client.c
+++ b/libgssdp/gssdp-client.c
@@ -72,6 +72,7 @@ typedef unsigned long in_addr_t;
 #include "gssdp-socket-source.h"
 #include "gssdp-marshal.h"
 #include "gssdp-protocol.h"
+#include "gssdp-socket-functions.h"
 
 #ifndef INET6_ADDRSTRLEN
 #define INET6_ADDRSTRLEN 46
@@ -107,6 +108,7 @@ typedef struct _GSSDPNetworkDevice GSSDPNetworkDevice;
 struct _GSSDPClientPrivate {
         char              *server_id;
 
+        GHashTable        *user_agent_cache;
         guint              socket_ttl;
         guint              msearch_port;
         GSSDPNetworkDevice device;
@@ -162,6 +164,10 @@ gssdp_client_initable_init    (GInitable     *initable,
                                GCancellable  *cancellable,
                                GError       **error);
 
+char *
+arp_lookup                    (GSSDPClient   *client,
+                               const char    *ip_address);
+
 static void
 gssdp_client_init (GSSDPClient *client)
 {
@@ -295,6 +301,11 @@ gssdp_client_initable_init (GInitable                   *initable,
 
         client->priv->initialized = TRUE;
 
+        client->priv->user_agent_cache = g_hash_table_new_full (g_str_hash,
+                                                                g_str_equal,
+                                                                g_free,
+                                                                g_free);
+
         return TRUE;
 }
 
@@ -431,6 +442,9 @@ gssdp_client_finalize (GObject *object)
         g_free (client->priv->device.host_ip);
         g_free (client->priv->device.network);
 
+        if (client->priv->user_agent_cache)
+                g_hash_table_unref (client->priv->user_agent_cache);
+
         G_OBJECT_CLASS (gssdp_client_parent_class)->finalize (object);
 }
 
@@ -791,6 +805,63 @@ gssdp_client_set_network (GSSDPClient *client,
 }
 
 /**
+ * gssdp_client_add_cache_entry:
+ * @client: A #GSSDPClient
+ * @ip_address: The host to add to the cache
+ * @user_agent: User agent ot the host to add
+ **/
+void
+gssdp_client_add_cache_entry (GSSDPClient  *client,
+                               const char   *ip_address,
+                               const char   *user_agent)
+{
+        char *hwaddr;
+
+        g_return_if_fail (client != NULL);
+        g_return_if_fail (ip_address != NULL);
+        g_return_if_fail (user_agent != NULL);
+
+        hwaddr = arp_lookup (client, ip_address);
+
+        if (hwaddr)
+                g_hash_table_insert (client->priv->user_agent_cache,
+                                     hwaddr,
+                                     g_strdup (user_agent));
+}
+
+/**
+ * gssdp_client_guess_user_agent:
+ * @client: A #GSSDPClient
+ * @ip_address: IP address to guess the user-agent for
+ *
+ * Returns: (transfer none): The user-agent cached for this IP, %NULL if none
+ * is cached.
+ **/
+const char *
+gssdp_client_guess_user_agent (GSSDPClient *client,
+                               const char  *ip_address)
+{
+        char *hwaddr;
+
+        g_return_val_if_fail (GSSDP_IS_CLIENT (client), NULL);
+        g_return_val_if_fail (ip_address != NULL, NULL);
+
+        hwaddr = arp_lookup (client, ip_address);
+
+        if (hwaddr) {
+                const char *agent;
+
+                agent =  g_hash_table_lookup (client->priv->user_agent_cache,
+                                              hwaddr);
+                g_free (hwaddr);
+
+                return agent;
+        }
+
+        return NULL;
+}
+
+/**
  * gssdp_client_get_network:
  * @client: A #GSSDPClient
  *
@@ -1610,3 +1681,8 @@ init_network_info (GSSDPClient *client, GError **error)
         return ret;
 }
 
+char *
+arp_lookup (GSSDPClient *client, const char *ip_address)
+{
+        return g_strdup (ip_address);
+}
diff --git a/libgssdp/gssdp-client.h b/libgssdp/gssdp-client.h
index 2b564f1..9590f55 100644
--- a/libgssdp/gssdp-client.h
+++ b/libgssdp/gssdp-client.h
@@ -103,6 +103,15 @@ gssdp_client_get_network      (GSSDPClient  *client);
 gboolean
 gssdp_client_get_active       (GSSDPClient  *client);
 
+void
+gssdp_client_add_cache_entry  (GSSDPClient  *client,
+                               const char   *ip_address,
+                               const char   *user_agent);
+
+const char *
+gssdp_client_guess_user_agent (GSSDPClient *client,
+                               const char  *ip_address);
+
 G_END_DECLS
 
 #endif /* __GSSDP_CLIENT_H__ */
diff --git a/libgssdp/gssdp-socket-functions.c b/libgssdp/gssdp-socket-functions.c
index 5919e2b..3fd8d25 100644
--- a/libgssdp/gssdp-socket-functions.c
+++ b/libgssdp/gssdp-socket-functions.c
@@ -269,4 +269,3 @@ gssdp_socket_mcast_group_join (GSocket       *socket,
         return result;
 }
 
-
diff --git a/libgssdp/gssdp-socket-functions.h b/libgssdp/gssdp-socket-functions.h
index 47b1095..72deb56 100644
--- a/libgssdp/gssdp-socket-functions.h
+++ b/libgssdp/gssdp-socket-functions.h
@@ -49,4 +49,5 @@ G_GNUC_INTERNAL gboolean
 gssdp_socket_reuse_address       (GSocket *socket,
                                   gboolean enable,
                                   GError **error);
+
 #endif


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