aboutsummaryrefslogtreecommitdiff
path: root/clean_table.h
diff options
context:
space:
mode:
authorBen Burwell <bburwell1@gmail.com>2013-04-29 16:13:34 -0400
committerBen Burwell <bburwell1@gmail.com>2013-04-29 16:13:34 -0400
commitca9e46c937e75df43d2f80f0d957fabb07892c29 (patch)
tree0d8ae2bc1d1e3e248c746586a5daf7cb58a79463 /clean_table.h
Init
Diffstat (limited to 'clean_table.h')
-rw-r--r--clean_table.h47
1 files changed, 47 insertions, 0 deletions
diff --git a/clean_table.h b/clean_table.h
new file mode 100644
index 0000000..d5dc42e
--- /dev/null
+++ b/clean_table.h
@@ -0,0 +1,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();
+ }
+} \ No newline at end of file