Implemented stop_kademlia.

yamux
Jose Marcial Vieira Bisneto 2017-03-07 02:38:36 -03:00
parent 95d2a06855
commit 0ffe73ae28
1 changed files with 21 additions and 1 deletions

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 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)