- fixed consensus for >2 peers
[oweals/gnunet.git] / src / consensus / gnunet-service-consensus.c
index 1b394db19ad457d09cdcb6a556b5ed68e295fb30..cb7ab2c6835f8477f0fa7aef88b91e80df6b6b0c 100644 (file)
@@ -1,6 +1,6 @@
 /*
       This file is part of GNUnet
-      (C) 2012 Christian Grothoff (and other contributing authors)
+      (C) 2012, 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
       Boston, MA 02111-1307, USA.
 */
 
-
 /**
  * @file consensus/gnunet-service-consensus.c
- * @brief 
+ * @brief multi-peer set reconciliation
  * @author Florian Dold
  */
 
 #include "gnunet_protocols.h"
 #include "gnunet_applications.h"
 #include "gnunet_util_lib.h"
+#include "gnunet_set_service.h"
 #include "gnunet_consensus_service.h"
-#include "gnunet_core_service.h"
-#include "gnunet_mesh_service.h"
+#include "consensus_protocol.h"
 #include "consensus.h"
 
 
-struct ConsensusSession;
+/**
+ * Log macro that prefixes the local peer and the peer we are in contact with.
+ */
+#define LOG_PP(kind, cpi, m,...) GNUNET_log (kind, "P%d for P%d: " m, \
+   cpi->session->local_peer_idx, (int) (cpi - cpi->session->info),##__VA_ARGS__)
+
+
+/**
+ * Number of exponential rounds, used in the exp and completion round.
+ */
+#define NUM_EXP_ROUNDS 4
 
+/* forward declarations */
+
+/* mutual recursion with struct ConsensusSession */
+struct ConsensusPeerInformation;
+
+/* mutual recursion with round_over */
 static void
-send_next (struct ConsensusSession *session);
+subround_over (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
 
 /**
- * An element that is waiting to be transmitted to a client.
+ * Describes the current round a consensus session is in.
  */
-struct PendingElement
+enum ConsensusRound
 {
   /**
-   * Pending elements are kept in a DLL.
+   * Not started the protocol yet.
    */
-  struct PendingElement *next;
-
+  CONSENSUS_ROUND_BEGIN=0,
   /**
-   * Pending elements are kept in a DLL.
+   * Distribution of elements with the exponential scheme.
    */
-  struct PendingElement *prev;
-
+  CONSENSUS_ROUND_EXCHANGE,
+  /**
+   * Exchange which elements each peer has, but don't
+   * transmit the element's data, only their SHA-512 hashes.
+   * This round uses the all-to-all scheme.
+   */
+  CONSENSUS_ROUND_INVENTORY,
   /**
-   * The actual element
+   * Collect and distribute missing values with the exponential scheme.
    */
-  struct GNUNET_CONSENSUS_Element *element;
+  CONSENSUS_ROUND_COMPLETION,
+  /**
+   * Consensus concluded. After timeout and finished communication with client,
+   * consensus session will be destroyed.
+   */
+  CONSENSUS_ROUND_FINISH
 };
 
 
-/*
- * A peer that is also in a consensus session.
- * Note that 'this' peer is not in the list.
+/**
+ * Complete information about the current round and all
+ * subrounds.
  */
-struct ConsensusPeer
+struct RoundInfo
 {
-  struct GNUNET_PeerIdentity *peer_id;
-
   /**
-   * Incoming tunnel from the peer.
+   * The current main round.
    */
-  struct GNUNET_MESH_Tunnel *incoming_tunnel;
-
-  struct InvertibleBloomFilter *last_ibf;
-
+  enum ConsensusRound round;
+  /**
+   * The current exp round, valid if
+   * the main round is an exp round.
+   */
+  uint32_t exp_round;
+  /**
+   * The current exp subround, valid if
+   * the main round is an exp round.
+   */
+  uint32_t exp_subround;
 };
 
 
@@ -97,117 +126,139 @@ struct ConsensusSession
    */
   struct ConsensusSession *prev;
 
-  /**
-   * Local consensus identification, chosen by clients.
-   */
-  struct GNUNET_HashCode *local_id;
   /**
   * Global consensus identification, computed
-  * from the local id and participating authorities.
+  * from the session id and participating authorities.
   */
-  struct GNUNET_HashCode *global_id;
+  struct GNUNET_HashCode global_id;
 
   /**
-   * Local client in this consensus session.
-   * There is only one client per consensus session.
+   * Client that inhabits the session
    */
   struct GNUNET_SERVER_Client *client;
 
   /**
-   * Values in the consensus set of this session,
-   * all of them either have been sent by or approved by the client.
+   * Queued messages to the client.
    */
-  struct GNUNET_CONTAINER_MultiHashMap *values;
+  struct GNUNET_MQ_Handle *client_mq;
 
   /**
-   * Elements that have not been sent to the client yet.
+   * Timeout for all rounds together, single rounds will schedule a timeout task
+   * with a fraction of the conclude timeout.
+   * Only valid once the current round is not CONSENSUS_ROUND_BEGIN.
+   */
+  struct GNUNET_TIME_Relative conclude_timeout;
+  
+  /**
+   * Timeout task identifier for the current round.
    */
-  struct PendingElement *transmit_pending_head;
+  GNUNET_SCHEDULER_TaskIdentifier round_timeout_tid;
 
   /**
-   * Elements that have not been sent to the client yet.
+   * Number of other peers in the consensus.
    */
-  struct PendingElement *transmit_pending_tail;
+  unsigned int num_peers;
 
   /**
-   * Elements that have not been approved (or rejected) by the client yet.
+   * Information about the other peers,
+   * their state, etc.
    */
-  struct PendingElement *approval_pending_head;
+  struct ConsensusPeerInformation *info;
 
   /**
-   * Elements that have not been approved (or rejected) by the client yet.
+   * Index of the local peer in the peers array
    */
-  struct PendingElement *approval_pending_tail;
+  unsigned int local_peer_idx;
 
   /**
-   * Currently active transmit handle for sending to the client
+   * Current round
    */
-  struct GNUNET_SERVER_TransmitHandle *th;
+  enum ConsensusRound current_round;
 
   /**
-   * Once conclude_requested is GNUNET_YES, the client may not
-   * insert any more values.
+   * Permutation of peers for the current round,
+   * maps logical index (for current round) to physical index (location in info array)
    */
-  int conclude_requested;
+  uint32_t *shuffle;
 
   /**
-   * Client has been informed about the conclusion.
+   * Current round of the exponential scheme.
    */
-  int conclude_sent;
+  uint32_t exp_round;
 
   /**
-   * Minimum number of peers to form a consensus group
+   * Current sub-round of the exponential scheme.
    */
-  int conclude_group_min;
+  uint32_t exp_subround;
 
   /**
-   * Current round of the conclusion
+   * The partner for the current exp-round
    */
-  int current_round;
+  struct ConsensusPeerInformation *partner_outgoing;
 
   /**
-   * Soft deadline for conclude.
-   * Speed up the speed of the consensus at the cost of consensus quality, as
-   * the time approached or crosses the deadline.
+   * The partner for the current exp-round
    */
-  struct GNUNET_TIME_Absolute conclude_deadline;
+  struct ConsensusPeerInformation *partner_incoming;
 
   /**
-   * Number of other peers in the consensus
+   * The consensus set of this session.
    */
-  unsigned int num_peers;
+  struct GNUNET_SET_Handle *element_set;
 
   /**
-   * Other peers in the consensus, array of ConsensusPeer
+   * Listener for requests from other peers.
+   * Uses the session's global id as app id.
    */
-  struct ConsensusPeer *peers;
+  struct GNUNET_SET_ListenHandle *set_listener;
+};
 
+
+/**
+ * Information about a peer that is in a consensus session.
+ */
+struct ConsensusPeerInformation
+{
   /**
-   * Tunnel for broadcasting to all other authorities
+   * Peer identitty of the peer in the consensus session
    */
-  struct GNUNET_MESH_Tunnel *broadcast_tunnel;
+  struct GNUNET_PeerIdentity peer_id;
 
   /**
-   * Time limit for one round of pairwise exchange.
-   * FIXME: should not actually be a constant
+   * Back-reference to the consensus session,
+   * to that ConsensusPeerInformation can be used as a closure
    */
-  struct GNUNET_TIME_Relative round_time;
+  struct ConsensusSession *session;
 
   /**
-   * Task identifier for the round timeout task
+   * We have finishes the exp-subround with the peer.
    */
-  GNUNET_SCHEDULER_TaskIdentifier round_timeout_tid;
+  int exp_subround_finished;
+
+  /**
+   * Set operation we are currently executing with this peer.
+   */
+  struct GNUNET_SET_OperationHandle *set_op;
+
+  /**
+   * Set operation we are planning on executing with this peer.
+   */
+  struct GNUNET_SET_OperationHandle *delayed_set_op;
+
+  /**
+   * Info about the round of the delayed set operation.
+   */
+  struct RoundInfo delayed_round_info;
 };
 
 
 /**
- * Linked list of sesstions this peer participates in.
+ * Linked list of sessions this peer participates in.
  */
 static struct ConsensusSession *sessions_head;
 
 /**
- * Linked list of sesstions this peer participates in.
+ * Linked list of sessions this peer participates in.
  */
 static struct ConsensusSession *sessions_tail;
 
@@ -222,572 +273,945 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
 static struct GNUNET_SERVER_Handle *srv;
 
 /**
- * Peer that runs this service
+ * Peer that runs this service.
  */
-static struct GNUNET_PeerIdentity *my_peer;
+static struct GNUNET_PeerIdentity my_peer;
 
-/**
- * Handle to the mesh service.
- */
-static struct GNUNET_MESH_Handle *mesh;
 
-/**
- * Handle to the core service. Only used during service startup, will be NULL after that.
- */
-static struct GNUNET_CORE_Handle *core;
-
-static void
-disconnect_client (struct GNUNET_SERVER_Client *client)
+static int
+have_exp_subround_finished (const struct ConsensusSession *session)
 {
-  GNUNET_SERVER_client_disconnect (client);
-  /* FIXME: free data structures that this client owns */
+  int not_finished;
+  not_finished = 0;
+  if ( (NULL != session->partner_outgoing) && 
+       (GNUNET_NO == session->partner_outgoing->exp_subround_finished) )
+    not_finished++;
+  if ( (NULL != session->partner_incoming) &&
+       (GNUNET_NO == session->partner_incoming->exp_subround_finished) )
+    not_finished++;
+  if (0 == not_finished)
+    return GNUNET_YES;
+  return GNUNET_NO;
 }
 
+
+/**
+ * Destroy a session, free all resources associated with it.
+ * 
+ * @param session the session to destroy
+ */
 static void
-compute_global_id (struct GNUNET_HashCode *dst,
-                   const struct GNUNET_HashCode *local_id,
-                   const struct GNUNET_PeerIdentity *peers,
-                   int num_peers)
+destroy_session (struct ConsensusSession *session)
 {
   int i;
-  struct GNUNET_HashCode tmp;
 
-  *dst = *local_id;
-  for (i = 0; i < num_peers; ++i)
+  GNUNET_CONTAINER_DLL_remove (sessions_head, sessions_tail, session);
+  if (NULL != session->element_set)
+  {
+    GNUNET_SET_destroy (session->element_set);
+    session->element_set = NULL;
+  }
+  if (NULL != session->set_listener)
   {
-    GNUNET_CRYPTO_hash_xor (dst, &peers[0].hashPubKey, &tmp);
-    *dst = tmp;
-    GNUNET_CRYPTO_hash (dst, sizeof (struct GNUNET_PeerIdentity), &tmp);
-    *dst = tmp;
+    GNUNET_SET_listen_cancel (session->set_listener);
+    session->set_listener = NULL;
   }
+  if (NULL != session->client_mq)
+  {
+    GNUNET_MQ_destroy (session->client_mq);
+    session->client_mq = NULL;
+  }
+  if (NULL != session->client)
+  {
+    GNUNET_SERVER_client_disconnect (session->client);
+    session->client = NULL;
+  }
+  if (NULL != session->shuffle)
+  {
+    GNUNET_free (session->shuffle);
+    session->shuffle = NULL;
+  }
+  if (NULL != session->info)
+  {
+    for (i = 0; i < session->num_peers; i++)
+    {
+      struct ConsensusPeerInformation *cpi;
+      cpi = &session->info[i];
+      if (NULL != cpi->set_op)
+      {
+        GNUNET_SET_operation_cancel (cpi->set_op);
+        cpi->set_op = NULL;
+      }
+    }
+    GNUNET_free (session->info);
+    session->info = NULL;
+  }
+  GNUNET_free (session);
 }
 
 
-static size_t
-transmit_pending (void *cls, size_t size, void *buf)
+/**
+ * Iterator for set elements.
+ *
+ * @param cls closure
+ * @param element the current element, NULL if all elements have been
+ *        iterated over
+ * @return GNUNET_YES to continue iterating, GNUNET_NO to stop.
+ */
+static int
+send_to_client_iter (void *cls,
+                     const struct GNUNET_SET_Element *element)
 {
-  struct GNUNET_CONSENSUS_Element *element;
-  struct GNUNET_CONSENSUS_ElementMessage *msg;
-  struct ConsensusSession *session;
-
-  session = (struct ConsensusSession *) cls;
-  msg = (struct GNUNET_CONSENSUS_ElementMessage *) buf;
-  element = session->transmit_pending_head->element;
-
-  GNUNET_assert (NULL != element);
-
-  session->th = NULL;
-
-  msg->element_type = element->type;
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT);
-  msg->header.size = htons (sizeof (struct GNUNET_CONSENSUS_ElementMessage) + element->size);
-  memcpy (&msg[1], element->data, element->size);
-
-  session->transmit_pending_head = session->transmit_pending_head->next;
+  struct ConsensusSession *session = cls;
+  struct GNUNET_MQ_Envelope *ev;
 
-  send_next (session);
-
-  return sizeof (struct GNUNET_CONSENSUS_ElementMessage) + element->size;
-}
-
-
-static size_t
-transmit_conclude_done (void *cls, size_t size, void *buf)
-{
-  struct GNUNET_CONSENSUS_ConcludeDoneMessage *msg;
+  if (NULL != element)
+  {
+    struct GNUNET_CONSENSUS_ElementMessage *m;
 
-  msg = (struct GNUNET_CONSENSUS_ConcludeDoneMessage *) buf;
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE_DONE);
-  msg->header.size = htons (sizeof (struct GNUNET_CONSENSUS_ConcludeDoneMessage));
-  msg->num_peers = htons (0);
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d: got element for client\n",
+                session->local_peer_idx);
 
-  return sizeof (struct GNUNET_CONSENSUS_ConcludeDoneMessage);
+    ev = GNUNET_MQ_msg_extra (m, element->size, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT);
+    m->element_type = htons (element->type);
+    memcpy (&m[1], element->data, element->size);
+    GNUNET_MQ_send (session->client_mq, ev);
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d: finished iterating elements for client\n",
+                session->local_peer_idx);
+    ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE_DONE);
+    GNUNET_MQ_send (session->client_mq, ev);
+  }
+  return GNUNET_YES;
 }
 
 
 /**
- * Schedule sending the next message (if there is any) to a client.
+ * Start the next round.
+ * This function can be invoked as a timeout task, or called manually (tc will be NULL then).
  *
- * @param cli the client to send the next message to
+ * @param cls the session
+ * @param tc task context, for when this task is invoked by the scheduler,
+ *           NULL if invoked for another reason
  */
-static void
-send_next (struct ConsensusSession *session)
+static void 
+round_over (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  int msize;
-
-  GNUNET_assert (NULL != session);
+  struct ConsensusSession *session;
 
-  if (NULL != session->th)
-  {
+  /* don't kick off next round if we're shutting down */
+  if ((NULL != tc) && (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
-  }
 
-  if ((session->conclude_requested == GNUNET_YES) && (session->conclude_sent == GNUNET_NO))
+  session = cls;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d: round over\n", session->local_peer_idx);
+
+  if (session->round_timeout_tid != GNUNET_SCHEDULER_NO_TASK)
   {
-    /* FIXME */
-    msize = sizeof (struct GNUNET_CONSENSUS_ConcludeMessage);
-    session->th =
-        GNUNET_SERVER_notify_transmit_ready (session->client, msize,
-                                             GNUNET_TIME_UNIT_FOREVER_REL, &transmit_conclude_done, session);
-    session->conclude_sent = GNUNET_YES;
+    GNUNET_SCHEDULER_cancel (session->round_timeout_tid);
+    session->round_timeout_tid = GNUNET_SCHEDULER_NO_TASK;
   }
-  else if (NULL != session->transmit_pending_head)
+
+  switch (session->current_round)
   {
-    msize = session->transmit_pending_head->element->size + sizeof (struct GNUNET_CONSENSUS_ElementMessage);
-    session->th =
-        GNUNET_SERVER_notify_transmit_ready (session->client, msize,
-                                             GNUNET_TIME_UNIT_FOREVER_REL, &transmit_pending, session);
-    /* TODO: insert into ack pending */
+    case CONSENSUS_ROUND_BEGIN:
+      session->current_round = CONSENSUS_ROUND_EXCHANGE;
+      session->exp_round = 0;
+      subround_over (session, NULL);
+      break;
+    case CONSENSUS_ROUND_EXCHANGE:
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d: finished, sending elements to client\n",
+                  session->local_peer_idx);
+      session->current_round = CONSENSUS_ROUND_FINISH;
+      GNUNET_SET_iterate (session->element_set, send_to_client_iter, session);
+      break;
+    default:
+      GNUNET_assert (0);
   }
 }
 
 
 /**
- * Method called whenever a peer has disconnected from the tunnel.
- * Implementations of this callback must NOT call
- * GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
- * to run in some other task later.  However, calling 
- * "GNUNET_MESH_notify_transmit_ready_cancel" is allowed.
+ * Create a new permutation for the session's peers in session->shuffle.
+ * Uses a Fisher-Yates shuffle with pseudo-randomness coming from
+ * both the global session id and the current round index.
  *
- * @param cls closure
- * @param peer peer identity the tunnel stopped working with
+ * @param session the session to create the new permutation for
  */
 static void
-disconnect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
+shuffle (struct ConsensusSession *session)
 {
-  /* FIXME: how do we handle this */
-}
+  uint32_t i;
+  uint32_t randomness[session->num_peers-1];
 
+  if (NULL == session->shuffle)
+    session->shuffle = GNUNET_malloc (session->num_peers * sizeof (*session->shuffle));
 
-/**
- * Method called whenever a peer has connected to the tunnel.
- *
- * @param cls closure
- * @param peer peer identity the tunnel was created to, NULL on timeout
- * @param atsi performance data for the connection
- */
-static void
-connect_handler (void *cls,
-                 const struct GNUNET_PeerIdentity *peer,
-                 const struct GNUNET_ATS_Information *atsi)
-{
-  /* not much we can do here, now we know the other peer has been added to our broadcast tunnel */
+  GNUNET_CRYPTO_kdf (randomness, sizeof (randomness), 
+                    &session->exp_round, sizeof (uint32_t),
+                     &session->global_id, sizeof (struct GNUNET_HashCode),
+                    NULL);
+
+  for (i = 0; i < session->num_peers; i++)
+    session->shuffle[i] = i;
+
+  for (i = session->num_peers - 1; i > 0; i--)
+  {
+    uint32_t x;
+    uint32_t tmp;
+    x = randomness[i-1] % session->num_peers;
+    tmp = session->shuffle[x];
+    session->shuffle[x] = session->shuffle[i];
+    session->shuffle[i] = tmp;
+  }
 }
 
 
 /**
- * Called when a client wants to join a consensus session.
+ * Find and set the partner_incoming and partner_outgoing of our peer,
+ * one of them may not exist (and thus set to NULL) if the number of peers
+ * in the session is not a power of two.
  *
- * @param cls unused
- * @param client client that sent the message
- * @param m message sent by the client
+ * @param session the consensus session
  */
 static void
-client_join (void *cls,
-             struct GNUNET_SERVER_Client *client,
-             const struct GNUNET_MessageHeader *m)
+find_partners (struct ConsensusSession *session)
 {
-  struct GNUNET_HashCode global_id;
-  const struct GNUNET_CONSENSUS_JoinMessage *msg;
-  struct ConsensusSession *session;
-  unsigned int i;
+  unsigned int arc;
+  unsigned int num_ghosts;
+  unsigned int largest_arc;
+  int partner_idx;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client joining\n");
+  /* shuffled local index */
+  int my_idx = session->shuffle[session->local_peer_idx];
 
-  msg = (struct GNUNET_CONSENSUS_JoinMessage *) m;
+  /* distance to neighboring peer in current subround */
+  arc = 1 << session->exp_subround;
+  largest_arc = 1;
+  while (largest_arc < session->num_peers)
+    largest_arc <<= 1;
+  num_ghosts = largest_arc - session->num_peers;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "session id is %s\n", GNUNET_h2s (&msg->session_id));
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "num ghosts: %d\n", num_ghosts);
 
-  compute_global_id (&global_id, &msg->session_id, (struct GNUNET_PeerIdentity *) &m[1], msg->num_peers);
-
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "computed global id is %s\n", GNUNET_h2s (&global_id));
-
-  session = sessions_head;
-  while (NULL != session)
+  if (0 == (my_idx & arc))
   {
-    if (client == session->client)
+    /* we are outgoing */
+    partner_idx = (my_idx + arc) % session->num_peers;
+    session->partner_outgoing = &session->info[session->shuffle[partner_idx]];
+    session->partner_outgoing->exp_subround_finished = GNUNET_NO;
+    /* are we a 'ghost' of a peer that would exist if
+     * the number of peers was a power of two, and thus have to partner
+     * with an additional peer?
+     */
+    if (my_idx < num_ghosts)
     {
-
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client already in session\n");
-      disconnect_client (client);
+      int ghost_partner_idx;
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "my index %d, arc %d, peers %u\n", my_idx, arc, session->num_peers);
+      ghost_partner_idx = (my_idx - (int) arc) % (int) session->num_peers;
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ghost partner is before %d\n", ghost_partner_idx);
+      /* platform dependent; modulo sometimes returns negative values */
+      if (ghost_partner_idx < 0)
+        ghost_partner_idx += session->num_peers;
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ghost partner is after %d\n", ghost_partner_idx);
+      session->partner_incoming = &session->info[session->shuffle[ghost_partner_idx]];
+      session->partner_incoming->exp_subround_finished = GNUNET_NO;
       return;
     }
-    if (0 == memcmp (session->global_id, &global_id, sizeof (struct GNUNET_HashCode)))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "session already owned by another client\n");
-      disconnect_client (client);
-      return;
-    }
-    session = session->next;
+    session->partner_incoming = NULL;
+    return;
   }
+  partner_idx = (my_idx - (int) arc) % (int) session->num_peers;
+  if (partner_idx < 0)
+    partner_idx += session->num_peers;
+  session->partner_outgoing = NULL;
+  session->partner_incoming = &session->info[session->shuffle[partner_idx]];
+  session->partner_incoming->exp_subround_finished = GNUNET_NO;
+}
 
-  GNUNET_SERVER_client_keep (client);
-
-  /* session does not exist yet, create it */
-  session = GNUNET_malloc (sizeof (struct ConsensusSession));
-  session->local_id = GNUNET_memdup (&msg->session_id, sizeof (struct GNUNET_HashCode));
-  session->global_id = GNUNET_memdup (&global_id, sizeof (struct GNUNET_HashCode));
-  session->values = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
-  session->client = client;
-  /* FIXME: should not be a constant, but chosen adaptively */
-  session->round_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5);
 
-  session->broadcast_tunnel = GNUNET_MESH_tunnel_create (mesh, session, connect_handler, disconnect_handler, session);
+/**
+ * Callback for set operation results. Called for each element
+ * in the result set.
+ *
+ * @param cls closure
+ * @param element a result element, only valid if status is GNUNET_SET_STATUS_OK
+ * @param status see enum GNUNET_SET_Status
+ */
+static void 
+set_result_cb (void *cls,
+               const struct GNUNET_SET_Element *element,
+               enum GNUNET_SET_Status status)
+{
+  struct ConsensusPeerInformation *cpi = cls;
+  unsigned int remote_idx = cpi - cpi->session->info;
+  unsigned int local_idx = cpi->session->local_peer_idx;
 
-  session->num_peers = 0;
+  GNUNET_assert ((cpi == cpi->session->partner_outgoing) ||
+                 (cpi == cpi->session->partner_incoming));
 
-  /* count the peers that are not the local peer */
-  for (i = 0; i < msg->num_peers; i++)
+  switch (status)
   {
-    struct GNUNET_PeerIdentity *peers;
-    peers = (struct GNUNET_PeerIdentity *) &msg[1];
-    if (0 != memcmp (&peers[i], my_peer, sizeof (struct GNUNET_PeerIdentity)))
-      session->num_peers++;
+    case GNUNET_SET_STATUS_OK:
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: set result from P%u: element\n",
+                  local_idx, remote_idx);
+      break;
+    case GNUNET_SET_STATUS_FAILURE:
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: set result from P%u: failure\n",
+                  local_idx, remote_idx);
+      cpi->set_op = NULL;
+      return;
+    case GNUNET_SET_STATUS_HALF_DONE:
+    case GNUNET_SET_STATUS_DONE:
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: set result from P%u: done\n",
+                  local_idx, remote_idx);
+      cpi->exp_subround_finished = GNUNET_YES;
+      cpi->set_op = NULL;
+      if (have_exp_subround_finished (cpi->session) == GNUNET_YES)
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: all reconciliations of subround done\n",
+                    local_idx);
+        subround_over (cpi->session, NULL);
+      }
+      else
+      {
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: waiting for further set results\n",
+                    local_idx);
+      }
+      return;
+    default:
+      GNUNET_break (0);
+      return;
   }
 
-  session->peers = GNUNET_malloc (session->num_peers * sizeof (struct ConsensusPeer));
-
-  /* copy the peer identities and add peers to broadcast tunnel */
-  for (i = 0; i < msg->num_peers; i++)
+  switch (cpi->session->current_round)
   {
-    struct GNUNET_PeerIdentity *peers;
-    peers = (struct GNUNET_PeerIdentity *) &msg[1];
-    if (0 != memcmp (&peers[i], my_peer, sizeof (struct GNUNET_PeerIdentity)))
-    {
-      *session->peers->peer_id = peers[i];
-      GNUNET_MESH_peer_request_connect_add (session->broadcast_tunnel, &peers[i]);
-    }
+    case CONSENSUS_ROUND_EXCHANGE:
+      GNUNET_SET_add_element (cpi->session->element_set, element, NULL, NULL);
+      break;
+    default:
+      GNUNET_break (0);
+      return;
   }
+}
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "created new session\n");
-
-  GNUNET_CONTAINER_DLL_insert (sessions_head, sessions_tail, session);
 
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+/**
+ * Compare the round the session is in with the round of the given context message.
+ *
+ * @param session a consensus session
+ * @param round a round context message
+ * @return 0 if it's the same round, -1 if the session is in an earlier round,
+ *         1 if the session is in a later round
+ */
+static int
+rounds_compare (struct ConsensusSession *session,
+                struct RoundInfo* ri)
+{
+  if (session->current_round < ri->round)
+    return -1;
+  if (session->current_round > ri->round)
+    return 1;
+  if (session->current_round == CONSENSUS_ROUND_EXCHANGE)
+  {
+    if (session->exp_round < ri->exp_round)
+      return -1;
+    if (session->exp_round > ri->exp_round)
+      return 1;
+    if (session->exp_subround < ri->exp_subround)
+      return -1;
+    if (session->exp_subround < ri->exp_subround)
+      return 1;
+    return 0;
+  }
+  /* comparing rounds when we are not in a exp round */
+  GNUNET_assert (0);
 }
 
 
 /**
- * Called when a client performs an insert operation.
+ * Do the next subround in the exp-scheme.
+ * This function can be invoked as a timeout task, or called manually (tc will be NULL then).
+ *
+ * @param cls the session
+ * @param tc task context, for when this task is invoked by the scheduler,
+ *           NULL if invoked for another reason
  */
-void
-client_insert (void *cls,
-             struct GNUNET_SERVER_Client *client,
-             const struct GNUNET_MessageHeader *m)
+static void
+subround_over (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct ConsensusSession *session;
-  struct GNUNET_CONSENSUS_ElementMessage *msg;
-  struct GNUNET_CONSENSUS_Element *element;
-  struct GNUNET_HashCode key;
-  int element_size;
-
-  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "insert\n");
+  int i;
 
-  session = sessions_head;
-  while (NULL != session)
+  /* don't kick off next subround if we're shutting down */
+  if ((NULL != tc) && (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return;
+  session = cls;
+  /* cancel timeout */
+  if (session->round_timeout_tid != GNUNET_SCHEDULER_NO_TASK)
   {
-    if (session->client == client)
-      break;
+    GNUNET_SCHEDULER_cancel (session->round_timeout_tid);
+    session->round_timeout_tid = GNUNET_SCHEDULER_NO_TASK;
   }
-
-  if (NULL == session)
+  
+  if (session->exp_round >= NUM_EXP_ROUNDS)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to insert, but client is not in any session\n");
-    GNUNET_SERVER_client_disconnect (client);
+    round_over (session, NULL);
     return;
   }
 
-  msg = (struct GNUNET_CONSENSUS_ElementMessage *) m;
-  element_size = msg->header.size - sizeof (struct GNUNET_CONSENSUS_ElementMessage);
+  if (session->exp_round == 0)
+  {
+    /* initialize everything for the log-rounds */
+    session->exp_round = 1;
+    session->exp_subround = 0;
+    if (NULL == session->shuffle)
+      session->shuffle = GNUNET_malloc ((sizeof (int)) * session->num_peers);
+    for (i = 0; i < session->num_peers; i++)
+      session->shuffle[i] = i;
+  }
+  else if (session->exp_subround + 1 >= (int) ceil (log2 (session->num_peers)))
+  {
+    /* subrounds done, start new log-round */
+    session->exp_round++;
+    session->exp_subround = 0;
+    //shuffle (session);
+  }
+  else 
+  {
+    session->exp_subround++;
+  }
 
-  element = GNUNET_malloc (sizeof (struct GNUNET_CONSENSUS_Element) + element_size);
+  /* determine the incoming and outgoing partner */
+  find_partners (session);
 
-  element->type = msg->element_type;
-  element->size = element_size;
-  memcpy (&element[1], &msg[1], element_size);
-  element->data = &element[1];
+  GNUNET_assert (session->partner_outgoing != &session->info[session->local_peer_idx]);
+  GNUNET_assert (session->partner_incoming != &session->info[session->local_peer_idx]);
 
-  GNUNET_CRYPTO_hash (element, element_size, &key);
+  /* initiate set operation with the outgoing partner */
+  if (NULL != session->partner_outgoing)
+  {
+    struct GNUNET_CONSENSUS_RoundContextMessage *msg;
+    msg = GNUNET_new (struct GNUNET_CONSENSUS_RoundContextMessage);
+    msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ROUND_CONTEXT);
+    msg->header.size = htons (sizeof *msg);
+    msg->round = htonl (session->current_round);
+    msg->exp_round = htonl (session->exp_round);
+    msg->exp_subround = htonl (session->exp_subround);
+
+    if (NULL != session->partner_outgoing->set_op)
+    {
+      GNUNET_SET_operation_cancel (session->partner_outgoing->set_op);
+    }
+    session->partner_outgoing->set_op =
+        GNUNET_SET_prepare (&session->partner_outgoing->peer_id,
+                            &session->global_id,
+                            (struct GNUNET_MessageHeader *) msg,
+                            0, /* FIXME: salt */
+                            GNUNET_SET_RESULT_ADDED,
+                            set_result_cb, session->partner_outgoing);
+    GNUNET_free (msg);
+    GNUNET_SET_commit (session->partner_outgoing->set_op, session->element_set);
+  }
 
-  GNUNET_CONTAINER_multihashmap_put (session->values, &key, element,
-                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
+  /* commit to the delayed set operation */
+  if ((NULL != session->partner_incoming) && (NULL != session->partner_incoming->delayed_set_op))
+  {
+    int cmp = rounds_compare (session, &session->partner_incoming->delayed_round_info);
 
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+    if (NULL != session->partner_incoming->set_op)
+    {
+      GNUNET_SET_operation_cancel (session->partner_incoming->set_op);
+      session->partner_incoming->set_op = NULL;
+    }
+    if (cmp == 0)
+    {
+      GNUNET_SET_commit (session->partner_incoming->delayed_set_op, session->element_set);
+      session->partner_incoming->set_op = session->partner_incoming->delayed_set_op;
+      session->partner_incoming->delayed_set_op = NULL;
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d resumed delayed round with P%d\n",
+                  session->local_peer_idx, (int) (session->partner_incoming - session->info));
+    }
+    else
+    {
+      /* this should not happen -- a round has been skipped! */
+      GNUNET_break_op (0);
+    }
+  }
+
+#ifdef GNUNET_EXTRA_LOGGING
+  {
+    int in;
+    int out;
+    if (session->partner_outgoing == NULL)
+      out = -1;
+    else
+      out = (int) (session->partner_outgoing - session->info);
+    if (session->partner_incoming == NULL)
+      in = -1;
+    else
+      in = (int) (session->partner_incoming - session->info);
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: doing exp-round, r=%d, sub=%d, in: %d, out: %d\n", session->local_peer_idx,
+                session->exp_round, session->exp_subround, in, out);
+  }
+#endif /* GNUNET_EXTRA_LOGGING */
 
-  send_next (session);
 }
 
 
 /**
- * Do one round of the conclusion.
- * Start by broadcasting the set difference estimator (IBF strata).
+ * Search peer in the list of peers in session.
  *
+ * @param peer peer to find
+ * @param session session with peer
+ * @return index of peer, -1 if peer is not in session
  */
-void
-conclude_do_round (struct ConsensusSession *session)
+static int
+get_peer_idx (const struct GNUNET_PeerIdentity *peer, const struct ConsensusSession *session)
 {
-  /* FIXME */
+  int i;
+  for (i = 0; i < session->num_peers; i++)
+    if (0 == memcmp (peer, &session->info[i].peer_id, sizeof *peer))
+      return i;
+  return -1;
 }
 
 
 /**
- * Cancel the current round if necessary, decide to run another round or
- * terminate.
+ * Compute a global, (hopefully) unique consensus session id,
+ * from the local id of the consensus session, and the identities of all participants.
+ * Thus, if the local id of two consensus sessions coincide, but are not comprised of
+ * exactly the same peers, the global id will be different.
+ *
+ * @param session session to generate the global id for
+ * @param session_id local id of the consensus session
  */
-void
-conclude_round_done (struct ConsensusSession *session)
+static void
+compute_global_id (struct ConsensusSession *session, const struct GNUNET_HashCode *session_id)
 {
-  /* FIXME */
+  int i;
+  struct GNUNET_HashCode tmp;
+
+  /* FIXME: use kdf? */
+
+  session->global_id = *session_id;
+  for (i = 0; i < session->num_peers; ++i)
+  {
+    GNUNET_CRYPTO_hash_xor (&session->global_id, &session->info[i].peer_id.hashPubKey, &tmp);
+    session->global_id = tmp;
+    GNUNET_CRYPTO_hash (&session->global_id, sizeof (struct GNUNET_PeerIdentity), &tmp);
+    session->global_id = tmp;
+  }
 }
 
 
 /**
- * Called when a client performs the conclude operation.
+ * Although GNUNET_CRYPTO_hash_cmp exisits, it does not have
+ * the correct signature to be used with e.g. qsort.
+ * We use this function instead.
+ *
+ * @param h1 some hash code
+ * @param h2 some hash code
+ * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
  */
-void
-client_conclude (void *cls,
-             struct GNUNET_SERVER_Client *client,
-             const struct GNUNET_MessageHeader *message)
+static int
+hash_cmp (const void *h1, const void *h2)
 {
-  struct ConsensusSession *session;
+  return GNUNET_CRYPTO_hash_cmp ((struct GNUNET_HashCode *) h1, (struct GNUNET_HashCode *) h2);
+}
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "conclude requested\n");
 
-  session = sessions_head;
-  while ((session != NULL) && (session->client != client))
+/**
+ * Create the sorted list of peers for the session,
+ * add the local peer if not in the join message.
+ */
+static void
+initialize_session_peer_list (struct ConsensusSession *session,
+                              struct GNUNET_CONSENSUS_JoinMessage *join_msg)
+{
+  unsigned int local_peer_in_list;
+  uint32_t listed_peers;
+  const struct GNUNET_PeerIdentity *msg_peers;
+  struct GNUNET_PeerIdentity *peers;
+  unsigned int i;
+
+  GNUNET_assert (NULL != join_msg);
+
+  /* peers in the join message, may or may not include the local peer */
+  listed_peers = ntohl (join_msg->num_peers);
+  
+  session->num_peers = listed_peers;
+
+  msg_peers = (struct GNUNET_PeerIdentity *) &join_msg[1];
+
+  local_peer_in_list = GNUNET_NO;
+  for (i = 0; i < listed_peers; i++)
   {
-    session = session->next;
+    if (0 == memcmp (&msg_peers[i], &my_peer, sizeof (struct GNUNET_PeerIdentity)))
+    {
+      local_peer_in_list = GNUNET_YES;
+      break;
+    }
   }
-  if (NULL == session)
+
+  if (GNUNET_NO == local_peer_in_list)
+    session->num_peers++;
+
+  peers = GNUNET_malloc (session->num_peers * sizeof (struct GNUNET_PeerIdentity));
+
+  if (GNUNET_NO == local_peer_in_list)
+    peers[session->num_peers - 1] = my_peer;
+
+  memcpy (peers, msg_peers, listed_peers * sizeof (struct GNUNET_PeerIdentity));
+  qsort (peers, session->num_peers, sizeof (struct GNUNET_PeerIdentity), &hash_cmp);
+
+  session->info = GNUNET_malloc (session->num_peers * sizeof (struct ConsensusPeerInformation));
+
+  for (i = 0; i < session->num_peers; ++i)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client not found\n");
-    GNUNET_SERVER_client_disconnect (client);
-    return;
+    /* initialize back-references, so consensus peer information can
+     * be used as closure */
+    session->info[i].session = session;
+    session->info[i].peer_id = peers[i];
   }
 
-  if (GNUNET_YES == session->conclude_requested)
+  GNUNET_free (peers);
+}
+
+
+/**
+ * Called when another peer wants to do a set operation with the
+ * local peer.
+ *
+ * @param cls closure
+ * @param other_peer the other peer
+ * @param context_msg message with application specific information from
+ *        the other peer
+ * @param request request from the other peer, use GNUNET_SET_accept
+ *        to accept it, otherwise the request will be refused
+ *        Note that we don't use a return value here, as it is also
+ *        necessary to specify the set we want to do the operation with,
+ *        whith sometimes can be derived from the context message.
+ *        Also necessary to specify the timeout.
+ */
+static void
+set_listen_cb (void *cls,
+               const struct GNUNET_PeerIdentity *other_peer,
+               const struct GNUNET_MessageHeader *context_msg,
+               struct GNUNET_SET_Request *request)
+{
+  struct ConsensusSession *session = cls;
+  struct GNUNET_CONSENSUS_RoundContextMessage *msg = (struct GNUNET_CONSENSUS_RoundContextMessage *) context_msg;
+  struct ConsensusPeerInformation *cpi;
+  struct GNUNET_SET_OperationHandle *set_op;
+  struct RoundInfo round_info;
+  int index;
+  int cmp;
+
+  if (NULL == context_msg)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client requested conclude twice\n");
-    GNUNET_SERVER_client_disconnect (client);
+    GNUNET_break_op (0);
     return;
   }
 
-  session->conclude_requested = GNUNET_YES;
+  index = get_peer_idx (other_peer, session);
 
-  conclude_do_round (session);
+  if (index < 0)
+  {
+    GNUNET_break_op (0);
+    return;
+  }
 
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+  round_info.round = ntohl (msg->round);
+  round_info.exp_round = ntohl (msg->exp_round);
+  round_info.exp_subround = ntohl (msg->exp_subround);
 
-  send_next (session);
-}
+  cpi = &session->info[index];
 
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d got set request from P%d\n", session->local_peer_idx, index);
 
-/**
- * Called when a client sends an ack
- */
-void
-client_ack (void *cls,
-             struct GNUNET_SERVER_Client *client,
-             const struct GNUNET_MessageHeader *message)
-{
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client ack received\n");
+  switch (session->current_round)
+  {
+    case CONSENSUS_ROUND_EXCHANGE:
+      cmp = rounds_compare (session, &round_info);
+      if (cmp > 0)
+      {
+        /* the other peer is too late */
+        GNUNET_break_op (0);
+        return;
+      }
+      /* kill old request, if any. this is legal,
+       * as the other peer would not make a new request if it would want to
+       * complete the old one! */
+      if (NULL != cpi->set_op)
+      {
+        GNUNET_SET_operation_cancel (cpi->set_op);
+        cpi->set_op = NULL;
+      }
+      set_op = GNUNET_SET_accept (request, GNUNET_SET_RESULT_ADDED,
+                                       set_result_cb, &session->info[index]);
+      if (cmp == 0)
+      {
+        cpi->set_op = set_op;
+        GNUNET_SET_commit (set_op, session->element_set);
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d commited to set request from P%d\n", session->local_peer_idx, index);
+      }
+      else
+      {
+        /* if there's a exp subround running, mark it as finished, as the set op has been canceled! */
+        cpi->delayed_set_op = set_op;
+        cpi->delayed_round_info = round_info;
+        cpi->exp_subround_finished = GNUNET_YES;
+        GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%d delaying set request from P%d\n", session->local_peer_idx, index);
+      }
+      break;
+    default:
+      GNUNET_break_op (0);
+      return;
+  }
 }
 
+
 /**
- * Task that disconnects from core.
+ * Initialize the session, continue receiving messages from the owning client
  *
- * @param cls core handle
- * @param tc context information (why was this task triggered now)
+ * @param session the session to initialize
+ * @param join_msg the join message from the client
  */
 static void
-disconnect_core (void *cls,
-                 const struct GNUNET_SCHEDULER_TaskContext *tc)
+initialize_session (struct ConsensusSession *session,
+                    struct GNUNET_CONSENSUS_JoinMessage *join_msg)
 {
-  GNUNET_CORE_disconnect (core);
-  core = NULL;
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "disconnected from core\n");
-}
+  struct ConsensusSession *other_session;
 
+  initialize_session_peer_list (session, join_msg);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "session with %u peers\n", session->num_peers);
+  compute_global_id (session, &join_msg->session_id);
 
-static void
-core_startup (void *cls,
-              struct GNUNET_CORE_Handle *core,
-              const struct GNUNET_PeerIdentity *peer)
-{
-  static const struct GNUNET_SERVER_MessageHandler handlers[] = {
-    {&client_join, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_JOIN, 0},
-    {&client_insert, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_INSERT, 0},
-    {&client_conclude, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE,
-        sizeof (struct GNUNET_CONSENSUS_ConcludeMessage)},
-    {&client_ack, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_ACK,
-        sizeof (struct GNUNET_CONSENSUS_AckMessage)},
-    {NULL, NULL, 0, 0}
-  };
+  /* check if some local client already owns the session.
+   * it is only legal to have a session with an existing global id
+   * if all other sessions with this global id are finished.*/
+  other_session = sessions_head;
+  while (NULL != other_session)
+  {
+    if ((other_session != session) && 
+        (0 == GNUNET_CRYPTO_hash_cmp (&session->global_id, &other_session->global_id)))
+    {
+      if (CONSENSUS_ROUND_FINISH != other_session->current_round)
+      {
+        GNUNET_break (0);
+        destroy_session (session);
+        return;
+      }
+      break;
+    }
+    other_session = other_session->next;
+  }
 
-  GNUNET_SERVER_add_handlers (srv, handlers);
-  my_peer = GNUNET_memdup(peer, sizeof (struct GNUNET_PeerIdentity));
-  /* core can't be disconnected directly in the core startup callback, schedule a task to do it! */
-  GNUNET_SCHEDULER_add_now (&disconnect_core, core);
-  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "connected to core\n");
+  session->local_peer_idx = get_peer_idx (&my_peer, session);
+  GNUNET_assert (-1 != session->local_peer_idx);
+  session->element_set = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
+  GNUNET_assert (NULL != session->element_set);
+  session->set_listener = GNUNET_SET_listen (cfg, GNUNET_SET_OPERATION_UNION,
+                                             &session->global_id,
+                                             set_listen_cb, session);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%d is the local peer\n", session->local_peer_idx);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "session %s initialized\n", GNUNET_h2s (&session->global_id));
 }
 
 
-
-/**
- * Method called whenever another peer has added us to a tunnel
- * 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.
- *
- * @param cls closure
- * @param tunnel new handle to the tunnel
- * @param initiator peer that started the tunnel
- * @param atsi performance information for the tunnel
- * @return initial tunnel context for the tunnel
- *         (can be NULL -- that's not an error)
- */
-static void *
-new_tunnel (void *cls,
-            struct GNUNET_MESH_Tunnel *tunnel,
-            const struct GNUNET_PeerIdentity *initiator,
-            const struct GNUNET_ATS_Information *atsi)
+static struct ConsensusSession *
+get_session_by_client (struct GNUNET_SERVER_Client *client)
 {
-  /* there's nothing we can do here, as we don't have the global consensus id yet */
+  struct ConsensusSession *session;
+
+  session = sessions_head;
+  while (NULL != session)
+  {
+    if (session->client == client)
+      return session;
+    session = session->next;
+  }
   return NULL;
 }
 
 
 /**
- * Function called whenever an inbound tunnel is destroyed.  Should clean up
- * any associated state.  This function is NOT called if the client has
- * explicitly asked for the tunnel to be destroyed using
- * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
- * the tunnel.
+ * Called when a client wants to join a consensus session.
  *
- * @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 unused
+ * @param client client that sent the message
+ * @param m message sent by the client
  */
 static void
-cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
+client_join (void *cls,
+             struct GNUNET_SERVER_Client *client,
+             const struct GNUNET_MessageHeader *m)
 {
-  /* FIXME: what to do here? */
-}
+  struct ConsensusSession *session;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "join message sent by client\n");
+
+  session = get_session_by_client (client);
+  if (NULL != session)
+  {
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
+  }
+  session = GNUNET_new (struct ConsensusSession);
+  session->client = client;
+  session->client_mq = GNUNET_MQ_queue_for_server_client (client);
+  GNUNET_CONTAINER_DLL_insert (sessions_head, sessions_tail, session);
+  initialize_session (session, (struct GNUNET_CONSENSUS_JoinMessage *) m);
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "join done\n");
+}
 
 
 /**
- * Called to clean up, after a shutdown has been requested.
+ * Called when a client performs an insert operation.
  *
- * @param cls closure
- * @param tc context information (why was this task triggered now)
+ * @param cls (unused)
+ * @param client client handle
+ * @param m message sent by the client
  */
-static void
-shutdown_task (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+void
+client_insert (void *cls,
+               struct GNUNET_SERVER_Client *client,
+               const struct GNUNET_MessageHeader *m)
 {
-  /* mesh requires all the tunnels to be destroyed manually */
-  while (NULL != sessions_head)
+  struct ConsensusSession *session;
+  struct GNUNET_CONSENSUS_ElementMessage *msg;
+  struct GNUNET_SET_Element *element;
+  ssize_t element_size;
+
+  session = get_session_by_client (client);
+
+  if (NULL == session)
   {
-    struct ConsensusSession *session;
-    session = sessions_head;
-    GNUNET_MESH_tunnel_destroy (sessions_head->broadcast_tunnel);
-    sessions_head = sessions_head->next;
-    GNUNET_free (session);
+    GNUNET_break (0);
+    GNUNET_SERVER_client_disconnect (client);
+    return;
   }
 
-  if (NULL != mesh)
+  if (CONSENSUS_ROUND_BEGIN != session->current_round)
   {
-    GNUNET_MESH_disconnect (mesh);
-    mesh = NULL;
+    GNUNET_break (0);
+    GNUNET_SERVER_client_disconnect (client);
+    return;
   }
-  if (NULL != core)
+
+  msg = (struct GNUNET_CONSENSUS_ElementMessage *) m;
+  element_size = ntohs (msg->header.size) - sizeof (struct GNUNET_CONSENSUS_ElementMessage);
+  if (element_size < 0)
   {
-    GNUNET_CORE_disconnect (core);
-    core = NULL;
+    GNUNET_break (0);
+    return;
   }
-}
 
+  element = GNUNET_malloc (sizeof (struct GNUNET_SET_Element) + element_size);
+  element->type = msg->element_type;
+  element->size = element_size;
+  memcpy (&element[1], &msg[1], element_size);
+  element->data = &element[1];
+  GNUNET_SET_add_element (session->element_set, element, NULL, NULL);
+  GNUNET_free (element);
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "P%u: element added\n", session->local_peer_idx);
+}
 
 
 /**
- * Functions with this signature are called whenever a message is
- * received.
+ * Called when a client performs the conclude operation.
  *
- * @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
- * @param sender who sent the message
- * @param message the actual message
- * @param atsi performance data for the connection
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
+ * @param cls (unused)
+ * @param client client handle
+ * @param message message sent by the client
  */
-static int
-p2p_delta_estimate (void *cls,
-                    struct GNUNET_MESH_Tunnel * tunnel,
-                    void **tunnel_ctx,
-                    const struct GNUNET_PeerIdentity *sender,
-                    const struct GNUNET_MessageHeader *message,
-                    const struct GNUNET_ATS_Information *atsi)
+static void
+client_conclude (void *cls,
+                 struct GNUNET_SERVER_Client *client,
+                 const struct GNUNET_MessageHeader *message)
 {
-  /* FIXME */
-  return GNUNET_OK;
+  struct ConsensusSession *session;
+  struct GNUNET_CONSENSUS_ConcludeMessage *cmsg;
+
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "conclude requested\n");
+  cmsg = (struct GNUNET_CONSENSUS_ConcludeMessage *) message;
+  session = get_session_by_client (client);
+  if (NULL == session)
+  {
+    /* client not found */
+    GNUNET_break (0);
+    GNUNET_SERVER_client_disconnect (client);
+    return;
+  }
+  if (CONSENSUS_ROUND_BEGIN != session->current_round)
+  {
+    /* client requested conclude twice */
+    GNUNET_break (0);
+    return;
+  }
+  if (session->num_peers <= 1)
+  {
+    /* FIXME: what to do here? */
+    //send_client_conclude_done (session);
+  }
+  else
+  {
+    session->conclude_timeout = GNUNET_TIME_relative_ntoh (cmsg->timeout);
+    /* the 'begin' round is over, start with the next, actual round */
+    round_over (session, NULL);
+  }
+
+  GNUNET_assert (CONSENSUS_ROUND_BEGIN != session->current_round);
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
 
 /**
- * Functions with this signature are called whenever a message is
- * received.
+ * Called to clean up, after a shutdown has been requested.
  *
- * @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
- * @param sender who sent the message
- * @param message the actual message
- * @param atsi performance data for the connection
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
+ * @param cls closure
+ * @param tc context information (why was this task triggered now)
  */
-static int
-p2p_difference_digest (void *cls,
-                       struct GNUNET_MESH_Tunnel * tunnel,
-                       void **tunnel_ctx,
-                       const struct GNUNET_PeerIdentity *sender,
-                       const struct GNUNET_MessageHeader *message,
-                       const struct GNUNET_ATS_Information *atsi)
+static void
+shutdown_task (void *cls,
+               const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  /* FIXME */
-  return GNUNET_OK;
+  while (NULL != sessions_head)
+    destroy_session (sessions_head);
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "handled shutdown request\n");
 }
 
 
 /**
- * Functions with this signature are called whenever a message is
- * received.
+ * Clean up after a client after it is
+ * disconnected (either by us or by itself)
  *
- * @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
- * @param sender who sent the message
- * @param message the actual message
- * @param atsi performance data for the connection
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
+ * @param cls closure, unused
+ * @param client the client to clean up after
  */
-static int
-p2p_elements_and_requests (void *cls,
-                           struct GNUNET_MESH_Tunnel * tunnel,
-                           void **tunnel_ctx,
-                           const struct GNUNET_PeerIdentity *sender,
-                           const struct GNUNET_MessageHeader *message,
-                           const struct GNUNET_ATS_Information *atsi)
+void
+handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
 {
-  /* FIXME */
-  return GNUNET_OK;
+  struct ConsensusSession *session;
+
+  session = get_session_by_client (client);
+  if (NULL == session)
+    return;
+  if ((CONSENSUS_ROUND_BEGIN == session->current_round) ||
+      (CONSENSUS_ROUND_FINISH == session->current_round))
+    destroy_session (session);
+  else
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client disconnected, but waiting for consensus to finish\n");
 }
 
 
@@ -799,35 +1223,30 @@ p2p_elements_and_requests (void *cls,
  * @param c configuration to use
  */
 static void
-run (void *cls, struct GNUNET_SERVER_Handle *server, const struct GNUNET_CONFIGURATION_Handle *c)
+run (void *cls, struct GNUNET_SERVER_Handle *server,
+     const struct GNUNET_CONFIGURATION_Handle *c)
 {
-  static const struct GNUNET_CORE_MessageHandler handlers[] = {
-    {NULL, 0, 0}
-  };
-  static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
-    {p2p_delta_estimate, GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_DELTA_ESTIMATE, 0},
-    {p2p_difference_digest, GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_DIFFERENCE_DIGEST, 0},
-    {p2p_elements_and_requests, GNUNET_MESSAGE_TYPE_CONSENSUS_P2P_ELEMENTS_AND_REQUESTS, 0},
-    {NULL, 0, 0}
-  };
-  static const GNUNET_MESH_ApplicationType app_types[] = { 
-    GNUNET_APPLICATION_TYPE_CONSENSUS,
-    GNUNET_APPLICATION_TYPE_END
+  static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
+    {&client_conclude, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE,
+        sizeof (struct GNUNET_CONSENSUS_ConcludeMessage)},
+    {&client_insert, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_INSERT, 0},
+    {&client_join, NULL, GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_JOIN, 0},
+    {NULL, NULL, 0, 0}
   };
 
-  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "consensus running\n");
-
   cfg = c;
   srv = server;
-
+  if (GNUNET_OK != GNUNET_CRYPTO_get_host_identity (cfg, &my_peer))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not retrieve host identity\n");
+    GNUNET_break (0);
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  GNUNET_SERVER_add_handlers (server, server_handlers);
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
-
-  mesh = GNUNET_MESH_connect (cfg, NULL, new_tunnel, cleaner, mesh_handlers, app_types);
-  GNUNET_assert (NULL != mesh);
-
-  /* we have to wait for the core_startup callback before proceeding with the consensus service startup */
-  core = GNUNET_CORE_connect (c, NULL, &core_startup, NULL, NULL, NULL, GNUNET_NO, NULL, GNUNET_NO, handlers);
-  GNUNET_assert (NULL != core);
+  GNUNET_SERVER_disconnect_notify (server, handle_client_disconnect, NULL);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "consensus running\n");
 }
 
 
@@ -843,6 +1262,7 @@ main (int argc, char *const *argv)
 {
   int ret;
   ret = GNUNET_SERVICE_run (argc, argv, "consensus", GNUNET_SERVICE_OPTION_NONE, &run, NULL);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit (%d)\n", GNUNET_OK != ret);
   return (GNUNET_OK == ret) ? 0 : 1;
 }