aboutsummaryrefslogtreecommitdiff
path: root/src/set/set_api.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2014-11-27 14:05:52 +0000
committerChristian Grothoff <christian@grothoff.org>2014-11-27 14:05:52 +0000
commit76ae17553cef81a92984a00e8401ca6ec8ddb856 (patch)
tree1d407f5e665d2b863be8522bdf102df6a57be13d /src/set/set_api.c
parente98619f67a3e1ddf4962c20f77c7a1a669345eaa (diff)
downloadgnunet-76ae17553cef81a92984a00e8401ca6ec8ddb856.tar.gz
gnunet-76ae17553cef81a92984a00e8401ca6ec8ddb856.zip
-simplify logic
Diffstat (limited to 'src/set/set_api.c')
-rw-r--r--src/set/set_api.c115
1 files changed, 63 insertions, 52 deletions
diff --git a/src/set/set_api.c b/src/set/set_api.c
index b919f6e02..9bab38c8b 100644
--- a/src/set/set_api.c
+++ b/src/set/set_api.c
@@ -344,54 +344,6 @@ handle_result (void *cls,
344 344
345 345
346/** 346/**
347 * Handle request message for a listen operation
348 *
349 * @param cls the listen handle
350 * @param mh the message
351 */
352static void
353handle_request (void *cls,
354 const struct GNUNET_MessageHeader *mh)
355{
356 const struct GNUNET_SET_RequestMessage *msg = (const struct GNUNET_SET_RequestMessage *) mh;
357 struct GNUNET_SET_ListenHandle *lh = cls;
358 struct GNUNET_SET_Request *req;
359 const struct GNUNET_MessageHeader *context_msg;
360
361 LOG (GNUNET_ERROR_TYPE_DEBUG,
362 "processing operation request\n");
363 req = GNUNET_new (struct GNUNET_SET_Request);
364 req->accept_id = ntohl (msg->accept_id);
365 context_msg = GNUNET_MQ_extract_nested_mh (msg);
366 /* calling #GNUNET_SET_accept() in the listen cb will set req->accepted */
367 lh->listen_cb (lh->listen_cls, &msg->peer_id, context_msg, req);
368
369 /* we got another request => reset the backoff */
370 lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
371
372 if (GNUNET_NO == req->accepted)
373 {
374 struct GNUNET_MQ_Envelope *mqm;
375 struct GNUNET_SET_RejectMessage *rmsg;
376
377 mqm = GNUNET_MQ_msg (rmsg,
378 GNUNET_MESSAGE_TYPE_SET_REJECT);
379 rmsg->accept_reject_id = msg->accept_id;
380 GNUNET_MQ_send (lh->mq, mqm);
381 LOG (GNUNET_ERROR_TYPE_DEBUG,
382 "rejecting request\n");
383 }
384 GNUNET_free (req);
385
386 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 "processed op request from service\n");
388
389 /* the accept-case is handled in GNUNET_SET_accept,
390 * as we have the accept message available there */
391}
392
393
394/**
395 * Destroy the given set operation. 347 * Destroy the given set operation.
396 * 348 *
397 * @param oh set operation to destroy 349 * @param oh set operation to destroy
@@ -499,8 +451,12 @@ GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
499 enum GNUNET_SET_OperationType op) 451 enum GNUNET_SET_OperationType op)
500{ 452{
501 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = { 453 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
502 { &handle_result, GNUNET_MESSAGE_TYPE_SET_RESULT, 0}, 454 { &handle_result,
503 { &handle_iter_element, GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT, 0}, 455 GNUNET_MESSAGE_TYPE_SET_RESULT,
456 0 },
457 { &handle_iter_element,
458 GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT,
459 0 },
504 { &handle_iter_done, 460 { &handle_iter_done,
505 GNUNET_MESSAGE_TYPE_SET_ITER_DONE, 461 GNUNET_MESSAGE_TYPE_SET_ITER_DONE,
506 sizeof (struct GNUNET_MessageHeader) }, 462 sizeof (struct GNUNET_MessageHeader) },
@@ -702,6 +658,61 @@ listen_connect (void *cls,
702 658
703 659
704/** 660/**
661 * Handle request message for a listen operation
662 *
663 * @param cls the listen handle
664 * @param mh the message
665 */
666static void
667handle_request (void *cls,
668 const struct GNUNET_MessageHeader *mh)
669{
670 struct GNUNET_SET_ListenHandle *lh = cls;
671 const struct GNUNET_SET_RequestMessage *msg;
672 struct GNUNET_SET_Request req;
673 const struct GNUNET_MessageHeader *context_msg;
674 uint16_t msize;
675 struct GNUNET_MQ_Envelope *mqm;
676 struct GNUNET_SET_RejectMessage *rmsg;
677
678 LOG (GNUNET_ERROR_TYPE_DEBUG,
679 "Processing incoming operation request\n");
680 msize = ntohs (mh->size);
681 if (msize < sizeof (struct GNUNET_SET_RequestMessage))
682 {
683 GNUNET_break (0);
684 GNUNET_CLIENT_disconnect (lh->client);
685 lh->client = NULL;
686 GNUNET_MQ_destroy (lh->mq);
687 lh->mq = NULL;
688 lh->reconnect_task = GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff,
689 &listen_connect, lh);
690 lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff);
691 return;
692 }
693 /* we got another valid request => reset the backoff */
694 lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
695 msg = (const struct GNUNET_SET_RequestMessage *) mh;
696 req.accept_id = ntohl (msg->accept_id);
697 req.accepted = GNUNET_NO;
698 context_msg = GNUNET_MQ_extract_nested_mh (msg);
699 /* calling #GNUNET_SET_accept() in the listen cb will set req->accepted */
700 lh->listen_cb (lh->listen_cls,
701 &msg->peer_id,
702 context_msg,
703 &req);
704 if (GNUNET_YES == req.accepted)
705 return; /* the accept-case is handled in #GNUNET_SET_accept() */
706 LOG (GNUNET_ERROR_TYPE_DEBUG,
707 "Rejecting request\n");
708 mqm = GNUNET_MQ_msg (rmsg,
709 GNUNET_MESSAGE_TYPE_SET_REJECT);
710 rmsg->accept_reject_id = msg->accept_id;
711 GNUNET_MQ_send (lh->mq, mqm);
712}
713
714
715/**
705 * Our connection with the set service encountered an error, 716 * Our connection with the set service encountered an error,
706 * re-initialize with exponential back-off. 717 * re-initialize with exponential back-off.
707 * 718 *
@@ -753,13 +764,13 @@ listen_connect (void *cls,
753 return; 764 return;
754 } 765 }
755 lh->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 766 lh->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
756
757 GNUNET_assert (NULL == lh->client); 767 GNUNET_assert (NULL == lh->client);
758 lh->client = GNUNET_CLIENT_connect ("set", lh->cfg); 768 lh->client = GNUNET_CLIENT_connect ("set", lh->cfg);
759 if (NULL == lh->client) 769 if (NULL == lh->client)
760 return; 770 return;
761 GNUNET_assert (NULL == lh->mq); 771 GNUNET_assert (NULL == lh->mq);
762 lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client, mq_handlers, 772 lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client,
773 mq_handlers,
763 &handle_client_listener_error, lh); 774 &handle_client_listener_error, lh);
764 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN); 775 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN);
765 msg->operation = htonl (lh->operation); 776 msg->operation = htonl (lh->operation);