quit clients via webinterface

This commit is contained in:
Olaf Rempel 2007-07-07 13:53:36 +02:00
parent 2e21d27d10
commit 4709dd9f6d
3 changed files with 37 additions and 7 deletions

View File

@ -219,7 +219,7 @@ int ctcs_accept_handler(int fd, void *privdata)
return 0;
}
int ctcs_httpd_handler(struct httpd_con *con, void *privdata)
int ctcs_httpd_show(struct httpd_con *con, void *privdata)
{
struct linebuffer *lbuf = create_linebuffer(16384);
if (lbuf == NULL) {
@ -235,9 +235,9 @@ int ctcs_httpd_handler(struct httpd_con *con, void *privdata)
if (list_empty(&torrent->client_list))
continue;
linebuffer_printf(lbuf, "<table border=\"1\">\n<tr><td colspan=\"5\" align=\"center\">%s</td></tr>\n", torrent->name);
linebuffer_printf(lbuf, "<tr></td><td><b>Client IP:Port</b></td><td><b>Chunks (have/total/avail)</b></td>");
linebuffer_printf(lbuf, "<td><b>Download total(current)</b></td><td><b>Upload total(current)</b></td><td><b>Completed since</b></td></tr>\n");
linebuffer_printf(lbuf, "<table border=\"1\">\n<tr><td colspan=\"6\" align=\"center\">%s</td></tr>\n", torrent->name);
linebuffer_printf(lbuf, "<tr></td><td><b>Client IP:Port</b></td><td><b>Chunks (have/total/avail)</b></td><td><b>Download total(current)</b></td>");
linebuffer_printf(lbuf, "<td><b>Upload total(current)</b></td><td><b>Completed since</b></td><td><b>Quit</b></td></tr>\n");
struct client_con *tmp;
list_for_each_entry(tmp, &torrent->client_list, list) {
@ -245,9 +245,11 @@ int ctcs_httpd_handler(struct httpd_con *con, void *privdata)
get_sockaddr_buf(&tmp->addr),
(double)tmp->chunk_have / (double)tmp->chunk_total * 100.0, tmp->chunk_have, tmp->chunk_total, tmp->chunk_avail);
linebuffer_printf(lbuf, "<td align=\"right\">%llu (%d)</td><td align=\"right\">%llu (%d)</td><td align=\"right\">%s</td></tr>\n",
linebuffer_printf(lbuf, "<td align=\"right\">%llu (%d)</td><td align=\"right\">%llu (%d)</td><td align=\"right\">%s</td>",
tmp->total_dn, tmp->bw_dn, tmp->total_up, tmp->bw_up,
(tmp->completed != 0) ? ctime(&tmp->completed) : "-");
linebuffer_printf(lbuf, "<td><a href=\"/quit?client=%s\">Quit</td></tr>\n", get_sockaddr_buf(&tmp->addr));
}
linebuffer_printf(lbuf, "</table>\n<br><br>\n");
}
@ -257,3 +259,29 @@ int ctcs_httpd_handler(struct httpd_con *con, void *privdata)
linebuffer_free(lbuf);
return 0;
}
int ctcs_httpd_quit(struct httpd_con *con, void *privdata)
{
if (con->req_arg_cnt == 2 && strncmp(con->req_args[1], "client=", 7) == 0) {
struct sockaddr_in addr;
if (parse_sockaddr(con->req_args[1] +7, &addr) == 0) {
struct torrent_file *torrent;
list_for_each_entry(torrent, &torrent_list, list) {
struct client_con *search;
list_for_each_entry(search, &torrent->client_list, list) {
if (search->addr.sin_addr.s_addr != addr.sin_addr.s_addr)
continue;
if (search->addr.sin_port != addr.sin_port)
continue;
write(event_get_fd(search->event), "CTQUIT\n", 7);
}
}
}
}
char *text = "HTTP/1.0 302 OK\r\nContent-Type: text/html\r\nConnection: close\r\nLocation: /\r\n\r\n";
write(con->fd, text, strlen(text));
return 0;
}

View File

@ -5,6 +5,7 @@
int ctcs_trigger_status(void *privdata);
int ctcs_accept_handler(int fd, void *privdata);
int ctcs_httpd_handler(struct httpd_con *con, void *privdata);
int ctcs_httpd_show(struct httpd_con *con, void *privdata);
int ctcs_httpd_quit(struct httpd_con *con, void *privdata);
#endif /* _CONNECTION_H_ */

View File

@ -39,7 +39,8 @@ int main(int argc, char *argv[])
config_get_strings("global", "listen", listen_cb, ctcs_accept_handler);
config_get_strings("global", "listen-http", listen_cb, httpd_accept_handler);
httpd_add_cb("/", 1, ctcs_httpd_handler, NULL);
httpd_add_cb("/quit", 0, ctcs_httpd_quit, NULL);
httpd_add_cb("/", 1, ctcs_httpd_show, NULL);
struct timeval tv = { .tv_sec = 10, .tv_usec = 0 };
event_add_timeout(&tv, ctcs_trigger_status, NULL);