- fixes, intendation
[oweals/gnunet.git] / src / gns / gnunet-service-gns.c
index ca2e80fd0ff1f4050aab2f05f7ec53682e761c18..092da35774af09e7abca1657e552efbfede50beb 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2011-2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2011-2013 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -218,7 +218,7 @@ static struct GNUNET_TIME_Relative zone_publish_time_window;
 /**
  * zone publish task
  */
-static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
+static struct GNUNET_SCHEDULER_Task * zone_publish_task;
 
 /**
  * #GNUNET_YES if zone has never been published before
@@ -235,15 +235,6 @@ static int v6_enabled;
  */
 static int v4_enabled;
 
-/**
- * Did we finish the initial iteration over the namestore?
- * (while we do the initial iteration, we do not generate
- * DHT PUTs as there might be WAY too many of those).
- * TODO: expand namestore monitor API with a way to
- * suppress this initial iteration.
- */
-static int sync_finished;
-
 /**
  * Handle to the statistics service
  */
@@ -290,10 +281,10 @@ shutdown_task (void *cls,
     GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
     statistics = NULL;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
+  if (NULL != zone_publish_task)
   {
     GNUNET_SCHEDULER_cancel (zone_publish_task);
-    zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
+    zone_publish_task = NULL;
   }
   if (NULL != namestore_iter)
   {
@@ -338,7 +329,7 @@ static void
 publish_zone_dht_next (void *cls,
                        const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
+  zone_publish_task = NULL;
   GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
 }
 
@@ -425,8 +416,7 @@ convert_records_for_export (const struct GNUNET_GNSRECORD_Data *rd,
   rd_public_count = 0;
   now = GNUNET_TIME_absolute_get ();
   for (i=0;i<rd_count;i++)
-    if (0 == (rd[i].flags & (GNUNET_GNSRECORD_RF_PRIVATE |
-                            GNUNET_GNSRECORD_RF_PENDING)))
+    if (0 == (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE))
     {
       rd_public[rd_public_count] = rd[i];
       if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
@@ -478,6 +468,8 @@ perform_dht_put (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
                                         label,
                                         rd_public,
                                         rd_public_count);
+  if (NULL == block)
+    return NULL; /* whoops */
   block_size = ntohl (block->purpose.size)
     + sizeof (struct GNUNET_CRYPTO_EcdsaSignature)
     + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
@@ -485,8 +477,11 @@ perform_dht_put (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
                                           label,
                                           &query);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Storing record in DHT with expiration `%s'\n",
-              GNUNET_STRINGS_absolute_time_to_string (expire));
+              "Storing %u record(s) for label `%s' in DHT with expiration `%s' under key %s\n",
+              rd_public_count,
+              label,
+              GNUNET_STRINGS_absolute_time_to_string (expire),
+              GNUNET_h2s (&query));
   ret = GNUNET_DHT_put (dht_handle, &query,
                         DHT_GNS_REPLICATION_LEVEL,
                         GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
@@ -507,21 +502,21 @@ perform_dht_put (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
  *
  * @param cls the closure (NULL)
  * @param key the private key of the authority (ours)
- * @param name the name of the records, NULL once the iteration is done
+ * @param label the name of the records, NULL once the iteration is done
  * @param rd_count the number of records in @a rd
  * @param rd the record data
  */
 static void
 put_gns_record (void *cls,
                 const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
-                const char *name,
+                const char *label,
                 unsigned int rd_count,
                 const struct GNUNET_GNSRECORD_Data *rd)
 {
   struct GNUNET_GNSRECORD_Data rd_public[rd_count];
   unsigned int rd_public_count;
 
-  if (NULL == name)
+  if (NULL == label)
   {
     /* we're done with one iteration, calculate when to do the next one */
     namestore_iter = NULL;
@@ -540,12 +535,14 @@ put_gns_record (void *cls,
     }
     else
     {
-      zone_publish_time_window
-        = GNUNET_TIME_relative_min (GNUNET_TIME_relative_divide (min_relative_record_time,
-                                                                 4),
-                                    zone_publish_time_window_default);
+      /* If records are present, next publication is based on the minimum
+       * relative expiration time of the records published divided by 4
+       */
+      zone_publish_time_window = GNUNET_TIME_relative_min (
+          GNUNET_TIME_relative_divide (min_relative_record_time, 4),
+          zone_publish_time_window_default);
       put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
-                                                 num_public_records);
+          num_public_records);
     }
     /* reset for next iteration */
     min_relative_record_time = GNUNET_TIME_UNIT_FOREVER_REL;
@@ -590,7 +587,7 @@ put_gns_record (void *cls,
   }
 
   active_put = perform_dht_put (key,
-                                name,
+                                label,
                                 rd_public,
                                 rd_public_count,
                                 NULL);
@@ -603,7 +600,7 @@ put_gns_record (void *cls,
 
 
 /**
- * Periodically iterate over our zone and store everything in dht
+ * Periodically iterate over all zones and store everything in DHT
  *
  * @param cls NULL
  * @param tc task context
@@ -612,16 +609,15 @@ static void
 publish_zone_dht_start (void *cls,
                        const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
+  zone_publish_task = NULL;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Starting DHT zone update!\n");
   /* start counting again */
   num_public_records = 0;
   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
-                                                         NULL, /* All zones */
-                                                         &put_gns_record,
-                                                         NULL);
+      NULL, /* All zones */
+      &put_gns_record, NULL );
 }
 
 
@@ -646,9 +642,10 @@ handle_monitor_event (void *cls,
   unsigned int rd_public_count;
   struct MonitorActivity *ma;
 
-  if (GNUNET_YES != sync_finished)
-    return; /* do not do DHT PUTs on initial sync, as that may
-               create far too many PUTs on startup */
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Received %u records for label `%s' via namestore monitor\n",
+              rd_count,
+              label);
   /* filter out records that are not public, and convert to
      absolute expiration time. */
   rd_public_count = convert_records_for_export (rd, rd_count,
@@ -765,7 +762,7 @@ handle_lookup (void *cls,
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-  GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
+  GNUNET_STRINGS_utf8_tolower (utf_in, nameptr);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 
   clh = GNUNET_new (struct ClientLookupHandle);
@@ -793,7 +790,7 @@ handle_lookup (void *cls,
                                     ntohl (sh_msg->type),
                                     name,
                                     key,
-                                    ntohl (sh_msg->only_cached),
+                                    (enum GNUNET_GNS_LocalOptions) ntohs (sh_msg->options),
                                     &send_lookup_response, clh);
   GNUNET_STATISTICS_update (statistics,
                             "Lookup attempts",
@@ -833,7 +830,6 @@ notify_client_disconnect (void *cls,
 static void
 monitor_sync_event (void *cls)
 {
-  sync_finished = GNUNET_YES;
   zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
                                                NULL);
 }
@@ -955,9 +951,11 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   nc = GNUNET_SERVER_notification_context_create (server, 1);
   zmon = GNUNET_NAMESTORE_zone_monitor_start (c,
                                               NULL,
+                                              GNUNET_NO,
                                               &handle_monitor_event,
                                               &monitor_sync_event,
                                               NULL);
+  GNUNET_break (NULL != zmon);
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                &shutdown_task, NULL);
 }