2 This file is part of GNUnet.
3 (C) 2012 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 License as published
7 by the Free Software Foundation; either version 2, 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 License for more details.
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/gnunet-service-testbed_cache.h
23 * @brief testbed cache implementation
24 * @author Sree Harsha Totakura
26 #include "gnunet-service-testbed.h"
29 * Redefine LOG with a changed log component string
34 #define LOG(kind,...) \
35 GNUNET_log_from (kind, "testbed-cache", __VA_ARGS__)
39 * Time to expire a cache entry
41 #define CACHE_EXPIRY \
42 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
46 * Type of cache-get requests
51 * Get transport handle
53 CGT_TRANSPORT_HANDLE = 1,
63 * The cache-get request handle
65 struct GSTCacheGetHandle;
69 * This context structure is used to maintain a queue of notifications to check
70 * which of them are to be notified when a peer is connected.
72 struct ConnectNotifyContext
75 * The next ptr for the DLL
77 struct ConnectNotifyContext *next;
80 * The prev ptr for the DLL
82 struct ConnectNotifyContext *prev;
85 * The peer identity of the target peer. When this target peer is connected,
86 * call the notify callback
88 const struct GNUNET_PeerIdentity *target;
91 * The notify callback to be called when the target peer is connected
93 GST_cache_peer_connect_notify cb;
96 * The closure for the notify callback
101 * The GSTCacheGetHandle reposible for creating this context
103 struct GSTCacheGetHandle *cgh;
109 * The cache-get request handle
111 struct GSTCacheGetHandle
114 * The next ptr for the DLL. Used in struct CacheEntry
116 struct GSTCacheGetHandle *next;
119 * The prev ptr for the DLL. Used in struct CacheEntry
121 struct GSTCacheGetHandle *prev;
124 * The cache entry object this handle corresponds to
126 struct CacheEntry *entry;
129 * The cache callback to call when a handle is available
131 GST_cache_handle_ready_cb cb;
134 * The closure for the above callback
139 * The peer connect notify context created for this handle; can be NULL
141 struct ConnectNotifyContext *nctxt;
144 * The type of this cache-get request
146 enum CacheGetType type;
149 * Did we call the cache callback already?
160 * DLL next ptr for least recently used cache entries
162 struct CacheEntry *next;
165 * DLL prev ptr for least recently used cache entries
167 struct CacheEntry *prev;
170 * The transport handle to the peer corresponding to this entry; can be NULL
172 struct GNUNET_TRANSPORT_Handle *transport_handle;
175 * The operation handle for transport handle
177 struct GNUNET_TESTBED_Operation *transport_op;
180 * The core handle to the peer corresponding to this entry; can be NULL
182 struct GNUNET_CORE_Handle *core_handle;
185 * The operation handle for core handle
187 struct GNUNET_TESTBED_Operation *core_op;
190 * The peer identity of this peer. Will be set upon opening a connection to
191 * the peers CORE service. Will be NULL until then and after the CORE
192 * connection is closed
194 struct GNUNET_PeerIdentity *peer_identity;
197 * The configuration of the peer. Should be not NULL as long as the core_handle
198 * or transport_handle are valid
200 struct GNUNET_CONFIGURATION_Handle *cfg;
203 * The key for this entry
205 struct GNUNET_HashCode key;
210 struct GNUNET_MessageHeader *hello;
213 * the head of the CacheGetHandle queue
215 struct GSTCacheGetHandle *cgh_qhead;
218 * the tail of the CacheGetHandle queue
220 struct GSTCacheGetHandle *cgh_qtail;
223 * DLL head for the queue of notifications contexts to check which of them are to
224 * be notified when a peer is connected.
226 struct ConnectNotifyContext *nctxt_qhead;
229 * DLL tail for the queue of notifications contexts to check which of them are to
230 * be notified when a peer is connected.
232 struct ConnectNotifyContext *nctxt_qtail;
235 * The task that calls the cache callback
237 GNUNET_SCHEDULER_TaskIdentifier notify_task;
240 * The task to expire this cache entry, free any handlers it has opened and
241 * mark their corresponding operations as done.
243 GNUNET_SCHEDULER_TaskIdentifier expire_task;
246 * Number of operations this cache entry is being used
251 * The id of the peer this entry corresponds to
253 unsigned int peer_id;
256 * Is this entry in LRU cache queue?
263 * Hashmap to maintain cache
265 static struct GNUNET_CONTAINER_MultiHashMap *cache;
268 * DLL head for least recently used cache entries; least recently used
269 * cache items are at the head. The cache enties are added to this queue when
270 * their demand becomes zero. They are removed from the queue when they are
271 * needed by any operation.
273 static struct CacheEntry *lru_cache_head;
276 * DLL tail for least recently used cache entries; recently used cache
277 * items are at the tail.The cache enties are added to this queue when
278 * their demand becomes zero. They are removed from the queue when they are
279 * needed by any operation.
281 static struct CacheEntry *lru_cache_tail;
284 * the size of the LRU queue
286 static unsigned int lru_cache_size;
289 * the threshold size for the LRU queue
291 static unsigned int lru_cache_threshold_size;
294 * The total number of elements in cache
296 static unsigned int cache_size;
300 * Looks up in the cache and returns the entry
302 * @param id the peer identity of the peer whose corresponding entry has to be looked up
303 * @return the HELLO message; NULL if not found
305 static struct CacheEntry *
306 cache_lookup (const struct GNUNET_HashCode *key)
308 struct CacheEntry *entry;
312 entry = GNUNET_CONTAINER_multihashmap_get (cache, key);
318 * Function to disconnect the core and transport handles; free the existing
319 * configuration; and remove from the LRU cache list. The entry is left to be in
320 * the hash table so that the HELLO can still be found later
322 * @param entry the cache entry
325 close_handles (struct CacheEntry *entry)
327 struct ConnectNotifyContext *ctxt;
329 GNUNET_assert (0 == entry->demand);
330 if (GNUNET_YES == entry->in_lru)
332 GNUNET_assert (0 < lru_cache_size);
333 if (GNUNET_SCHEDULER_NO_TASK != entry->expire_task)
335 GNUNET_SCHEDULER_cancel (entry->expire_task);
336 entry->expire_task = GNUNET_SCHEDULER_NO_TASK;
338 GNUNET_CONTAINER_DLL_remove (lru_cache_head, lru_cache_tail, entry);
340 entry->in_lru = GNUNET_NO;
342 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == entry->expire_task);
343 while (NULL != (ctxt = entry->nctxt_qhead))
345 GNUNET_CONTAINER_DLL_remove (entry->nctxt_qhead, entry->nctxt_qtail, ctxt);
348 LOG_DEBUG ("Cleaning up handles from an entry in cache\n");
349 if (NULL != entry->transport_handle)
350 GNUNET_assert (NULL != entry->transport_op);
351 if (NULL != entry->transport_op)
353 GNUNET_TESTBED_operation_done (entry->transport_op);
354 entry->transport_op = NULL;
356 if (NULL != entry->core_op)
358 GNUNET_TESTBED_operation_done (entry->core_op);
359 entry->core_op = NULL;
361 GNUNET_assert (NULL == entry->core_handle);
362 if (NULL != entry->cfg)
364 GNUNET_CONFIGURATION_destroy (entry->cfg);
371 * The task to expire this cache entry, free any handlers it has opened and
372 * mark their corresponding operations as done.
374 * @param cls the CacheEntry
375 * @param tc the scheduler task context
378 expire_cache_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
380 struct CacheEntry *entry = cls;
382 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != entry->expire_task);
383 entry->expire_task = GNUNET_SCHEDULER_NO_TASK;
384 close_handles (entry);
389 * Creates a new cache entry and then puts it into the cache's hashtable.
391 * @param key the hash code to use for inserting the newly created entry
392 * @param peer_id the index of the peer to tag the newly created entry
393 * @return the newly created entry
395 static struct CacheEntry *
396 add_entry (const struct GNUNET_HashCode *key, unsigned int peer_id)
398 struct CacheEntry *entry;
400 entry = GNUNET_malloc (sizeof (struct CacheEntry));
401 entry->peer_id = peer_id;
402 memcpy (&entry->key, key, sizeof (struct GNUNET_HashCode));
403 GNUNET_assert (GNUNET_OK ==
404 GNUNET_CONTAINER_multihashmap_put (cache, &entry->key, entry,
405 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
412 * Function to find a suitable GSTCacheGetHandle which is waiting for one of the
413 * handles in given entry to be available.
415 * @param entry the cache entry whose GSTCacheGetHandle list has to be searched
416 * @param head the starting list element in the GSTCacheGetHandle where the
417 * search has to be begin
418 * @return a suitable GSTCacheGetHandle whose handle ready notify callback
419 * hasn't been called yet. NULL if no such suitable GSTCacheGetHandle
422 static struct GSTCacheGetHandle *
423 search_suitable_cgh (const struct CacheEntry *entry,
424 const struct GSTCacheGetHandle *head)
426 const struct GSTCacheGetHandle *cgh;
428 for (cgh = head; NULL != cgh; cgh = cgh->next)
430 if (GNUNET_YES == cgh->notify_called)
434 case CGT_TRANSPORT_HANDLE:
435 if (NULL == entry->transport_handle)
438 case CGT_CORE_HANDLE:
439 if (NULL == entry->core_handle)
441 if (NULL == entry->peer_identity) /* Our CORE connection isn't ready yet */
447 return (struct GSTCacheGetHandle *) cgh;
452 * Task to call the handle ready notify callback of a queued GSTCacheGetHandle
453 * of an entry when one or all of its handles are available.
455 * @param cls the cache entry
456 * @param tc the task context from scheduler
459 call_cgh_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
461 struct CacheEntry *entry = cls;
462 struct GSTCacheGetHandle *cgh;
463 const struct GSTCacheGetHandle *cgh2;
465 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != entry->notify_task);
466 entry->notify_task = GNUNET_SCHEDULER_NO_TASK;
467 cgh = search_suitable_cgh (entry, entry->cgh_qhead);
468 GNUNET_assert (NULL != cgh);
470 if (NULL != cgh->next)
471 cgh2 = search_suitable_cgh (entry, cgh->next);
472 GNUNET_CONTAINER_DLL_remove (entry->cgh_qhead, entry->cgh_qtail, cgh);
473 cgh->notify_called = GNUNET_YES;
474 GNUNET_CONTAINER_DLL_insert_tail (entry->cgh_qhead, entry->cgh_qtail, cgh);
476 entry->notify_task = GNUNET_SCHEDULER_add_now (&call_cgh_cb, entry);
477 if (NULL != cgh->nctxt)
478 { /* Register the peer connect notify callback */
479 GNUNET_CONTAINER_DLL_insert_tail (entry->nctxt_qhead, entry->nctxt_qtail,
482 LOG_DEBUG ("Calling notify for handle type %u\n", cgh->type);
483 GNUNET_break ((NULL == entry->core_handle) || (NULL != entry->peer_identity));
484 cgh->cb (cgh->cb_cls, entry->core_handle, entry->transport_handle,
485 entry->peer_identity);
490 * Function called from peer connect notify callbacks from CORE and TRANSPORT
491 * connections. This function calls the pendning peer connect notify callbacks
492 * which are queued in an entry.
494 * @param cls the cache entry
495 * @param peer the peer that connected
496 * @param type the type of the handle this notification corresponds to
499 peer_connect_notify_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
500 const enum CacheGetType type)
502 struct CacheEntry *entry = cls;
503 struct ConnectNotifyContext *ctxt;
504 struct ConnectNotifyContext *ctxt2;
505 GST_cache_peer_connect_notify cb;
509 for (ctxt = entry->nctxt_qhead; NULL != ctxt;)
511 GNUNET_assert (NULL != ctxt->cgh);
512 if (type != ctxt->cgh->type)
517 if (0 != memcmp (ctxt->target, peer, sizeof (struct GNUNET_PeerIdentity)))
523 cb_cls = ctxt->cb_cls;
524 ctxt->cgh->nctxt = NULL;
526 GNUNET_CONTAINER_DLL_remove (entry->nctxt_qhead, entry->nctxt_qtail, ctxt);
538 * Function called to notify transport users that another
539 * peer connected to us.
542 * @param peer the peer that connected
543 * @param ats performance data
544 * @param ats_count number of entries in ats (excluding 0-termination)
547 transport_peer_connect_notify_cb (void *cls,
548 const struct GNUNET_PeerIdentity *peer,
549 const struct GNUNET_ATS_Information *ats,
552 peer_connect_notify_cb (cls, peer, CGT_TRANSPORT_HANDLE);
557 * Function called when resources for opening a connection to TRANSPORT are
560 * @param cls the cache entry
563 opstart_get_handle_transport (void *cls)
565 struct CacheEntry *entry = cls;
567 GNUNET_assert (NULL != entry);
568 LOG_DEBUG ("Opening a transport connection to peer %u\n", entry->peer_id);
569 entry->transport_handle =
570 GNUNET_TRANSPORT_connect (entry->cfg, NULL, entry, NULL,
571 &transport_peer_connect_notify_cb, NULL);
572 if (NULL == entry->transport_handle)
577 if (0 == entry->demand)
579 if (GNUNET_SCHEDULER_NO_TASK != entry->notify_task)
581 if (NULL != search_suitable_cgh (entry, entry->cgh_qhead))
582 entry->notify_task = GNUNET_SCHEDULER_add_now (&call_cgh_cb, entry);
587 * Function called when the operation responsible for opening a TRANSPORT
588 * connection is marked as done.
590 * @param cls the cache entry
593 oprelease_get_handle_transport (void *cls)
595 struct CacheEntry *entry = cls;
597 if (NULL == entry->transport_handle)
599 GNUNET_TRANSPORT_disconnect (entry->transport_handle);
600 entry->transport_handle = NULL;
605 * Function called after GNUNET_CORE_connect has succeeded (or failed
606 * for good). Note that the private key of the peer is intentionally
607 * not exposed here; if you need it, your process should try to read
608 * the private key file directly (which should work if you are
609 * authorized...). Implementations of this function must not call
610 * GNUNET_CORE_disconnect (other than by scheduling a new task to
614 * @param server handle to the server, NULL if we failed
615 * @param my_identity ID of this peer, NULL if we failed
618 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
619 const struct GNUNET_PeerIdentity *my_identity)
621 struct CacheEntry *entry = cls;
623 if (NULL == my_identity)
628 GNUNET_assert (NULL == entry->peer_identity);
629 GNUNET_break (NULL != server);
630 entry->core_handle = server;
631 entry->peer_identity = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
632 memcpy (entry->peer_identity, my_identity,
633 sizeof (struct GNUNET_PeerIdentity));
634 if (0 == entry->demand)
636 if (GNUNET_SCHEDULER_NO_TASK != entry->notify_task)
638 if (NULL != search_suitable_cgh (entry, entry->cgh_qhead))
639 entry->notify_task = GNUNET_SCHEDULER_add_now (&call_cgh_cb, entry);
644 * Method called whenever a given peer connects at CORE level
647 * @param peer peer identity this notification is about
648 * @param atsi performance data for the connection
649 * @param atsi_count number of records in 'atsi'
652 core_peer_connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
653 const struct GNUNET_ATS_Information *atsi,
654 unsigned int atsi_count)
656 peer_connect_notify_cb (cls, peer, CGT_CORE_HANDLE);
661 * Function called when resources for opening a connection to CORE are
664 * @param cls the cache entry
667 opstart_get_handle_core (void *cls)
669 struct CacheEntry *entry = cls;
671 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
675 GNUNET_assert (NULL != entry);
676 LOG_DEBUG ("Opening a CORE connection to peer %u\n", entry->peer_id);
678 GNUNET_CORE_connect (entry->cfg, entry, /* closure */
679 &core_startup_cb, /* core startup notify */
680 &core_peer_connect_cb, /* peer connect notify */
681 NULL, /* peer disconnect notify */
682 NULL, /* inbound notify */
683 GNUNET_NO, /* inbound header only? */
684 NULL, /* outbound notify */
685 GNUNET_NO, /* outbound header only? */
691 * Function called when the operation responsible for opening a TRANSPORT
692 * connection is marked as done.
694 * @param cls the cache entry
697 oprelease_get_handle_core (void *cls)
699 struct CacheEntry *entry = cls;
701 if (NULL == entry->core_handle)
703 GNUNET_CORE_disconnect (entry->core_handle);
704 entry->core_handle = NULL;
705 GNUNET_free_non_null (entry->peer_identity);
706 entry->peer_identity = NULL;
711 * Function to get a handle with given configuration. The type of the handle is
712 * implicitly provided in the GSTCacheGetHandle. If the handle is already cached
713 * before, it will be retured in the given callback; the peer_id is used to
714 * lookup in the cache; if not, a new operation is started to open the transport
715 * handle and will be given in the callback when it is available.
717 * @param cls the cache entry
719 static struct GSTCacheGetHandle *
720 cache_get_handle (unsigned int peer_id, struct GSTCacheGetHandle *cgh,
721 const struct GNUNET_CONFIGURATION_Handle *cfg,
722 const struct GNUNET_PeerIdentity *target,
723 GST_cache_peer_connect_notify connect_notify_cb,
724 void *connect_notify_cb_cls)
726 struct GNUNET_HashCode key;
728 struct CacheEntry *entry;
729 struct ConnectNotifyContext *ctxt;
730 struct GNUNET_TESTBED_Operation *op;
732 GNUNET_assert (0 != cgh->type);
733 GNUNET_CRYPTO_hash (&peer_id, sizeof (peer_id), &key);
735 entry = cache_lookup (&key);
738 if (GNUNET_YES == entry->in_lru)
740 GNUNET_assert (0 == entry->demand);
741 GNUNET_assert (0 < lru_cache_size);
742 if (GNUNET_SCHEDULER_NO_TASK != entry->expire_task)
744 GNUNET_SCHEDULER_cancel (entry->expire_task);
745 entry->expire_task = GNUNET_SCHEDULER_NO_TASK;
747 GNUNET_CONTAINER_DLL_remove (lru_cache_head, lru_cache_tail, entry);
749 entry->in_lru = GNUNET_NO;
753 case CGT_TRANSPORT_HANDLE:
754 handle = entry->transport_handle;
756 LOG_DEBUG ("Found TRANSPORT handle in cache for peer %u\n",
759 case CGT_CORE_HANDLE:
760 handle = entry->core_handle;
762 LOG_DEBUG ("Found CORE handle in cache for peer %u\n", entry->peer_id);
767 entry = add_entry (&key, peer_id);
768 if (NULL == entry->cfg)
769 entry->cfg = GNUNET_CONFIGURATION_dup (cfg);
772 GNUNET_CONTAINER_DLL_insert (entry->cgh_qhead, entry->cgh_qtail, cgh);
773 if ((NULL != target) && (NULL != connect_notify_cb))
775 ctxt = GNUNET_malloc (sizeof (struct ConnectNotifyContext));
776 ctxt->target = target;
777 ctxt->cb = connect_notify_cb;
778 ctxt->cb_cls = connect_notify_cb_cls;
779 GNUNET_assert (NULL == cgh->nctxt);
785 if (GNUNET_SCHEDULER_NO_TASK == entry->notify_task)
787 if (NULL != search_suitable_cgh (entry, entry->cgh_qhead))
788 entry->notify_task = GNUNET_SCHEDULER_add_now (&call_cgh_cb, entry);
794 case CGT_TRANSPORT_HANDLE:
795 if (NULL != entry->transport_op)
797 op = GNUNET_TESTBED_operation_create_ (entry, &opstart_get_handle_transport,
798 &oprelease_get_handle_transport);
799 entry->transport_op = op;
801 case CGT_CORE_HANDLE:
802 if (NULL != entry->core_op)
804 op = GNUNET_TESTBED_operation_create_ (entry, &opstart_get_handle_core,
805 &oprelease_get_handle_core);
809 GNUNET_TESTBED_operation_queue_insert_ (GST_opq_openfds, op);
810 GNUNET_TESTBED_operation_begin_wait_ (op);
816 * Iterator over hash map entries.
819 * @param key current key code
820 * @param value value in the hash map
821 * @return GNUNET_YES if we should continue to
826 cache_clear_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
828 struct CacheEntry *entry = value;
829 static unsigned int ncleared;
831 GNUNET_assert (NULL != entry);
832 GNUNET_break (0 == entry->demand);
833 LOG_DEBUG ("Clearing entry %u of %u\n", ++ncleared, cache_size);
834 GNUNET_CONTAINER_multihashmap_remove (cache, key, value);
835 close_handles (entry);
836 GNUNET_free_non_null (entry->hello);
837 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == entry->expire_task);
838 GNUNET_assert (NULL == entry->transport_handle);
839 GNUNET_assert (NULL == entry->transport_op);
840 GNUNET_assert (NULL == entry->core_handle);
841 GNUNET_assert (NULL == entry->core_op);
842 GNUNET_assert (NULL == entry->cfg);
843 GNUNET_assert (NULL == entry->cgh_qhead);
844 GNUNET_assert (NULL == entry->cgh_qtail);
845 GNUNET_assert (NULL == entry->nctxt_qhead);
846 GNUNET_assert (NULL == entry->nctxt_qtail);
858 GNUNET_CONTAINER_multihashmap_iterate (cache, &cache_clear_iterator, NULL);
859 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (cache));
860 GNUNET_CONTAINER_multihashmap_destroy (cache);
863 lru_cache_threshold_size = 0;
865 lru_cache_head = NULL;
866 lru_cache_tail = NULL;
871 * Initializes the cache
873 * @param size the size of the cache
876 GST_cache_init (unsigned int size)
880 lru_cache_threshold_size = size;
883 cache = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_YES);
888 * Mark the GetCacheHandle as being done if a handle has been provided already
889 * or as being cancelled if the callback for the handle hasn't been called.
891 * @param cgh the CacheGetHandle handle
894 GST_cache_get_handle_done (struct GSTCacheGetHandle *cgh)
896 struct CacheEntry *entry;
899 GNUNET_assert (NULL != entry);
900 GNUNET_assert (0 < entry->demand);
902 if (GNUNET_SCHEDULER_NO_TASK != entry->notify_task)
904 GNUNET_SCHEDULER_cancel (entry->notify_task);
905 entry->notify_task = GNUNET_SCHEDULER_NO_TASK;
907 GNUNET_CONTAINER_DLL_remove (entry->cgh_qhead, entry->cgh_qtail, cgh);
908 if (NULL != cgh->nctxt)
910 GNUNET_assert (cgh == cgh->nctxt->cgh);
911 if (GNUNET_YES == cgh->notify_called)
912 GNUNET_CONTAINER_DLL_remove (entry->nctxt_qhead, entry->nctxt_qtail,
914 GNUNET_free (cgh->nctxt);
917 if (0 == entry->demand)
920 GNUNET_SCHEDULER_add_delayed (CACHE_EXPIRY, &expire_cache_entry, entry);
921 GNUNET_CONTAINER_DLL_insert_tail (lru_cache_head, lru_cache_tail, entry);
923 entry->in_lru = GNUNET_YES;
924 if (lru_cache_size > lru_cache_threshold_size)
925 close_handles (lru_cache_head);
929 struct GSTCacheGetHandle *cgh2;
931 if (NULL != (cgh2 = search_suitable_cgh (entry, entry->cgh_qhead)))
932 entry->notify_task = GNUNET_SCHEDULER_add_now (&call_cgh_cb, entry);
938 * Get a transport handle with the given configuration. If the handle is
939 * already cached before, it will be retured in the given callback; the peer_id
940 * is used to lookup in the cache; if not, a new operation is started to open the
941 * transport handle and will be given in the callback when it is available.
943 * @param peer_id the index of the peer
944 * @param cfg the configuration with which the transport handle has to be
945 * created if it was not present in the cache
946 * @param cb the callback to notify when the transport handle is available
947 * @param cb_cls the closure for the above callback
948 * @param target the peer identify of the peer whose connection to our TRANSPORT
949 * subsystem will be notified through the connect_notify_cb. Can be NULL
950 * @param connect_notify_cb the callback to call when the given target peer is
951 * connected. This callback will only be called once or never again (in
952 * case the target peer cannot be connected). Can be NULL
953 * @param connect_notify_cb_cls the closure for the above callback
954 * @return the handle which can be used cancel or mark that the handle is no
957 struct GSTCacheGetHandle *
958 GST_cache_get_handle_transport (unsigned int peer_id,
959 const struct GNUNET_CONFIGURATION_Handle *cfg,
960 GST_cache_handle_ready_cb cb, void *cb_cls,
961 const struct GNUNET_PeerIdentity *target,
962 GST_cache_peer_connect_notify connect_notify_cb,
963 void *connect_notify_cb_cls)
965 struct GSTCacheGetHandle *cgh;
967 cgh = GNUNET_malloc (sizeof (struct GSTCacheGetHandle));
969 cgh->cb_cls = cb_cls;
970 cgh->type = CGT_TRANSPORT_HANDLE;
971 return cache_get_handle (peer_id, cgh, cfg, target, connect_notify_cb,
972 connect_notify_cb_cls);
977 * Get a CORE handle with the given configuration. If the handle is already
978 * cached before, it will be retured in the given callback; the peer_id is used
979 * to lookup in the cache. If the handle is not cached before, a new operation
980 * is started to open the CORE handle and will be given in the callback when it
981 * is available along with the peer identity
983 * @param peer_id the index of the peer
984 * @param cfg the configuration with which the transport handle has to be
985 * created if it was not present in the cache
986 * @param cb the callback to notify when the transport handle is available
987 * @param cb_cls the closure for the above callback
988 * @param target the peer identify of the peer whose connection to our CORE
989 * subsystem will be notified through the connect_notify_cb. Can be NULL
990 * @param connect_notify_cb the callback to call when the given target peer is
991 * connected. This callback will only be called once or never again (in
992 * case the target peer cannot be connected). Can be NULL
993 * @param connect_notify_cb_cls the closure for the above callback
994 * @return the handle which can be used cancel or mark that the handle is no
997 struct GSTCacheGetHandle *
998 GST_cache_get_handle_core (unsigned int peer_id,
999 const struct GNUNET_CONFIGURATION_Handle *cfg,
1000 GST_cache_handle_ready_cb cb, void *cb_cls,
1001 const struct GNUNET_PeerIdentity *target,
1002 GST_cache_peer_connect_notify connect_notify_cb,
1003 void *connect_notify_cb_cls)
1005 struct GSTCacheGetHandle *cgh;
1007 cgh = GNUNET_malloc (sizeof (struct GSTCacheGetHandle));
1009 cgh->cb_cls = cb_cls;
1010 cgh->type = CGT_CORE_HANDLE;
1011 return cache_get_handle (peer_id, cgh, cfg, target, connect_notify_cb,
1012 connect_notify_cb_cls);
1017 * Looks up in the hello cache and returns the HELLO of the given peer
1019 * @param peer_id the index of the peer whose HELLO has to be looked up
1020 * @return the HELLO message; NULL if not found
1022 const struct GNUNET_MessageHeader *
1023 GST_cache_lookup_hello (const unsigned int peer_id)
1025 struct CacheEntry *entry;
1026 struct GNUNET_HashCode key;
1028 LOG_DEBUG ("Looking up HELLO for peer %u\n", peer_id);
1029 GNUNET_CRYPTO_hash (&peer_id, sizeof (peer_id), &key);
1030 entry = cache_lookup (&key);
1033 if (NULL != entry->hello)
1034 LOG_DEBUG ("HELLO found for peer %u\n", peer_id);
1035 return entry->hello;
1040 * Caches the HELLO of the given peer. Updates the HELLO if it was already
1043 * @param id the peer identity of the peer whose HELLO has to be cached
1044 * @param hello the HELLO message
1047 GST_cache_add_hello (const unsigned int peer_id,
1048 const struct GNUNET_MessageHeader *hello)
1050 struct CacheEntry *entry;
1051 struct GNUNET_HashCode key;
1053 GNUNET_CRYPTO_hash (&peer_id, sizeof (peer_id), &key);
1054 entry = GNUNET_CONTAINER_multihashmap_get (cache, &key);
1056 entry = add_entry (&key, peer_id);
1057 GNUNET_free_non_null (entry->hello);
1058 entry->hello = GNUNET_copy_message (hello);
1061 /* end of gnunet-service-testbed_hc.c */