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