diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-09-19 18:12:49 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-09-19 18:12:49 +0000 |
commit | 506899aa2be2b4d5dc09c1740969c28ddf43c82d (patch) | |
tree | 12815e3faeca1c535a67a601ecab014c871068b6 /src | |
parent | 1532728eb9a0fd858528a574ce27155436eaf76a (diff) | |
download | gnunet-506899aa2be2b4d5dc09c1740969c28ddf43c82d.tar.gz gnunet-506899aa2be2b4d5dc09c1740969c28ddf43c82d.zip |
converting nse to new service API
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_mq_lib.h | 4 | ||||
-rw-r--r-- | src/nse/gnunet-service-nse.c | 158 | ||||
-rw-r--r-- | src/util/mq.c | 88 |
3 files changed, 143 insertions, 107 deletions
diff --git a/src/include/gnunet_mq_lib.h b/src/include/gnunet_mq_lib.h index ff8c9ba1b..567624985 100644 --- a/src/include/gnunet_mq_lib.h +++ b/src/include/gnunet_mq_lib.h | |||
@@ -619,12 +619,12 @@ GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq, | |||
619 | * | 619 | * |
620 | * @param ev message to call the notify callback for | 620 | * @param ev message to call the notify callback for |
621 | * @param cb the notify callback | 621 | * @param cb the notify callback |
622 | * @param cls closure for the callback | 622 | * @param cb_cls closure for the callback |
623 | */ | 623 | */ |
624 | void | 624 | void |
625 | GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev, | 625 | GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev, |
626 | GNUNET_MQ_NotifyCallback cb, | 626 | GNUNET_MQ_NotifyCallback cb, |
627 | void *cls); | 627 | void *cb_cls); |
628 | 628 | ||
629 | 629 | ||
630 | /** | 630 | /** |
diff --git a/src/nse/gnunet-service-nse.c b/src/nse/gnunet-service-nse.c index bf7c79fcb..511f95514 100644 --- a/src/nse/gnunet-service-nse.c +++ b/src/nse/gnunet-service-nse.c | |||
@@ -286,7 +286,7 @@ static struct GNUNET_SCHEDULER_Task *proof_task; | |||
286 | /** | 286 | /** |
287 | * Notification context, simplifies client broadcasts. | 287 | * Notification context, simplifies client broadcasts. |
288 | */ | 288 | */ |
289 | static struct GNUNET_SERVER_NotificationContext *nc; | 289 | static struct GNUNET_NotificationContext *nc; |
290 | 290 | ||
291 | /** | 291 | /** |
292 | * The next major time. | 292 | * The next major time. |
@@ -313,11 +313,6 @@ static struct GNUNET_PeerIdentity my_identity; | |||
313 | */ | 313 | */ |
314 | static uint64_t my_proof; | 314 | static uint64_t my_proof; |
315 | 315 | ||
316 | /** | ||
317 | * Handle to this serivce's server. | ||
318 | */ | ||
319 | static struct GNUNET_SERVER_Handle *srv; | ||
320 | |||
321 | 316 | ||
322 | /** | 317 | /** |
323 | * Initialize a message to clients with the current network | 318 | * Initialize a message to clients with the current network |
@@ -328,8 +323,6 @@ static struct GNUNET_SERVER_Handle *srv; | |||
328 | static void | 323 | static void |
329 | setup_estimate_message (struct GNUNET_NSE_ClientMessage *em) | 324 | setup_estimate_message (struct GNUNET_NSE_ClientMessage *em) |
330 | { | 325 | { |
331 | unsigned int i; | ||
332 | unsigned int j; | ||
333 | double mean; | 326 | double mean; |
334 | double sum; | 327 | double sum; |
335 | double std_dev; | 328 | double std_dev; |
@@ -350,9 +343,10 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em) | |||
350 | sum = 0.0; | 343 | sum = 0.0; |
351 | sumweight = 0.0; | 344 | sumweight = 0.0; |
352 | variance = 0.0; | 345 | variance = 0.0; |
353 | for (i = 0; i < estimate_count; i++) | 346 | for (unsigned int i = 0; i < estimate_count; i++) |
354 | { | 347 | { |
355 | j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE; | 348 | unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE; |
349 | |||
356 | val = htonl (size_estimate_messages[j].matching_bits); | 350 | val = htonl (size_estimate_messages[j].matching_bits); |
357 | weight = estimate_count + 1 - i; | 351 | weight = estimate_count + 1 - i; |
358 | 352 | ||
@@ -375,9 +369,10 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em) | |||
375 | variance = 0.0; | 369 | variance = 0.0; |
376 | mean = 0.0; | 370 | mean = 0.0; |
377 | 371 | ||
378 | for (i = 0; i < estimate_count; i++) | 372 | for (unsigned int i = 0; i < estimate_count; i++) |
379 | { | 373 | { |
380 | j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE; | 374 | unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE; |
375 | |||
381 | val = htonl (size_estimate_messages[j].matching_bits); | 376 | val = htonl (size_estimate_messages[j].matching_bits); |
382 | sum += val; | 377 | sum += val; |
383 | vsq += val * val; | 378 | vsq += val * val; |
@@ -399,20 +394,22 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em) | |||
399 | em->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE); | 394 | em->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE); |
400 | em->reserved = htonl (0); | 395 | em->reserved = htonl (0); |
401 | em->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ()); | 396 | em->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ()); |
402 | double se = mean - 0.332747; | 397 | { |
403 | j = GNUNET_CONTAINER_multipeermap_size (peers); | 398 | double se = mean - 0.332747; |
404 | if (0 == j) | 399 | unsigned int j = GNUNET_CONTAINER_multipeermap_size (peers); |
405 | j = 1; /* Avoid log2(0); can only happen if CORE didn't report | 400 | if (0 == j) |
406 | connection to self yet */ | 401 | j = 1; /* Avoid log2(0); can only happen if CORE didn't report |
407 | nsize = log2 (j); | 402 | connection to self yet */ |
408 | em->size_estimate = GNUNET_hton_double (GNUNET_MAX (se, | 403 | nsize = log2 (j); |
409 | nsize)); | 404 | em->size_estimate = GNUNET_hton_double (GNUNET_MAX (se, |
410 | em->std_deviation = GNUNET_hton_double (std_dev); | 405 | nsize)); |
411 | GNUNET_STATISTICS_set (stats, | 406 | em->std_deviation = GNUNET_hton_double (std_dev); |
412 | "# nodes in the network (estimate)", | 407 | GNUNET_STATISTICS_set (stats, |
413 | (uint64_t) pow (2, GNUNET_MAX (se, | 408 | "# nodes in the network (estimate)", |
414 | nsize)), | 409 | (uint64_t) pow (2, GNUNET_MAX (se, |
415 | GNUNET_NO); | 410 | nsize)), |
411 | GNUNET_NO); | ||
412 | } | ||
416 | } | 413 | } |
417 | 414 | ||
418 | 415 | ||
@@ -422,28 +419,28 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em) | |||
422 | * Also, we remember the client for updates upon future | 419 | * Also, we remember the client for updates upon future |
423 | * estimate measurements. | 420 | * estimate measurements. |
424 | * | 421 | * |
425 | * @param cls unused | 422 | * @param cls client who sent the message |
426 | * @param client who sent the message | ||
427 | * @param message the message received | 423 | * @param message the message received |
428 | */ | 424 | */ |
429 | static void | 425 | static void |
430 | handle_start_message (void *cls, | 426 | handle_start (void *cls, |
431 | struct GNUNET_SERVER_Client *client, | 427 | const struct GNUNET_MessageHeader *message) |
432 | const struct GNUNET_MessageHeader *message) | ||
433 | { | 428 | { |
429 | struct GNUNET_SERVICE_Client *client = cls; | ||
430 | struct GNUNET_MQ_Handle *mq; | ||
434 | struct GNUNET_NSE_ClientMessage em; | 431 | struct GNUNET_NSE_ClientMessage em; |
432 | struct GNUNET_MQ_Envelope *env; | ||
435 | 433 | ||
436 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 434 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
437 | "Received START message from client\n"); | 435 | "Received START message from client\n"); |
438 | GNUNET_SERVER_notification_context_add (nc, | 436 | mq = GNUNET_SERVICE_client_get_mq (client); |
439 | client); | 437 | GNUNET_notification_context_add (nc, |
438 | mq); | ||
440 | setup_estimate_message (&em); | 439 | setup_estimate_message (&em); |
441 | GNUNET_SERVER_notification_context_unicast (nc, | 440 | env = GNUNET_MQ_msg_copy (&em.header); |
442 | client, | 441 | GNUNET_MQ_send (mq, |
443 | &em.header, | 442 | env); |
444 | GNUNET_YES); | 443 | GNUNET_SERVICE_client_continue (client); |
445 | GNUNET_SERVER_receive_done (client, | ||
446 | GNUNET_OK); | ||
447 | } | 444 | } |
448 | 445 | ||
449 | 446 | ||
@@ -676,9 +673,9 @@ update_network_size_estimate () | |||
676 | struct GNUNET_NSE_ClientMessage em; | 673 | struct GNUNET_NSE_ClientMessage em; |
677 | 674 | ||
678 | setup_estimate_message (&em); | 675 | setup_estimate_message (&em); |
679 | GNUNET_SERVER_notification_context_broadcast (nc, | 676 | GNUNET_notification_context_broadcast (nc, |
680 | &em.header, | 677 | &em.header, |
681 | GNUNET_YES); | 678 | GNUNET_YES); |
682 | } | 679 | } |
683 | 680 | ||
684 | 681 | ||
@@ -1292,7 +1289,7 @@ handle_core_disconnect (void *cls, | |||
1292 | GNUNET_CONTAINER_multipeermap_remove (peers, | 1289 | GNUNET_CONTAINER_multipeermap_remove (peers, |
1293 | peer, | 1290 | peer, |
1294 | pos)); | 1291 | pos)); |
1295 | if (pos->transmit_task != NULL) | 1292 | if (NULL != pos->transmit_task) |
1296 | { | 1293 | { |
1297 | GNUNET_SCHEDULER_cancel (pos->transmit_task); | 1294 | GNUNET_SCHEDULER_cancel (pos->transmit_task); |
1298 | pos->transmit_task = NULL; | 1295 | pos->transmit_task = NULL; |
@@ -1344,7 +1341,7 @@ shutdown_task (void *cls) | |||
1344 | } | 1341 | } |
1345 | if (NULL != nc) | 1342 | if (NULL != nc) |
1346 | { | 1343 | { |
1347 | GNUNET_SERVER_notification_context_destroy (nc); | 1344 | GNUNET_notification_context_destroy (nc); |
1348 | nc = NULL; | 1345 | nc = NULL; |
1349 | } | 1346 | } |
1350 | if (NULL != core_api) | 1347 | if (NULL != core_api) |
@@ -1476,19 +1473,14 @@ status_cb (void *cls, | |||
1476 | * Handle network size estimate clients. | 1473 | * Handle network size estimate clients. |
1477 | * | 1474 | * |
1478 | * @param cls closure | 1475 | * @param cls closure |
1479 | * @param server the initialized server | ||
1480 | * @param c configuration to use | 1476 | * @param c configuration to use |
1477 | * @param service the initialized service | ||
1481 | */ | 1478 | */ |
1482 | static void | 1479 | static void |
1483 | run (void *cls, | 1480 | run (void *cls, |
1484 | struct GNUNET_SERVER_Handle *server, | 1481 | const struct GNUNET_CONFIGURATION_Handle *c, |
1485 | const struct GNUNET_CONFIGURATION_Handle *c) | 1482 | struct GNUNET_SERVICE_Handle *service) |
1486 | { | 1483 | { |
1487 | static const struct GNUNET_SERVER_MessageHandler handlers[] = { | ||
1488 | {&handle_start_message, NULL, GNUNET_MESSAGE_TYPE_NSE_START, | ||
1489 | sizeof (struct GNUNET_MessageHeader)}, | ||
1490 | {NULL, NULL, 0, 0} | ||
1491 | }; | ||
1492 | struct GNUNET_MQ_MessageHandler core_handlers[] = { | 1484 | struct GNUNET_MQ_MessageHandler core_handlers[] = { |
1493 | GNUNET_MQ_hd_fixed_size (p2p_estimate, | 1485 | GNUNET_MQ_hd_fixed_size (p2p_estimate, |
1494 | GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD, | 1486 | GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD, |
@@ -1500,7 +1492,6 @@ run (void *cls, | |||
1500 | struct GNUNET_CRYPTO_EddsaPrivateKey *pk; | 1492 | struct GNUNET_CRYPTO_EddsaPrivateKey *pk; |
1501 | 1493 | ||
1502 | cfg = c; | 1494 | cfg = c; |
1503 | srv = server; | ||
1504 | if (GNUNET_OK != | 1495 | if (GNUNET_OK != |
1505 | GNUNET_CONFIGURATION_get_value_time (cfg, | 1496 | GNUNET_CONFIGURATION_get_value_time (cfg, |
1506 | "NSE", | 1497 | "NSE", |
@@ -1614,9 +1605,7 @@ run (void *cls, | |||
1614 | 1605 | ||
1615 | peers = GNUNET_CONTAINER_multipeermap_create (128, | 1606 | peers = GNUNET_CONTAINER_multipeermap_create (128, |
1616 | GNUNET_YES); | 1607 | GNUNET_YES); |
1617 | GNUNET_SERVER_add_handlers (srv, | 1608 | nc = GNUNET_notification_context_create (1); |
1618 | handlers); | ||
1619 | nc = GNUNET_SERVER_notification_context_create (srv, 1); | ||
1620 | /* Connect to core service and register core handlers */ | 1609 | /* Connect to core service and register core handlers */ |
1621 | core_api = GNUNET_CORE_connecT (cfg, /* Main configuration */ | 1610 | core_api = GNUNET_CORE_connecT (cfg, /* Main configuration */ |
1622 | NULL, /* Closure passed to functions */ | 1611 | NULL, /* Closure passed to functions */ |
@@ -1635,26 +1624,55 @@ run (void *cls, | |||
1635 | 1624 | ||
1636 | 1625 | ||
1637 | /** | 1626 | /** |
1638 | * The main function for the network size estimation service. | 1627 | * Callback called when a client connects to the service. |
1639 | * | 1628 | * |
1640 | * @param argc number of arguments from the command line | 1629 | * @param cls closure for the service |
1641 | * @param argv command line arguments | 1630 | * @param c the new client that connected to the service |
1642 | * @return 0 ok, 1 on error | 1631 | * @param mq the message queue used to send messages to the client |
1632 | * @return @a c | ||
1643 | */ | 1633 | */ |
1644 | int | 1634 | static void * |
1645 | main (int argc, | 1635 | client_connect_cb (void *cls, |
1646 | char *const *argv) | 1636 | struct GNUNET_SERVICE_Client *c, |
1637 | struct GNUNET_MQ_Handle *mq) | ||
1647 | { | 1638 | { |
1648 | return (GNUNET_OK == | 1639 | return c; |
1649 | GNUNET_SERVICE_run (argc, | ||
1650 | argv, | ||
1651 | "nse", | ||
1652 | GNUNET_SERVICE_OPTION_NONE, | ||
1653 | &run, | ||
1654 | NULL)) ? 0 : 1; | ||
1655 | } | 1640 | } |
1656 | 1641 | ||
1657 | 1642 | ||
1643 | /** | ||
1644 | * Callback called when a client disconnected from the service | ||
1645 | * | ||
1646 | * @param cls closure for the service | ||
1647 | * @param c the client that disconnected | ||
1648 | * @param internal_cls should be equal to @a c | ||
1649 | */ | ||
1650 | static void | ||
1651 | client_disconnect_cb (void *cls, | ||
1652 | struct GNUNET_SERVICE_Client *c, | ||
1653 | void *internal_cls) | ||
1654 | { | ||
1655 | GNUNET_assert (c == internal_cls); | ||
1656 | } | ||
1657 | |||
1658 | |||
1659 | /** | ||
1660 | * Define "main" method using service macro. | ||
1661 | */ | ||
1662 | GNUNET_SERVICE_MAIN | ||
1663 | ("nse", | ||
1664 | GNUNET_SERVICE_OPTION_NONE, | ||
1665 | &run, | ||
1666 | &client_connect_cb, | ||
1667 | &client_disconnect_cb, | ||
1668 | NULL, | ||
1669 | GNUNET_MQ_hd_fixed_size (start, | ||
1670 | GNUNET_MESSAGE_TYPE_NSE_START, | ||
1671 | struct GNUNET_MessageHeader, | ||
1672 | NULL), | ||
1673 | GNUNET_MQ_handler_end ()); | ||
1674 | |||
1675 | |||
1658 | #if defined(LINUX) && defined(__GLIBC__) | 1676 | #if defined(LINUX) && defined(__GLIBC__) |
1659 | #include <malloc.h> | 1677 | #include <malloc.h> |
1660 | 1678 | ||
diff --git a/src/util/mq.c b/src/util/mq.c index 01cdf764b..e69d1c483 100644 --- a/src/util/mq.c +++ b/src/util/mq.c | |||
@@ -952,19 +952,68 @@ GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq, | |||
952 | } | 952 | } |
953 | 953 | ||
954 | 954 | ||
955 | /** | ||
956 | * Call a callback once the envelope has been sent, that is, | ||
957 | * sending it can not be canceled anymore. | ||
958 | * There can be only one notify sent callback per envelope. | ||
959 | * | ||
960 | * @param ev message to call the notify callback for | ||
961 | * @param cb the notify callback | ||
962 | * @param cb_cls closure for the callback | ||
963 | */ | ||
955 | void | 964 | void |
956 | GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm, | 965 | GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm, |
957 | GNUNET_MQ_NotifyCallback cb, | 966 | GNUNET_MQ_NotifyCallback cb, |
958 | void *cls) | 967 | void *cb_cls) |
959 | { | 968 | { |
960 | mqm->sent_cb = cb; | 969 | mqm->sent_cb = cb; |
961 | mqm->sent_cls = cls; | 970 | mqm->sent_cls = cb_cls; |
962 | } | 971 | } |
963 | 972 | ||
964 | 973 | ||
974 | /** | ||
975 | * Handle we return for callbacks registered to be | ||
976 | * notified when #GNUNET_MQ_destroy() is called on a queue. | ||
977 | */ | ||
978 | struct GNUNET_MQ_DestroyNotificationHandle | ||
979 | { | ||
980 | /** | ||
981 | * Kept in a DLL. | ||
982 | */ | ||
983 | struct GNUNET_MQ_DestroyNotificationHandle *prev; | ||
984 | |||
985 | /** | ||
986 | * Kept in a DLL. | ||
987 | */ | ||
988 | struct GNUNET_MQ_DestroyNotificationHandle *next; | ||
989 | |||
990 | /** | ||
991 | * Queue to notify about. | ||
992 | */ | ||
993 | struct GNUNET_MQ_Handle *mq; | ||
994 | |||
995 | /** | ||
996 | * Function to call. | ||
997 | */ | ||
998 | GNUNET_SCHEDULER_TaskCallback cb; | ||
999 | |||
1000 | /** | ||
1001 | * Closure for @e cb. | ||
1002 | */ | ||
1003 | void *cb_cls; | ||
1004 | }; | ||
1005 | |||
1006 | |||
1007 | /** | ||
1008 | * Destroy the message queue. | ||
1009 | * | ||
1010 | * @param mq message queue to destroy | ||
1011 | */ | ||
965 | void | 1012 | void |
966 | GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq) | 1013 | GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq) |
967 | { | 1014 | { |
1015 | struct GNUNET_MQ_DestroyNotificationHandle *dnh; | ||
1016 | |||
968 | if (NULL != mq->destroy_impl) | 1017 | if (NULL != mq->destroy_impl) |
969 | { | 1018 | { |
970 | mq->destroy_impl (mq, mq->impl_state); | 1019 | mq->destroy_impl (mq, mq->impl_state); |
@@ -996,6 +1045,8 @@ GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq) | |||
996 | mq->queue_length--; | 1045 | mq->queue_length--; |
997 | } | 1046 | } |
998 | GNUNET_assert (0 == mq->queue_length); | 1047 | GNUNET_assert (0 == mq->queue_length); |
1048 | while (NULL != (dnh = mq->dnh_head)) | ||
1049 | dnh->cb (dnh->cb_cls); | ||
999 | if (NULL != mq->assoc_map) | 1050 | if (NULL != mq->assoc_map) |
1000 | { | 1051 | { |
1001 | GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map); | 1052 | GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map); |
@@ -1184,39 +1235,6 @@ GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq, | |||
1184 | 1235 | ||
1185 | 1236 | ||
1186 | /** | 1237 | /** |
1187 | * Handle we return for callbacks registered to be | ||
1188 | * notified when #GNUNET_MQ_destroy() is called on a queue. | ||
1189 | */ | ||
1190 | struct GNUNET_MQ_DestroyNotificationHandle | ||
1191 | { | ||
1192 | /** | ||
1193 | * Kept in a DLL. | ||
1194 | */ | ||
1195 | struct GNUNET_MQ_DestroyNotificationHandle *prev; | ||
1196 | |||
1197 | /** | ||
1198 | * Kept in a DLL. | ||
1199 | */ | ||
1200 | struct GNUNET_MQ_DestroyNotificationHandle *next; | ||
1201 | |||
1202 | /** | ||
1203 | * Queue to notify about. | ||
1204 | */ | ||
1205 | struct GNUNET_MQ_Handle *mq; | ||
1206 | |||
1207 | /** | ||
1208 | * Function to call. | ||
1209 | */ | ||
1210 | GNUNET_SCHEDULER_TaskCallback cb; | ||
1211 | |||
1212 | /** | ||
1213 | * Closure for @e cb. | ||
1214 | */ | ||
1215 | void *cb_cls; | ||
1216 | }; | ||
1217 | |||
1218 | |||
1219 | /** | ||
1220 | * Register function to be called whenever @a mq is being | 1238 | * Register function to be called whenever @a mq is being |
1221 | * destroyed. | 1239 | * destroyed. |
1222 | * | 1240 | * |