use single trigger for all connections

This commit is contained in:
Olaf Rempel 2007-06-19 15:44:57 +02:00
parent 6d76ec29ec
commit 67dcf4a8e4
5 changed files with 36 additions and 22 deletions

View File

@ -17,7 +17,6 @@ struct client_con {
struct list_head list; struct list_head list;
struct sockaddr_in addr; struct sockaddr_in addr;
struct event_timeout *trigger;
struct event_fd *event; struct event_fd *event;
struct linebuffer *lbuf; struct linebuffer *lbuf;
@ -36,18 +35,10 @@ static void free_client(struct client_con *con)
{ {
close(event_get_fd(con->event)); close(event_get_fd(con->event));
event_remove_fd(con->event); event_remove_fd(con->event);
event_remove_timeout(con->trigger);
free(con->lbuf); free(con->lbuf);
free(con); free(con);
} }
static int trigger_status(void *privdata)
{
struct client_con *con = (struct client_con *)privdata;
write(event_get_fd(con->event), "SENDSTATUS\n", 11);
return 0;
}
static int data_cb(int fd, void *privdata) static int data_cb(int fd, void *privdata)
{ {
struct client_con *con = (struct client_con *)privdata; struct client_con *con = (struct client_con *)privdata;
@ -92,6 +83,15 @@ static int data_cb(int fd, void *privdata)
return 0; return 0;
} }
int ctcs_trigger_status(void *privdata)
{
struct client_con *con;
list_for_each_entry(con, &client_list, list)
write(event_get_fd(con->event), "SENDSTATUS\n", 11);
return 0;
}
int ctcs_accept_handler(int fd, void *privdata) int ctcs_accept_handler(int fd, void *privdata)
{ {
struct client_con *con = malloc(sizeof(struct client_con)); struct client_con *con = malloc(sizeof(struct client_con));
@ -118,9 +118,6 @@ int ctcs_accept_handler(int fd, void *privdata)
return 0; return 0;
} }
struct timeval tv = { .tv_sec = 10, .tv_usec = 0 };
con->trigger = event_add_timeout(&tv, trigger_status, con);
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(&con->list, &client_list);
@ -129,27 +126,27 @@ int ctcs_accept_handler(int fd, void *privdata)
int ctcs_httpd_handler(struct httpd_con *con, void *privdata) int ctcs_httpd_handler(struct httpd_con *con, void *privdata)
{ {
int size = 4096, len = 0; struct linebuffer *lbuf = create_linebuffer(4096);
char *text = malloc(size); if (lbuf == NULL) {
if (text == NULL) {
httpd_send_error(con, "500 ERROR", "Out of Memory"); httpd_send_error(con, "500 ERROR", "Out of Memory");
return -1; return -1;
} }
len += snprintf(text + len, size - len, "<html><body><h1>ctorrent stats</h1>\n<table border=\"1\">\n"); linebuffer_printf(lbuf, "<html><body><h1>ctorrent stats</h1>\n<table border=\"1\">\n");
len += snprintf(text + len, size - len, "<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, "<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");
struct client_con *tmp; struct client_con *tmp;
list_for_each_entry(tmp, &client_list, list) { list_for_each_entry(tmp, &client_list, list) {
len += snprintf(text + len, size - len, "<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></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);
} }
len += snprintf(text + len, size - len, "</table>\n</body></html>\n"); linebuffer_printf(lbuf, "</table>\n</body></html>\n");
httpd_send_header(con, "200 OK", "text/html"); httpd_send_header(con, "200 OK", "text/html");
write(con->fd, text, len); linebuffer_writefd(lbuf, con->fd);
linebuffer_free(lbuf);
return 0; return 0;
} }

View File

@ -3,6 +3,7 @@
#include "httpd.h" #include "httpd.h"
int ctcs_trigger_status(void *privdata);
int ctcs_accept_handler(int fd, void *privdata); int ctcs_accept_handler(int fd, void *privdata);
int ctcs_httpd_handler(struct httpd_con *con, void *privdata); int ctcs_httpd_handler(struct httpd_con *con, void *privdata);

View File

@ -49,7 +49,7 @@ int linebuffer_readfd(struct linebuffer *buf, int fd)
{ {
int len = read(fd, buf->data + buf->pos, buf->size - buf->pos); int len = read(fd, buf->data + buf->pos, buf->size - buf->pos);
if (len <= 0) if (len <= 0)
return len; return -1;
buf->pos += len; buf->pos += len;
return len; return len;
@ -79,6 +79,17 @@ int linebuffer_parsefd(struct linebuffer *buf, int fd)
return 0; return 0;
} }
int linebuffer_writefd(struct linebuffer *buf, int fd)
{
int len = write(fd, buf->data, buf->pos);
if (len <= 0)
return -1;
/* TODO: now assuming everything is written */
buf->pos = 0;
return len;
}
int linebuffer_printf(struct linebuffer *buf, const char *fmt, ...) int linebuffer_printf(struct linebuffer *buf, const char *fmt, ...)
{ {
va_list az; va_list az;

View File

@ -14,9 +14,11 @@ void linebuffer_free(struct linebuffer *buf);
int linebuffer_clear(struct linebuffer *buf); int linebuffer_clear(struct linebuffer *buf);
int linebuffer_put(struct linebuffer *buf, const char *src, unsigned int size);
int linebuffer_readfd(struct linebuffer *buf, int fd); int linebuffer_readfd(struct linebuffer *buf, int fd);
int linebuffer_parsefd(struct linebuffer *buf, int fd); int linebuffer_parsefd(struct linebuffer *buf, int fd);
int linebuffer_writefd(struct linebuffer *buf, int fd);
int linebuffer_put(struct linebuffer *buf, const char *src, unsigned int size);
int linebuffer_printf(struct linebuffer *buf, const char *fmt, ...); int linebuffer_printf(struct linebuffer *buf, const char *fmt, ...);
char * linebuffer_getline(struct linebuffer *buf, int *len); char * linebuffer_getline(struct linebuffer *buf, int *len);

View File

@ -41,6 +41,9 @@ int main(int argc, char *argv[])
httpd_add_cb("/", 1, ctcs_httpd_handler, NULL); httpd_add_cb("/", 1, ctcs_httpd_handler, NULL);
struct timeval tv = { .tv_sec = 10, .tv_usec = 0 };
event_add_timeout(&tv, ctcs_trigger_status, NULL);
event_loop(); event_loop();
return 0; return 0;
} }