GNUnet  0.10.x
ats_api2_application.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010-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 */
26 #include "platform.h"
28 #include "ats2.h"
29 
30 
31 #define LOG(kind,...) GNUNET_log_from(kind, "ats-application-api", __VA_ARGS__)
32 
33 
38 {
43 
48 
53 
58 };
59 
60 
65 {
66 
71 
78 
83 
88 
93 };
94 
95 
101 static void
103 
104 
110 static void
111 reconnect_task (void *cls)
112 {
113  struct GNUNET_ATS_ApplicationHandle *ch = cls;
114 
115  ch->task = NULL;
116  reconnect (ch);
117 }
118 
119 
125 static void
127 {
128  if (NULL != ch->mq)
129  {
130  GNUNET_MQ_destroy (ch->mq);
131  ch->mq = NULL;
132  }
136  ch);
137 }
138 
139 
147 static void
148 error_handler (void *cls,
149  enum GNUNET_MQ_Error error)
150 {
151  struct GNUNET_ATS_ApplicationHandle *ch = cls;
152 
154  "ATS connection died (code %d), reconnecting\n",
155  (int) error);
156  force_reconnect (ch);
157 }
158 
159 
169 static int
171  const struct GNUNET_PeerIdentity *peer,
172  void *value)
173 {
174  struct GNUNET_ATS_ApplicationHandle *ch = cls;
176  struct GNUNET_MQ_Envelope *ev;
177  struct ExpressPreferenceMessage *m;
178 
179  if (NULL == ch->mq)
180  return GNUNET_SYSERR;
181  ev = GNUNET_MQ_msg (m,
183  m->pk = htonl ((uint32_t) sh->pk);
184  m->bw = sh->bw;
185  m->peer = *peer;
186  GNUNET_MQ_send (ch->mq, ev);
187  return GNUNET_OK;
188 }
189 
190 
196 static void
198 {
199  static const struct GNUNET_MQ_MessageHandler handlers[] = {
200  { NULL, 0, 0 }
201  };
202 
203  GNUNET_assert (NULL == ch->mq);
204  ch->mq = GNUNET_CLIENT_connect (ch->cfg,
205  "ats",
206  handlers,
207  &error_handler,
208  ch);
209  if (NULL == ch->mq)
210  {
211  force_reconnect (ch);
212  return;
213  }
216  ch);
217 }
218 
219 
228 {
230 
232  ch->cfg = cfg;
234  GNUNET_YES);
235  reconnect (ch);
236  return ch;
237 }
238 
239 
249 static int
250 free_sug_handle (void *cls,
251  const struct GNUNET_PeerIdentity *key,
252  void *value)
253 {
255 
256  GNUNET_free (cur);
257  return GNUNET_OK;
258 }
259 
260 
266 void
268 {
269  if (NULL != ch->mq)
270  {
271  GNUNET_MQ_destroy (ch->mq);
272  ch->mq = NULL;
273  }
274  if (NULL != ch->task)
275  {
277  ch->task = NULL;
278  }
281  NULL);
283  GNUNET_free (ch);
284 }
285 
286 
302  const struct GNUNET_PeerIdentity *peer,
304  struct GNUNET_BANDWIDTH_Value32NBO bw)
305 {
307 
309  s->ch = ch;
310  s->id = *peer;
311  s->pk = pk;
312  s->bw = bw;
314  &s->id,
315  s,
318  "Requesting ATS to suggest address for `%s'\n",
319  GNUNET_i2s (peer));
320  if (NULL == ch->mq)
321  return s;
324  &s->id,
325  s));
326  return s;
327 }
328 
329 
335 void
337 {
338  struct GNUNET_ATS_ApplicationHandle *ch = sh->ch;
339  struct GNUNET_MQ_Envelope *ev;
340  struct ExpressPreferenceMessage *m;
341 
343  "Telling ATS we no longer care for an address for `%s'\n",
344  GNUNET_i2s (&sh->id));
347  &sh->id,
348  sh));
349  if (NULL == ch->mq)
350  {
351  GNUNET_free (sh);
352  return;
353  }
354  ev = GNUNET_MQ_msg (m,
356  m->pk = htonl ((uint32_t) sh->pk);
357  m->bw = sh->bw;
358  m->peer = sh->id;
359  GNUNET_MQ_send (ch->mq,
360  ev);
361  GNUNET_free (sh);
362 }
363 
364 
365 /* end of ats_api2_application.c */
static void error_handler(void *cls, enum GNUNET_MQ_Error error)
We encountered an error handling the MQ to the ATS service.
static void reconnect(struct GNUNET_ATS_ApplicationHandle *ch)
Re-establish the connection to the ATS service.
static struct GNUNET_TIME_Relative backoff
How long should we wait to reconnect?
Definition: resolver_api.c:81
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:901
struct GNUNET_ATS_ApplicationHandle * ch
Connecitivity handle this suggestion handle belongs to.
GNUNET_MQ_Error
Error codes for the queue.
static int transmit_suggestion(void *cls, const struct GNUNET_PeerIdentity *peer, void *value)
Transmit request for an address suggestion.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
int GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, const void *value)
Remove the given key-value pair from the map.
#define LOG(kind,...)
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
static void force_reconnect(struct GNUNET_ATS_ApplicationHandle *ch)
Disconnect from ATS and then reconnect.
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
struct GNUNET_SCHEDULER_Task * task
Task to trigger reconnect.
uint32_t pk
What type of performance preference does the client have? A enum GNUNET_MQ_PreferenceKind in NBO...
Definition: ats2.h:125
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:99
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:1246
GNUNET_MQ_PreferenceKind
Enum defining all known preference categories.
static char * value
Value of the record to add/remove.
static struct SolverHandle * sh
Handle to the ATS subsystem for application management.
Internal representation of the hash map.
automatic transport selection messages
static void reconnect_task(void *cls)
Re-establish the connection to the ATS service.
Message handler for a specific message type.
struct GNUNET_PeerIdentity id
ID of the peer for which address suggestion was requested.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
static int free_sug_handle(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Function called to free all struct GNUNET_ATS_ApplicationSuggestHandles in the map.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
#define GNUNET_MESSAGE_TYPE_ATS_SUGGEST
Type of the &#39;struct ExpressPreferenceMessage&#39; send by clients to ATS to establish bandwidth preferenc...
struct GNUNET_ATS_ApplicationHandle * GNUNET_ATS_application_init(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the ATS application suggestion client handle.
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
32-bit bandwidth used for network exchange by GNUnet, in bytes per second.
#define GNUNET_MESSAGE_TYPE_ATS_SUGGEST_CANCEL
Type of the &#39;struct ExpressPreferenceMessage&#39; send by clients to ATS to abandon bandwidth preference...
void GNUNET_ATS_application_suggest_cancel(struct GNUNET_ATS_ApplicationSuggestHandle *sh)
We no longer care about being connected to a peer.
struct GNUNET_ATS_ApplicationSuggestHandle * GNUNET_ATS_application_suggest(struct GNUNET_ATS_ApplicationHandle *ch, const struct GNUNET_PeerIdentity *peer, enum GNUNET_MQ_PreferenceKind pk, struct GNUNET_BANDWIDTH_Value32NBO bw)
We would like to receive address suggestions for a peer.
enum GNUNET_MQ_PreferenceKind pk
What preference is being expressed?
Allow multiple values with the same key.
Handle to a message queue.
Definition: mq.c:85
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map.
The identity of the host (wraps the signing key of the peer).
struct GNUNET_TIME_Relative backoff
Reconnect backoff delay.
configuration data
Definition: configuration.c:85
Handle for ATS address suggestion requests.
void GNUNET_ATS_application_done(struct GNUNET_ATS_ApplicationHandle *ch)
Client is done with ATS application management, release resources.
Entry in list of pending tasks.
Definition: scheduler.c:134
struct GNUNET_CONTAINER_MultiPeerMap * sug_requests
Map with the identities of all the peers for which we would like to have address suggestions.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
#define GNUNET_YES
Definition: gnunet_common.h:80
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:353
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
struct GNUNET_MQ_Handle * mq
Message queue for sending requests to the ATS service.
Bandwidth allocation API for applications to interact with.
struct GNUNET_BANDWIDTH_Value32NBO bw
How much bandwidth in bytes/second does the application expect?
Definition: ats2.h:135
Application client to ATS service: we would like to have address suggestions for this peer...
Definition: ats2.h:112
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
struct GNUNET_PeerIdentity peer
Peer to get address suggestions for.
Definition: ats2.h:130
struct GNUNET_BANDWIDTH_Value32NBO bw
How much bandwidth does the client expect?
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965