refactor DHT for new service API
[oweals/gnunet.git] / src / nse / gnunet-nse.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2014 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  * Handle to our configuration
32  */
33 static struct GNUNET_CONFIGURATION_Handle *cfg;
34
35 /**
36  * The handle to the NSE service
37  */
38 static struct GNUNET_NSE_Handle *nse;
39
40 /**
41  * The handle to test if NSE service is running or not
42  */
43 static struct GNUNET_CLIENT_TestHandle *test;
44
45 /**
46  * Shutdown task
47  */
48 static struct GNUNET_SCHEDULER_Task * shutdown_task;
49
50 /**
51  * The program status; 0 for success.
52  */
53 static int status;
54
55
56 /**
57  * Task to shutdown and clean up all state
58  *
59  * @param cls NULL
60  */
61 static void
62 do_shutdown (void *cls)
63 {
64   shutdown_task = NULL;
65   if (NULL != test)
66     GNUNET_CLIENT_service_test_cancel (test);
67   if (NULL != nse)
68     GNUNET_NSE_disconnect (nse);
69   if (NULL != cfg)
70     GNUNET_CONFIGURATION_destroy (cfg);
71 }
72
73
74 /**
75  * Callback to call when network size estimate is updated.
76  *
77  * @param cls NULL
78  * @param timestamp server timestamp
79  * @param estimate the value of the current network size estimate
80  * @param std_dev standard deviation (rounded down to nearest integer)
81  *                of the size estimation values seen
82  */
83 static void
84 handle_estimate (void *cls,
85                  struct GNUNET_TIME_Absolute timestamp,
86                  double estimate, double std_dev)
87 {
88   FPRINTF (stdout, "%llu %f %f %f\n",
89            (unsigned long long) timestamp.abs_value_us,
90            GNUNET_NSE_log_estimate_to_n (estimate),
91            estimate,
92            std_dev);
93 }
94
95
96 /**
97  * Function called with the result on the service test for the NSE service
98  *
99  * @param cls NULL
100  * @param result #GNUNET_YES if the service is running,
101  *               #GNUNET_NO if the service is not running
102  *               #GNUNET_SYSERR if the configuration is invalid
103  */
104 static void
105 nse_test_result (void *cls, int result)
106 {
107   test = NULL;
108   switch (result)
109   {
110   case GNUNET_YES:
111     nse = GNUNET_NSE_connect (cfg, &handle_estimate, NULL);
112     status = 0;
113     break;
114   case GNUNET_NO:
115     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116                 _("NSE service is not running\n"));
117     GNUNET_SCHEDULER_shutdown ();
118     return;
119   default:
120     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
121                 _("Error while checking if NSE service is running or not\n"));
122     GNUNET_SCHEDULER_shutdown ();
123     return;
124   }
125 }
126
127
128 /**
129  * Actual main function that runs the emulation.
130  *
131  * @param cls unused
132  * @param args remaining args, unused
133  * @param cfgfile name of the configuration
134  * @param cfg configuration handle
135  */
136 static void
137 run (void *cls, char *const *args, const char *cfgfile,
138      const struct GNUNET_CONFIGURATION_Handle *_cfg)
139 {
140   cfg = GNUNET_CONFIGURATION_dup (_cfg);
141   test = GNUNET_CLIENT_service_test ("nse",
142                                      cfg,
143                                      GNUNET_TIME_UNIT_SECONDS,
144                                      nse_test_result,
145                                      NULL);
146   shutdown_task = GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
147                                                  NULL);
148 }
149
150
151 /**
152  * Main function.
153  *
154  * @return 0 on success
155  */
156 int
157 main (int argc, char *const *argv)
158 {
159   static struct GNUNET_GETOPT_CommandLineOption options[] = {
160     GNUNET_GETOPT_OPTION_END
161   };
162
163   status = 1;
164   if (GNUNET_OK !=
165       GNUNET_PROGRAM_run (argc, argv, "gnunet-nse",
166                           gettext_noop
167                           ("Show network size estimates from NSE service."),
168                           options, &run, NULL))
169     return 2;
170   return status;
171 }