gnunet-nse.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2008--2014, 2016 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 nse/gnunet-nse.c
  18. * @brief Program to display network size estimates from the NSE service
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "platform.h"
  22. #include "gnunet_nse_service.h"
  23. /**
  24. * The handle to the NSE service
  25. */
  26. static struct GNUNET_NSE_Handle *nse;
  27. /**
  28. * The program status; 0 for success.
  29. */
  30. static int status;
  31. /**
  32. * Task to shutdown and clean up all state
  33. *
  34. * @param cls NULL
  35. */
  36. static void
  37. do_shutdown (void *cls)
  38. {
  39. (void) cls;
  40. if (NULL != nse)
  41. {
  42. GNUNET_NSE_disconnect (nse);
  43. nse = NULL;
  44. }
  45. }
  46. /**
  47. * Callback to call when network size estimate is updated.
  48. *
  49. * @param cls NULL
  50. * @param timestamp server timestamp
  51. * @param estimate the value of the current network size estimate
  52. * @param std_dev standard deviation (rounded down to nearest integer)
  53. * of the size estimation values seen
  54. */
  55. static void
  56. handle_estimate (void *cls,
  57. struct GNUNET_TIME_Absolute timestamp,
  58. double estimate,
  59. double std_dev)
  60. {
  61. (void) cls;
  62. status = 0;
  63. FPRINTF (stdout,
  64. "%llu %f %f %f\n",
  65. (unsigned long long) timestamp.abs_value_us,
  66. GNUNET_NSE_log_estimate_to_n (estimate),
  67. estimate,
  68. std_dev);
  69. }
  70. /**
  71. * Actual main function that runs the emulation.
  72. *
  73. * @param cls unused
  74. * @param args remaining args, unused
  75. * @param cfgfile name of the configuration
  76. * @param cfg configuration handle
  77. */
  78. static void
  79. run (void *cls,
  80. char *const *args,
  81. const char *cfgfile,
  82. const struct GNUNET_CONFIGURATION_Handle *cfg)
  83. {
  84. (void) cls;
  85. (void) args;
  86. (void) cfgfile;
  87. nse = GNUNET_NSE_connect (cfg, &handle_estimate, NULL);
  88. GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  89. }
  90. /**
  91. * Main function.
  92. *
  93. * @return 0 on success
  94. */
  95. int
  96. main (int argc, char *const *argv)
  97. {
  98. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  99. GNUNET_GETOPT_OPTION_END};
  100. status = 1;
  101. if (GNUNET_OK !=
  102. GNUNET_PROGRAM_run (argc,
  103. argv,
  104. "gnunet-nse",
  105. gettext_noop (
  106. "Show network size estimates from NSE service."),
  107. options,
  108. &run,
  109. NULL))
  110. return 2;
  111. return status;
  112. }