diff options
Diffstat (limited to 'src/service/nse/nse_api.c')
-rw-r--r-- | src/service/nse/nse_api.c | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/src/service/nse/nse_api.c b/src/service/nse/nse_api.c new file mode 100644 index 000000000..7f3e03b98 --- /dev/null +++ b/src/service/nse/nse_api.c | |||
@@ -0,0 +1,208 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009, 2010, 2011, 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 | 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 nse/nse_api.c | ||
23 | * @brief api to get information from the network size estimation service | ||
24 | * @author Nathan Evans | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_constants.h" | ||
28 | #include "gnunet_arm_service.h" | ||
29 | #include "gnunet_protocols.h" | ||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_nse_service.h" | ||
32 | #include "nse.h" | ||
33 | |||
34 | #define LOG(kind, ...) GNUNET_log_from (kind, "nse-api", __VA_ARGS__) | ||
35 | |||
36 | /** | ||
37 | * Handle for talking with the NSE service. | ||
38 | */ | ||
39 | struct GNUNET_NSE_Handle | ||
40 | { | ||
41 | /** | ||
42 | * Configuration to use. | ||
43 | */ | ||
44 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
45 | |||
46 | /** | ||
47 | * Message queue (if available). | ||
48 | */ | ||
49 | struct GNUNET_MQ_Handle *mq; | ||
50 | |||
51 | /** | ||
52 | * Task doing exponential back-off trying to reconnect. | ||
53 | */ | ||
54 | struct GNUNET_SCHEDULER_Task *reconnect_task; | ||
55 | |||
56 | /** | ||
57 | * Time for next connect retry. | ||
58 | */ | ||
59 | struct GNUNET_TIME_Relative reconnect_delay; | ||
60 | |||
61 | /** | ||
62 | * Callback function to call when message is received. | ||
63 | */ | ||
64 | GNUNET_NSE_Callback recv_cb; | ||
65 | |||
66 | /** | ||
67 | * Closure to pass to @e recv_cb callback. | ||
68 | */ | ||
69 | void *recv_cb_cls; | ||
70 | }; | ||
71 | |||
72 | |||
73 | /** | ||
74 | * Try again to connect to network size estimation service. | ||
75 | * | ||
76 | * @param cls closure with the `struct GNUNET_NSE_Handle *` | ||
77 | */ | ||
78 | static void | ||
79 | reconnect (void *cls); | ||
80 | |||
81 | |||
82 | /** | ||
83 | * Generic error handler, called with the appropriate | ||
84 | * error code and the same closure specified at the creation of | ||
85 | * the message queue. | ||
86 | * Not every message queue implementation supports an error handler. | ||
87 | * | ||
88 | * @param cls closure with the `struct GNUNET_NSE_Handle *` | ||
89 | * @param error error code | ||
90 | */ | ||
91 | static void | ||
92 | mq_error_handler (void *cls, enum GNUNET_MQ_Error error) | ||
93 | { | ||
94 | struct GNUNET_NSE_Handle *h = cls; | ||
95 | |||
96 | (void) error; | ||
97 | GNUNET_MQ_destroy (h->mq); | ||
98 | h->mq = NULL; | ||
99 | h->reconnect_task = | ||
100 | GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h); | ||
101 | h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay); | ||
102 | } | ||
103 | |||
104 | |||
105 | /** | ||
106 | * Type of a function to call when we receive a message | ||
107 | * from the service. | ||
108 | * | ||
109 | * @param cls closure | ||
110 | * @param client_msg message received | ||
111 | */ | ||
112 | static void | ||
113 | handle_estimate (void *cls, const struct GNUNET_NSE_ClientMessage *client_msg) | ||
114 | { | ||
115 | struct GNUNET_NSE_Handle *h = cls; | ||
116 | |||
117 | h->reconnect_delay = GNUNET_TIME_UNIT_ZERO; | ||
118 | h->recv_cb (h->recv_cb_cls, | ||
119 | GNUNET_TIME_absolute_ntoh (client_msg->timestamp), | ||
120 | GNUNET_ntoh_double (client_msg->size_estimate), | ||
121 | GNUNET_ntoh_double (client_msg->std_deviation)); | ||
122 | } | ||
123 | |||
124 | |||
125 | /** | ||
126 | * Try again to connect to network size estimation service. | ||
127 | * | ||
128 | * @param cls the `struct GNUNET_NSE_Handle *` | ||
129 | */ | ||
130 | static void | ||
131 | reconnect (void *cls) | ||
132 | { | ||
133 | struct GNUNET_NSE_Handle *h = cls; | ||
134 | struct GNUNET_MQ_MessageHandler handlers[] = | ||
135 | { GNUNET_MQ_hd_fixed_size (estimate, | ||
136 | GNUNET_MESSAGE_TYPE_NSE_ESTIMATE, | ||
137 | struct GNUNET_NSE_ClientMessage, | ||
138 | h), | ||
139 | GNUNET_MQ_handler_end () }; | ||
140 | struct GNUNET_MessageHeader *msg; | ||
141 | struct GNUNET_MQ_Envelope *env; | ||
142 | |||
143 | h->reconnect_task = NULL; | ||
144 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
145 | "Connecting to network size estimation service.\n"); | ||
146 | GNUNET_assert (NULL == h->mq); | ||
147 | h->mq = GNUNET_CLIENT_connect (h->cfg, "nse", handlers, &mq_error_handler, h); | ||
148 | if (NULL == h->mq) | ||
149 | return; | ||
150 | env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_NSE_START); | ||
151 | GNUNET_MQ_send (h->mq, env); | ||
152 | } | ||
153 | |||
154 | |||
155 | /** | ||
156 | * Connect to the network size estimation service. | ||
157 | * | ||
158 | * @param cfg the configuration to use | ||
159 | * @param func function to call with network size estimate | ||
160 | * @param func_cls closure to pass to @a func | ||
161 | * @return handle to use | ||
162 | */ | ||
163 | struct GNUNET_NSE_Handle * | ||
164 | GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
165 | GNUNET_NSE_Callback func, | ||
166 | void *func_cls) | ||
167 | { | ||
168 | struct GNUNET_NSE_Handle *h; | ||
169 | |||
170 | GNUNET_assert (NULL != func); | ||
171 | h = GNUNET_new (struct GNUNET_NSE_Handle); | ||
172 | h->cfg = cfg; | ||
173 | h->recv_cb = func; | ||
174 | h->recv_cb_cls = func_cls; | ||
175 | h->reconnect_delay = GNUNET_TIME_UNIT_ZERO; | ||
176 | reconnect (h); | ||
177 | if (NULL == h->mq) | ||
178 | { | ||
179 | GNUNET_free (h); | ||
180 | return NULL; | ||
181 | } | ||
182 | return h; | ||
183 | } | ||
184 | |||
185 | |||
186 | /** | ||
187 | * Disconnect from network size estimation service | ||
188 | * | ||
189 | * @param h handle to destroy | ||
190 | */ | ||
191 | void | ||
192 | GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h) | ||
193 | { | ||
194 | if (NULL != h->reconnect_task) | ||
195 | { | ||
196 | GNUNET_SCHEDULER_cancel (h->reconnect_task); | ||
197 | h->reconnect_task = NULL; | ||
198 | } | ||
199 | if (NULL != h->mq) | ||
200 | { | ||
201 | GNUNET_MQ_destroy (h->mq); | ||
202 | h->mq = NULL; | ||
203 | } | ||
204 | GNUNET_free (h); | ||
205 | } | ||
206 | |||
207 | |||
208 | /* end of nse_api.c */ | ||