+/**
+ * Handle a request from the client to
+ * copy a set.
+ *
+ * @param cls unused
+ * @param client the client
+ * @param mh the message
+ */
+static void
+handle_client_copy_lazy_prepare (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *mh)
+{
+ struct Set *set;
+ struct LazyCopyRequest *cr;
+ struct GNUNET_MQ_Envelope *ev;
+ struct GNUNET_SET_CopyLazyResponseMessage *resp_msg;
+
+ set = set_get (client);
+ if (NULL == set)
+ {
+ /* client without a set requested an operation */
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+
+ cr = GNUNET_new (struct LazyCopyRequest);
+
+ cr->cookie = lazy_copy_cookie;
+ lazy_copy_cookie += 1;
+ cr->source_set = set;
+
+ GNUNET_CONTAINER_DLL_insert (lazy_copy_head,
+ lazy_copy_tail,
+ cr);
+
+
+ ev = GNUNET_MQ_msg (resp_msg,
+ GNUNET_MESSAGE_TYPE_SET_COPY_LAZY_RESPONSE);
+ resp_msg->cookie = cr->cookie;
+ GNUNET_MQ_send (set->client_mq, ev);
+
+
+ GNUNET_SERVER_receive_done (client,
+ GNUNET_OK);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Client requested lazy copy\n");
+}
+
+
+/**
+ * Handle a request from the client to
+ * connect to a copy of a set.
+ *
+ * @param cls unused
+ * @param client the client
+ * @param mh the message
+ */
+static void
+handle_client_copy_lazy_connect (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *mh)
+{
+ struct LazyCopyRequest *cr;
+ const struct GNUNET_SET_CopyLazyConnectMessage *msg =
+ (const struct GNUNET_SET_CopyLazyConnectMessage *) mh;
+ struct Set *set;
+ int found;
+
+ if (NULL != set_get (client))
+ {
+ /* There can only be one set per client */
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+
+ found = GNUNET_NO;
+
+ for (cr = lazy_copy_head; NULL != cr; cr = cr->next)
+ {
+ if (cr->cookie == msg->cookie)
+ {
+ found = GNUNET_YES;
+ break;
+ }
+ }
+
+ if (GNUNET_NO == found)
+ {
+ /* client asked for copy with cookie we don't know */
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+
+ GNUNET_CONTAINER_DLL_remove (lazy_copy_head,
+ lazy_copy_tail,
+ cr);
+
+ set = GNUNET_new (struct Set);
+
+ switch (cr->source_set->operation)
+ {
+ case GNUNET_SET_OPERATION_INTERSECTION:
+ set->vt = _GSS_intersection_vt ();
+ break;
+ case GNUNET_SET_OPERATION_UNION:
+ set->vt = _GSS_union_vt ();
+ break;
+ default:
+ GNUNET_assert (0);
+ return;
+ }
+
+ if (NULL == set->vt->copy_state) {
+ /* Lazy copy not supported for this set operation */
+ GNUNET_break (0);
+ GNUNET_free (set);
+ GNUNET_free (cr);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+
+ set->operation = cr->source_set->operation;
+ set->state = set->vt->copy_state (cr->source_set);
+ set->content = cr->source_set->content;
+ set->content->refcount += 1;
+
+ set->current_generation = cr->source_set->current_generation;
+ set->excluded_generations_size = cr->source_set->excluded_generations_size;
+ set->excluded_generations = GNUNET_memdup (cr->source_set->excluded_generations,
+ set->excluded_generations_size * sizeof (struct GenerationRange));
+
+ /* Advance the generation of the new set, so that mutations to the
+ of the cloned set and the source set are independent. */
+ advance_generation (set);
+
+
+ set->client = client;
+ set->client_mq = GNUNET_MQ_queue_for_server_client (client);
+ GNUNET_CONTAINER_DLL_insert (sets_head,
+ sets_tail,
+ set);
+
+ GNUNET_free (cr);
+
+ GNUNET_SERVER_receive_done (client,
+ GNUNET_OK);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Client connected to lazy set\n");
+}
+
+