diff --git a/brandt.c b/brandt.c index 6c334b8..b175c3e 100644 --- a/brandt.c +++ b/brandt.c @@ -49,6 +49,41 @@ BRANDT_init (struct GNUNET_CRYPTO_EccDlogContext *dlogctx) } +static void +advance_round (struct BRANDT_Auction *auction, enum auction_type atype, enum outcome_type outcome) +{ + unsigned char *buf; + size_t buflen; + + /* if we got the current round message from all participants, advance to + * next round */ + for (uint16_t i = 0; i < auction->n; i++) + if (!gcry_mpi_test_bit (auction->round_progress, i)) + return; + + gcry_mpi_clear_highbit (auction->round_progress, 0); + if (msg_last == ++(auction->cur_round)) + { + } + + if (!handler_out[atype][outcome][auction->cur_round] || + !(buf = handler_out[atype][outcome][auction->cur_round](auction, &buflen))) + { + /** \todo */ + weprintf ("wow fail out"); + return; + } + + /** \todo: add msgtype header in the handler_out functions */ + + /* last message only sent to seller, others are broadcasted */ + if (msg_decrypt == auction->cur_round) + auction->ucast (auction->closure, buf, buflen); + else + auction->bcast (auction->closure, buf, buflen); +} + + void BRANDT_got_message (struct BRANDT_Auction *auction, uint16_t sender, @@ -56,8 +91,8 @@ BRANDT_got_message (struct BRANDT_Auction *auction, size_t msg_len) { uint16_t mtype = *(uint16_t *)msg; - int atype; - int outcome; + enum auction_type atype; + enum outcome_type outcome; enum rounds round = auction->cur_round; atype = auction->desc->m > 0 ? auction_mPlusFirstPrice : auction_firstPrice; @@ -65,6 +100,13 @@ BRANDT_got_message (struct BRANDT_Auction *auction, /** \todo: cache out of order messages */ + /* check if we already got that round message from the same user */ + if (gcry_mpi_test_bit (auction->round_progress, sender)) + { + weprintf ("got a duplicate message from user %d", sender); + return; + } + if (!handler_in[atype][outcome][round] || !handler_in[atype][outcome][round](auction, msg + sizeof (mtype), @@ -72,6 +114,10 @@ BRANDT_got_message (struct BRANDT_Auction *auction, sender)) { /** \todo */ - weprintf ("wow fail"); + weprintf ("wow fail in"); + return; } + gcry_mpi_set_bit (auction->round_progress, sender); + + advance_round (auction, atype, outcome); } diff --git a/brandt.h b/brandt.h index fe50896..ec3c959 100644 --- a/brandt.h +++ b/brandt.h @@ -170,6 +170,10 @@ BRANDT_free (); /** * Receive a message related to a specific auction. * + * If the message is from a future round and cannot be processed yet, it is + * cached in RAM. It will not be stored on disc until it can be used, since the + * messages completing the previous round should all arrive relatively soon. + * * @param[in] auction The pointer returned by BRANDT_join() or BRANDT_new() from * which message @a msg was received. * @param[in] sender The id of the sender. diff --git a/internals.h b/internals.h index 656e798..ce67934 100644 --- a/internals.h +++ b/internals.h @@ -79,6 +79,8 @@ struct BRANDT_DescrP { struct BRANDT_Auction { struct BRANDT_DescrP *desc; /** pointer to the auction information */ + void *closure; /** auction closure given by the user */ + BRANDT_CbBroadcast bcast; /** broadcast callback */ BRANDT_CbUnicast ucast; /** unicast callback */ BRANDT_CbResult result; /** result reporting callback */