-ftbfs
[oweals/gnunet.git] / src / set / gnunet-service-set.c
index 443694d0830f6b06cabc3a01ae2b8a96866b4e11..99deb3b23d7ddf2cb131ca32b0b524f8e4b803f1 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
@@ -260,6 +260,28 @@ listener_destroy (struct Listener *listener)
 }
 
 
+/**
+ * Iterator over hash map entries.
+ *
+ * @param cls closure
+ * @param key current key code
+ * @param value value in the hash map
+ * @return GNUNET_YES if we should continue to
+ *         iterate,
+ *         GNUNET_NO if not.
+ */
+static int
+destroy_elements_iterator (void *cls,
+                           const struct GNUNET_HashCode * key,
+                           void *value)
+{
+  struct ElementEntry *ee = value;
+
+  GNUNET_free (ee);
+  return GNUNET_YES;
+}
+
+
 /**
  * Destroy a set, and free all resources associated with it.
  *
@@ -269,7 +291,9 @@ static void
 set_destroy (struct Set *set)
 {
   /* If the client is not dead yet, destroy it.
-   * The client's destroy callback will destroy the set again. */
+   * The client's destroy callback will destroy the set again.
+   * We do this so that the tunnel end handler still has a valid set handle
+   * to destroy. */
   if (NULL != set->client)
   {
     struct GNUNET_SERVER_Client *client = set->client;
@@ -277,15 +301,27 @@ set_destroy (struct Set *set)
     GNUNET_SERVER_client_disconnect (client);
     return;
   }
+  GNUNET_assert (NULL != set->state);
+  set->vt->destroy_set (set->state);
+  set->state = NULL;
   if (NULL != set->client_mq)
   {
     GNUNET_MQ_destroy (set->client_mq);
     set->client_mq = NULL;
   }
-  GNUNET_assert (NULL != set->state);
-  set->vt->destroy_set (set->state);
-  set->state = NULL;
+  if (NULL != set->iter)
+  {
+    GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
+    set->iter = NULL;
+  }
   GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
+  if (NULL != set->elements)
+  {
+    GNUNET_CONTAINER_multihashmap_iterate (set->elements,
+                                           destroy_elements_iterator, NULL);
+    GNUNET_CONTAINER_multihashmap_destroy (set->elements);
+    set->elements = NULL;
+  }
   GNUNET_free (set);
 }
 
@@ -442,8 +478,8 @@ handle_incoming_msg (struct OperationState *op,
   }
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received P2P operation request (op %u, app %s)\n",
-              ntohs (msg->operation), GNUNET_h2s (&msg->app_id));
-  listener = listener_get_by_target (ntohs (msg->operation), &msg->app_id);
+              ntohl (msg->operation), GNUNET_h2s (&msg->app_id));
+  listener = listener_get_by_target (ntohl (msg->operation), &msg->app_id);
   if (NULL == listener)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -455,6 +491,32 @@ handle_incoming_msg (struct OperationState *op,
 }
 
 
+static void
+send_client_element (struct Set *set)
+{
+  int ret;
+  struct ElementEntry *ee;
+  struct GNUNET_MQ_Envelope *ev;
+
+  GNUNET_assert (NULL != set->iter);
+  ret = GNUNET_CONTAINER_multihashmap_iterator_next (set->iter, NULL, (const void **) &ee);
+  if (GNUNET_NO == ret)
+  {
+    ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
+  }
+  else
+  {
+    struct GNUNET_SET_IterResponseMessage *msg;
+
+    GNUNET_assert (NULL != ee);
+    ev = GNUNET_MQ_msg_extra (msg, ee->element.size, GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT);
+    memcpy (&msg[1], ee->element.data, ee->element.size);
+    msg->element_type = ee->element.type;
+  }
+  GNUNET_MQ_send (set->client_mq, ev);
+}
+
+
 /**
  * Called when a client wants to iterate the elements of a set.
  *
@@ -468,7 +530,7 @@ handle_client_iterate (void *cls,
                        const struct GNUNET_MessageHeader *m)
 {
   struct Set *set;
-  
+
   set = set_get (client);
   if (NULL == set)
   {
@@ -477,7 +539,17 @@ handle_client_iterate (void *cls,
     return;
   }
 
-  set->vt->iterate (set);
+  if (NULL != set->iter)
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_client_disconnect (client);
+    return;
+  }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "iterating union set with %u elements\n",
+              GNUNET_CONTAINER_multihashmap_size (set->elements));
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+  set->iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
+  send_client_element (set);
 }
 
 
@@ -524,6 +596,7 @@ handle_client_create (void *cls,
   }
 
   set->state = set->vt->create ();
+  set->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
   set->client = client;
   set->client_mq = GNUNET_MQ_queue_for_server_client (client);
   GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
@@ -557,21 +630,30 @@ handle_client_listen (void *cls,
   listener->client = client;
   listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
   listener->app_id = msg->app_id;
-  listener->operation = ntohs (msg->operation);
+  listener->operation = ntohl (msg->operation);
   GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new listener created (op %u, app %s)\n",
               listener->operation, GNUNET_h2s (&listener->app_id));
   for (incoming = incoming_head; NULL != incoming; incoming = incoming->next)
   {
-    if ( (NULL == incoming->spec) ||
-         (0 != incoming->suggest_id) )
+    if (NULL == incoming->spec)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "request has no spec yet\n");
+      continue;
+    }
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "considering (op: %u, app: %s, suggest: %u)\n",
+                incoming->spec->operation, GNUNET_h2s (&incoming->spec->app_id), incoming->suggest_id);
+
+    if (0 != incoming->suggest_id)
       continue;
     if (listener->operation != incoming->spec->operation)
       continue;
     if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &incoming->spec->app_id))
       continue;
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "request suggested\n");
     incoming_suggest (incoming, listener);
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "considered all incoming requests\n");
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -623,6 +705,7 @@ handle_client_add_remove (void *cls,
   struct Set *set;
   const struct GNUNET_SET_ElementMessage *msg;
   struct GNUNET_SET_Element el;
+  struct ElementEntry *ee;
 
   set = set_get (client);
   if (NULL == set)
@@ -631,16 +714,55 @@ handle_client_add_remove (void *cls,
     GNUNET_SERVER_client_disconnect (client);
     return;
   }
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
   msg = (const struct GNUNET_SET_ElementMessage *) m;
   el.size = ntohs (m->size) - sizeof *msg;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "client ins/rem element of size %u\n", el.size);
   el.data = &msg[1];
   if (GNUNET_MESSAGE_TYPE_SET_REMOVE == ntohs (m->type))
-    set->vt->remove (set->state, &el);
+  {
+    struct GNUNET_HashCode hash;
+
+    GNUNET_CRYPTO_hash (el.data, el.size, &hash);
+    ee = GNUNET_CONTAINER_multihashmap_get (set->elements, &hash);
+    if (NULL == ee)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove non-existing element\n");
+      return;
+    }
+    if (GNUNET_YES == ee->removed)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove element twice\n");
+      return;
+    }
+    ee->removed = GNUNET_YES;
+    ee->generation_removed = set->current_generation;
+    set->vt->remove (set->state, ee);
+  }
   else
-    set->vt->add (set->state, &el);
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+  {
+    struct ElementEntry *ee_dup;
+
+    ee = GNUNET_malloc (el.size + sizeof *ee);
+    ee->element.size = el.size;
+    memcpy (&ee[1], el.data, el.size);
+    ee->element.data = &ee[1];
+    ee->generation_added = set->current_generation;
+    ee->remote = GNUNET_NO;
+    GNUNET_CRYPTO_hash (ee->element.data, el.size, &ee->element_hash);
+    ee_dup = GNUNET_CONTAINER_multihashmap_get (set->elements,
+                                                &ee->element_hash);
+    if (NULL != ee_dup)
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "element inserted twice, ignoring\n");
+      GNUNET_free (ee);
+      return;
+    }
+    GNUNET_CONTAINER_multihashmap_put (set->elements, &ee->element_hash, ee,
+                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
+    set->vt->add (set->state, ee);
+  }
 }
 
 
@@ -702,12 +824,29 @@ handle_client_evaluate (void *cls,
  * @param m the message
  */
 static void
-handle_client_ack (void *cls,
+handle_client_iter_ack (void *cls,
                    struct GNUNET_SERVER_Client *client,
                    const struct GNUNET_MessageHeader *m)
 {
-  /* FIXME: implement */
+  struct Set *set;
+
+  set = set_get (client);
+  if (NULL == set)
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_client_disconnect (client);
+    return;
+  }
+
+  if (NULL == set->iter)
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_client_disconnect (client);
+    return;
+  }
+
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
+  send_client_element (set);
 }
 
 
@@ -884,7 +1023,7 @@ tunnel_new_cb (void *cls,
   incoming->tc = GNUNET_new (struct TunnelContext);;
   incoming->tc->vt = &incoming_vt;
   incoming->tc->op = (struct OperationState *) incoming;
-  incoming->timeout_task = 
+  incoming->timeout_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, incoming_timeout_cb, incoming);
   GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
 
@@ -918,7 +1057,7 @@ tunnel_end_cb (void *cls,
 /**
  * Functions with this signature are called whenever a message is
  * received.
- * 
+ *
  * Each time the function must call GNUNET_MESH_receive_done on the tunnel
  * in order to receive the next message. This doesn't need to be immediate:
  * can be delayed if some processing is done on the message.
@@ -927,7 +1066,7 @@ tunnel_end_cb (void *cls,
  * @param tunnel Connection to the other end.
  * @param tunnel_ctx Place to store local state associated with the tunnel.
  * @param message The actual message.
- * 
+ *
  * @return GNUNET_OK to keep the tunnel open,
  *         GNUNET_SYSERR to close it (signal serious error).
  */
@@ -942,8 +1081,9 @@ dispatch_p2p_message (void *cls,
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dispatching mesh message (type: %u)\n",
               ntohs (message->type));
-  ret = tc->vt->msg_handler (tc->op, message);
+  /* do this before the handler, as the handler might kill the tunnel */
   GNUNET_MESH_receive_done (tunnel);
+  ret = tc->vt->msg_handler (tc->op, message);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handled mesh message (type: %u)\n",
               ntohs (message->type));
   return ret;
@@ -965,7 +1105,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
     {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT,
         sizeof (struct GNUNET_SET_AcceptRejectMessage)},
-    {handle_client_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ACK, 0},
+    {handle_client_iter_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_ACK, 0},
     {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
     {handle_client_create, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE,
         sizeof (struct GNUNET_SET_CreateMessage)},
@@ -1023,7 +1163,7 @@ main (int argc, char *const *argv)
   int ret;
   ret = GNUNET_SERVICE_run (argc, argv, "set",
                             GNUNET_SERVICE_OPTION_NONE, &run, NULL);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit (%d)\n", GNUNET_OK != ret);
   return (GNUNET_OK == ret) ? 0 : 1;
 }