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