diff --git a/a.out b/a.out index 59a6a27fa0f9cc3a0ea4b862fa67d16f795b3823..d09150a276fe6fcef6dbd8fb6994b7e23abaf262 100644 Binary files a/a.out and b/a.out differ diff --git a/t2.c b/t2.c index 070387f6e6ade0ba6d01c9c0f5f4d87aa7f79915..676612b76db41320b3bfd6715e81d5aec1e9853a 100644 --- a/t2.c +++ b/t2.c @@ -168,7 +168,6 @@ void calculate_finger_table(Node* head, int max_id) { Node* current = head; printf("Max_id:%d\n", max_id); int log2_max_id = ceil(log2(max_id)); - current->tam_finger = log2_max_id; printf("Log2_max_id:%d\n", log2_max_id); do { for (int k = 1; k <= log2_max_id; k++) { @@ -182,13 +181,26 @@ void calculate_finger_table(Node* head, int max_id) { } current->finger[k-1] = temp->id; } + current->tam_finger = log2_max_id; current = current->next; } while (current != head); } // Função para imprimir todas as tabelas finger -void print_finger_table(Node* head, int timestamp) { +void print_finger_table(Node** nodes_involved,int num_nodes, int timestamp) { + 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]); + if (j < nodes_involved[i]->tam_finger - 1) + printf(","); + } + printf("}\n"); + } +} + +void print_finger_table_completa(Node* head, int timestamp) { Node* current = head; do { printf("%d F %d {", timestamp, current->id); @@ -201,11 +213,12 @@ void print_finger_table(Node* head, int timestamp) { } + // Função para realizar lookup de uma chave -void lookup(Node* head, int timestamp, int node_id, int key){ +Node ** lookup(Node* head, int timestamp, int node_id, int key, int *num_nodes) { Node* current = head; - Node* nodes_involved[TAM_MAX_FINGER]; - int nodes_count = 0; + Node** nodes_involved = (Node**)malloc(TAM_MAX_FINGER * sizeof(Node*)); + *num_nodes = 0; while (current->id != node_id) { current = current->next; @@ -214,6 +227,7 @@ void lookup(Node* head, int timestamp, int node_id, int key){ printf("%d L %d {", timestamp, key); Node* start = current; //Meu nó partida int id_aux = current->id; + nodes_involved[(*num_nodes)++] = current; printf("%d,", current->id); while(1){ @@ -222,7 +236,8 @@ void lookup(Node* head, int timestamp, int node_id, int key){ for (int i = 0; i < current->key_count; i++) { if (current->keys[i] == key) { printf("%d}\n", current->id); - return; + nodes_involved[(*num_nodes)++] = current; + return nodes_involved; } } //Procura o maior nó conhecido pela finger_table que está acima do valor da chave @@ -249,13 +264,14 @@ void lookup(Node* head, int timestamp, int node_id, int key){ while(current->id != id_aux){ current = current->next; } - nodes_involved[nodes_count++] = 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; + return nodes_involved; } } //Se o nó atual não tiver a chave, continuar a busca @@ -263,7 +279,8 @@ void lookup(Node* head, int timestamp, int node_id, int key){ start = current; } printf("}\n"); - nodes_involved[nodes_count++] = current; + nodes_involved[(*num_nodes)++] = current; + return nodes_involved; } // Função para adicionar uma chave a um nó respeitando o limite de um nó guarda as chaves maiores que seu antecessor e menores e iguais a ele. @@ -299,6 +316,7 @@ void imprime_chaves(Node* head) { // Função principal para testar a inserção de nós int main() { Node* dht = NULL; + Node **nodes_involved = NULL; int timestamp, id, key; int max_id = 0; char op; @@ -315,10 +333,12 @@ int main() { } else if (op == 'I') { insert_key(dht, id, key); } else if (op == 'L') { - lookup(dht, timestamp, id, key); + int num_nodes = 0; + nodes_involved= lookup(dht, timestamp, id, key, &num_nodes); + print_finger_table(nodes_involved, num_nodes, timestamp); } else if (op == 'P'){ imprime_chaves(dht); - print_finger_table(dht, timestamp); + print_finger_table_completa(dht, timestamp); } }