2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU 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.
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.
18 * @brief api to get information from the network size estimation service
19 * @author Nathan Evans
22 #include "gnunet_constants.h"
23 #include "gnunet_arm_service.h"
24 #include "gnunet_hello_lib.h"
25 #include "gnunet_protocols.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_nse_service.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "nse-api",__VA_ARGS__)
33 * Handle for talking with the NSE service.
35 struct GNUNET_NSE_Handle
38 * Configuration to use.
40 const struct GNUNET_CONFIGURATION_Handle *cfg;
43 * Message queue (if available).
45 struct GNUNET_MQ_Handle *mq;
48 * Task doing exponential back-off trying to reconnect.
50 struct GNUNET_SCHEDULER_Task *reconnect_task;
53 * Time for next connect retry.
55 struct GNUNET_TIME_Relative reconnect_delay;
58 * Callback function to call when message is received.
60 GNUNET_NSE_Callback recv_cb;
63 * Closure to pass to @e recv_cb callback.
71 * Try again to connect to network size estimation service.
73 * @param cls closure with the `struct GNUNET_NSE_Handle *`
76 reconnect (void *cls);
80 * Generic error handler, called with the appropriate
81 * error code and the same closure specified at the creation of
83 * Not every message queue implementation supports an error handler.
85 * @param cls closure with the `struct GNUNET_NSE_Handle *`
86 * @param error error code
89 mq_error_handler (void *cls,
90 enum GNUNET_MQ_Error error)
92 struct GNUNET_NSE_Handle *h = cls;
94 GNUNET_MQ_destroy (h->mq);
97 = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
100 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
105 * Type of a function to call when we receive a message
109 * @param client_msg message received
112 handle_estimate (void *cls,
113 const struct GNUNET_NSE_ClientMessage *client_msg)
115 struct GNUNET_NSE_Handle *h = cls;
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));
126 * Try again to connect to network size estimation service.
128 * @param cls the `struct GNUNET_NSE_Handle *`
131 reconnect (void *cls)
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,
139 GNUNET_MQ_handler_end ()
141 struct GNUNET_MessageHeader *msg;
142 struct GNUNET_MQ_Envelope *env;
144 h->reconnect_task = NULL;
145 LOG (GNUNET_ERROR_TYPE_DEBUG,
146 "Connecting to network size estimation service.\n");
147 GNUNET_assert (NULL == h->mq);
148 h->mq = GNUNET_CLIENT_connect (h->cfg,
155 env = GNUNET_MQ_msg (msg,
156 GNUNET_MESSAGE_TYPE_NSE_START);
157 GNUNET_MQ_send (h->mq,
163 * Connect to the network size estimation service.
165 * @param cfg the configuration to use
166 * @param func funtion to call with network size estimate
167 * @param func_cls closure to pass to @a func
168 * @return handle to use
170 struct GNUNET_NSE_Handle *
171 GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
172 GNUNET_NSE_Callback func,
175 struct GNUNET_NSE_Handle *h;
177 GNUNET_assert (NULL != func);
178 h = GNUNET_new (struct GNUNET_NSE_Handle);
181 h->recv_cb_cls = func_cls;
182 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
194 * Disconnect from network size estimation service
196 * @param h handle to destroy
199 GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h)
201 if (NULL != h->reconnect_task)
203 GNUNET_SCHEDULER_cancel (h->reconnect_task);
204 h->reconnect_task = NULL;
208 GNUNET_MQ_destroy (h->mq);
214 /* end of nse_api.c */