446ae2dbdad1855a75c2f1d3469287397a6198f9
[oweals/gnunet.git] / src / dht / gnunet-service-dht_nse.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2011 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 dht/gnunet-service-dht_nse.c
21  * @brief GNUnet DHT integration with NSE
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_nse_service.h"
26 #include "gnunet-service-dht.h"
27 #include "gnunet-service-dht_nse.h"
28
29 /**
30  * log of the current network size estimate, used as the point where
31  * we switch between random and deterministic routing.  Default
32  * value of 4.0 is used if NSE module is not available (i.e. not
33  * configured).
34  */
35 static double log_of_network_size_estimate = 4.0;
36
37 /**
38  * Network size estimation handle.
39  */
40 static struct GNUNET_NSE_Handle *nse;
41
42
43 /**
44  * Callback that is called when network size estimate is updated.
45  *
46  * @param cls closure
47  * @param timestamp time when the estimate was received from the server (or created by the server)
48  * @param logestimate the log(Base 2) value of the current network size estimate
49  * @param std_dev standard deviation for the estimate
50  *
51  */
52 static void
53 update_network_size_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
54                               double logestimate, double std_dev)
55 {
56   GNUNET_STATISTICS_update (GDS_stats,
57                             gettext_noop ("# Network size estimates received"),
58                             1, GNUNET_NO);
59   /* do not allow estimates < 0.5 */
60   log_of_network_size_estimate = GNUNET_MAX (0.5, logestimate);
61 }
62
63
64 /**
65  * Return the log of the current network size estimate.
66  *
67  * @return log of NSE
68  */
69 double
70 GDS_NSE_get ()
71 {
72   return log_of_network_size_estimate;
73 }
74
75
76 /**
77  * Initialize NSE subsystem.
78  */
79 void
80 GDS_NSE_init ()
81 {
82   unsigned long long hops;
83
84   if ( (GNUNET_YES ==
85         GNUNET_CONFIGURATION_have_value (GDS_cfg,
86                                          "dht",
87                                          "FORCE_NSE")) &&
88        (GNUNET_OK ==
89         GNUNET_CONFIGURATION_get_value_number (GDS_cfg,
90                                                "dht",
91                                                "FORCE_NSE",
92                                                &hops)) )
93   {
94     log_of_network_size_estimate = (double) hops;
95     return;
96   }
97   nse = GNUNET_NSE_connect (GDS_cfg, &update_network_size_estimate, NULL);
98 }
99
100
101 /**
102  * Shutdown NSE subsystem.
103  */
104 void
105 GDS_NSE_done ()
106 {
107   if (NULL != nse)
108   {
109     GNUNET_NSE_disconnect (nse);
110     nse = NULL;
111   }
112 }
113
114 /* end of gnunet-service-dht_nse.c */