new timeout tests for WLAN and bluetooth
[oweals/gnunet.git] / src / set / gnunet-service-set.c
index fa6fb5a6b8a330180060c00ffe737279efe066ee..9cc2e56f6b280cadb0fae091317638eeb9322df5 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
 
 
 /**
- * Peer that has connected to us, but is not yet evaluating a set operation.
- * Once the peer has sent a request, and the client has
- * accepted or rejected it, this information will be deleted.
+ * State of an operation where the peer has connected to us, but is not yet
+ * evaluating a set operation.  Once the peer has sent a concrete request, and
+ * the client has accepted or rejected it, this information will be deleted
+ * and replaced by the real set operation state.
  */
-struct Incoming
+struct OperationState
 {
-  /**
-   * Incoming peers are held in a linked list
-   */
-  struct Incoming *next;
-
-  /**
-   * Incoming peers are held in a linked list
-   */
-  struct Incoming *prev;
-
-  /**
-   * Detail information about the operation.
-   * NULL as long as we did not receive the operation
-   * request from the remote peer.
-   */
-  struct OperationSpecification *spec;
-
   /**
    * The identity of the requesting peer.  Needs to
    * be stored here as the op spec might not have been created yet.
    */
   struct GNUNET_PeerIdentity peer;
 
-  /**
-   * Tunnel to the peer.
-   */
-  struct GNUNET_MESH_Tunnel *tunnel;
-
   /**
    * Unique request id for the request from
    * a remote peer, sent to the client, which will
@@ -76,12 +55,6 @@ struct Incoming
    * after the timeout, it will be disconnected.
    */
   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
-
-  /**
-   * Tunnel context, needs to be stored here as a client's accept will change
-   * the tunnel context.
-   */
-  struct TunnelContext *tc;
 };
 
 
@@ -160,13 +133,13 @@ static struct Listener *listeners_tail;
  * Incoming sockets from remote peers are
  * held in a doubly linked list.
  */
-static struct Incoming *incoming_head;
+static struct Operation *incoming_head;
 
 /**
  * Incoming sockets from remote peers are
  * held in a doubly linked list.
  */
-static struct Incoming *incoming_tail;
+static struct Operation *incoming_tail;
 
 /**
  * Counter for allocating unique IDs for clients,
@@ -221,14 +194,18 @@ listener_get (struct GNUNET_SERVER_Client *client)
  * @return the incoming socket associated with the id,
  *         or NULL if there is none
  */
-static struct Incoming *
+static struct Operation *
 get_incoming (uint32_t id)
 {
-  struct Incoming *incoming;
+  struct Operation *op;
 
-  for (incoming = incoming_head; NULL != incoming; incoming = incoming->next)
-    if (incoming->suggest_id == id)
-      return incoming;
+  for (op = incoming_head; NULL != op; op = op->next)
+    if (op->state->suggest_id == id)
+    {
+      // FIXME: remove this assertion once the corresponding bug is gone!
+      GNUNET_assert (GNUNET_YES == op->is_incoming);
+      return op;
+    }
   return NULL;
 }
 
@@ -261,14 +238,112 @@ listener_destroy (struct Listener *listener)
 
 
 /**
- * Iterator over hash map entries.
+ * Collect and destroy elements that are not needed anymore, because
+ * their lifetime (as determined by their generation) does not overlap with any active
+ * set operation.
+ *
+ * We hereby replace the old element hashmap with a new one, instead of removing elements.
+ */
+void
+collect_generation_garbage (struct Set *set)
+{
+  struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
+  struct ElementEntry *ee;
+  struct GNUNET_CONTAINER_MultiHashMap *new_elements;
+  int res;
+  struct Operation *op;
+
+  new_elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
+  iter = GNUNET_CONTAINER_multihashmap_iterator_create (set->elements);
+  while (GNUNET_OK ==
+         (res = GNUNET_CONTAINER_multihashmap_iterator_next (iter, NULL, (const void **) &ee)))
+  {
+    if (GNUNET_NO == ee->removed)
+      goto still_needed;
+    for (op = set->ops_head; NULL != op; op = op->next)
+      if ((op->generation_created >= ee->generation_added) &&
+          (op->generation_created < ee->generation_removed))
+        goto still_needed;
+    GNUNET_free (ee);
+    continue;
+still_needed:
+    // we don't expect collisions, thus the replace option
+    GNUNET_CONTAINER_multihashmap_put (new_elements, &ee->element_hash, ee,
+                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
+  }
+  GNUNET_CONTAINER_multihashmap_iterator_destroy (iter);
+  GNUNET_CONTAINER_multihashmap_destroy (set->elements);
+  set->elements = new_elements;
+}
+
+
+/**
+ * Destroy the given operation.  Call the implementation-specific cancel function
+ * of the operation.  Disconnects from the remote peer.
+ * Does not disconnect the client, as there may be multiple operations per set.
+ *
+ * @param op operation to destroy
+ */
+void
+_GSS_operation_destroy (struct Operation *op)
+{
+  struct Set *set;
+  struct GNUNET_MESH_Channel *channel;
+
+  if (NULL == op->vt)
+    return;
+
+  set = op->spec->set;
+
+  GNUNET_assert (GNUNET_NO == op->is_incoming);
+  GNUNET_assert (NULL != op->spec);
+  GNUNET_CONTAINER_DLL_remove (op->spec->set->ops_head,
+                               op->spec->set->ops_tail,
+                               op);
+
+  op->vt->cancel (op);
+  op->vt = NULL;
+
+  if (NULL != op->spec)
+  {
+    if (NULL != op->spec->context_msg)
+    {
+      GNUNET_free (op->spec->context_msg);
+      op->spec->context_msg = NULL;
+    }
+    GNUNET_free (op->spec);
+    op->spec = NULL;
+  }
+
+  if (NULL != op->mq)
+  {
+    GNUNET_MQ_destroy (op->mq);
+    op->mq = NULL;
+  }
+
+  if (NULL != (channel = op->channel))
+  {
+    op->channel = NULL;
+    GNUNET_MESH_channel_destroy (channel);
+  }
+
+  collect_generation_garbage (set);
+
+  /* We rely on the channel end handler to free 'op'. When 'op->channel' was NULL,
+   * there was a channel end handler that will free 'op' on the call stack. */
+}
+
+
+/**
+ * Iterator over hash map entries to free
+ * element entries.
  *
  * @param cls closure
  * @param key current key code
- * @param value value in the hash map
- * @return GNUNET_YES if we should continue to
+ * @param value a `struct ElementEntry *` to be free'd
+ * @return #GNUNET_YES if we should continue to
  *         iterate,
- *         GNUNET_NO if not.
+ *         #GNUNET_NO if not.
  */
 static int
 destroy_elements_iterator (void *cls,
@@ -292,7 +367,7 @@ set_destroy (struct Set *set)
 {
   /* If the client is not dead yet, destroy it.
    * 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
+   * We do this so that the channel end handler still has a valid set handle
    * to destroy. */
   if (NULL != set->client)
   {
@@ -302,6 +377,8 @@ set_destroy (struct Set *set)
     return;
   }
   GNUNET_assert (NULL != set->state);
+  while (NULL != set->ops_head)
+    _GSS_operation_destroy (set->ops_head);
   set->vt->destroy_set (set->state);
   set->state = NULL;
   if (NULL != set->client_mq)
@@ -317,6 +394,7 @@ set_destroy (struct Set *set)
   GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
   if (NULL != set->elements)
   {
+    // free all elements in the hashtable, before destroying the table
     GNUNET_CONTAINER_multihashmap_iterate (set->elements,
                                            destroy_elements_iterator, NULL);
     GNUNET_CONTAINER_multihashmap_destroy (set->elements);
@@ -327,8 +405,7 @@ set_destroy (struct Set *set)
 
 
 /**
- * Clean up after a client after it is
- * disconnected (either by us or by itself)
+ * Clean up after a client has disconnected
  *
  * @param cls closure, unused
  * @param client the client to clean up after
@@ -339,21 +416,23 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
   struct Set *set;
   struct Listener *listener;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected, cleaning up\n");
-
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "client disconnected, cleaning up\n");
   set = set_get (client);
   if (NULL != set)
   {
     set->client = NULL;
     set_destroy (set);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(client's set destroyed)\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "(client's set destroyed)\n");
   }
   listener = listener_get (client);
   if (NULL != listener)
   {
     listener->client = NULL;
     listener_destroy (listener);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(client's listener destroyed)\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "(client's listener destroyed)\n");
   }
 }
 
@@ -364,20 +443,50 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
  * @param incoming remote request to destroy
  */
 static void
-incoming_destroy (struct Incoming *incoming)
+incoming_destroy (struct Operation *incoming)
 {
+  GNUNET_assert (GNUNET_YES == incoming->is_incoming);
   GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
-  if (NULL != incoming->tunnel)
+  if (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task)
   {
-    struct GNUNET_MESH_Tunnel *t = incoming->tunnel;
-    incoming->tunnel = NULL;
-    GNUNET_MESH_tunnel_destroy (t);
-    return;
+    GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
+    incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  GNUNET_free (incoming);
+  GNUNET_assert (NULL != incoming->state);
+  GNUNET_free (incoming->state);
+  // make sure that the tunnel end handler will not
+  // destroy us again
+  incoming->vt = NULL;
+  incoming->state = NULL;
 }
 
 
+/**
+ * remove & free state of the operation from the incoming list
+ *
+ * @param incoming the element to remove
+ */
+static void
+incoming_retire (struct Operation *incoming)
+{
+  GNUNET_assert (GNUNET_YES == incoming->is_incoming);
+  incoming->is_incoming = GNUNET_NO;
+  GNUNET_assert (NULL != incoming->state);
+  GNUNET_free (incoming->state);
+  incoming->state = NULL;
+  GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
+}
+
+
+/**
+ * Find a listener that is interested in the given operation type
+ * and application id.
+ *
+ * @param op operation type to look for
+ * @param app_id application id to look for
+ * @return a matching listener, or NULL if no listener matches the
+ *         given operation and application id
+ */
 static struct Listener *
 listener_get_by_target (enum GNUNET_SET_OperationType op,
                         const struct GNUNET_HashCode *app_id)
@@ -397,32 +506,36 @@ listener_get_by_target (enum GNUNET_SET_OperationType op,
 
 
 /**
- * Suggest the given request to the listener,
- * who can accept or reject the request.
+ * Suggest the given request to the listener. The listening client can then
+ * accept or reject the remote request.
  *
  * @param incoming the incoming peer with the request to suggest
  * @param listener the listener to suggest the request to
  */
 static void
-incoming_suggest (struct Incoming *incoming, struct Listener *listener)
+incoming_suggest (struct Operation *incoming, struct Listener *listener)
 {
   struct GNUNET_MQ_Envelope *mqm;
   struct GNUNET_SET_RequestMessage *cmsg;
-
-  GNUNET_assert (0 == incoming->suggest_id);
+  
+  GNUNET_assert (GNUNET_YES == incoming->is_incoming);
+  GNUNET_assert (NULL != incoming->state);
   GNUNET_assert (NULL != incoming->spec);
-  incoming->suggest_id = suggest_id++;
+  GNUNET_assert (0 == incoming->state->suggest_id);
+  incoming->state->suggest_id = suggest_id++;
+
+  GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != incoming->state->timeout_task);
+  GNUNET_SCHEDULER_cancel (incoming->state->timeout_task);
+  incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
 
-  GNUNET_SCHEDULER_cancel (incoming->timeout_task);
   mqm = GNUNET_MQ_msg_nested_mh (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST,
                                  incoming->spec->context_msg);
   GNUNET_assert (NULL != mqm);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "suggesting request with accept id %u\n",
-              incoming->suggest_id);
-  cmsg->accept_id = htonl (incoming->suggest_id);
+              incoming->state->suggest_id);
+  cmsg->accept_id = htonl (incoming->state->suggest_id);
   cmsg->peer_id = incoming->spec->peer;
   GNUNET_MQ_send (listener->client_mq, mqm);
-
 }
 
 
@@ -430,20 +543,25 @@ incoming_suggest (struct Incoming *incoming, struct Listener *listener)
  * Handle a request for a set operation from
  * another peer.
  *
+ * This msg is expected as the first and only msg handled through the
+ * non-operation bound virtual table, acceptance of this operation replaces
+ * our virtual table and subsequent msgs would be routed differently.
+ *
  * @param op the operation state
  * @param mh the received message
- * @return GNUNET_OK if the tunnel should be kept alive,
- *         GNUNET_SYSERR to destroy the tunnel
+ * @return #GNUNET_OK if the channel should be kept alive,
+ *         #GNUNET_SYSERR to destroy the channel
  */
 static int
-handle_incoming_msg (struct OperationState *op,
+handle_incoming_msg (struct Operation *op,
                      const struct GNUNET_MessageHeader *mh)
 {
-  struct Incoming *incoming = (struct Incoming *) op;
   const struct OperationRequestMessage *msg = (const struct OperationRequestMessage *) mh;
   struct Listener *listener;
   struct OperationSpecification *spec;
 
+  GNUNET_assert (GNUNET_YES == op->is_incoming);
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got op request\n");
 
   if (GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST != ntohs (mh->type))
@@ -452,23 +570,25 @@ handle_incoming_msg (struct OperationState *op,
     return GNUNET_SYSERR;
   }
 
-  if (NULL != incoming->spec)
+  /* double operation request */
+  if (NULL != op->spec)
   {
-    /* double operation request */
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
 
   spec = GNUNET_new (struct OperationSpecification);
   spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
+  // for simplicity we just backup the context msg instead of rebuilding it later on
   if (NULL != spec->context_msg)
     spec->context_msg = GNUNET_copy_message (spec->context_msg);
   spec->operation = ntohl (msg->operation);
   spec->app_id = msg->app_id;
   spec->salt = ntohl (msg->salt);
-  spec->peer = incoming->peer;
+  spec->peer = op->state->peer;
+  spec->remote_element_count = ntohl (msg->element_count);
 
-  incoming->spec = spec;
+  op->spec = spec;
 
   if ( (NULL != spec->context_msg) &&
        (ntohs (spec->context_msg->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE) )
@@ -486,11 +606,19 @@ handle_incoming_msg (struct OperationState *op,
                 "no listener matches incoming request, waiting with timeout\n");
     return GNUNET_OK;
   }
-  incoming_suggest (incoming, listener);
+  incoming_suggest (op, listener);
   return GNUNET_OK;
 }
 
 
+/**
+ * Send the next element of a set to the set's client.  The next element is given by
+ * the set's current hashmap iterator.  The set's iterator will be set to NULL if there
+ * are no more elements in the set.  The caller must ensure that the set's iterator is
+ * valid.
+ *
+ * @param set set that should send its next element to its client
+ */
 static void
 send_client_element (struct Set *set)
 {
@@ -503,6 +631,8 @@ send_client_element (struct Set *set)
   if (GNUNET_NO == ret)
   {
     ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
+    GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
+    set->iter = NULL;
   }
   else
   {
@@ -530,7 +660,8 @@ handle_client_iterate (void *cls,
                        const struct GNUNET_MessageHeader *m)
 {
   struct Set *set;
-  
+
+  // iterate over a non existing set
   set = set_get (client);
   if (NULL == set)
   {
@@ -539,6 +670,7 @@ handle_client_iterate (void *cls,
     return;
   }
 
+  // only one concurrent iterate-action per set
   if (NULL != set->iter)
   {
     GNUNET_break (0);
@@ -561,16 +693,19 @@ handle_client_iterate (void *cls,
  * @param m message sent by the client
  */
 static void
-handle_client_create (void *cls,
-                      struct GNUNET_SERVER_Client *client,
-                      const struct GNUNET_MessageHeader *m)
+handle_client_create_set (void *cls,
+                          struct GNUNET_SERVER_Client *client,
+                          const struct GNUNET_MessageHeader *m)
 {
-  struct GNUNET_SET_CreateMessage *msg = (struct GNUNET_SET_CreateMessage *) m;
+  const struct GNUNET_SET_CreateMessage *msg;
   struct Set *set;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client created new set (operation %u)\n",
+  msg = (const struct GNUNET_SET_CreateMessage *) m;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "client created new set (operation %u)\n",
               ntohs (msg->operation));
 
+  // max. one set per client!
   if (NULL != set_get (client))
   {
     GNUNET_break (0);
@@ -582,17 +717,18 @@ handle_client_create (void *cls,
 
   switch (ntohs (msg->operation))
   {
-    case GNUNET_SET_OPERATION_INTERSECTION:
-      // FIXME
-      break;
-    case GNUNET_SET_OPERATION_UNION:
-      set->vt = _GSS_union_vt ();
-      break;
-    default:
-      GNUNET_free (set);
-      GNUNET_break (0);
-      GNUNET_SERVER_client_disconnect (client);
-      return;
+  case GNUNET_SET_OPERATION_INTERSECTION:
+    // FIXME: implement intersection vt
+    // set->vt = _GSS_intersection_vt ();
+    break;
+  case GNUNET_SET_OPERATION_UNION:
+    set->vt = _GSS_union_vt ();
+    break;
+  default:
+    GNUNET_free (set);
+    GNUNET_break (0);
+    GNUNET_SERVER_client_disconnect (client);
+    return;
   }
 
   set->state = set->vt->create ();
@@ -616,51 +752,65 @@ handle_client_listen (void *cls,
                       struct GNUNET_SERVER_Client *client,
                       const struct GNUNET_MessageHeader *m)
 {
-  struct GNUNET_SET_ListenMessage *msg = (struct GNUNET_SET_ListenMessage *) m;
+  const struct GNUNET_SET_ListenMessage *msg;
   struct Listener *listener;
-  struct Incoming *incoming;
+  struct Operation *op;
 
+  msg = (const struct GNUNET_SET_ListenMessage *) m;
+  /* max. one per client! */
   if (NULL != listener_get (client))
   {
     GNUNET_break (0);
     GNUNET_SERVER_client_disconnect (client);
     return;
   }
+
   listener = GNUNET_new (struct Listener);
   listener->client = client;
   listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
   listener->app_id = msg->app_id;
   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)
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "new listener created (op %u, app %s)\n",
+              listener->operation,
+              GNUNET_h2s (&listener->app_id));
+
+  /* check for incoming requests the listener is interested in */
+  for (op = incoming_head; NULL != op; op = op->next)
   {
-    if (NULL == incoming->spec)
+    if (NULL == op->spec)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "request has no spec yet\n");
+      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);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "considering (op: %u, app: %s, suggest: %u)\n",
+                op->spec->operation,
+                GNUNET_h2s (&op->spec->app_id),
+                op->state->suggest_id);
 
-    if (0 != incoming->suggest_id)
+    /* don't consider the incoming request if it has been already suggested to a listener */
+    if (0 != op->state->suggest_id)
       continue;
-    if (listener->operation != incoming->spec->operation)
+    if (listener->operation != op->spec->operation)
       continue;
-    if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &incoming->spec->app_id))
+    if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &op->spec->app_id))
       continue;
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "request suggested\n");
-    incoming_suggest (incoming, listener);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "request suggested\n");
+    incoming_suggest (op, listener);
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "considered all incoming requests\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "considered all incoming requests\n");
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
 
 /**
- * Called when the client wants to reject an operation
- * request from another peer.
+ * Called when the listening client rejects an operation
+ * request by another peer.
  *
  * @param cls unused
  * @param client client that sent the message
@@ -671,26 +821,30 @@ handle_client_reject (void *cls,
                       struct GNUNET_SERVER_Client *client,
                       const struct GNUNET_MessageHeader *m)
 {
-  struct Incoming *incoming;
+  struct Operation *incoming;
   const struct GNUNET_SET_AcceptRejectMessage *msg;
 
   msg = (const struct GNUNET_SET_AcceptRejectMessage *) m;
   GNUNET_break (0 == ntohl (msg->request_id));
 
+  // no matching incoming operation for this reject
   incoming = get_incoming (ntohl (msg->accept_reject_id));
   if (NULL == incoming)
   {
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer request rejected by client\n");
-  GNUNET_MESH_tunnel_destroy (incoming->tunnel);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "peer request rejected by client\n");
+
+  GNUNET_MESH_channel_destroy (incoming->channel);
+  //channel destruction handler called immediately upon destruction
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
 
 /**
- * Called when a client wants to add an element to a
+ * Called when a client wants to add/remove an element to/from a
  * set it inhabits.
  *
  * @param cls unused
@@ -707,6 +861,7 @@ handle_client_add_remove (void *cls,
   struct GNUNET_SET_Element el;
   struct ElementEntry *ee;
 
+  // client without a set requested an operation
   set = set_get (client);
   if (NULL == set)
   {
@@ -717,7 +872,7 @@ handle_client_add_remove (void *cls,
   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))
@@ -728,12 +883,14 @@ handle_client_add_remove (void *cls,
     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");
+      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");
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "client tried to remove element twice\n");
       return;
     }
     ee->removed = GNUNET_YES;
@@ -755,7 +912,8 @@ handle_client_add_remove (void *cls,
                                                 &ee->element_hash);
     if (NULL != ee_dup)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "element inserted twice, ignoring\n");
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "element inserted twice, ignoring\n");
       GNUNET_free (ee);
       return;
     }
@@ -779,10 +937,9 @@ handle_client_evaluate (void *cls,
                         const struct GNUNET_MessageHeader *m)
 {
   struct Set *set;
-  struct TunnelContext *tc;
-  struct GNUNET_MESH_Tunnel *tunnel;
-  struct GNUNET_SET_EvaluateMessage *msg;
+  const struct GNUNET_SET_EvaluateMessage *msg;
   struct OperationSpecification *spec;
+  struct Operation *op;
 
   set = set_get (client);
   if (NULL == set)
@@ -792,32 +949,40 @@ handle_client_evaluate (void *cls,
     return;
   }
 
-  msg = (struct GNUNET_SET_EvaluateMessage *) m;
-  tc = GNUNET_new (struct TunnelContext);
+  msg = (const struct GNUNET_SET_EvaluateMessage *) m;
   spec = GNUNET_new (struct OperationSpecification);
   spec->operation = set->operation;
   spec->app_id = msg->app_id;
   spec->salt = ntohl (msg->salt);
   spec->peer = msg->target_peer;
   spec->set = set;
+  spec->result_mode = ntohs (msg->result_mode);
   spec->client_request_id = ntohl (msg->request_id);
   spec->context_msg = GNUNET_MQ_extract_nested_mh (msg);
+
+  // for simplicity we just backup the context msg instead of rebuilding it later on
   if (NULL != spec->context_msg)
     spec->context_msg = GNUNET_copy_message (spec->context_msg);
 
-  tunnel = GNUNET_MESH_tunnel_create (mesh, tc, &msg->target_peer,
-                                      GNUNET_APPLICATION_TYPE_SET,
-                                      GNUNET_YES,
-                                      GNUNET_YES);
+  op = GNUNET_new (struct Operation);
+  op->spec = spec;
+  op->generation_created = set->current_generation++;
+  op->vt = set->vt;
+  GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
 
-  set->vt->evaluate (spec, tunnel, tc);
+  op->channel = GNUNET_MESH_channel_create (mesh, op, &msg->target_peer,
+                                            GNUNET_APPLICATION_TYPE_SET,
+                                            GNUNET_MESH_OPTION_RELIABLE);
 
+  op->mq = GNUNET_MESH_mq_create (op->channel);
+
+  set->vt->evaluate (op);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
 
 /**
- * Handle an ack from a client.
+ * Handle an ack from a client, and send the next element.
  *
  * @param cls unused
  * @param client the client
@@ -829,7 +994,8 @@ handle_client_iter_ack (void *cls,
                    const struct GNUNET_MessageHeader *m)
 {
   struct Set *set;
-  
+
+  // client without a set requested an operation
   set = set_get (client);
   if (NULL == set)
   {
@@ -838,6 +1004,7 @@ handle_client_iter_ack (void *cls,
     return;
   }
 
+  // client sent an ack, but we were not expecting one
   if (NULL == set->iter)
   {
     GNUNET_break (0);
@@ -851,8 +1018,8 @@ handle_client_iter_ack (void *cls,
 
 
 /**
- * Handle a request from the client to accept
- * a set operation that came from a remote peer.
+ * Handle a request from the client to
+ * cancel a running set operation.
  *
  * @param cls unused
  * @param client the client
@@ -866,7 +1033,10 @@ handle_client_cancel (void *cls,
   const struct GNUNET_SET_CancelMessage *msg =
       (const struct GNUNET_SET_CancelMessage *) mh;
   struct Set *set;
+  struct Operation *op;
+  int found;
 
+  // client without a set requested an operation
   set = set_get (client);
   if (NULL == set)
   {
@@ -874,14 +1044,31 @@ handle_client_cancel (void *cls,
     GNUNET_SERVER_client_disconnect (client);
     return;
   }
-  /* FIXME: maybe cancel should return success/error code? */
-  set->vt->cancel (set->state, ntohl (msg->request_id));
+  found = GNUNET_NO;
+  for (op = set->ops_head; NULL != op; op = op->next)
+  {
+    if (op->spec->client_request_id == msg->request_id)
+    {
+      found = GNUNET_YES;
+      break;
+    }
+  }
+
+  if (GNUNET_NO == found)
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_client_disconnect (client);
+    return;
+  }
+
+  _GSS_operation_destroy (op);
 }
 
 
 /**
  * Handle a request from the client to accept
  * a set operation that came from a remote peer.
+ * We forward the accept to the associated operation for handling
  *
  * @param cls unused
  * @param client the client
@@ -893,21 +1080,27 @@ handle_client_accept (void *cls,
                       const struct GNUNET_MessageHeader *mh)
 {
   struct Set *set;
-  struct Incoming *incoming;
-  struct GNUNET_SET_AcceptRejectMessage *msg = (struct GNUNET_SET_AcceptRejectMessage *) mh;
-
-  incoming = get_incoming (ntohl (msg->accept_reject_id));
+  const struct GNUNET_SET_AcceptRejectMessage *msg;
+  struct Operation *op;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client accepting %u\n", ntohl (msg->accept_reject_id));
+  msg = (const struct GNUNET_SET_AcceptRejectMessage *) mh;
+  op = get_incoming (ntohl (msg->accept_reject_id));
 
-  if (NULL == incoming)
+  // incoming operation does not exist
+  if (NULL == op)
   {
-
     GNUNET_break (0);
     GNUNET_SERVER_client_disconnect (client);
     return;
   }
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "client accepting %u\n",
+              ntohl (msg->accept_reject_id));
+
+  GNUNET_assert (GNUNET_YES == op->is_incoming);
+
+  // client without a set requested an operation
   set = set_get (client);
 
   if (NULL == set)
@@ -917,12 +1110,21 @@ handle_client_accept (void *cls,
     return;
   }
 
-  incoming->spec->set = set;
-  incoming->spec->client_request_id = ntohl (msg->request_id);
-  set->vt->accept (incoming->spec, incoming->tunnel, incoming->tc);
-  /* tunnel ownership goes to operation */
-  incoming->tunnel = NULL;
-  incoming_destroy (incoming);
+  op->spec->set = set;
+
+  incoming_retire (op);
+
+  GNUNET_assert (NULL != op->spec->set);
+  GNUNET_assert (NULL != op->spec->set->vt);
+
+  GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, op);
+
+  op->spec->client_request_id = ntohl (msg->request_id);
+  op->spec->result_mode = ntohs (msg->result_mode);
+  op->generation_created = set->current_generation++;
+  op->vt = op->spec->set->vt;
+  GNUNET_assert (NULL != op->vt->accept);
+  set->vt->accept (op);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -946,145 +1148,194 @@ shutdown_task (void *cls,
   while (NULL != sets_head)
     set_destroy (sets_head);
 
-
-  /* it's important to destroy mesh at the end, as tunnels
-   * must be destroyed first! */
+  /* it's important to destroy mesh at the end, as all channels
+   * must be destroyed before the mesh handle! */
   if (NULL != mesh)
   {
     GNUNET_MESH_disconnect (mesh);
     mesh = NULL;
   }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handled shutdown request\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "handled shutdown request\n");
 }
 
 
 /**
- * Signature of the main function of a task.
+ * Timeout happens iff:
+ *  - we suggested an operation to our listener,
+ *    but did not receive a response in time
+ *  - we got the channel from a peer but no #GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
+ *  - shutdown (obviously)
  *
- * @param cls closure
+ * @param cls channel context
  * @param tc context information (why was this task triggered now)
  */
 static void
 incoming_timeout_cb (void *cls,
                      const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct Incoming *incoming = cls;
+  struct Operation *incoming = cls;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "remote peer timed out\n");
+  incoming->state->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+  GNUNET_assert (GNUNET_YES == incoming->is_incoming);
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "remote peer timed out\n");
   incoming_destroy (incoming);
 }
 
 
+/**
+ * Terminates an incoming operation in case we have not yet received an
+ * operation request. Called by the channel destruction handler.
+ *
+ * @param op the channel context
+ */
 static void
-handle_incoming_disconnect (struct OperationState *op_state)
+handle_incoming_disconnect (struct Operation *op)
 {
-  struct Incoming *incoming = (struct Incoming *) op_state;
-  if (NULL == incoming->tunnel)
-    return;
-
-  incoming_destroy (incoming);
+  GNUNET_assert (GNUNET_YES == op->is_incoming);
+  incoming_destroy (op);
+  op->vt = NULL;
 }
 
 
 /**
- * Method called whenever another peer has added us to a tunnel
+ * Method called whenever another peer has added us to a channel
  * the other peer initiated.
  * Only called (once) upon reception of data with a message type which was
- * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
- * causes te tunnel to be ignored and no further notifications are sent about
- * the same tunnel.
+ * subscribed to in GNUNET_MESH_connect().
+ *
+ * The channel context represents the operation itself and gets added to a DLL,
+ * from where it gets looked up when our local listener client responds
+ * to a proposed/suggested operation or connects and associates with this operation.
  *
  * @param cls closure
- * @param tunnel new handle to the tunnel
- * @param initiator peer that started the tunnel
- * @param port Port this tunnel is for.
- * @return initial tunnel context for the tunnel
+ * @param channel new handle to the channel
+ * @param initiator peer that started the channel
+ * @param port Port this channel is for.
+ * @param options Unused.
+ * @return initial channel context for the channel
  *         (can be NULL -- that's not an error)
  */
 static void *
-tunnel_new_cb (void *cls,
-               struct GNUNET_MESH_Tunnel *tunnel,
+channel_new_cb (void *cls,
+               struct GNUNET_MESH_Channel *channel,
                const struct GNUNET_PeerIdentity *initiator,
-               uint32_t port)
+               uint32_t port, enum GNUNET_MESH_ChannelOption options)
 {
-  struct Incoming *incoming;
+  struct Operation *incoming;
   static const struct SetVT incoming_vt = {
     .msg_handler = handle_incoming_msg,
     .peer_disconnect = handle_incoming_disconnect
   };
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new incoming tunnel\n");
-
-  GNUNET_assert (port == GNUNET_APPLICATION_TYPE_SET);
-  incoming = GNUNET_new (struct Incoming);
-  incoming->peer = *initiator;
-  incoming->tunnel = tunnel;
-  incoming->tc = GNUNET_new (struct TunnelContext);;
-  incoming->tc->vt = &incoming_vt;
-  incoming->tc->op = (struct OperationState *) incoming;
-  incoming->timeout_task = 
-      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, incoming_timeout_cb, incoming);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "new incoming channel\n");
+
+  if (GNUNET_APPLICATION_TYPE_SET != port)
+  {
+    GNUNET_break (0);
+    GNUNET_MESH_channel_destroy (channel);
+    return NULL;
+  }
+
+  incoming = GNUNET_new (struct Operation);
+  incoming->is_incoming = GNUNET_YES;
+  incoming->state = GNUNET_new (struct OperationState);
+  incoming->state->peer = *initiator;
+  incoming->channel = channel;
+  incoming->mq = GNUNET_MESH_mq_create (incoming->channel);
+  incoming->vt = &incoming_vt;
+  incoming->state->timeout_task =
+      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
+                                    &incoming_timeout_cb, incoming);
   GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
 
-  return incoming->tc;
+  return incoming;
 }
 
 
 /**
- * Function called whenever a tunnel is destroyed.  Should clean up
- * any associated state.
- * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
- * the tunnel.
+ * Function called whenever a channel is destroyed.  Should clean up
+ * any associated state.  It must NOT call
+ * GNUNET_MESH_channel_destroy() on the channel.
+ *
+ * The peer_disconnect function is part of a a virtual table set initially either
+ * when a peer creates a new channel with us (channel_new_cb), or once we create
+ * a new channel ourselves (evaluate).
+ *
+ * Once we know the exact type of operation (union/intersection), the vt is
+ * replaced with an operation specific instance (_GSS_[op]_vt).
  *
- * @param cls closure (set from GNUNET_MESH_connect)
- * @param tunnel connection to the other end (henceforth invalid)
- * @param tunnel_ctx place where local state associated
- *                   with the tunnel is stored
+ * @param cls closure (set from GNUNET_MESH_connect())
+ * @param channel connection to the other end (henceforth invalid)
+ * @param channel_ctx place where local state associated
+ *                   with the channel is stored
  */
 static void
-tunnel_end_cb (void *cls,
-               const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
+channel_end_cb (void *cls,
+                const struct GNUNET_MESH_Channel *channel, void *channel_ctx)
 {
-  struct TunnelContext *ctx = tunnel_ctx;
+  struct Operation *op = channel_ctx;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "channel end cb called\n");
+  op->channel = NULL;
+  /* the vt can be null if a client already requested canceling op. */
+  if (NULL != op->vt)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "calling peer disconnect due to channel end\n");
+    op->vt->peer_disconnect (op);
+  }
+
+  if (GNUNET_YES == op->keep)
+    return;
 
-  ctx->vt->peer_disconnect (ctx->op);
   /* mesh will never call us with the context again! */
-  GNUNET_free (tunnel_ctx);
+  GNUNET_free (channel_ctx);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "channel end cb finished\n");
 }
 
 
 /**
  * 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.
+ * received via a mesh channel.
+ *
+ * The msg_handler is a virtual table set in initially either when a peer
+ * creates a new channel with us (channel_new_cb), or once we create a new channel
+ * ourselves (evaluate).
  *
- * @param cls Closure (set from GNUNET_MESH_connect).
- * @param tunnel Connection to the other end.
- * @param tunnel_ctx Place to store local state associated with the tunnel.
+ * Once we know the exact type of operation (union/intersection), the vt is
+ * replaced with an operation specific instance (_GSS_[op]_vt).
+ *
+ * @param cls Closure (set from GNUNET_MESH_connect()).
+ * @param channel Connection to the other end.
+ * @param channel_ctx Place to store local state associated with the channel.
  * @param message The actual message.
- * 
- * @return GNUNET_OK to keep the tunnel open,
- *         GNUNET_SYSERR to close it (signal serious error).
+ * @return #GNUNET_OK to keep the channel open,
+ *         #GNUNET_SYSERR to close it (signal serious error).
  */
 static int
 dispatch_p2p_message (void *cls,
-                      struct GNUNET_MESH_Tunnel *tunnel,
-                      void **tunnel_ctx,
+                      struct GNUNET_MESH_Channel *channel,
+                      void **channel_ctx,
                       const struct GNUNET_MessageHeader *message)
 {
-  struct TunnelContext *tc = *tunnel_ctx;
+  struct Operation *op = *channel_ctx;
   int ret;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dispatching mesh message (type: %u)\n",
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "dispatching mesh message (type: %u)\n",
               ntohs (message->type));
-  /* 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",
+  /* do this before the handler, as the handler might kill the channel */
+  GNUNET_MESH_receive_done (channel);
+  ret = op->vt->msg_handler (op, message);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "handled mesh message (type: %u)\n",
               ntohs (message->type));
   return ret;
 }
@@ -1107,7 +1358,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
         sizeof (struct GNUNET_SET_AcceptRejectMessage)},
     {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,
+    {handle_client_create_set, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE,
         sizeof (struct GNUNET_SET_CreateMessage)},
     {handle_client_iterate, NULL, GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST,
         sizeof (struct GNUNET_MessageHeader)},
@@ -1117,18 +1368,20 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
     {handle_client_reject, NULL, GNUNET_MESSAGE_TYPE_SET_REJECT,
         sizeof (struct GNUNET_SET_AcceptRejectMessage)},
     {handle_client_add_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
-    {handle_client_cancel, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE,
+    {handle_client_cancel, NULL, GNUNET_MESSAGE_TYPE_SET_CANCEL,
         sizeof (struct GNUNET_SET_CancelMessage)},
     {NULL, NULL, 0, 0}
   };
   static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
-    {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_IBF, 0},
+    {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF, 0},
     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_DONE, 0},
-    {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_DIE, 0},
     {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
-    {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_P2P_SE, 0},
+    {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE, 0},
+    {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO, 0},
+    {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF, 0},
+    {dispatch_p2p_message, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF_PART, 0},
     {NULL, 0, 0}
   };
   static const uint32_t mesh_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
@@ -1139,15 +1392,14 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
   GNUNET_SERVER_add_handlers (server, server_handlers);
 
-  mesh = GNUNET_MESH_connect (cfg, NULL, tunnel_new_cb, tunnel_end_cb,
+  mesh = GNUNET_MESH_connect (cfg, NULL, channel_new_cb, channel_end_cb,
                               mesh_handlers, mesh_ports);
   if (NULL == mesh)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not connect to mesh\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                _("Could not connect to mesh service\n"));
     return;
   }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "started\n");
 }
 
 
@@ -1162,9 +1414,11 @@ int
 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 (%d)\n", GNUNET_OK != ret);
   return (GNUNET_OK == ret) ? 0 : 1;
 }
 
+/* end of gnunet-service-set.c */
+