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