convert fs publish to MQ
[oweals/gnunet.git] / src / core / gnunet-service-core_typemap.c
index 8d9fcbec76555c6e2bf4a7f94ab8e7e3100839b1..0b3f93a262f49c145a59d2994c26df92fd6c6add 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2011-2014 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -51,6 +51,63 @@ static struct GSC_TypeMap my_type_map;
  */
 static uint8_t map_counters[UINT16_MAX + 1];
 
+/**
+ * Current hash of our (uncompressed) type map.
+ * Lazily computed when needed.
+ */
+static struct GNUNET_HashCode my_tm_hash;
+
+/**
+ * Is #my_tm_hash() current with respect to our type map?
+ */
+static int hash_current;
+
+
+/**
+ * Our type map changed, recompute its hash.
+ */
+static void
+rehash_typemap ()
+{
+  hash_current = GNUNET_NO;
+}
+
+
+/**
+ * Hash the contents of a type map.
+ *
+ * @param tm map to hash
+ * @param hc where to store the hash code
+ */
+void
+GSC_TYPEMAP_hash (const struct GSC_TypeMap *tm,
+                  struct GNUNET_HashCode *hc)
+{
+  GNUNET_CRYPTO_hash (tm,
+                      sizeof (struct GSC_TypeMap),
+                      hc);
+}
+
+
+/**
+ * Check if the given hash matches our current type map.
+ *
+ * @param hc hash code to check if it matches our type map
+ * @return #GNUNET_YES if the hash matches, #GNUNET_NO if not
+ */
+int
+GSC_TYPEMAP_check_hash (const struct GNUNET_HashCode *hc)
+{
+  if (GNUNET_NO == hash_current)
+  {
+    GSC_TYPEMAP_hash (&my_type_map,
+                      &my_tm_hash);
+    hash_current = GNUNET_YES;
+  }
+  return (0 == memcmp (hc, &my_tm_hash, sizeof (struct GNUNET_HashCode)))
+    ? GNUNET_YES : GNUNET_NO;
+}
+
 
 /**
  * Compute a type map message for this peer.
@@ -152,7 +209,7 @@ broadcast_my_type_map ()
   GNUNET_STATISTICS_update (GSC_stats,
                             gettext_noop ("# updates to my type map"), 1,
                             GNUNET_NO);
-  GSC_SESSIONS_broadcast (hdr);
+  GSC_SESSIONS_broadcast_typemap (hdr);
   GNUNET_free (hdr);
 }
 
@@ -180,15 +237,18 @@ GSC_TYPEMAP_add (const uint16_t *types,
     }
   }
   if (GNUNET_YES == changed)
+  {
+    rehash_typemap ();
     broadcast_my_type_map ();
+  }
 }
 
 
 /**
  * Remove a set of types from our type map.
  *
- * @param types array of message types no longer supported by this peer
- * @param tlen number of entries in @a types
+ * @param types array of types to remove
+ * @param tlen length of the @a types array
  */
 void
 GSC_TYPEMAP_remove (const uint16_t *types,
@@ -207,7 +267,10 @@ GSC_TYPEMAP_remove (const uint16_t *types,
     }
   }
   if (GNUNET_YES == changed)
+  {
+    rehash_typemap ();
     broadcast_my_type_map ();
+  }
 }