Fixed group peer chosing algorithm.

Groups should be much more stable.
This commit is contained in:
irungentoo 2014-10-04 12:03:39 -04:00
parent 3cc93792da
commit 07ab8e5287
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98

View File

@ -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;