2
0

gnunet-service-dht_nse.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2011 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file dht/gnunet-service-dht_nse.c
  18. * @brief GNUnet DHT integration with NSE
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_nse_service.h"
  23. #include "gnunet-service-dht.h"
  24. #include "gnunet-service-dht_nse.h"
  25. /**
  26. * log of the current network size estimate, used as the point where
  27. * we switch between random and deterministic routing. Default
  28. * value of 4.0 is used if NSE module is not available (i.e. not
  29. * configured).
  30. */
  31. static double log_of_network_size_estimate = 4.0;
  32. /**
  33. * Network size estimation handle.
  34. */
  35. static struct GNUNET_NSE_Handle *nse;
  36. /**
  37. * Callback that is called when network size estimate is updated.
  38. *
  39. * @param cls closure
  40. * @param timestamp time when the estimate was received from the server (or created by the server)
  41. * @param logestimate the log(Base 2) value of the current network size estimate
  42. * @param std_dev standard deviation for the estimate
  43. *
  44. */
  45. static void
  46. update_network_size_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
  47. double logestimate, double std_dev)
  48. {
  49. GNUNET_STATISTICS_update (GDS_stats,
  50. gettext_noop ("# Network size estimates received"),
  51. 1, GNUNET_NO);
  52. /* do not allow estimates < 0.5 */
  53. log_of_network_size_estimate = GNUNET_MAX (0.5, logestimate);
  54. }
  55. /**
  56. * Return the log of the current network size estimate.
  57. *
  58. * @return log of NSE
  59. */
  60. double
  61. GDS_NSE_get ()
  62. {
  63. return log_of_network_size_estimate;
  64. }
  65. /**
  66. * Initialize NSE subsystem.
  67. */
  68. void
  69. GDS_NSE_init ()
  70. {
  71. unsigned long long hops;
  72. if ( (GNUNET_YES ==
  73. GNUNET_CONFIGURATION_have_value (GDS_cfg,
  74. "dht",
  75. "FORCE_NSE")) &&
  76. (GNUNET_OK ==
  77. GNUNET_CONFIGURATION_get_value_number (GDS_cfg,
  78. "dht",
  79. "FORCE_NSE",
  80. &hops)) )
  81. {
  82. log_of_network_size_estimate = (double) hops;
  83. return;
  84. }
  85. nse = GNUNET_NSE_connect (GDS_cfg, &update_network_size_estimate, NULL);
  86. }
  87. /**
  88. * Shutdown NSE subsystem.
  89. */
  90. void
  91. GDS_NSE_done ()
  92. {
  93. if (NULL != nse)
  94. {
  95. GNUNET_NSE_disconnect (nse);
  96. nse = NULL;
  97. }
  98. }
  99. /* end of gnunet-service-dht_nse.c */