From 07ab8e52876df4e80171bcdbeccd4bb6872bb3e5 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sat, 4 Oct 2014 12:03:39 -0400 Subject: [PATCH] Fixed group peer chosing algorithm. Groups should be much more stable. --- toxcore/group.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/toxcore/group.c b/toxcore/group.c index 4644b837..2fbd341c 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -198,7 +198,7 @@ static uint16_t calculate_comp_value(const uint8_t *pk1, const uint8_t *pk2) if (pk1[index] == pk2[index]) continue; - cmp2 = abs((int)pk1[index] - (int)pk2[index]); + cmp2 = pk1[index] - pk2[index]; break; } @@ -244,8 +244,20 @@ static int add_to_closest(Group_Chats *g_c, int groupnumber, const uint8_t *real uint16_t comp_val = calculate_comp_value(g->real_pk, real_pk); uint16_t comp_d = 0; - for (i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) { - uint16_t comp = calculate_comp_value(g->real_pk, g->closest_peers[i].real_pk); + for (i = 0; i < (DESIRED_CLOSE_CONNECTIONS / 2); ++i) { + uint16_t comp; + comp = calculate_comp_value(g->real_pk, g->closest_peers[i].real_pk); + + if (comp > comp_val && comp > comp_d) { + index = i; + comp_d = comp; + } + } + + comp_val = calculate_comp_value(real_pk, g->real_pk); + + for (i = (DESIRED_CLOSE_CONNECTIONS / 2); i < DESIRED_CLOSE_CONNECTIONS; ++i) { + uint16_t comp = calculate_comp_value(g->closest_peers[i].real_pk, g->real_pk); if (comp > comp_val && comp > comp_d) { index = i; @@ -258,10 +270,24 @@ static int add_to_closest(Group_Chats *g_c, int groupnumber, const uint8_t *real return -1; } + uint8_t old_real_pk[crypto_box_PUBLICKEYBYTES]; + uint8_t old_temp_pk[crypto_box_PUBLICKEYBYTES]; + uint8_t old = 0; + + if (g->closest_peers[index].entry) { + memcpy(old_real_pk, g->closest_peers[index].real_pk, crypto_box_PUBLICKEYBYTES); + memcpy(old_temp_pk, g->closest_peers[index].temp_pk, crypto_box_PUBLICKEYBYTES); + old = 1; + } + g->closest_peers[index].entry = 1; memcpy(g->closest_peers[index].real_pk, real_pk, crypto_box_PUBLICKEYBYTES); memcpy(g->closest_peers[index].temp_pk, temp_pk, crypto_box_PUBLICKEYBYTES); + if (old) { + add_to_closest(g_c, groupnumber, old_real_pk, old_temp_pk); + } + if (!g->changed) g->changed = GROUPCHAT_CLOSEST_ADDED;