diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-07-19 13:31:01 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-07-19 13:31:01 +0000 |
commit | 2d59b3649626bd7eab7f5555824cb568a71ca25a (patch) | |
tree | 52621b2aa0fb27539132aecf9fc2cbaf01188c3f /src | |
parent | e619e9b05287d3944813acb2926b9955b8de2908 (diff) | |
download | gnunet-2d59b3649626bd7eab7f5555824cb568a71ca25a.tar.gz gnunet-2d59b3649626bd7eab7f5555824cb568a71ca25a.zip |
simplify
Diffstat (limited to 'src')
-rw-r--r-- | src/dht/gnunet-service-dht.c | 132 |
1 files changed, 40 insertions, 92 deletions
diff --git a/src/dht/gnunet-service-dht.c b/src/dht/gnunet-service-dht.c index 28c5b22e3..be635d4a0 100644 --- a/src/dht/gnunet-service-dht.c +++ b/src/dht/gnunet-service-dht.c | |||
@@ -1321,6 +1321,7 @@ inverse_distance (const GNUNET_HashCode * target, | |||
1321 | return ((unsigned int) -1) - distance (target, have); | 1321 | return ((unsigned int) -1) - distance (target, have); |
1322 | } | 1322 | } |
1323 | 1323 | ||
1324 | |||
1324 | /** | 1325 | /** |
1325 | * Find the optimal bucket for this key, regardless | 1326 | * Find the optimal bucket for this key, regardless |
1326 | * of the current number of buckets in use. | 1327 | * of the current number of buckets in use. |
@@ -1341,6 +1342,7 @@ find_bucket (const GNUNET_HashCode * hc) | |||
1341 | return MAX_BUCKETS - bits - 1; | 1342 | return MAX_BUCKETS - bits - 1; |
1342 | } | 1343 | } |
1343 | 1344 | ||
1345 | |||
1344 | /** | 1346 | /** |
1345 | * Find which k-bucket this peer should go into, | 1347 | * Find which k-bucket this peer should go into, |
1346 | * taking into account the size of the k-bucket | 1348 | * taking into account the size of the k-bucket |
@@ -1357,14 +1359,13 @@ static int | |||
1357 | find_current_bucket (const GNUNET_HashCode * hc) | 1359 | find_current_bucket (const GNUNET_HashCode * hc) |
1358 | { | 1360 | { |
1359 | int actual_bucket; | 1361 | int actual_bucket; |
1362 | |||
1360 | actual_bucket = find_bucket (hc); | 1363 | actual_bucket = find_bucket (hc); |
1361 | |||
1362 | if (actual_bucket == GNUNET_SYSERR) /* hc and our peer identity match! */ | 1364 | if (actual_bucket == GNUNET_SYSERR) /* hc and our peer identity match! */ |
1363 | return lowest_bucket; | 1365 | return lowest_bucket; |
1364 | else if (actual_bucket < lowest_bucket) /* actual_bucket not yet used */ | 1366 | if (actual_bucket < lowest_bucket) /* actual_bucket not yet used */ |
1365 | return lowest_bucket; | 1367 | return lowest_bucket; |
1366 | else | 1368 | return actual_bucket; |
1367 | return actual_bucket; | ||
1368 | } | 1369 | } |
1369 | 1370 | ||
1370 | #if EXTRA_CHECKS | 1371 | #if EXTRA_CHECKS |
@@ -1528,51 +1529,6 @@ update_core_preference (void *cls, | |||
1528 | peer); | 1529 | peer); |
1529 | } | 1530 | } |
1530 | 1531 | ||
1531 | /** | ||
1532 | * Really add a peer to a bucket (only do assertions | ||
1533 | * on size, etc.) | ||
1534 | * | ||
1535 | * @param peer GNUNET_PeerIdentity of the peer to add | ||
1536 | * @param bucket the already figured out bucket to add | ||
1537 | * the peer to | ||
1538 | * @param atsi performance information | ||
1539 | * | ||
1540 | * @return the newly added PeerInfo | ||
1541 | */ | ||
1542 | static struct PeerInfo * | ||
1543 | add_peer (const struct GNUNET_PeerIdentity *peer, | ||
1544 | unsigned int bucket, | ||
1545 | const struct GNUNET_TRANSPORT_ATS_Information *atsi) | ||
1546 | { | ||
1547 | struct PeerInfo *new_peer; | ||
1548 | GNUNET_assert (bucket < MAX_BUCKETS); | ||
1549 | GNUNET_assert (peer != NULL); | ||
1550 | new_peer = GNUNET_malloc (sizeof (struct PeerInfo)); | ||
1551 | #if 0 | ||
1552 | new_peer->latency = latency; | ||
1553 | new_peer->distance = distance; | ||
1554 | #endif | ||
1555 | |||
1556 | memcpy (&new_peer->id, peer, sizeof (struct GNUNET_PeerIdentity)); | ||
1557 | |||
1558 | GNUNET_CONTAINER_DLL_insert_after (k_buckets[bucket].head, | ||
1559 | k_buckets[bucket].tail, | ||
1560 | k_buckets[bucket].tail, new_peer); | ||
1561 | k_buckets[bucket].peers_size++; | ||
1562 | |||
1563 | #if DO_UPDATE_PREFERENCE | ||
1564 | if ((GNUNET_CRYPTO_hash_matching_bits | ||
1565 | (&my_identity.hashPubKey, &peer->hashPubKey) > 0) | ||
1566 | && (k_buckets[bucket].peers_size <= bucket_size)) | ||
1567 | { | ||
1568 | |||
1569 | new_peer->preference_task = | ||
1570 | GNUNET_SCHEDULER_add_now (&update_core_preference, new_peer); | ||
1571 | } | ||
1572 | #endif | ||
1573 | |||
1574 | return new_peer; | ||
1575 | } | ||
1576 | 1532 | ||
1577 | /** | 1533 | /** |
1578 | * Given a peer and its corresponding bucket, | 1534 | * Given a peer and its corresponding bucket, |
@@ -1903,40 +1859,6 @@ schedule_ping_messages () | |||
1903 | } | 1859 | } |
1904 | #endif | 1860 | #endif |
1905 | 1861 | ||
1906 | /** | ||
1907 | * Attempt to add a peer to our k-buckets. | ||
1908 | * | ||
1909 | * @param peer the peer identity of the peer being added | ||
1910 | * @param bucket the bucket that we want this peer to go in | ||
1911 | * @param atsi transport ATS information | ||
1912 | * | ||
1913 | * @return NULL if the peer was not added, | ||
1914 | * pointer to PeerInfo for new peer otherwise | ||
1915 | */ | ||
1916 | static struct PeerInfo * | ||
1917 | try_add_peer (const struct GNUNET_PeerIdentity *peer, | ||
1918 | unsigned int bucket, | ||
1919 | const struct GNUNET_TRANSPORT_ATS_Information *atsi) | ||
1920 | { | ||
1921 | int peer_bucket; | ||
1922 | struct PeerInfo *new_peer; | ||
1923 | |||
1924 | if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity))) | ||
1925 | return NULL; | ||
1926 | |||
1927 | peer_bucket = find_current_bucket (&peer->hashPubKey); | ||
1928 | |||
1929 | GNUNET_assert (peer_bucket >= lowest_bucket); | ||
1930 | new_peer = add_peer (peer, peer_bucket, atsi); | ||
1931 | |||
1932 | if ((k_buckets[lowest_bucket].peers_size) >= bucket_size) | ||
1933 | enable_next_bucket (); | ||
1934 | #if DO_PING | ||
1935 | schedule_ping_messages (); | ||
1936 | #endif | ||
1937 | return new_peer; | ||
1938 | } | ||
1939 | |||
1940 | 1862 | ||
1941 | /** | 1863 | /** |
1942 | * Task run to check for messages that need to be sent to a client. | 1864 | * Task run to check for messages that need to be sent to a client. |
@@ -5206,6 +5128,8 @@ handle_core_connect (void *cls, | |||
5206 | { | 5128 | { |
5207 | struct PeerInfo *ret; | 5129 | struct PeerInfo *ret; |
5208 | struct DHTPutEntry *put_entry; | 5130 | struct DHTPutEntry *put_entry; |
5131 | int peer_bucket; | ||
5132 | |||
5209 | /* Check for connect to self message */ | 5133 | /* Check for connect to self message */ |
5210 | if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity))) | 5134 | if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity))) |
5211 | return; | 5135 | return; |
@@ -5225,6 +5149,7 @@ handle_core_connect (void *cls, | |||
5225 | "%s:%s Received %s message for peer %s, but already have peer in RT!", | 5149 | "%s:%s Received %s message for peer %s, but already have peer in RT!", |
5226 | my_short_id, "DHT", "CORE CONNECT", GNUNET_i2s (peer)); | 5150 | my_short_id, "DHT", "CORE CONNECT", GNUNET_i2s (peer)); |
5227 | #endif | 5151 | #endif |
5152 | GNUNET_break (0); | ||
5228 | return; | 5153 | return; |
5229 | } | 5154 | } |
5230 | 5155 | ||
@@ -5242,15 +5167,38 @@ handle_core_connect (void *cls, | |||
5242 | } | 5167 | } |
5243 | else if (datacache == NULL) | 5168 | else if (datacache == NULL) |
5244 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DHT has no connection to datacache!\n"); | 5169 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DHT has no connection to datacache!\n"); |
5245 | ret = try_add_peer (peer, find_current_bucket (&peer->hashPubKey), atsi); | 5170 | |
5246 | if (ret != NULL) | 5171 | peer_bucket = find_current_bucket (&peer->hashPubKey); |
5247 | { | 5172 | GNUNET_assert (peer_bucket >= lowest_bucket); |
5248 | newly_found_peers++; | 5173 | GNUNET_assert (peer_bucket < MAX_BUCKETS); |
5249 | GNUNET_CONTAINER_multihashmap_put (all_known_peers, &peer->hashPubKey, | 5174 | ret = GNUNET_malloc (sizeof (struct PeerInfo)); |
5250 | ret, | 5175 | #if 0 |
5251 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | 5176 | ret->latency = latency; |
5252 | increment_stats(STAT_PEERS_KNOWN); | 5177 | ret->distance = distance; |
5253 | } | 5178 | #endif |
5179 | ret->id = *peer; | ||
5180 | GNUNET_CONTAINER_DLL_insert_after (k_buckets[peer_bucket].head, | ||
5181 | k_buckets[peer_bucket].tail, | ||
5182 | k_buckets[peer_bucket].tail, | ||
5183 | ret); | ||
5184 | k_buckets[peer_bucket].peers_size++; | ||
5185 | #if DO_UPDATE_PREFERENCE | ||
5186 | if ( (GNUNET_CRYPTO_hash_matching_bits | ||
5187 | (&my_identity.hashPubKey, &peer->hashPubKey) > 0) && | ||
5188 | (k_buckets[peer_bucket].peers_size <= bucket_size) ) | ||
5189 | ret->preference_task = GNUNET_SCHEDULER_add_now (&update_core_preference, | ||
5190 | ret); | ||
5191 | #endif | ||
5192 | if ((k_buckets[lowest_bucket].peers_size) >= bucket_size) | ||
5193 | enable_next_bucket (); | ||
5194 | #if DO_PING | ||
5195 | schedule_ping_messages (); | ||
5196 | #endif | ||
5197 | newly_found_peers++; | ||
5198 | GNUNET_CONTAINER_multihashmap_put (all_known_peers, &peer->hashPubKey, | ||
5199 | ret, | ||
5200 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
5201 | increment_stats (STAT_PEERS_KNOWN); | ||
5254 | 5202 | ||
5255 | #if DEBUG_DHT | 5203 | #if DEBUG_DHT |
5256 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 5204 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |