From 0ffe73ae28202df7453be74b6502093a0c2df13b Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Tue, 7 Mar 2017 02:38:36 -0300 Subject: [PATCH] Implemented stop_kademlia. --- routing/kademlia.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/routing/kademlia.c b/routing/kademlia.c index d02e574..cacb24e 100644 --- a/routing/kademlia.c +++ b/routing/kademlia.c @@ -28,6 +28,7 @@ int net_family = 0; volatile int searching = 0; // search lock, -1 to busy, 0 to free, 1 to running. volatile char hash[20]; // hash to be search or announce. volatile int announce_port = 0; +volatile int closing = 0; struct bs_list { char *ip; @@ -112,11 +113,16 @@ void *kademlia_thread (void *ptr) char h[sizeof hash]; int i; for (i = 0 ; i < sizeof hash ; i++) { - h[i] = hash[i]; + h[i] = hash[i]; // Copy hash array to new array so can call + // dht_search without volatile variable. } dht_search(h, announce_port, net_family, callback, NULL); searching = 0; } + if(closing) { + // TODO: Create a routine to save the cache nodes in the file sometimes and before closing. + return 0; // end thread. + } } } @@ -173,6 +179,8 @@ int bootstrap_kademlia(int sock, int family, char* peer_id, int timeout) usleep(random() % 100000); } + // TODO: Read cache nodes from file and load using dht_insert_node. + ksock = sock; net_family = family; tosleep = timeout; @@ -180,6 +188,18 @@ int bootstrap_kademlia(int sock, int family, char* peer_id, int timeout) return pthread_create(&pth, NULL, kademlia_thread, NULL); } +void stop_kademlia (void) +{ + closing = 1; + + // Wait kademlia_thread finish. + (void) pthread_join(pth, NULL); + + dht_uninit(); + + close (ksock); +} + /* Functions called by the DHT. */ int dht_blacklisted (const struct sockaddr *sa, int salen)