diff --git a/a.out b/a.out index d09150a276fe6fcef6dbd8fb6994b7e23abaf262..141e658942b535d3ec055993f72e88b14421b025 100644 Binary files a/a.out and b/a.out differ diff --git a/t2.c b/t2.c index 676612b76db41320b3bfd6715e81d5aec1e9853a..284893cde836f39584d44264e054e866f3bd22aa 100644 --- a/t2.c +++ b/t2.c @@ -187,9 +187,9 @@ void calculate_finger_table(Node* head, int max_id) { } -// Função para imprimir todas as tabelas finger +//Função para imprimir todas as tabelas finger void print_finger_table(Node** nodes_involved,int num_nodes, int timestamp) { - for(int i = 0; i <= num_nodes; i++) { + for(int i = 0; i < num_nodes; i++) { printf("%d T %d {", timestamp, nodes_involved[i]->id); for (int j = 0; j < nodes_involved[i]->tam_finger; j++) { printf("%d", nodes_involved[i]->finger[j]); @@ -216,6 +216,7 @@ void print_finger_table_completa(Node* head, int timestamp) { // Função para realizar lookup de uma chave Node ** lookup(Node* head, int timestamp, int node_id, int key, int *num_nodes) { + printf("Lookup\n"); Node* current = head; Node** nodes_involved = (Node**)malloc(TAM_MAX_FINGER * sizeof(Node*)); *num_nodes = 0; @@ -225,25 +226,23 @@ Node ** lookup(Node* head, int timestamp, int node_id, int key, int *num_nodes) } printf("%d L %d {", timestamp, key); - Node* start = current; //Meu nó partida + Node *start = current; int id_aux = current->id; nodes_involved[(*num_nodes)++] = current; - printf("%d,", current->id); - while(1){ int found = 0; + printf("%d", current->id); //Verificar se o nó atual já tem a chave for (int i = 0; i < current->key_count; i++) { if (current->keys[i] == key) { - printf("%d}\n", current->id); - nodes_involved[(*num_nodes)++] = current; + printf("}\n"); return nodes_involved; } } + printf(","); //Procura o maior nó conhecido pela finger_table que está acima do valor da chave for (int i = 0; i < current->tam_finger; i++) { if (current->finger[i] > key) { - printf("%d", current->finger[i]); id_aux = current->finger[i]; found = 1; break; @@ -258,28 +257,21 @@ Node ** lookup(Node* head, int timestamp, int node_id, int key, int *num_nodes) max = current->finger[i]; } } + id_aux = max; } - // Mover para o nó com ID id_aux while(current->id != id_aux){ current = current->next; } - nodes_involved[*num_nodes++] = current; //Adiciona o nó atual ao array de nós envolvidos + nodes_involved[(*num_nodes)++] = current; //Adiciona o nó atual ao array de nós envolvidos - //Verificar se o nó atual já tem a chave - for (int i = 0; i < current->key_count; i++) { - if (current->keys[i] == key) { - nodes_involved[(*num_nodes)++] = current; - printf("}\n"); - return nodes_involved; - } + //Previne loops infinitos + if(current->id == start->id){ + printf("}\n"); + nodes_involved[(*num_nodes)++] = current; + break; } - //Se o nó atual não tiver a chave, continuar a busca - printf(","); - start = current; } - printf("}\n"); - nodes_involved[(*num_nodes)++] = current; return nodes_involved; } @@ -334,8 +326,9 @@ int main() { insert_key(dht, id, key); } else if (op == 'L') { int num_nodes = 0; - nodes_involved= lookup(dht, timestamp, id, key, &num_nodes); + nodes_involved = lookup(dht, timestamp, id, key, &num_nodes); print_finger_table(nodes_involved, num_nodes, timestamp); + free(nodes_involved); } else if (op == 'P'){ imprime_chaves(dht); print_finger_table_completa(dht, timestamp);