GNUnet  0.19.5
nse_api.c
Go to the documentation of this file.
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 
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 
41 {
46 
51 
56 
61 
66 
70  void *recv_cb_cls;
71 };
72 
73 
79 static void
80 reconnect (void *cls);
81 
82 
92 static void
93 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
94 {
95  struct GNUNET_NSE_Handle *h = cls;
96 
97  (void) error;
99  h->mq = NULL;
100  h->reconnect_task =
101  GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
102  h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
103 }
104 
105 
113 static void
114 handle_estimate (void *cls, const struct GNUNET_NSE_ClientMessage *client_msg)
115 {
116  struct GNUNET_NSE_Handle *h = cls;
117 
118  h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
119  h->recv_cb (h->recv_cb_cls,
120  GNUNET_TIME_absolute_ntoh (client_msg->timestamp),
121  GNUNET_ntoh_double (client_msg->size_estimate),
122  GNUNET_ntoh_double (client_msg->std_deviation));
123 }
124 
125 
131 static void
132 reconnect (void *cls)
133 {
134  struct GNUNET_NSE_Handle *h = cls;
136  { GNUNET_MQ_hd_fixed_size (estimate,
139  h),
141  struct GNUNET_MessageHeader *msg;
142  struct GNUNET_MQ_Envelope *env;
143 
144  h->reconnect_task = NULL;
146  "Connecting to network size estimation service.\n");
147  GNUNET_assert (NULL == h->mq);
149  if (NULL == h->mq)
150  return;
152  GNUNET_MQ_send (h->mq, env);
153 }
154 
155 
164 struct GNUNET_NSE_Handle *
166  GNUNET_NSE_Callback func,
167  void *func_cls)
168 {
169  struct GNUNET_NSE_Handle *h;
170 
171  GNUNET_assert (NULL != func);
172  h = GNUNET_new (struct GNUNET_NSE_Handle);
173  h->cfg = cfg;
174  h->recv_cb = func;
175  h->recv_cb_cls = func_cls;
176  h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
177  reconnect (h);
178  if (NULL == h->mq)
179  {
180  GNUNET_free (h);
181  return NULL;
182  }
183  return h;
184 }
185 
186 
192 void
194 {
195  if (NULL != h->reconnect_task)
196  {
198  h->reconnect_task = NULL;
199  }
200  if (NULL != h->mq)
201  {
203  h->mq = NULL;
204  }
205  GNUNET_free (h);
206 }
207 
208 
209 /* end of nse_api.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
Helper library for handling HELLOs.
API to retrieve the current network size estimate.
Constants for network protocols.
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:1057
double GNUNET_ntoh_double(double d)
Convert double to host byte order.
Definition: common_endian.c:83
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MQ_Error
Error codes for the queue.
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:304
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:77
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:683
void GNUNET_NSE_disconnect(struct GNUNET_NSE_Handle *h)
Disconnect from network size estimation service.
Definition: nse_api.c:193
void(* GNUNET_NSE_Callback)(void *cls, struct GNUNET_TIME_Absolute timestamp, double logestimate, double std_dev)
Callback to call when network size estimate is updated.
struct GNUNET_NSE_Handle * GNUNET_NSE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_NSE_Callback func, void *func_cls)
Connect to the network size estimation service.
Definition: nse_api.c:165
#define GNUNET_MESSAGE_TYPE_NSE_START
client->service message indicating start
#define GNUNET_MESSAGE_TYPE_NSE_ESTIMATE
service->client message indicating
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1272
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:737
#define GNUNET_TIME_UNIT_ZERO
Relative time zero.
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
Common type definitions for the network size estimation service and API.
static void handle_estimate(void *cls, const struct GNUNET_NSE_ClientMessage *client_msg)
Type of a function to call when we receive a message from the service.
Definition: nse_api.c:114
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
Definition: nse_api.c:93
static void reconnect(void *cls)
Try again to connect to network size estimation service.
Definition: nse_api.c:132
#define LOG(kind,...)
Definition: nse_api.c:35
struct GNUNET_MQ_Handle * mq
Our connection to the ARM service.
Definition: arm_api.c:107
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration that we are using.
Definition: arm_api.c:112
struct GNUNET_SCHEDULER_Task * reconnect_task
ID of the reconnect task (if any).
Definition: arm_api.c:147
Handle to a message queue.
Definition: mq.c:87
Message handler for a specific message type.
Header for all communications.
Network size estimate sent from the service to clients.
Definition: nse.h:43
double size_estimate
The current estimated network size.
Definition: nse.h:62
struct GNUNET_TIME_AbsoluteNBO timestamp
Timestamp at which the server received the message.
Definition: nse.h:57
double std_deviation
The standard deviation (rounded down to the nearest integer) of size estimations.
Definition: nse.h:69
Handle for talking with the NSE service.
Definition: nse_api.c:41
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: nse_api.c:45
struct GNUNET_TIME_Relative reconnect_delay
Time for next connect retry.
Definition: nse_api.c:60
struct GNUNET_MQ_Handle * mq
Message queue (if available).
Definition: nse_api.c:50
void * recv_cb_cls
Closure to pass to recv_cb callback.
Definition: nse_api.c:70
struct GNUNET_SCHEDULER_Task * reconnect_task
Task doing exponential back-off trying to reconnect.
Definition: nse_api.c:55
GNUNET_NSE_Callback recv_cb
Callback function to call when message is received.
Definition: nse_api.c:65
Entry in list of pending tasks.
Definition: scheduler.c:136
Time for relative time used by GNUnet, in microseconds.