From ca82b99f4237e1870c6605d17db880fd259fed46 Mon Sep 17 00:00:00 2001 From: "roberto@debian32" Date: Sat, 18 Jun 2011 08:59:02 +0200 Subject: [PATCH] subscribe from file --- master.c | 73 +++++++++++++++++++++++++++++++++++++++++++----------- protocol.c | 2 ++ utils.c | 5 ++++ uwsgi.h | 1 + 4 files changed, 66 insertions(+), 15 deletions(-) diff --git a/master.c b/master.c index 5e6d50dc..13ecb7a1 100644 --- a/master.c +++ b/master.c @@ -42,32 +42,24 @@ void expire_rb_timeouts(struct rb_root *root) { } } - -void uwsgi_subscribe(char *subscription) { - +void uwsgi_send_subscription(char *udp_address, char *key, size_t keysize) { char *ssb; char subscrbuf[4096]; + uint16_t ustrlen; - char *udp_address = strchr(subscription,':'); - if (!udp_address) return; + ssb = subscrbuf; - char *subscription_key = strchr(udp_address+1, ':'); - if (!subscription_key) return; - udp_address = uwsgi_concat2n(subscription, subscription_key-subscription, "", 0); - - ssb = subscrbuf; - - ustrlen = 3; + ustrlen = 3; *ssb++ = (uint8_t) (ustrlen & 0xff); *ssb++ = (uint8_t) ((ustrlen >>8) & 0xff); memcpy(ssb, "key", ustrlen); ssb+=ustrlen; - ustrlen = strlen(subscription_key+1); + ustrlen = keysize; *ssb++ = (uint8_t) (ustrlen & 0xff); *ssb++ = (uint8_t) ((ustrlen >>8) & 0xff); - memcpy(ssb, subscription_key+1, ustrlen); + memcpy(ssb, key, ustrlen); ssb+=ustrlen; ustrlen = 7; @@ -83,7 +75,58 @@ void uwsgi_subscribe(char *subscription) { ssb+=ustrlen; send_udp_message(224, udp_address, subscrbuf, ssb-subscrbuf); - free(udp_address); +} + +void uwsgi_subscribe(char *subscription) { + + int subfile_size; + int i; + char *key = NULL; + int keysize = 0; + + char *udp_address = strchr(subscription,':'); + if (!udp_address) return; + + char *subscription_key = strchr(udp_address+1, ':'); + if (!subscription_key) return; + + udp_address = uwsgi_concat2n(subscription, subscription_key-subscription, "", 0); + + if (subscription_key[1] == '@') { + if (!uwsgi_file_exists(subscription_key+2)) goto clear; + char *lines = uwsgi_open_and_read(subscription_key+2, &subfile_size, 1, NULL); + if (subfile_size > 0) { + key = lines; + for(i=0;i 0) { + if (key[0] != '#' && key[0] != '\n') { + uwsgi_send_subscription(udp_address, key, keysize); + } + } + break; + } + else if (lines[i] == '\n') { + if (keysize > 0) { + if (key[0] != '#' && key[0] != '\n') { + uwsgi_send_subscription(udp_address, key, keysize); + } + } + key = lines+i+1; + keysize = 0; + continue; + } + keysize++; + } + } + } + else { + uwsgi_send_subscription(udp_address, subscription_key, strlen(subscription_key)); + } + +clear: + + free(udp_address); } diff --git a/protocol.c b/protocol.c index e1e91bba..1336db05 100644 --- a/protocol.c +++ b/protocol.c @@ -173,6 +173,8 @@ ssize_t send_udp_message(uint8_t modifier1, char *host, char *message, uint16_t } close(fd); + udp_port[0] = ':'; + return ret; } diff --git a/utils.c b/utils.c index aeb150fd..74908ac4 100644 --- a/utils.c +++ b/utils.c @@ -1570,6 +1570,11 @@ char *uwsgi_resolve_ip(char *domain) { return inet_ntoa(*(struct in_addr *) he->h_addr_list[0]); } +int uwsgi_file_exists(char *filename) { + // TODO check for http url or stdin + return !access(filename, R_OK); +} + char *uwsgi_open_and_read(char *url, int *size, int add_zero, char *magic_table[]) { int fd; diff --git a/uwsgi.h b/uwsgi.h index a696c854..e053ed46 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -2031,3 +2031,4 @@ void uwsgi_manage_command_cron(time_t); int *uwsgi_attach_fd(int, int, char *, size_t); int uwsgi_count_sockets(struct uwsgi_socket *); +int uwsgi_file_exists(char *);