summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 1d13aa73..bcaaf6d8 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -319,6 +319,28 @@ static int replace_bad( Client_data * list,
319 319
320 return 1; 320 return 1;
321} 321}
322/*Sort the list. It will be sorted from furthest to closest.
323 TODO: this is innefficient and needs to be optimized.*/
324static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id)
325{
326 if(length == 0)
327 return;
328 uint32_t i, count;
329 while(1) {
330 count = 0;
331 for(i = 0; i < (length - 1); ++i) {
332 if(id_closest(comp_client_id, list[i].client_id, list[i + 1].client_id) == 1) {
333 Client_data temp = list[i + 1];
334 list[i + 1] = list[i];
335 list[i] = temp;
336 ++count;
337 }
338 }
339 if(count == 0)
340 return;
341 }
342}
343
322 344
323/* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ 345/* replace the first good node that is further to the comp_client_id than that of the client_id in the list */
324static int replace_good( Client_data * list, 346static int replace_good( Client_data * list,
@@ -329,6 +351,7 @@ static int replace_good( Client_data * list,
329{ 351{
330 uint32_t i; 352 uint32_t i;
331 uint64_t temp_time = unix_time(); 353 uint64_t temp_time = unix_time();
354 sort_list(list, length, comp_client_id);
332 355
333 for(i = 0; i < length; ++i) 356 for(i = 0; i < length; ++i)
334 if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) { 357 if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) {