doc: gnunet-c-tutorial: Add nodes.
[oweals/gnunet.git] / src / nse / nse_api.c
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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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_hello_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_nse_service.h"
33 #include "nse.h"
34
35 #define LOG(kind,...) GNUNET_log_from (kind, "nse-api",__VA_ARGS__)
36
37 /**
38  * Handle for talking with the NSE service.
39  */
40 struct GNUNET_NSE_Handle
41 {
42   /**
43    * Configuration to use.
44    */
45   const struct GNUNET_CONFIGURATION_Handle *cfg;
46
47   /**
48    * Message queue (if available).
49    */
50   struct GNUNET_MQ_Handle *mq;
51
52   /**
53    * Task doing exponential back-off trying to reconnect.
54    */
55   struct GNUNET_SCHEDULER_Task *reconnect_task;
56
57   /**
58    * Time for next connect retry.
59    */
60   struct GNUNET_TIME_Relative reconnect_delay;
61
62   /**
63    * Callback function to call when message is received.
64    */
65   GNUNET_NSE_Callback recv_cb;
66
67   /**
68    * Closure to pass to @e recv_cb callback.
69    */
70   void *recv_cb_cls;
71
72 };
73
74
75 /**
76  * Try again to connect to network size estimation service.
77  *
78  * @param cls closure with the `struct GNUNET_NSE_Handle *`
79  */
80 static void
81 reconnect (void *cls);
82
83
84 /**
85  * Generic error handler, called with the appropriate
86  * error code and the same closure specified at the creation of
87  * the message queue.
88  * Not every message queue implementation supports an error handler.
89  *
90  * @param cls closure with the `struct GNUNET_NSE_Handle *`
91  * @param error error code
92  */
93 static void
94 mq_error_handler (void *cls,
95                   enum GNUNET_MQ_Error error)
96 {
97   struct GNUNET_NSE_Handle *h = cls;
98
99   GNUNET_MQ_destroy (h->mq);
100   h->mq = NULL;
101   h->reconnect_task
102     = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
103                                     &reconnect,
104                                     h);
105   h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
106 }
107
108
109 /**
110  * Type of a function to call when we receive a message
111  * from the service.
112  *
113  * @param cls closure
114  * @param client_msg message received
115  */
116 static void
117 handle_estimate (void *cls,
118                  const struct GNUNET_NSE_ClientMessage *client_msg)
119 {
120   struct GNUNET_NSE_Handle *h = cls;
121
122   h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
123   h->recv_cb (h->recv_cb_cls,
124               GNUNET_TIME_absolute_ntoh (client_msg->timestamp),
125               GNUNET_ntoh_double (client_msg->size_estimate),
126               GNUNET_ntoh_double (client_msg->std_deviation));
127 }
128
129
130 /**
131  * Try again to connect to network size estimation service.
132  *
133  * @param cls the `struct GNUNET_NSE_Handle *`
134  */
135 static void
136 reconnect (void *cls)
137 {
138   struct GNUNET_NSE_Handle *h = cls;
139   struct GNUNET_MQ_MessageHandler handlers[] = {
140     GNUNET_MQ_hd_fixed_size (estimate,
141                              GNUNET_MESSAGE_TYPE_NSE_ESTIMATE,
142                              struct GNUNET_NSE_ClientMessage,
143                              h),
144     GNUNET_MQ_handler_end ()
145   };
146   struct GNUNET_MessageHeader *msg;
147   struct GNUNET_MQ_Envelope *env;
148
149   h->reconnect_task = NULL;
150   LOG (GNUNET_ERROR_TYPE_DEBUG,
151        "Connecting to network size estimation service.\n");
152   GNUNET_assert (NULL == h->mq);
153   h->mq = GNUNET_CLIENT_connect (h->cfg,
154                                  "nse",
155                                  handlers,
156                                  &mq_error_handler,
157                                  h);
158   if (NULL == h->mq)
159     return;
160   env = GNUNET_MQ_msg (msg,
161                        GNUNET_MESSAGE_TYPE_NSE_START);
162   GNUNET_MQ_send (h->mq,
163                   env);
164 }
165
166
167 /**
168  * Connect to the network size estimation service.
169  *
170  * @param cfg the configuration to use
171  * @param func funtion to call with network size estimate
172  * @param func_cls closure to pass to @a func
173  * @return handle to use
174  */
175 struct GNUNET_NSE_Handle *
176 GNUNET_NSE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
177                     GNUNET_NSE_Callback func,
178                     void *func_cls)
179 {
180   struct GNUNET_NSE_Handle *h;
181
182   GNUNET_assert (NULL != func);
183   h = GNUNET_new (struct GNUNET_NSE_Handle);
184   h->cfg = cfg;
185   h->recv_cb = func;
186   h->recv_cb_cls = func_cls;
187   h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
188   reconnect (h);
189   if (NULL == h->mq)
190   {
191     GNUNET_free (h);
192     return NULL;
193   }
194   return h;
195 }
196
197
198 /**
199  * Disconnect from network size estimation service
200  *
201  * @param h handle to destroy
202  */
203 void
204 GNUNET_NSE_disconnect (struct GNUNET_NSE_Handle *h)
205 {
206   if (NULL != h->reconnect_task)
207   {
208     GNUNET_SCHEDULER_cancel (h->reconnect_task);
209     h->reconnect_task = NULL;
210   }
211   if (NULL != h->mq)
212   {
213     GNUNET_MQ_destroy (h->mq);
214     h->mq = NULL;
215   }
216   GNUNET_free (h);
217 }
218
219 /* end of nse_api.c */