Implemented stop_kademlia.

This commit is contained in:
Jose Marcial Vieira Bisneto 2017-03-07 02:38:36 -03:00
parent 95d2a06855
commit 0ffe73ae28

View file

@ -28,6 +28,7 @@ int net_family = 0;
volatile int searching = 0; // search lock, -1 to busy, 0 to free, 1 to running. 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 char hash[20]; // hash to be search or announce.
volatile int announce_port = 0; volatile int announce_port = 0;
volatile int closing = 0;
struct bs_list { struct bs_list {
char *ip; char *ip;
@ -112,11 +113,16 @@ void *kademlia_thread (void *ptr)
char h[sizeof hash]; char h[sizeof hash];
int i; int i;
for (i = 0 ; i < sizeof hash ; 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); dht_search(h, announce_port, net_family, callback, NULL);
searching = 0; 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); usleep(random() % 100000);
} }
// TODO: Read cache nodes from file and load using dht_insert_node.
ksock = sock; ksock = sock;
net_family = family; net_family = family;
tosleep = timeout; 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); 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. */ /* Functions called by the DHT. */
int dht_blacklisted (const struct sockaddr *sa, int salen) int dht_blacklisted (const struct sockaddr *sa, int salen)