2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public Liceidentity as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public Liceidentity for more details.
15 You should have received a copy of the GNU General Public Liceidentity
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file identity/identity_api.c
23 * @brief api to get information from the network size estimation service
24 * @author Nathan Evans
27 #include "gnunet_client_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_time_lib.h"
35 #include "gnunet_identity_service.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "identity-api",__VA_ARGS__)
41 * Handle for the service.
43 struct GNUNET_IDENTITY_Handle
46 * Configuration to use.
48 const struct GNUNET_CONFIGURATION_Handle *cfg;
51 * Socket (if available).
53 struct GNUNET_CLIENT_Connection *client;
56 * Currently pending transmission request.
58 struct GNUNET_CLIENT_TransmitHandle *th;
61 * Task doing exponential back-off trying to reconnect.
63 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
66 * Time for next connect retry.
68 struct GNUNET_TIME_Relative reconnect_delay;
74 * Try again to connect to network size estimation service.
76 * @param cls the handle to the transport service
77 * @param tc scheduler context
80 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
84 * Type of a function to call when we receive a message
88 * @param msg message received, NULL on timeout or fatal error
91 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
93 struct GNUNET_IDENTITY_Handle *h = cls;
94 const struct GNUNET_IDENTITY_ClientMessage *client_msg;
98 /* Error, timeout, death */
99 GNUNET_CLIENT_disconnect (h->client);
102 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
106 GNUNET_CLIENT_receive (h->client, &message_handler, h,
107 GNUNET_TIME_UNIT_FOREVER_REL);
113 * Reschedule a connect attempt to the service.
115 * @param h transport service to reconnect
118 reschedule_connect (struct GNUNET_IDENTITY_Handle *h)
120 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
124 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
127 if (NULL != h->client)
129 GNUNET_CLIENT_disconnect (h->client);
133 LOG (GNUNET_ERROR_TYPE_DEBUG,
134 "Scheduling task to reconnect to identity service in %llu ms.\n",
135 h->reconnect_delay.rel_value);
137 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
138 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
143 * Transmit START message to service.
146 * @param size number of bytes available in buf
147 * @param buf where to copy the message
148 * @return number of bytes copied to buf
151 send_start (void *cls, size_t size, void *buf)
153 return sizeof (struct GNUNET_MessageHeader);
158 * Try again to connect to network size estimation service.
160 * @param cls the handle to the transport service
161 * @param tc scheduler context
164 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
166 struct GNUNET_IDENTITY_Handle *h = cls;
168 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
169 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
171 /* shutdown, just give up */
174 LOG (GNUNET_ERROR_TYPE_DEBUG,
175 "Connecting to network size estimation service.\n");
176 GNUNET_assert (h->client == NULL);
177 h->client = GNUNET_CLIENT_connect ("identity", h->cfg);
178 GNUNET_assert (h->client != NULL);
181 GNUNET_CLIENT_notify_transmit_ready (h->client,
182 sizeof (struct GNUNET_MessageHeader),
183 GNUNET_TIME_UNIT_FOREVER_REL,
184 GNUNET_NO, &send_start, h);
185 GNUNET_assert (h->th != NULL);
190 * Connect to the identity service.
192 * @param cfg the configuration to use
193 * @param cb function to call on all identity events, can be NULL
194 * @param cb_cls closure for 'cb'
195 * @return handle to use
197 struct GNUNET_IDENTITY_Handle *
198 GNUNET_IDENTITY_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
199 GNUNET_IDENTITY_Callback cb,
202 struct GNUNET_IDENTITY_Handle *ret;
204 ret = GNUNET_malloc (sizeof (struct GNUNET_IDENTITY_Handle));
206 ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
207 ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
213 * Disconnect from identity service
215 * @param h handle to destroy
218 GNUNET_IDENTITY_disconnect (struct GNUNET_IDENTITY_Handle *h)
220 GNUNET_assert (NULL != h);
221 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
223 GNUNET_SCHEDULER_cancel (h->reconnect_task);
224 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
228 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
231 if (h->client != NULL)
233 GNUNET_CLIENT_disconnect (h->client);
239 /* end of identity_api.c */