+
+
+/**
+ * Called when a client wants to add or remove an element to a set it inhabits.
+ *
+ * @param cls unused
+ * @param client client that sent the message
+ * @param m message sent by the client
+ */
+static void
+handle_client_mutation (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *m)
+{
+ struct Set *set;
+
+ set = set_get (client);
+ if (NULL == set)
+ {
+ /* client without a set requested an operation */
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+
+ GNUNET_SERVER_receive_done (client,
+ GNUNET_OK);
+
+ if (0 != set->content->iterator_count)
+ {
+ struct PendingMutation *pm;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Scheduling mutation on set\n");
+
+ pm = GNUNET_new (struct PendingMutation);
+ pm->mutation_message = GNUNET_copy_message (m);
+ pm->set = set;
+ GNUNET_CONTAINER_DLL_insert (set->content->pending_mutations_head,
+ set->content->pending_mutations_tail,
+ pm);
+ return;
+ }
+
+ execute_mutation (set, m);
+}
+
+
+/**
+ * Advance the current generation of a set,
+ * adding exclusion ranges if necessary.
+ *
+ * @param set the set where we want to advance the generation
+ */
+static void
+advance_generation (struct Set *set)
+{
+ struct GenerationRange r;
+
+ if (set->current_generation == set->content->latest_generation)
+ {
+ set->content->latest_generation += 1;
+ set->current_generation += 1;
+ return;
+ }
+
+ GNUNET_assert (set->current_generation < set->content->latest_generation);
+
+ r.start = set->current_generation + 1;
+ r.end = set->content->latest_generation + 1;
+
+ set->content->latest_generation = r.end;
+ set->current_generation = r.end;
+
+ GNUNET_array_append (set->excluded_generations,
+ set->excluded_generations_size,
+ r);
+}
+
+/**
+ * Called when a client wants to initiate a set operation with another
+ * peer. Initiates the CADET connection to the listener and sends the
+ * request.
+ *
+ * @param cls unused
+ * @param client client that sent the message
+ * @param m message sent by the client
+ */
+static void
+handle_client_evaluate (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *m)
+{
+ struct Set *set;
+ const struct GNUNET_SET_EvaluateMessage *msg;
+ struct OperationSpecification *spec;
+ struct Operation *op;
+ const struct GNUNET_MessageHeader *context;
+
+ set = set_get (client);
+ if (NULL == set)
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+ msg = (const struct GNUNET_SET_EvaluateMessage *) m;
+ spec = GNUNET_new (struct OperationSpecification);
+ spec->operation = set->operation;
+ spec->app_id = msg->app_id;
+ spec->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
+ UINT32_MAX);
+ spec->peer = msg->target_peer;
+ spec->set = set;
+ spec->result_mode = ntohl (msg->result_mode);
+ spec->client_request_id = ntohl (msg->request_id);
+ context = GNUNET_MQ_extract_nested_mh (msg);
+ op = GNUNET_new (struct Operation);
+ op->spec = spec;
+
+ // Advance generation values, so that
+ // mutations won't interfer with the running operation.
+ op->generation_created = set->current_generation;
+ advance_generation (set);
+
+ op->vt = set->vt;
+ GNUNET_CONTAINER_DLL_insert (set->ops_head,
+ set->ops_tail,
+ op);
+ op->channel = GNUNET_CADET_channel_create (cadet,
+ op,
+ &msg->target_peer,
+ GNUNET_APPLICATION_TYPE_SET,
+ GNUNET_CADET_OPTION_RELIABLE);
+ op->mq = GNUNET_CADET_mq_create (op->channel);
+ set->vt->evaluate (op,
+ context);
+ GNUNET_SERVER_receive_done (client,
+ GNUNET_OK);
+}
+
+
+/**
+ * Handle an ack from a client, and send the next element. Note
+ * that we only expect acks for set elements, not after the
+ * #GNUNET_MESSAGE_TYPE_SET_ITER_DONE message.
+ *
+ * @param cls unused
+ * @param client the client
+ * @param m the message
+ */
+static void
+handle_client_iter_ack (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *m)
+{
+ const struct GNUNET_SET_IterAckMessage *ack;
+ struct Set *set;
+
+ set = set_get (client);
+ if (NULL == set)
+ {
+ /* client without a set acknowledged receiving a value */
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+ if (NULL == set->iter)
+ {
+ /* client sent an ack, but we were not expecting one (as
+ set iteration has finished) */
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+ ack = (const struct GNUNET_SET_IterAckMessage *) m;
+ GNUNET_SERVER_receive_done (client,
+ GNUNET_OK);
+ if (ntohl (ack->send_more))
+ {
+ send_client_element (set);
+ }
+ else
+ {
+ GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
+ set->iter = NULL;
+ set->iteration_id++;
+ }
+}
+
+
+/**
+ * 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");
+}
+
+
+/**
+ * Handle a request from the client to
+ * cancel a running set operation.
+ *
+ * @param cls unused
+ * @param client the client
+ * @param mh the message
+ */
+static void
+handle_client_cancel (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *mh)
+{
+ const struct GNUNET_SET_CancelMessage *msg =
+ (const struct GNUNET_SET_CancelMessage *) mh;
+ struct Set *set;
+ struct Operation *op;
+ int found;
+
+ set = set_get (client);
+ if (NULL == set)
+ {
+ /* client without a set requested an operation */
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Client requested cancel for op %u\n",
+ ntohl (msg->request_id));
+ found = GNUNET_NO;
+ for (op = set->ops_head; NULL != op; op = op->next)
+ {
+ if (op->spec->client_request_id == ntohl (msg->request_id))
+ {
+ found = GNUNET_YES;
+ break;
+ }
+ }
+ if (GNUNET_NO == found)
+ {
+ /* It may happen that the operation was already destroyed due to
+ * the other peer disconnecting. The client may not know about this
+ * yet and try to cancel the (just barely non-existent) operation.
+ * So this is not a hard error.
+ */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Client canceled non-existent op\n");
+ }
+ else
+ {
+ _GSS_operation_destroy (op,
+ GNUNET_YES);
+ }
+ GNUNET_SERVER_receive_done (client,
+ GNUNET_OK);
+}
+
+
+/**
+ * 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
+ * @param mh the message
+ */
+static void
+handle_client_accept (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *mh)
+{
+ struct Set *set;
+ const struct GNUNET_SET_AcceptMessage *msg;
+ struct Operation *op;
+ struct GNUNET_SET_ResultMessage *result_message;
+ struct GNUNET_MQ_Envelope *ev;
+
+ msg = (const struct GNUNET_SET_AcceptMessage *) mh;
+ set = set_get (client);
+ if (NULL == set)
+ {
+ /* client without a set requested to accept */
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+ op = get_incoming (ntohl (msg->accept_reject_id));
+ if (NULL == op)
+ {
+ /* It is not an error if the set op does not exist -- it may
+ * have been destroyed when the partner peer disconnected. */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Client accepted request that is no longer active\n");
+ ev = GNUNET_MQ_msg (result_message,
+ GNUNET_MESSAGE_TYPE_SET_RESULT);
+ result_message->request_id = msg->request_id;
+ result_message->element_type = 0;
+ result_message->result_status = htons (GNUNET_SET_STATUS_FAILURE);
+ GNUNET_MQ_send (set->client_mq, ev);
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return;
+ }
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Client accepting request %u\n",
+ ntohl (msg->accept_reject_id));
+ GNUNET_assert (GNUNET_YES == op->is_incoming);
+ op->is_incoming = GNUNET_NO;
+ GNUNET_CONTAINER_DLL_remove (incoming_head,
+ incoming_tail,
+ op);
+ op->spec->set = set;
+ GNUNET_CONTAINER_DLL_insert (set->ops_head,
+ set->ops_tail,
+ op);
+ op->spec->client_request_id = ntohl (msg->request_id);
+ op->spec->result_mode = ntohl (msg->result_mode);
+
+ // Advance generation values, so that
+ // mutations won't interfer with the running operation.
+ op->generation_created = set->current_generation;
+ advance_generation (set);
+
+ op->vt = set->vt;
+ op->vt->accept (op);
+ GNUNET_SERVER_receive_done (client,
+ GNUNET_OK);