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