diff options
author | Omar Tarabai <tarabai@devegypt.com> | 2014-07-14 17:40:32 +0000 |
---|---|---|
committer | Omar Tarabai <tarabai@devegypt.com> | 2014-07-14 17:40:32 +0000 |
commit | 600e56e89d1d1fc7417e8be20c40e197c9d58b2a (patch) | |
tree | 7191cb9f3e2c64fa1c6f78dee49f79d442249c37 /src/fs/gnunet-service-fs_cp.c | |
parent | c937c2c64700475cc04e7ee3d912bd9dff7e33b2 (diff) | |
download | gnunet-600e56e89d1d1fc7417e8be20c40e197c9d58b2a.tar.gz gnunet-600e56e89d1d1fc7417e8be20c40e197c9d58b2a.zip |
fs now uses peerstore to store respect value
Diffstat (limited to 'src/fs/gnunet-service-fs_cp.c')
-rw-r--r-- | src/fs/gnunet-service-fs_cp.c | 130 |
1 files changed, 58 insertions, 72 deletions
diff --git a/src/fs/gnunet-service-fs_cp.c b/src/fs/gnunet-service-fs_cp.c index 087376218..f031688c3 100644 --- a/src/fs/gnunet-service-fs_cp.c +++ b/src/fs/gnunet-service-fs_cp.c | |||
@@ -31,6 +31,7 @@ | |||
31 | #include "gnunet-service-fs_pe.h" | 31 | #include "gnunet-service-fs_pe.h" |
32 | #include "gnunet-service-fs_pr.h" | 32 | #include "gnunet-service-fs_pr.h" |
33 | #include "gnunet-service-fs_push.h" | 33 | #include "gnunet-service-fs_push.h" |
34 | #include "gnunet_peerstore_service.h" | ||
34 | 35 | ||
35 | 36 | ||
36 | /** | 37 | /** |
@@ -302,6 +303,16 @@ struct GSF_ConnectedPeer | |||
302 | */ | 303 | */ |
303 | int did_reserve; | 304 | int did_reserve; |
304 | 305 | ||
306 | /** | ||
307 | * Function called when the creation of this record is complete. | ||
308 | */ | ||
309 | GSF_ConnectedPeerCreationCallback creation_cb; | ||
310 | |||
311 | /** | ||
312 | * Closure for @e creation_cb | ||
313 | */ | ||
314 | void *creation_cb_cls; | ||
315 | |||
305 | }; | 316 | }; |
306 | 317 | ||
307 | 318 | ||
@@ -311,30 +322,9 @@ struct GSF_ConnectedPeer | |||
311 | static struct GNUNET_CONTAINER_MultiPeerMap *cp_map; | 322 | static struct GNUNET_CONTAINER_MultiPeerMap *cp_map; |
312 | 323 | ||
313 | /** | 324 | /** |
314 | * Where do we store respect information? | 325 | * Handle to peerstore service. |
315 | */ | 326 | */ |
316 | static char *respectDirectory; | 327 | static struct GNUNET_PEERSTORE_Handle *peerstore; |
317 | |||
318 | |||
319 | /** | ||
320 | * Get the filename under which we would store respect | ||
321 | * for the given peer. | ||
322 | * | ||
323 | * @param id peer to get the filename for | ||
324 | * @return filename of the form DIRECTORY/PEERID | ||
325 | */ | ||
326 | static char * | ||
327 | get_respect_filename (const struct GNUNET_PeerIdentity *id) | ||
328 | { | ||
329 | char *fn; | ||
330 | |||
331 | GNUNET_asprintf (&fn, | ||
332 | "%s%s%s", | ||
333 | respectDirectory, | ||
334 | DIR_SEPARATOR_STR, | ||
335 | GNUNET_i2s_full (id)); | ||
336 | return fn; | ||
337 | } | ||
338 | 328 | ||
339 | 329 | ||
340 | /** | 330 | /** |
@@ -569,20 +559,50 @@ ats_reserve_callback (void *cls, const struct GNUNET_PeerIdentity *peer, | |||
569 | } | 559 | } |
570 | } | 560 | } |
571 | 561 | ||
562 | /** | ||
563 | * Function called by PEERSTORE with peer respect record | ||
564 | * | ||
565 | * @param cls handle to connected peer entry | ||
566 | * @param record peerstore record information | ||
567 | * @param emsg error message, or NULL if no errors | ||
568 | * @return #GNUNET_NO to stop iterating since we only expect 0 or 1 records | ||
569 | */ | ||
570 | static int | ||
571 | peer_respect_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, char *emsg) | ||
572 | { | ||
573 | struct GSF_ConnectedPeer *cp = cls; | ||
574 | |||
575 | if ((NULL != record) && (sizeof (cp->disk_respect) == record->value_size)) | ||
576 | cp->disk_respect = cp->ppd.respect = *((uint32_t *)record->value); | ||
577 | cp->request_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO); | ||
578 | GNUNET_break (GNUNET_OK == | ||
579 | GNUNET_CONTAINER_multipeermap_put (cp_map, | ||
580 | GSF_connected_peer_get_identity2_ (cp), | ||
581 | cp, | ||
582 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
583 | GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"), | ||
584 | GNUNET_CONTAINER_multipeermap_size (cp_map), | ||
585 | GNUNET_NO); | ||
586 | GSF_push_start_ (cp); | ||
587 | if (NULL != cp->creation_cb) | ||
588 | cp->creation_cb (cp->creation_cb_cls, cp); | ||
589 | return GNUNET_NO; | ||
590 | } | ||
572 | 591 | ||
573 | /** | 592 | /** |
574 | * A peer connected to us. Setup the connected peer | 593 | * A peer connected to us. Setup the connected peer |
575 | * records. | 594 | * records. |
576 | * | 595 | * |
577 | * @param peer identity of peer that connected | 596 | * @param peer identity of peer that connected |
578 | * @return handle to connected peer entry | 597 | * @param creation_cb callback function when the record is created. |
598 | * @param creation_cb_cls closure for @creation_cb | ||
579 | */ | 599 | */ |
580 | struct GSF_ConnectedPeer * | 600 | void |
581 | GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer) | 601 | GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer, |
602 | GSF_ConnectedPeerCreationCallback creation_cb, | ||
603 | void *creation_cb_cls) | ||
582 | { | 604 | { |
583 | struct GSF_ConnectedPeer *cp; | 605 | struct GSF_ConnectedPeer *cp; |
584 | char *fn; | ||
585 | uint32_t respect; | ||
586 | 606 | ||
587 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s\n", | 607 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s\n", |
588 | GNUNET_i2s (peer)); | 608 | GNUNET_i2s (peer)); |
@@ -592,22 +612,10 @@ GSF_peer_connect_handler_ (const struct GNUNET_PeerIdentity *peer) | |||
592 | cp->rc = | 612 | cp->rc = |
593 | GNUNET_ATS_reserve_bandwidth (GSF_ats, peer, DBLOCK_SIZE, | 613 | GNUNET_ATS_reserve_bandwidth (GSF_ats, peer, DBLOCK_SIZE, |
594 | &ats_reserve_callback, cp); | 614 | &ats_reserve_callback, cp); |
595 | fn = get_respect_filename (peer); | 615 | cp->creation_cb = creation_cb; |
596 | if ((GNUNET_YES == GNUNET_DISK_file_test (fn)) && | 616 | cp->creation_cb_cls = creation_cb_cls; |
597 | (sizeof (respect) == GNUNET_DISK_fn_read (fn, &respect, sizeof (respect)))) | 617 | GNUNET_PEERSTORE_iterate (peerstore, "fs", peer, "respect", |
598 | cp->disk_respect = cp->ppd.respect = ntohl (respect); | 618 | GNUNET_TIME_UNIT_FOREVER_REL, &peer_respect_cb, cp); |
599 | GNUNET_free (fn); | ||
600 | cp->request_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO); | ||
601 | GNUNET_break (GNUNET_OK == | ||
602 | GNUNET_CONTAINER_multipeermap_put (cp_map, | ||
603 | GSF_connected_peer_get_identity2_ (cp), | ||
604 | cp, | ||
605 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
606 | GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# peers connected"), | ||
607 | GNUNET_CONTAINER_multipeermap_size (cp_map), | ||
608 | GNUNET_NO); | ||
609 | GSF_push_start_ (cp); | ||
610 | return cp; | ||
611 | } | 619 | } |
612 | 620 | ||
613 | 621 | ||
@@ -1718,33 +1726,16 @@ static int | |||
1718 | flush_respect (void *cls, const struct GNUNET_PeerIdentity * key, void *value) | 1726 | flush_respect (void *cls, const struct GNUNET_PeerIdentity * key, void *value) |
1719 | { | 1727 | { |
1720 | struct GSF_ConnectedPeer *cp = value; | 1728 | struct GSF_ConnectedPeer *cp = value; |
1721 | char *fn; | ||
1722 | uint32_t respect; | ||
1723 | struct GNUNET_PeerIdentity pid; | 1729 | struct GNUNET_PeerIdentity pid; |
1724 | 1730 | ||
1725 | if (cp->ppd.respect == cp->disk_respect) | 1731 | if (cp->ppd.respect == cp->disk_respect) |
1726 | return GNUNET_OK; /* unchanged */ | 1732 | return GNUNET_OK; /* unchanged */ |
1727 | GNUNET_assert (0 != cp->ppd.pid); | 1733 | GNUNET_assert (0 != cp->ppd.pid); |
1728 | GNUNET_PEER_resolve (cp->ppd.pid, &pid); | 1734 | GNUNET_PEER_resolve (cp->ppd.pid, &pid); |
1729 | fn = get_respect_filename (&pid); | 1735 | GNUNET_PEERSTORE_store (peerstore, "fs", &pid, "respect", &cp->ppd.respect, |
1730 | if (cp->ppd.respect == 0) | 1736 | sizeof (cp->ppd.respect), |
1731 | { | 1737 | GNUNET_TIME_UNIT_FOREVER_ABS, |
1732 | if ((0 != UNLINK (fn)) && (errno != ENOENT)) | 1738 | GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL); |
1733 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING | | ||
1734 | GNUNET_ERROR_TYPE_BULK, "unlink", fn); | ||
1735 | } | ||
1736 | else | ||
1737 | { | ||
1738 | respect = htonl (cp->ppd.respect); | ||
1739 | if (sizeof (uint32_t) == | ||
1740 | GNUNET_DISK_fn_write (fn, &respect, sizeof (uint32_t), | ||
1741 | GNUNET_DISK_PERM_USER_READ | | ||
1742 | GNUNET_DISK_PERM_USER_WRITE | | ||
1743 | GNUNET_DISK_PERM_GROUP_READ | | ||
1744 | GNUNET_DISK_PERM_OTHER_READ)) | ||
1745 | cp->disk_respect = cp->ppd.respect; | ||
1746 | } | ||
1747 | GNUNET_free (fn); | ||
1748 | return GNUNET_OK; | 1739 | return GNUNET_OK; |
1749 | } | 1740 | } |
1750 | 1741 | ||
@@ -1796,11 +1787,7 @@ void | |||
1796 | GSF_connected_peer_init_ () | 1787 | GSF_connected_peer_init_ () |
1797 | { | 1788 | { |
1798 | cp_map = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES); | 1789 | cp_map = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES); |
1799 | GNUNET_assert (GNUNET_OK == | 1790 | peerstore = GNUNET_PEERSTORE_connect (GSF_cfg); |
1800 | GNUNET_CONFIGURATION_get_value_filename (GSF_cfg, "fs", | ||
1801 | "RESPECT", | ||
1802 | &respectDirectory)); | ||
1803 | GNUNET_break (GNUNET_OK == GNUNET_DISK_directory_create (respectDirectory)); | ||
1804 | GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH, | 1791 | GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_HIGH, |
1805 | &cron_flush_respect, NULL); | 1792 | &cron_flush_respect, NULL); |
1806 | } | 1793 | } |
@@ -1834,8 +1821,7 @@ GSF_connected_peer_done_ () | |||
1834 | GNUNET_CONTAINER_multipeermap_iterate (cp_map, &clean_peer, NULL); | 1821 | GNUNET_CONTAINER_multipeermap_iterate (cp_map, &clean_peer, NULL); |
1835 | GNUNET_CONTAINER_multipeermap_destroy (cp_map); | 1822 | GNUNET_CONTAINER_multipeermap_destroy (cp_map); |
1836 | cp_map = NULL; | 1823 | cp_map = NULL; |
1837 | GNUNET_free (respectDirectory); | 1824 | GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_YES); |
1838 | respectDirectory = NULL; | ||
1839 | } | 1825 | } |
1840 | 1826 | ||
1841 | 1827 | ||