-indentation, code cleanup
[oweals/gnunet.git] / src / set / gnunet-service-set_union.c
index 0e58e71ceefb74f96de6868ab9c9c9693e048813..436d707d7f05009204c22a2faa978cdef9b0fc57 100644 (file)
@@ -4,7 +4,7 @@
 
       GNUnet is free software; you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published
-      by the Free Software Foundation; either version 2, or (at your
+      by the Free Software Foundation; either version 3, or (at your
       option) any later version.
 
       GNUnet is distributed in the hope that it will be useful, but
  * @brief two-peer set operations
  * @author Florian Dold
  */
-
-
+#include "platform.h"
+#include "gnunet_util_lib.h"
 #include "gnunet-service-set.h"
-#include "gnunet_container_lib.h"
-#include "gnunet_crypto_lib.h"
 #include "ibf.h"
 #include "strata_estimator.h"
 #include "set_protocol.h"
@@ -45,7 +43,7 @@
 /**
  * hash num parameter for the difference digests and strata estimators
  */
-#define SE_IBF_HASH_NUM 3
+#define SE_IBF_HASH_NUM 4
 
 /**
  * Number of buckets that can be transmitted in one message.
@@ -63,7 +61,7 @@
  * Number of buckets used in the ibf per estimated
  * difference.
  */
-#define IBF_ALPHA 1
+#define IBF_ALPHA 4
 
 
 /**
@@ -168,13 +166,13 @@ struct OperationState
    * belongs to.
    */
   struct Set *set;
-  
+
   /**
    * Evaluate operations are held in
    * a linked list.
    */
   struct OperationState *next;
-  
+
    /**
     * Evaluate operations are held in
     * a linked list.
@@ -275,7 +273,7 @@ destroy_key_to_element_iter (void *cls,
                              void *value)
 {
   struct KeyEntry *k = value;
-  
+
   while (NULL != k)
   {
     struct KeyEntry *k_tmp = k;
@@ -376,7 +374,7 @@ fail_union_operation (struct OperationState *eo)
 
 
 /**
- * Derive the IBF key from a hash code and 
+ * Derive the IBF key from a hash code and
  * a salt.
  *
  * @param src the hash code
@@ -521,6 +519,8 @@ prepare_ibf_iterator (void *cls,
   struct InvertibleBloomFilter *ibf = cls;
   struct KeyEntry *ke = value;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inserting %x into ibf\n", ke->ibf_key.key_val);
+
   ibf_insert (ibf, ke->ibf_key);
   return GNUNET_YES;
 }
@@ -534,6 +534,8 @@ prepare_ibf_iterator (void *cls,
  * @param key unised
  * @param value the element entry to insert
  *        into the key-to-element mapping
+ * @return GNUNET_YES to continue iterating,
+ *         GNUNET_NO to stop
  */
 static int
 init_key_to_element_iterator (void *cls,
@@ -550,7 +552,7 @@ init_key_to_element_iterator (void *cls,
          (e->generation_removed < eo->generation_created)))
     return GNUNET_YES;
 
-  e->remote = GNUNET_NO;
+  GNUNET_assert (GNUNET_NO == e->remote);
 
   op_register_element (eo, e);
   return GNUNET_YES;
@@ -663,7 +665,7 @@ get_order_from_difference (unsigned int diff)
   unsigned int ibf_order;
 
   ibf_order = 2;
-  while ((1<<ibf_order) < (IBF_ALPHA * diff))
+  while ((1<<ibf_order) < (IBF_ALPHA * diff) || (1<<ibf_order) < SE_IBF_HASH_NUM)
     ibf_order++;
   if (ibf_order > MAX_IBF_ORDER)
     ibf_order = MAX_IBF_ORDER;
@@ -694,12 +696,12 @@ handle_p2p_strata_estimator (void *cls, const struct GNUNET_MessageHeader *mh)
                                        SE_IBF_HASH_NUM);
   strata_estimator_read (&mh[1], remote_se);
   GNUNET_assert (NULL != eo->se);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got se, calculating diff\n");
   diff = strata_estimator_difference (remote_se, eo->se);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "se diff=%d\n", diff);
   strata_estimator_destroy (remote_se);
   strata_estimator_destroy (eo->se);
   eo->se = NULL;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got se diff=%d, using ibf size %d\n",
+              diff, 1<<get_order_from_difference (diff));
   send_ibf (eo, get_order_from_difference (diff));
 }
 
@@ -786,7 +788,7 @@ decode_and_send (struct OperationState *eo)
   prepare_ibf (eo, eo->remote_ibf->size);
   diff_ibf = ibf_dup (eo->local_ibf);
   ibf_subtract (diff_ibf, eo->remote_ibf);
-  
+
   ibf_destroy (eo->remote_ibf);
   eo->remote_ibf = NULL;
 
@@ -824,14 +826,14 @@ decode_and_send (struct OperationState *eo)
       next_order++;
       if (next_order <= MAX_IBF_ORDER)
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 
+        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    "decoding failed, sending larger ibf (size %u)\n",
                     1<<next_order);
         send_ibf (eo, next_order);
       }
       else
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "set union failed: reached ibf limit\n");
       }
       break;
@@ -860,7 +862,7 @@ decode_and_send (struct OperationState *eo)
        * so that we don't request it again with the next ibf if decoding fails */
       ev = GNUNET_MQ_msg_header_extra (msg, sizeof (struct IBF_Key),
                                         GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS);
-      
+
       *(struct IBF_Key *) &msg[1] = key;
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element request\n");
       GNUNET_MQ_send (eo->mq, ev);
@@ -928,7 +930,7 @@ handle_p2p_ibf (void *cls, const struct GNUNET_MessageHeader *mh)
     fail_union_operation (eo);
     return;
   }
-  
+
   ibf_read_slice (&msg[1], eo->ibf_buckets_received, buckets_in_message, eo->remote_ibf);
   eo->ibf_buckets_received += buckets_in_message;
 
@@ -1023,7 +1025,7 @@ handle_p2p_elements (void *cls, const struct GNUNET_MessageHeader *mh)
     return;
   }
   element_size = ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader);
-  ee = GNUNET_malloc (sizeof *eo + element_size);
+  ee = GNUNET_malloc (sizeof *ee + element_size);
   memcpy (&ee[1], &mh[1], element_size);
   ee->element.size = element_size;
   ee->element.data = &ee[1];
@@ -1076,25 +1078,9 @@ handle_p2p_element_requests (void *cls, const struct GNUNET_MessageHeader *mh)
 }
 
 
-/**
- * Handle a 'DIE' message from the remote peer.
- * This indicates that the other peer is terminated.
- * 
- * @param cls the union operation
- * @param mh the message
- */
-static void
-handle_p2p_die (void *cls, const struct GNUNET_MessageHeader *mh)
-{
-  struct OperationState *eo = cls;
-
-  send_client_done_and_destroy (eo);
-}
-
-
 /**
  * Handle a done message from a remote peer
- * 
+ *
  * @param cls the union operation
  * @param mh the message
  */
@@ -1116,10 +1102,7 @@ handle_p2p_done (void *cls, const struct GNUNET_MessageHeader *mh)
   }
   if (eo->phase == PHASE_EXPECT_ELEMENTS)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got final DONE, trying to send DIE\n");
-    /* send the die message, which might not even be delivered,
-     * as we could have shut down before that */
-    ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DIE);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got final DONE\n");
     eo->phase = PHASE_FINISHED;
     send_client_done_and_destroy (eo);
     return;
@@ -1150,13 +1133,14 @@ union_evaluate (struct OperationSpecification *spec,
   tc->vt = _GSS_union_vt ();
   tc->op = eo;
   eo->se = strata_estimator_dup (spec->set->state->se);
+  eo->generation_created = spec->set->current_generation++;
   eo->set = spec->set;
   eo->spec = spec;
   eo->tunnel = tunnel;
   eo->mq = GNUNET_MESH_mq_create (tunnel);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
-             "evaluating union operation, (app %s)\n", 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "evaluating union operation, (app %s)\n",
               GNUNET_h2s (&eo->spec->app_id));
 
   /* we started the operation, thus we have to send the operation request */
@@ -1217,10 +1201,10 @@ union_set_create (void)
   struct SetState *set_state;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "union set created\n");
-  
+
   set_state = GNUNET_new (struct SetState);
   set_state->se = strata_estimator_create (SE_STRATA_COUNT,
-                                              SE_IBF_SIZE, SE_IBF_HASH_NUM);  
+                                              SE_IBF_SIZE, SE_IBF_HASH_NUM);
   return set_state;
 }
 
@@ -1229,7 +1213,7 @@ union_set_create (void)
  * Add the element from the given element message to the set.
  *
  * @param set_state state of the set want to add to
- * @param element the element to add to the set
+ * @param ee the element to add to the set
  */
 static void
 union_add (struct SetState *set_state, struct ElementEntry *ee)
@@ -1304,9 +1288,6 @@ union_handle_p2p_message (struct OperationState *eo,
     case GNUNET_MESSAGE_TYPE_SET_P2P_DONE:
       handle_p2p_done (eo, mh);
       break;
-    case GNUNET_MESSAGE_TYPE_SET_P2P_DIE:
-      handle_p2p_die (eo, mh);
-      break;
     default:
       /* something wrong with mesh's message handlers? */
       GNUNET_assert (0);
@@ -1342,7 +1323,6 @@ union_peer_disconnect (struct OperationState *op)
     return;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "other peer disconnected (finished)\n");
-  /* maybe the other peer did not get to send his 'DIE' message before he died? */
   if (GNUNET_NO == op->client_done_sent)
     send_client_done_and_destroy (op);
 }