diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2010-04-14 15:22:36 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2010-04-14 15:22:36 +0000 |
commit | ecd595792c22186f1dc610296ad7c1e39b54a902 (patch) | |
tree | ece46e53094ad21a243f7b9f5997a9a08ffde8a8 /src/hostlist/hostlist-client.c | |
parent | 522e1cb8f86d7e8933f8c3ff9052b0d87de90fb3 (diff) | |
download | gnunet-ecd595792c22186f1dc610296ad7c1e39b54a902.tar.gz gnunet-ecd595792c22186f1dc610296ad7c1e39b54a902.zip |
Diffstat (limited to 'src/hostlist/hostlist-client.c')
-rw-r--r-- | src/hostlist/hostlist-client.c | 67 |
1 files changed, 62 insertions, 5 deletions
diff --git a/src/hostlist/hostlist-client.c b/src/hostlist/hostlist-client.c index 0b05556e9..ec02f4545 100644 --- a/src/hostlist/hostlist-client.c +++ b/src/hostlist/hostlist-client.c | |||
@@ -726,6 +726,38 @@ disconnect_handler (void *cls, | |||
726 | GNUNET_NO); | 726 | GNUNET_NO); |
727 | } | 727 | } |
728 | 728 | ||
729 | struct compare_struct | ||
730 | { | ||
731 | uint64_t lowest_quality; | ||
732 | const GNUNET_HashCode * key; | ||
733 | struct GNUNET_Hostlist * value; | ||
734 | }; | ||
735 | |||
736 | static int iterate_hashmap_for_replacing ( void *cls, const GNUNET_HashCode *key, void *value ) | ||
737 | { | ||
738 | struct compare_struct * cmp = cls; | ||
739 | |||
740 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
741 | ("Now iterating over peer entry: %s\n"), GNUNET_i2s ( (const struct GNUNET_PeerIdentity *) key)); | ||
742 | if ( NULL == cmp->key ) | ||
743 | { | ||
744 | cmp->key = key; | ||
745 | cmp->lowest_quality = ((struct GNUNET_Hostlist *) value)->quality; | ||
746 | cmp->value = (struct GNUNET_Hostlist *) value; | ||
747 | } | ||
748 | else | ||
749 | { | ||
750 | if ( cmp->lowest_quality > ((struct GNUNET_Hostlist *) value)->quality) | ||
751 | { | ||
752 | cmp->lowest_quality = ((struct GNUNET_Hostlist *) value)->quality; | ||
753 | cmp->key = key; | ||
754 | cmp->value = (struct GNUNET_Hostlist *) value; | ||
755 | } | ||
756 | } | ||
757 | |||
758 | return GNUNET_YES; | ||
759 | } | ||
760 | |||
729 | /** | 761 | /** |
730 | * Method called whenever an advertisement message arrives. | 762 | * Method called whenever an advertisement message arrives. |
731 | * | 763 | * |
@@ -768,6 +800,9 @@ advertisement_handler (void *cls, | |||
768 | memcpy ( hostlist->hostlist_uri, &incoming[1], uri_size ); | 800 | memcpy ( hostlist->hostlist_uri, &incoming[1], uri_size ); |
769 | hostlist->time_creation = GNUNET_TIME_absolute_get(); | 801 | hostlist->time_creation = GNUNET_TIME_absolute_get(); |
770 | hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero(); | 802 | hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero(); |
803 | hostlist->times_used = 0; | ||
804 | hostlist->quality = HOSTLIST_INITIAL; | ||
805 | |||
771 | 806 | ||
772 | GNUNET_HashCode * peer_ident_hash = (GNUNET_HashCode * ) &(peer->hashPubKey); | 807 | GNUNET_HashCode * peer_ident_hash = (GNUNET_HashCode * ) &(peer->hashPubKey); |
773 | 808 | ||
@@ -775,14 +810,17 @@ advertisement_handler (void *cls, | |||
775 | struct GNUNET_Hostlist * hostlist2; | 810 | struct GNUNET_Hostlist * hostlist2; |
776 | hostlist2 = GNUNET_malloc ( sizeof (struct GNUNET_Hostlist) ); | 811 | hostlist2 = GNUNET_malloc ( sizeof (struct GNUNET_Hostlist) ); |
777 | char * str = "test"; | 812 | char * str = "test"; |
813 | GNUNET_HashCode * peer_ident_test = GNUNET_malloc ( sizeof (GNUNET_HashCode) ); | ||
814 | GNUNET_CRYPTO_hash_from_string( "TEST", peer_ident_test); | ||
778 | 815 | ||
779 | hostlist2->peer = (*peer); | 816 | hostlist2->peer.hashPubKey = (*peer_ident_test) ; |
780 | hostlist2->hello_count = 0; | 817 | hostlist2->hello_count = 0; |
781 | hostlist2->hostlist_uri = GNUNET_malloc ( strlen(str) +1 ); | 818 | hostlist2->hostlist_uri = GNUNET_malloc ( strlen(str) +1 ); |
782 | strcpy(hostlist2->hostlist_uri,str); | 819 | strcpy(hostlist2->hostlist_uri,str); |
783 | hostlist2->time_creation = GNUNET_TIME_absolute_get(); | 820 | hostlist2->time_creation = GNUNET_TIME_absolute_get(); |
784 | hostlist2->time_last_usage = GNUNET_TIME_absolute_get_zero(); | 821 | hostlist2->time_last_usage = GNUNET_TIME_absolute_get_zero(); |
785 | GNUNET_CONTAINER_multihashmap_put ( hostlist_hashmap, peer_ident_hash, hostlist2, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE ); | 822 | hostlist2->quality = HOSTLIST_INITIAL - 100; |
823 | GNUNET_CONTAINER_multihashmap_put ( hostlist_hashmap, peer_ident_test, hostlist2, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE ); | ||
786 | /* test */ | 824 | /* test */ |
787 | 825 | ||
788 | if ( GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (hostlist_hashmap, peer_ident_hash) ) | 826 | if ( GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (hostlist_hashmap, peer_ident_hash) ) |
@@ -798,13 +836,32 @@ advertisement_handler (void *cls, | |||
798 | else | 836 | else |
799 | { | 837 | { |
800 | /* No free entries available, replace existing entry */ | 838 | /* No free entries available, replace existing entry */ |
839 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
840 | "No free slots for hostlist available, searching for hostlist to replace\n", GNUNET_i2s (peer) ); | ||
841 | /* iterate over all entries in hashmap */ | ||
842 | struct compare_struct * cmp = GNUNET_malloc( sizeof( struct compare_struct) ); | ||
843 | cmp->lowest_quality = 0; | ||
844 | cmp->key = NULL; | ||
845 | GNUNET_CONTAINER_multihashmap_iterate ( hostlist_hashmap, | ||
846 | &iterate_hashmap_for_replacing, | ||
847 | cmp ); | ||
848 | |||
849 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
850 | "Peer %4s's hostlist has the worst quality of all peers with value %u \n", GNUNET_h2s (cmp->key), cmp->lowest_quality ); | ||
851 | /* replacing the entry with worst quality, if quality is below initial quality value */ | ||
852 | if ( cmp->lowest_quality < HOSTLIST_INITIAL) | ||
853 | { | ||
854 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
855 | "Peer %4s' removed, added peer %4s \n", GNUNET_h2s (cmp->key), GNUNET_h2s ( peer_ident_hash) ); | ||
856 | GNUNET_CONTAINER_multihashmap_remove ( hostlist_hashmap, cmp->key, cmp->value ); | ||
857 | GNUNET_CONTAINER_multihashmap_put ( hostlist_hashmap, peer_ident_hash, hostlist, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE ); | ||
858 | } | ||
801 | 859 | ||
802 | return GNUNET_YES; | 860 | return GNUNET_YES; |
803 | } | 861 | } |
804 | } | 862 | } |
805 | else | 863 | else |
806 | { | 864 | { |
807 | |||
808 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 865 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
809 | "Peer already in hashmap\n"); | 866 | "Peer already in hashmap\n"); |
810 | /* hostlist entry already existing in hashmap */ | 867 | /* hostlist entry already existing in hashmap */ |
@@ -920,7 +977,7 @@ static int load_hostlist_file () | |||
920 | } | 977 | } |
921 | 978 | ||
922 | 979 | ||
923 | static int iterate_hashmap ( void *cls, const GNUNET_HashCode *key, void *value ) | 980 | static int iterate_hashmap_for_saving ( void *cls, const GNUNET_HashCode *key, void *value ) |
924 | { | 981 | { |
925 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 982 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
926 | ("Now iterating over peer entry: %s\n"), GNUNET_i2s ( (const struct GNUNET_PeerIdentity *) key)); | 983 | ("Now iterating over peer entry: %s\n"), GNUNET_i2s ( (const struct GNUNET_PeerIdentity *) key)); |
@@ -972,7 +1029,7 @@ static int save_hostlist_file () | |||
972 | 1029 | ||
973 | /* iterate over all entries in hashmap */ | 1030 | /* iterate over all entries in hashmap */ |
974 | GNUNET_CONTAINER_multihashmap_iterate ( hostlist_hashmap, | 1031 | GNUNET_CONTAINER_multihashmap_iterate ( hostlist_hashmap, |
975 | &iterate_hashmap, | 1032 | &iterate_hashmap_for_saving, |
976 | wh ); | 1033 | wh ); |
977 | 1034 | ||
978 | if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) ) | 1035 | if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) ) |