drop completed clients after some time

This commit is contained in:
Olaf Rempel 2007-06-20 16:08:38 +02:00
parent 67dcf4a8e4
commit 419ce7766e

View File

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <time.h>
#include "event.h" #include "event.h"
#include "httpd.h" #include "httpd.h"
@ -29,6 +30,8 @@ struct client_con {
int chunk_total; int chunk_total;
int chunk_avail; int chunk_avail;
int chunk_have; int chunk_have;
long completed;
}; };
static void free_client(struct client_con *con) static void free_client(struct client_con *con)
@ -80,15 +83,50 @@ static int data_cb(int fd, void *privdata)
} }
linebuffer_freeline(con->lbuf); linebuffer_freeline(con->lbuf);
} }
/* move completed clients to top of the list, ordered by their timestamp */
if (con->chunk_have == con->chunk_total && con->chunk_total != 0 && con->completed == 0) {
con->completed = time(NULL);
list_del(&con->list);
struct client_con *search;
list_for_each_entry(search, &client_list, list) {
if (search->completed == 0)
break;
if (search->completed > con->completed)
break;
}
list_add_tail(&con->list, &search->list);
return 0;
}
return 0; return 0;
} }
int ctcs_trigger_status(void *privdata) int ctcs_trigger_status(void *privdata)
{ {
int delete = 0;
long timeout = time(NULL) - 300;
struct client_con *con; struct client_con *con;
list_for_each_entry(con, &client_list, list) list_for_each_entry(con, &client_list, list) {
write(event_get_fd(con->event), "SENDSTATUS\n", 11); write(event_get_fd(con->event), "SENDSTATUS\n", 11);
delete += (con->completed == 0) ? -1 : 1;
}
/* delete holds the number of clients to quit */
list_for_each_entry(con, &client_list, list) {
if (delete <= 0)
break;
if (con->completed == 0 || con->completed > timeout)
continue;
write(event_get_fd(con->event), "CTQUIT\n", 7);
delete--;
}
return 0; return 0;
} }
@ -120,7 +158,7 @@ int ctcs_accept_handler(int fd, void *privdata)
con->event = event_add_readfd(NULL, sockfd, data_cb, con); con->event = event_add_readfd(NULL, sockfd, data_cb, con);
list_add(&con->list, &client_list); list_add_tail(&con->list, &client_list);
return 0; return 0;
} }
@ -132,20 +170,21 @@ int ctcs_httpd_handler(struct httpd_con *con, void *privdata)
return -1; return -1;
} }
linebuffer_printf(lbuf, "<html><body><h1>ctorrent stats</h1>\n<table border=\"1\">\n"); linebuffer_printf(lbuf, "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n");
linebuffer_printf(lbuf, "<tr><td><b>Client IP:Port</b></td><td><b>Chunks (have/total/avail)</b></td><td><b>Download total(current)</b></td><td><b>Upload total(current)</b></td></tr>\n"); linebuffer_printf(lbuf, "<html><head><meta http-equiv=\"refresh\" content=\"30;\"></head><body><h1>ctorrent stats</h1>\n<table border=\"1\">\n");
linebuffer_printf(lbuf, "<tr><td><b>Client IP:Port</b></td><td><b>Chunks (have/total/avail)</b></td><td><b>Download total(current)</b></td><td><b>Upload total(current)</b></td><td><b>Completed since</b></td></tr>\n");
struct client_con *tmp; struct client_con *tmp;
list_for_each_entry(tmp, &client_list, list) { list_for_each_entry(tmp, &client_list, list) {
linebuffer_printf(lbuf, "<tr><td align=\"right\">%s</td><td align=\"right\">%3.2lf%% (%d/%d/%d)</td><td align=\"right\">%llu (%d)</td><td align=\"right\">%llu (%d)</td></tr>\n", linebuffer_printf(lbuf, "<tr><td align=\"right\">%s</td><td align=\"right\">%3.2lf%% (%d/%d/%d)</td><td align=\"right\">%llu (%d)</td><td align=\"right\">%llu (%d)</td><td align=\"right\">%s</td></tr>\n",
get_sockaddr_buf(&tmp->addr), get_sockaddr_buf(&tmp->addr),
(double)tmp->chunk_have / (double)tmp->chunk_total * 100.0, tmp->chunk_have, tmp->chunk_total, tmp->chunk_avail, (double)tmp->chunk_have / (double)tmp->chunk_total * 100.0, tmp->chunk_have, tmp->chunk_total, tmp->chunk_avail,
tmp->total_dn, tmp->bw_dn, tmp->total_up, tmp->bw_up); tmp->total_dn, tmp->bw_dn, tmp->total_up, tmp->bw_up,
(tmp->completed != 0) ? ctime(&tmp->completed) : "-");
} }
linebuffer_printf(lbuf, "</table>\n</body></html>\n"); linebuffer_printf(lbuf, "</table>\n</body></html>\n");
httpd_send_header(con, "200 OK", "text/html");
linebuffer_writefd(lbuf, con->fd); linebuffer_writefd(lbuf, con->fd);
linebuffer_free(lbuf); linebuffer_free(lbuf);
return 0; return 0;