aboutsummaryrefslogtreecommitdiff
path: root/clean_table.h
blob: d5dc42e823e0d0f45930ea326ea417411c2e78f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
 * Localchat
 * Ben Burwell
 *
 * Table cleaning functions
 */

void *clean_table(void *arg) {

	int i;
	int j;
	time_t now;
	double diff;

	while (1) {

		pthread_mutex_lock(&peer_table_lock);
		
		time(&now);

		for (i = 0; i < num_peers_in_table; i++) {
			diff = difftime(now, peers[i].last_seen);
			if (diff > 10) {
				for (j = i; j < num_peers_in_table-1; j++) {
					peers[j] = peers[j+1];
				}
				num_peers_in_table--;
			}
		}

		pthread_mutex_unlock(&peer_table_lock);
		sleep(1);
	}

	return NULL;
}

void start_clean_table_thread() {

	pthread_t thread;
	pthread_mutex_init(&peer_table_lock, NULL);

	if (pthread_create(&thread, NULL, clean_table, NULL) > 0) {
		printf("Error starting clean_table thread\n");
		abort();
	}
}