aboutsummaryrefslogtreecommitdiff
path: root/src/cli/nse/gnunet-nse.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli/nse/gnunet-nse.c')
-rw-r--r--src/cli/nse/gnunet-nse.c143
1 files changed, 143 insertions, 0 deletions
diff --git a/src/cli/nse/gnunet-nse.c b/src/cli/nse/gnunet-nse.c
new file mode 100644
index 000000000..b88697695
--- /dev/null
+++ b/src/cli/nse/gnunet-nse.c
@@ -0,0 +1,143 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2008--2014, 2016 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file nse/gnunet-nse.c
23 * @brief Program to display network size estimates from the NSE service
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25 */
26
27#include "platform.h"
28#include "gnunet_nse_service.h"
29#include <gnunet_util_lib.h>
30
31/**
32 * The handle to the NSE service
33 */
34static struct GNUNET_NSE_Handle *nse;
35
36/**
37 * The program status; 0 for success.
38 */
39static int status;
40
41/**
42 * Monitor flag.
43 */
44static int monitor;
45
46
47/**
48 * Task to shutdown and clean up all state
49 *
50 * @param cls NULL
51 */
52static void
53do_shutdown (void *cls)
54{
55 (void) cls;
56 if (NULL != nse)
57 {
58 GNUNET_NSE_disconnect (nse);
59 nse = NULL;
60 }
61}
62
63
64/**
65 * Callback to call when network size estimate is updated.
66 *
67 * @param cls NULL
68 * @param timestamp server timestamp
69 * @param estimate the value of the current network size estimate
70 * @param std_dev standard deviation (rounded down to nearest integer)
71 * of the size estimation values seen
72 */
73static void
74handle_estimate (void *cls,
75 struct GNUNET_TIME_Absolute timestamp,
76 double estimate,
77 double std_dev)
78{
79 (void) cls;
80 status = 0;
81 fprintf (stdout,
82 "%s: #peers=%f (ld(#peers)=%f, stddev=%f)\n",
83 GNUNET_STRINGS_absolute_time_to_string (timestamp),
84 GNUNET_NSE_log_estimate_to_n (estimate),
85 estimate,
86 std_dev);
87 if (! monitor)
88 GNUNET_SCHEDULER_shutdown ();
89}
90
91
92/**
93 * Actual main function that runs the emulation.
94 *
95 * @param cls unused
96 * @param args remaining args, unused
97 * @param cfgfile name of the configuration
98 * @param cfg configuration handle
99 */
100static void
101run (void *cls,
102 char *const *args,
103 const char *cfgfile,
104 const struct GNUNET_CONFIGURATION_Handle *cfg)
105{
106 (void) cls;
107 (void) args;
108 (void) cfgfile;
109 nse = GNUNET_NSE_connect (cfg, &handle_estimate, NULL);
110 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
111}
112
113
114/**
115 * Main function.
116 *
117 * @return 0 on success
118 */
119int
120main (int argc, char *const *argv)
121{
122 struct GNUNET_GETOPT_CommandLineOption options[] = {
123 GNUNET_GETOPT_option_flag ('m',
124 "monitor",
125 gettext_noop (
126 "Monitor and output current estimates"),
127 &monitor),
128 GNUNET_GETOPT_OPTION_END
129 };
130
131 status = 1;
132 if (GNUNET_OK !=
133 GNUNET_PROGRAM_run (argc,
134 argv,
135 "gnunet-nse",
136 gettext_noop (
137 "Show network size estimates from NSE service."),
138 options,
139 &run,
140 NULL))
141 return 2;
142 return status;
143}