doc: gnunet-c-tutorial: Add nodes.
[oweals/gnunet.git] / src / nse / gnunet-nse.c
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
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       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       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18       Boston, MA 02110-1301, USA.
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
30 /**
31  * The handle to the NSE service
32  */
33 static struct GNUNET_NSE_Handle *nse;
34
35 /**
36  * The program status; 0 for success.
37  */
38 static int status;
39
40
41 /**
42  * Task to shutdown and clean up all state
43  *
44  * @param cls NULL
45  */
46 static void
47 do_shutdown (void *cls)
48 {
49   if (NULL != nse)
50   {
51     GNUNET_NSE_disconnect (nse);
52     nse = NULL;
53   }
54 }
55
56
57 /**
58  * Callback to call when network size estimate is updated.
59  *
60  * @param cls NULL
61  * @param timestamp server timestamp
62  * @param estimate the value of the current network size estimate
63  * @param std_dev standard deviation (rounded down to nearest integer)
64  *                of the size estimation values seen
65  */
66 static void
67 handle_estimate (void *cls,
68                  struct GNUNET_TIME_Absolute timestamp,
69                  double estimate,
70                  double std_dev)
71 {
72   status = 0;
73   FPRINTF (stdout, "%llu %f %f %f\n",
74            (unsigned long long) timestamp.abs_value_us,
75            GNUNET_NSE_log_estimate_to_n (estimate),
76            estimate,
77            std_dev);
78 }
79
80
81 /**
82  * Actual main function that runs the emulation.
83  *
84  * @param cls unused
85  * @param args remaining args, unused
86  * @param cfgfile name of the configuration
87  * @param cfg configuration handle
88  */
89 static void
90 run (void *cls,
91      char *const *args,
92      const char *cfgfile,
93      const struct GNUNET_CONFIGURATION_Handle *cfg)
94 {
95   nse = GNUNET_NSE_connect (cfg,
96                             &handle_estimate,
97                             NULL);
98   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
99                                  NULL);
100 }
101
102
103 /**
104  * Main function.
105  *
106  * @return 0 on success
107  */
108 int
109 main (int argc,
110       char *const *argv)
111 {
112   static struct GNUNET_GETOPT_CommandLineOption options[] = {
113     GNUNET_GETOPT_OPTION_END
114   };
115
116   status = 1;
117   if (GNUNET_OK !=
118       GNUNET_PROGRAM_run (argc,
119                           argv,
120                           "gnunet-nse",
121                           gettext_noop
122                           ("Show network size estimates from NSE service."),
123                           options,
124                           &run, NULL))
125     return 2;
126   return status;
127 }