aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2010-04-14 13:27:53 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2010-04-14 13:27:53 +0000
commit522e1cb8f86d7e8933f8c3ff9052b0d87de90fb3 (patch)
tree2b49a52029519560181b49864fbc775f44c91ce0
parent81c9bad6197621eddc27d885827ca47f31cf9704 (diff)
downloadgnunet-522e1cb8f86d7e8933f8c3ff9052b0d87de90fb3.tar.gz
gnunet-522e1cb8f86d7e8933f8c3ff9052b0d87de90fb3.zip
update a hostlist if already existing in database
-rw-r--r--src/hostlist/hostlist-client.c91
1 files changed, 68 insertions, 23 deletions
diff --git a/src/hostlist/hostlist-client.c b/src/hostlist/hostlist-client.c
index a496588db..0b05556e9 100644
--- a/src/hostlist/hostlist-client.c
+++ b/src/hostlist/hostlist-client.c
@@ -21,7 +21,7 @@
21/** 21/**
22 * @file hostlist/hostlist-client.c 22 * @file hostlist/hostlist-client.c
23 * @brief hostlist support. Downloads HELLOs via HTTP. 23 * @brief hostlist support. Downloads HELLOs via HTTP.
24 * @author Christian Grothoff 24 * @author Christian Grothoff, Matthias Wachs
25 */ 25 */
26 26
27#include "platform.h" 27#include "platform.h"
@@ -749,6 +749,7 @@ advertisement_handler (void *cls,
749 int uri_size = size - sizeof ( struct GNUNET_HOSTLIST_ADV_Message ); 749 int uri_size = size - sizeof ( struct GNUNET_HOSTLIST_ADV_Message );
750 char * uri = GNUNET_malloc ( uri_size ); 750 char * uri = GNUNET_malloc ( uri_size );
751 struct GNUNET_Hostlist * hostlist; 751 struct GNUNET_Hostlist * hostlist;
752 struct GNUNET_Hostlist * existing_hostlist;
752 753
753 if ( ntohs (message->type) != GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT) 754 if ( ntohs (message->type) != GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT)
754 return GNUNET_NO; 755 return GNUNET_NO;
@@ -760,7 +761,7 @@ advertisement_handler (void *cls,
760 761
761 /* search in map for peer identity */ 762 /* search in map for peer identity */
762 hostlist = GNUNET_malloc ( sizeof (struct GNUNET_Hostlist) ); 763 hostlist = GNUNET_malloc ( sizeof (struct GNUNET_Hostlist) );
763 hostlist = GNUNET_malloc ( sizeof (struct GNUNET_Hostlist) ); 764
764 hostlist->peer = (*peer); 765 hostlist->peer = (*peer);
765 hostlist->hello_count = 0; 766 hostlist->hello_count = 0;
766 hostlist->hostlist_uri = GNUNET_malloc ( uri_size); 767 hostlist->hostlist_uri = GNUNET_malloc ( uri_size);
@@ -769,27 +770,77 @@ advertisement_handler (void *cls,
769 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero(); 770 hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
770 771
771 GNUNET_HashCode * peer_ident_hash = (GNUNET_HashCode * ) &(peer->hashPubKey); 772 GNUNET_HashCode * peer_ident_hash = (GNUNET_HashCode * ) &(peer->hashPubKey);
773
774 /* test */
775 struct GNUNET_Hostlist * hostlist2;
776 hostlist2 = GNUNET_malloc ( sizeof (struct GNUNET_Hostlist) );
777 char * str = "test";
778
779 hostlist2->peer = (*peer);
780 hostlist2->hello_count = 0;
781 hostlist2->hostlist_uri = GNUNET_malloc ( strlen(str) +1 );
782 strcpy(hostlist2->hostlist_uri,str);
783 hostlist2->time_creation = GNUNET_TIME_absolute_get();
784 hostlist2->time_last_usage = GNUNET_TIME_absolute_get_zero();
785 GNUNET_CONTAINER_multihashmap_put ( hostlist_hashmap, peer_ident_hash, hostlist2, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE );
786 /* test */
787
772 if ( GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (hostlist_hashmap, peer_ident_hash) ) 788 if ( GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains (hostlist_hashmap, peer_ident_hash) )
773 { 789 {
774 if ( MAX_NUMBER_HOSTLISTS > GNUNET_CONTAINER_multihashmap_size (hostlist_hashmap) ) 790 if ( MAX_NUMBER_HOSTLISTS > GNUNET_CONTAINER_multihashmap_size (hostlist_hashmap) )
775 { 791 {
776 /* Entries available, add hostlist to hashmap */ 792 /* Entries available, add hostlist to hashmap */
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
778 "Adding peer '%s' to hashmap %s\n", GNUNET_i2s (peer), uri ); 794 "Adding peer '%s' to hashmap\n", GNUNET_i2s (peer) );
779 GNUNET_CONTAINER_multihashmap_put ( hostlist_hashmap, peer_ident_hash, hostlist, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE ); 795 GNUNET_CONTAINER_multihashmap_put ( hostlist_hashmap, peer_ident_hash, hostlist, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE );
796 return GNUNET_YES;
780 } 797 }
781 else 798 else
782 { 799 {
783 /* No free entries available, replace existing entry */ 800 /* No free entries available, replace existing entry */
801
802 return GNUNET_YES;
784 } 803 }
785 } 804 }
786 else 805 else
787 { 806 {
807
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Peer already in hashmap\n");
788 /* hostlist entry already existing in hashmap */ 810 /* hostlist entry already existing in hashmap */
789 /* compare uri to new uri ? */ 811 /* compare uri to new uri and update if different */
790 /* update recieved date (vs using last download time to check reachability)? */ 812 /* update recieved date (vs using last download time to check reachability)? */
813 existing_hostlist = GNUNET_CONTAINER_multihashmap_get ( hostlist_hashmap, peer_ident_hash );
814 if ( 0 != strcmp (hostlist->hostlist_uri, ((struct GNUNET_Hostlist *) existing_hostlist)->hostlist_uri) )
815 {
816 /* uri is different */
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "Updating peer '%s' from old URI '%s' to new URI '%s'\n", GNUNET_i2s (peer), existing_hostlist->hostlist_uri , hostlist->hostlist_uri);
819 existing_hostlist->hostlist_uri = GNUNET_realloc( existing_hostlist->hostlist_uri, strlen(hostlist->hostlist_uri) +1 );
820 if ( NULL != existing_hostlist->hostlist_uri )
821 {
822 strcpy(existing_hostlist->hostlist_uri,hostlist->hostlist_uri);
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
824 "URI updated to %s \n", existing_hostlist->hostlist_uri);
825 /* reset hostlist usage information*/
826 existing_hostlist->hello_count = 0;
827 existing_hostlist->time_last_usage = GNUNET_TIME_absolute_get_zero();
828 existing_hostlist->times_used = 0;
829 return GNUNET_YES;
830 }
831 else
832 {
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
834 "Updating peer '%s' failed \n", GNUNET_i2s (peer));
835 return GNUNET_NO;
836 }
837 }
791 } 838 }
792 839
840 /* since hostlist already existed in hashmap, object can be destroyed */
841 GNUNET_free ( hostlist->hostlist_uri );
842 GNUNET_free ( hostlist );
843
793 return GNUNET_YES; 844 return GNUNET_YES;
794} 845}
795 846
@@ -869,18 +920,21 @@ static int load_hostlist_file ()
869} 920}
870 921
871 922
872static int iterate_hashmap(void *cls, const GNUNET_HashCode *key, void *value) 923static int iterate_hashmap ( void *cls, const GNUNET_HashCode *key, void *value )
873{ 924{
874 /* add code to process hostlist entries */
875 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 925 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
876 ("Now iterating over peer entry: %s\n"), GNUNET_i2s ( (const struct GNUNET_PeerIdentity *) key)); 926 ("Now iterating over peer entry: %s\n"), GNUNET_i2s ( (const struct GNUNET_PeerIdentity *) key));
877 927
878 /* Testing */ 928 /* code to serialize hostlists to file*/
879 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 929 GNUNET_BIO_write_string ( (struct GNUNET_BIO_WriteHandle *) cls, ((struct GNUNET_Hostlist *) value)->hostlist_uri );
880 ("Entry url: %s \n"), ((struct GNUNET_Hostlist *) value)->hostlist_uri );
881 930
931 /* code to free hostlist */
882 if ( NULL != value ) 932 if ( NULL != value )
883 GNUNET_free ( value ); 933 {
934
935 GNUNET_free ( value );
936 GNUNET_free ( ((struct GNUNET_Hostlist *) value)->hostlist_uri );
937 }
884 938
885 return GNUNET_YES; 939 return GNUNET_YES;
886} 940}
@@ -919,9 +973,7 @@ static int save_hostlist_file ()
919 /* iterate over all entries in hashmap */ 973 /* iterate over all entries in hashmap */
920 GNUNET_CONTAINER_multihashmap_iterate ( hostlist_hashmap, 974 GNUNET_CONTAINER_multihashmap_iterate ( hostlist_hashmap,
921 &iterate_hashmap, 975 &iterate_hashmap,
922 NULL ); 976 wh );
923
924 GNUNET_BIO_write_string ( wh, "DUMMY TEXT" );
925 977
926 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) ) 978 if ( GNUNET_OK != GNUNET_BIO_write_close ( wh ) )
927 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 979 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -967,10 +1019,7 @@ GNUNET_HOSTLIST_client_start (const struct GNUNET_CONFIGURATION_Handle *c,
967 *msgh = &advertisement_handler; 1019 *msgh = &advertisement_handler;
968 1020
969 learning = learn; 1021 learning = learn;
970 if ( learning ) 1022 hostlist_hashmap = GNUNET_CONTAINER_multihashmap_create ( MAX_NUMBER_HOSTLISTS );
971 {
972 hostlist_hashmap = GNUNET_CONTAINER_multihashmap_create ( MAX_NUMBER_HOSTLISTS );
973 }
974 load_hostlist_file (); 1023 load_hostlist_file ();
975 1024
976 GNUNET_STATISTICS_get (stats, 1025 GNUNET_STATISTICS_get (stats,
@@ -995,11 +1044,7 @@ GNUNET_HOSTLIST_client_stop ()
995 "Hostlist client shutdown\n"); 1044 "Hostlist client shutdown\n");
996#endif 1045#endif
997 save_hostlist_file (); 1046 save_hostlist_file ();
998 1047 GNUNET_CONTAINER_multihashmap_destroy ( hostlist_hashmap );
999 if ( learning )
1000 {
1001 GNUNET_CONTAINER_multihashmap_destroy ( hostlist_hashmap );
1002 }
1003 1048
1004 if (current_task != GNUNET_SCHEDULER_NO_TASK) 1049 if (current_task != GNUNET_SCHEDULER_NO_TASK)
1005 { 1050 {