aboutsummaryrefslogtreecommitdiff
path: root/src/transport
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-08-11 12:05:11 +0000
committerChristian Grothoff <christian@grothoff.org>2011-08-11 12:05:11 +0000
commit1fe306c6b89e25fddf96bb588ce7989bd4c53a59 (patch)
tree1b2936afea919d94a5865821e96081339f8d8e49 /src/transport
parentd60198e00c1b46ba3c3398cb13f51e9e04ded3b5 (diff)
downloadgnunet-1fe306c6b89e25fddf96bb588ce7989bd4c53a59.tar.gz
gnunet-1fe306c6b89e25fddf96bb588ce7989bd4c53a59.zip
more ats API hacking
Diffstat (limited to 'src/transport')
-rw-r--r--src/transport/gnunet-service-transport_ats-new.c309
-rw-r--r--src/transport/gnunet-service-transport_ats-new.h28
2 files changed, 331 insertions, 6 deletions
diff --git a/src/transport/gnunet-service-transport_ats-new.c b/src/transport/gnunet-service-transport_ats-new.c
index c731ec911..d6eef7bee 100644
--- a/src/transport/gnunet-service-transport_ats-new.c
+++ b/src/transport/gnunet-service-transport_ats-new.c
@@ -28,15 +28,80 @@
28 28
29 29
30/** 30/**
31 * Allocation record for a peer's address.
32 */
33struct AllocationRecord
34{
35
36 /**
37 * Performance information associated with this address (array).
38 */
39 struct GNUNET_TRANSPORT_ATS_Information *ats;
40
41 /**
42 * Name of the plugin
43 */
44 char *plugin_name;
45
46 /**
47 * Address this record represents, allocated at the end of this struct.
48 */
49 const void *plugin_addr;
50
51 /**
52 * Session associated with this record.
53 */
54 struct Session *session;
55
56 /**
57 * Number of bytes in plugin_addr.
58 */
59 size_t plugin_addr_len;
60
61 /**
62 * Number of entries in 'ats'.
63 */
64 uint32_t ats_count;
65
66 /**
67 * Bandwidth assigned to this address right now, 0 for none.
68 */
69 struct GNUNET_BANDWIDTH_Value32NBO bandwidth;
70
71 /**
72 * Set to GNUNET_YES if this is the connected address of a connected peer.
73 */
74 int connected;
75
76};
77
78
79/**
31 * Handle to the ATS subsystem. 80 * Handle to the ATS subsystem.
32 */ 81 */
33struct GST_AtsHandle 82struct GST_AtsHandle
34{ 83{
84 /**
85 * Configuration.
86 */
35 const struct GNUNET_CONFIGURATION_Handle *cfg; 87 const struct GNUNET_CONFIGURATION_Handle *cfg;
36 88
89 /**
90 * Function to call when the allocation changes.
91 */
37 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb; 92 GNUNET_TRANSPORT_ATS_AllocationNotification alloc_cb;
38 93
94 /**
95 * Closure for 'alloc_cb'.
96 */
39 void *alloc_cb_cls; 97 void *alloc_cb_cls;
98
99 /**
100 * Information about all connected peers. Maps peer identities
101 * to one or more 'struct AllocationRecord' values.
102 */
103 struct GNUNET_CONTAINER_MultiHashMap *peers;
104
40}; 105};
41 106
42 107
@@ -59,11 +124,34 @@ GST_ats_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
59 atc->cfg = cfg; 124 atc->cfg = cfg;
60 atc->alloc_cb = alloc_cb; 125 atc->alloc_cb = alloc_cb;
61 atc->alloc_cb_cls = alloc_cb_cls; 126 atc->alloc_cb_cls = alloc_cb_cls;
127 atc->peers = GNUNET_CONTAINER_multihashmap_create (256);
62 return atc; 128 return atc;
63} 129}
64 130
65 131
66/** 132/**
133 * Free an allocation record.
134 *
135 * @param cls unused
136 * @param key identity of the peer associated with the record
137 * @param value the 'struct AllocationRecord' to free
138 * @return GNUNET_OK (continue to iterate)
139 */
140static int
141destroy_allocation_record (void *cls,
142 const GNUNET_HashCode *key,
143 void *value)
144{
145 struct AllocationRecord *ar = value;
146
147 GNUNET_array_grow (ar->ats, ar->ats_count, 0);
148 GNUNET_free (ar->plugin_name);
149 GNUNET_free (ar);
150 return GNUNET_OK;
151}
152
153
154/**
67 * Shutdown the ATS subsystem. 155 * Shutdown the ATS subsystem.
68 * 156 *
69 * @param atc handle 157 * @param atc handle
@@ -71,11 +159,88 @@ GST_ats_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
71void 159void
72GST_ats_shutdown (struct GST_AtsHandle *atc) 160GST_ats_shutdown (struct GST_AtsHandle *atc)
73{ 161{
162 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
163 &destroy_allocation_record,
164 NULL);
165 GNUNET_CONTAINER_multihashmap_destroy (atc->peers);
74 GNUNET_free (atc); 166 GNUNET_free (atc);
75} 167}
76 168
77 169
78/** 170/**
171 * Update an allocation record, merging with the new information
172 *
173 * @param cls a new 'struct AllocationRecord'
174 * @param key identity of the peer associated with the records
175 * @param value the old 'struct AllocationRecord'
176 * @return GNUNET_YES if the records do not match,
177 * GNUNET_NO if the record do match and 'old' was updated
178 */
179static int
180update_session (void *cls,
181 const GNUNET_HashCode *key,
182 void *value)
183{
184 struct AllocationRecord *arnew = cls;
185 struct AllocationRecord *arold = value;
186
187 if (0 != strcmp (arnew->plugin_name, arold->plugin_name))
188 return GNUNET_YES;
189 if ( (arnew->session == arold->session) ||
190 ( (arold->session == NULL) &&
191 (arold->plugin_addr_len == arnew->plugin_addr_len) &&
192 (0 == memcmp (arold->plugin_addr,
193 arnew->plugin_addr,
194 arnew->plugin_addr_len)) ) )
195 {
196 /* records match */
197 arold->session = arnew->session;
198 if (arnew->connected == GNUNET_YES)
199 arold->connected = GNUNET_YES;
200 // FIXME: merge ats arrays of (arold, arnew);
201 return GNUNET_NO;
202 }
203 return GNUNET_YES;
204}
205
206
207/**
208 * Create an allocation record with the given properties.
209 *
210 * @param plugin_name name of the currently used transport plugin
211 * @param session session in use (if available)
212 * @param plugin_addr address in use (if available)
213 * @param plugin_addr_len number of bytes in plugin_addr
214 * @param ats performance data for the connection
215 * @param ats_count number of performance records in 'ats'
216 */
217static struct AllocationRecord *
218create_allocation_record (const char *plugin_name,
219 struct Session *session,
220 const void *plugin_addr,
221 size_t plugin_addr_len,
222 const struct GNUNET_TRANSPORT_ATS_Information *ats,
223 uint32_t ats_count)
224{
225 struct AllocationRecord *ar;
226
227 ar = GNUNET_malloc (sizeof (struct AllocationRecord) + plugin_addr_len);
228 ar->plugin_name = GNUNET_strdup (plugin_name);
229 ar->plugin_addr = &ar[1];
230 memcpy (&ar[1], plugin_addr, plugin_addr_len);
231 ar->session = session;
232 ar->plugin_addr_len = plugin_addr_len;
233 GNUNET_array_grow (ar->ats,
234 ar->ats_count,
235 ats_count);
236 memcpy (ar->ats,
237 ats,
238 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
239 return ar;
240}
241
242
243/**
79 * We established a new connection with a peer (for example, because 244 * We established a new connection with a peer (for example, because
80 * core asked for it or because the other peer connected to us). 245 * core asked for it or because the other peer connected to us).
81 * Calculate bandwidth assignments including the new peer. 246 * Calculate bandwidth assignments including the new peer.
@@ -83,7 +248,8 @@ GST_ats_shutdown (struct GST_AtsHandle *atc)
83 * @param atc handle 248 * @param atc handle
84 * @param peer identity of the new peer 249 * @param peer identity of the new peer
85 * @param plugin_name name of the currently used transport plugin 250 * @param plugin_name name of the currently used transport plugin
86 * @param plugin_addr address in use 251 * @param session session in use (if available)
252 * @param plugin_addr address in use (if available)
87 * @param plugin_addr_len number of bytes in plugin_addr 253 * @param plugin_addr_len number of bytes in plugin_addr
88 * @param ats performance data for the connection 254 * @param ats performance data for the connection
89 * @param ats_count number of performance records in 'ats' 255 * @param ats_count number of performance records in 'ats'
@@ -92,11 +258,54 @@ void
92GST_ats_peer_connect (struct GST_AtsHandle *atc, 258GST_ats_peer_connect (struct GST_AtsHandle *atc,
93 const struct GNUNET_PeerIdentity *peer, 259 const struct GNUNET_PeerIdentity *peer,
94 const char *plugin_name, 260 const char *plugin_name,
261 struct Session *session,
95 const void *plugin_addr, 262 const void *plugin_addr,
96 size_t plugin_addr_len, 263 size_t plugin_addr_len,
97 const struct GNUNET_TRANSPORT_ATS_Information *ats, 264 const struct GNUNET_TRANSPORT_ATS_Information *ats,
98 uint32_t ats_count) 265 uint32_t ats_count)
99{ 266{
267 struct AllocationRecord *ar;
268
269 ar = create_allocation_record (plugin_name,
270 session,
271 plugin_addr,
272 plugin_addr_len,
273 ats,
274 ats_count);
275 ar->connected = GNUNET_YES;
276 if (GNUNET_SYSERR ==
277 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
278 &update_session,
279 ar))
280 {
281 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
282 return;
283 }
284 GNUNET_assert (GNUNET_OK ==
285 GNUNET_CONTAINER_multihashmap_put (atc->peers,
286 &peer->hashPubKey,
287 ar,
288 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
289}
290
291
292/**
293 * Mark all matching allocation records as not connected.
294 *
295 * @param cls unused
296 * @param key identity of the peer associated with the record
297 * @param value the 'struct AllocationRecord' to clear the 'connected' flag
298 * @return GNUNET_OK (continue to iterate)
299 */
300static int
301disconnect_peer (void *cls,
302 const GNUNET_HashCode *key,
303 void *value)
304{
305 struct AllocationRecord *ar = value;
306
307 ar->connected = GNUNET_NO;
308 return GNUNET_OK;
100} 309}
101 310
102 311
@@ -112,6 +321,78 @@ void
112GST_ats_peer_disconnect (struct GST_AtsHandle *atc, 321GST_ats_peer_disconnect (struct GST_AtsHandle *atc,
113 const struct GNUNET_PeerIdentity *peer) 322 const struct GNUNET_PeerIdentity *peer)
114{ 323{
324 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
325 &disconnect_peer,
326 NULL);
327}
328
329
330/**
331 * Closure for 'destroy_allocation_record'
332 */
333struct SessionDestroyContext
334{
335 /**
336 * Ats handle.
337 */
338 struct GST_AtsHandle *atc;
339
340 /**
341 * Session being destroyed.
342 */
343 const struct Session *session;
344};
345
346
347/**
348 * Free an allocation record matching the given session.
349 *
350 * @param cls the 'struct SessionDestroyContext'
351 * @param key identity of the peer associated with the record
352 * @param value the 'struct AllocationRecord' to free
353 * @return GNUNET_OK (continue to iterate)
354 */
355static int
356destroy_session (void *cls,
357 const GNUNET_HashCode *key,
358 void *value)
359{
360 struct SessionDestroyContext *sdc = cls;
361 struct AllocationRecord *ar = value;
362
363 if (ar->session != sdc->session)
364 return GNUNET_OK;
365 ar->session = NULL;
366 if (ar->plugin_addr != NULL)
367 return GNUNET_OK;
368 GNUNET_assert (GNUNET_OK ==
369 GNUNET_CONTAINER_multihashmap_remove (sdc->atc->peers,
370 key,
371 ar));
372 destroy_allocation_record (NULL, key, ar);
373 return GNUNET_OK;
374}
375
376
377/**
378 * A session got destroyed, stop including it as a valid address.
379 *
380 * @param atc handle
381 * @param peer identity of the peer
382 * @param session session handle that is no longer valid
383 */
384void
385GST_ats_session_destroyed (struct GST_AtsHandle *atc,
386 const struct GNUNET_PeerIdentity *peer,
387 const struct Session *session)
388{
389 struct SessionDestroyContext sdc;
390
391 sdc.atc = atc;
392 sdc.session = session;
393 (void) GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
394 &destroy_session,
395 &sdc);
115} 396}
116 397
117 398
@@ -126,7 +407,8 @@ GST_ats_peer_disconnect (struct GST_AtsHandle *atc,
126 * @param atc handle 407 * @param atc handle
127 * @param peer identity of the new peer 408 * @param peer identity of the new peer
128 * @param plugin_name name of the transport plugin 409 * @param plugin_name name of the transport plugin
129 * @param plugin_addr address 410 * @param session session handle (if available)
411 * @param plugin_addr address (if available)
130 * @param plugin_addr_len number of bytes in plugin_addr 412 * @param plugin_addr_len number of bytes in plugin_addr
131 * @param ats performance data for the address 413 * @param ats performance data for the address
132 * @param ats_count number of performance records in 'ats' 414 * @param ats_count number of performance records in 'ats'
@@ -135,11 +417,34 @@ void
135GST_ats_address_update (struct GST_AtsHandle *atc, 417GST_ats_address_update (struct GST_AtsHandle *atc,
136 const struct GNUNET_PeerIdentity *peer, 418 const struct GNUNET_PeerIdentity *peer,
137 const char *plugin_name, 419 const char *plugin_name,
420 struct Session *session,
138 const void *plugin_addr, 421 const void *plugin_addr,
139 size_t plugin_addr_len, 422 size_t plugin_addr_len,
140 const struct GNUNET_TRANSPORT_ATS_Information *ats, 423 const struct GNUNET_TRANSPORT_ATS_Information *ats,
141 uint32_t ats_count) 424 uint32_t ats_count)
142{ 425{
426 struct AllocationRecord *ar;
427
428 ar = create_allocation_record (plugin_name,
429 session,
430 plugin_addr,
431 plugin_addr_len,
432 ats,
433 ats_count);
434
435 if (GNUNET_SYSERR ==
436 GNUNET_CONTAINER_multihashmap_iterate (atc->peers,
437 &update_session,
438 ar))
439 {
440 destroy_allocation_record (NULL, &peer->hashPubKey, ar);
441 return;
442 }
443 GNUNET_assert (GNUNET_OK ==
444 GNUNET_CONTAINER_multihashmap_put (atc->peers,
445 &peer->hashPubKey,
446 ar,
447 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
143} 448}
144 449
145/* end of file gnunet-service-transport_ats.c */ 450/* end of file gnunet-service-transport_ats.c */
diff --git a/src/transport/gnunet-service-transport_ats-new.h b/src/transport/gnunet-service-transport_ats-new.h
index 360674bb9..e4e85fee1 100644
--- a/src/transport/gnunet-service-transport_ats-new.h
+++ b/src/transport/gnunet-service-transport_ats-new.h
@@ -29,6 +29,7 @@
29#include "gnunet_constants.h" 29#include "gnunet_constants.h"
30#include "gnunet_util_lib.h" 30#include "gnunet_util_lib.h"
31#include "gnunet_transport_service.h" 31#include "gnunet_transport_service.h"
32#include "gnunet_transport_plugin.h"
32 33
33 34
34/** 35/**
@@ -47,13 +48,15 @@ struct GST_AtsHandle;
47 * @param cls closure 48 * @param cls closure
48 * @param peer identity of the peer 49 * @param peer identity of the peer
49 * @param plugin_name name of the transport plugin, NULL to disconnect 50 * @param plugin_name name of the transport plugin, NULL to disconnect
50 * @param plugin_addr address to use, NULL to disconnect 51 * @param session session to use (if available)
52 * @param plugin_addr address to use (if available)
51 * @param plugin_addr_len number of bytes in addr 53 * @param plugin_addr_len number of bytes in addr
52 * @param bandwidth assigned bandwidth for the connection 54 * @param bandwidth assigned bandwidth for the connection
53 */ 55 */
54typedef void (*GNUNET_TRANSPORT_ATS_AllocationNotification)(void *cls, 56typedef void (*GNUNET_TRANSPORT_ATS_AllocationNotification)(void *cls,
55 const struct GNUNET_PeerIdentity *peer, 57 const struct GNUNET_PeerIdentity *peer,
56 const char *plugin_name, 58 const char *plugin_name,
59 struct Session *session,
57 const void *plugin_addr, 60 const void *plugin_addr,
58 size_t plugin_addr_len, 61 size_t plugin_addr_len,
59 struct GNUNET_BANDWIDTH_Value32NBO bandwidth); 62 struct GNUNET_BANDWIDTH_Value32NBO bandwidth);
@@ -89,7 +92,8 @@ GST_ats_shutdown (struct GST_AtsHandle *atc);
89 * @param atc handle 92 * @param atc handle
90 * @param peer identity of the new peer 93 * @param peer identity of the new peer
91 * @param plugin_name name of the currently used transport plugin 94 * @param plugin_name name of the currently used transport plugin
92 * @param plugin_addr address in use 95 * @param session session in use (if available)
96 * @param plugin_addr address in use (if available)
93 * @param plugin_addr_len number of bytes in plugin_addr 97 * @param plugin_addr_len number of bytes in plugin_addr
94 * @param ats performance data for the connection 98 * @param ats performance data for the connection
95 * @param ats_count number of performance records in 'ats' 99 * @param ats_count number of performance records in 'ats'
@@ -98,6 +102,7 @@ void
98GST_ats_peer_connect (struct GST_AtsHandle *atc, 102GST_ats_peer_connect (struct GST_AtsHandle *atc,
99 const struct GNUNET_PeerIdentity *peer, 103 const struct GNUNET_PeerIdentity *peer,
100 const char *plugin_name, 104 const char *plugin_name,
105 struct Session *session,
101 const void *plugin_addr, 106 const void *plugin_addr,
102 size_t plugin_addr_len, 107 size_t plugin_addr_len,
103 const struct GNUNET_TRANSPORT_ATS_Information *ats, 108 const struct GNUNET_TRANSPORT_ATS_Information *ats,
@@ -110,7 +115,7 @@ GST_ats_peer_connect (struct GST_AtsHandle *atc,
110 * Calculate bandwidth assignments without the peer. 115 * Calculate bandwidth assignments without the peer.
111 * 116 *
112 * @param atc handle 117 * @param atc handle
113 * @param peer identity of the new peer 118 * @param peer identity of the peer
114 */ 119 */
115void 120void
116GST_ats_peer_disconnect (struct GST_AtsHandle *atc, 121GST_ats_peer_disconnect (struct GST_AtsHandle *atc,
@@ -118,6 +123,19 @@ GST_ats_peer_disconnect (struct GST_AtsHandle *atc,
118 123
119 124
120/** 125/**
126 * A session got destroyed, stop including it as a valid address.
127 *
128 * @param atc handle
129 * @param peer identity of the peer
130 * @param session session handle that is no longer valid
131 */
132void
133GST_ats_session_destroyed (struct GST_AtsHandle *atc,
134 const struct GNUNET_PeerIdentity *peer,
135 const struct Session *session);
136
137
138/**
121 * We have updated performance statistics for a given address. Note 139 * We have updated performance statistics for a given address. Note
122 * that this function can be called for addresses that are currently 140 * that this function can be called for addresses that are currently
123 * in use as well as addresses that are valid but not actively in use. 141 * in use as well as addresses that are valid but not actively in use.
@@ -128,7 +146,8 @@ GST_ats_peer_disconnect (struct GST_AtsHandle *atc,
128 * @param atc handle 146 * @param atc handle
129 * @param peer identity of the new peer 147 * @param peer identity of the new peer
130 * @param plugin_name name of the transport plugin 148 * @param plugin_name name of the transport plugin
131 * @param plugin_addr address 149 * @param session session handle (if available)
150 * @param plugin_addr address (if available)
132 * @param plugin_addr_len number of bytes in plugin_addr 151 * @param plugin_addr_len number of bytes in plugin_addr
133 * @param ats performance data for the address 152 * @param ats performance data for the address
134 * @param ats_count number of performance records in 'ats' 153 * @param ats_count number of performance records in 'ats'
@@ -137,6 +156,7 @@ void
137GST_ats_address_update (struct GST_AtsHandle *atc, 156GST_ats_address_update (struct GST_AtsHandle *atc,
138 const struct GNUNET_PeerIdentity *peer, 157 const struct GNUNET_PeerIdentity *peer,
139 const char *plugin_name, 158 const char *plugin_name,
159 struct Session *session,
140 const void *plugin_addr, 160 const void *plugin_addr,
141 size_t plugin_addr_len, 161 size_t plugin_addr_len,
142 const struct GNUNET_TRANSPORT_ATS_Information *ats, 162 const struct GNUNET_TRANSPORT_ATS_Information *ats,