aboutsummaryrefslogtreecommitdiff
path: root/src/nse/gnunet-service-nse.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-09-19 18:12:49 +0000
committerChristian Grothoff <christian@grothoff.org>2016-09-19 18:12:49 +0000
commit506899aa2be2b4d5dc09c1740969c28ddf43c82d (patch)
tree12815e3faeca1c535a67a601ecab014c871068b6 /src/nse/gnunet-service-nse.c
parent1532728eb9a0fd858528a574ce27155436eaf76a (diff)
downloadgnunet-506899aa2be2b4d5dc09c1740969c28ddf43c82d.tar.gz
gnunet-506899aa2be2b4d5dc09c1740969c28ddf43c82d.zip
converting nse to new service API
Diffstat (limited to 'src/nse/gnunet-service-nse.c')
-rw-r--r--src/nse/gnunet-service-nse.c158
1 files changed, 88 insertions, 70 deletions
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 */
289static struct GNUNET_SERVER_NotificationContext *nc; 289static 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 */
314static uint64_t my_proof; 314static uint64_t my_proof;
315 315
316/**
317 * Handle to this serivce's server.
318 */
319static 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;
328static void 323static void
329setup_estimate_message (struct GNUNET_NSE_ClientMessage *em) 324setup_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 */
429static void 425static void
430handle_start_message (void *cls, 426handle_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 */
1482static void 1479static void
1483run (void *cls, 1480run (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 */
1644int 1634static void *
1645main (int argc, 1635client_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 */
1650static void
1651client_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 */
1662GNUNET_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