gluon-neighbour-info: Add -l and -c parameter, make event type for -s configurable

This commit is contained in:
Jan-Philipp Litza 2015-09-01 00:26:38 +02:00 committed by Matthias Schiffer
parent 3725f7bca3
commit d2ff7d5c22
2 changed files with 41 additions and 14 deletions

View File

@ -36,13 +36,17 @@
#include <time.h> #include <time.h>
void usage() { void usage() {
puts("Usage: gluon-neighbour-info [-h] [-s] [-t <sec>] -d <dest> -p <port> -i <if0> -r <request>"); puts("Usage: gluon-neighbour-info [-h] [-s] [-l] [-c <count>] [-t <sec>] -d <dest> -p <port> -i <if0> -r <request>");
puts(" -p <int> UDP port"); puts(" -p <int> UDP port");
puts(" -d <ip6> multicast group, e.g. ff02:0:0:0:0:0:2:1001"); puts(" -d <ip6> multicast group, e.g. ff02:0:0:0:0:0:2:1001");
puts(" -i <string> interface, e.g. eth0 "); puts(" -i <string> interface, e.g. eth0 ");
puts(" -r <string> request, e.g. nodeinfo"); puts(" -r <string> request, e.g. nodeinfo");
puts(" -t <sec> timeout in seconds (default: 3)"); puts(" -t <sec> timeout in seconds (default: 3)");
puts(" -s output as server-sent events"); puts(" -s <event> output as server-sent events of type <event>");
puts(" or without type if <event> is the empty string");
puts(" -c <count> only wait for at most <count> replies");
puts(" -l after timeout (or <count> replies if -c is given),");
puts(" send another request and loop forever");
puts(" -h this help\n"); puts(" -h this help\n");
} }
@ -78,9 +82,10 @@ ssize_t recvtimeout(int socket, void *buffer, size_t length, int flags, struct t
return ret; return ret;
} }
int request(const int sock, const struct sockaddr_in6 *client_addr, const char *request, bool sse, double timeout) { int request(const int sock, const struct sockaddr_in6 *client_addr, const char *request, const char *sse, double timeout, unsigned int max_count) {
ssize_t ret; ssize_t ret;
char buffer[8192]; char buffer[8192];
unsigned int count = 0;
ret = sendto(sock, request, strlen(request), 0, (struct sockaddr *)client_addr, sizeof(struct sockaddr_in6)); ret = sendto(sock, request, strlen(request), 0, (struct sockaddr *)client_addr, sizeof(struct sockaddr_in6));
@ -95,14 +100,17 @@ int request(const int sock, const struct sockaddr_in6 *client_addr, const char *
getclock(&tv_offset); getclock(&tv_offset);
while (1) { do {
ret = recvtimeout(sock, buffer, sizeof(buffer), 0, &tv_timeout, &tv_offset); ret = recvtimeout(sock, buffer, sizeof(buffer), 0, &tv_timeout, &tv_offset);
if (ret < 0) if (ret < 0)
break; break;
if (sse) if (sse) {
fputs("event: neighbour\ndata: ", stdout); if (sse[0] != '\0')
fprintf(stdout, "event: %s\n", sse);
fputs("data: ", stdout);
}
fwrite(buffer, sizeof(char), ret, stdout); fwrite(buffer, sizeof(char), ret, stdout);
@ -112,9 +120,13 @@ int request(const int sock, const struct sockaddr_in6 *client_addr, const char *
fputs("\n", stdout); fputs("\n", stdout);
fflush(stdout); fflush(stdout);
} count++;
} while (max_count == 0 || count < max_count);
return 0; if ((max_count == 0 && count == 0) || count < max_count)
return EXIT_FAILURE;
else
return EXIT_SUCCESS;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
@ -137,11 +149,14 @@ int main(int argc, char **argv) {
int port_set = 0; int port_set = 0;
int destination_set = 0; int destination_set = 0;
unsigned int max_count = 0;
double timeout = 3.0; double timeout = 3.0;
bool sse = false; char *sse = NULL;
bool loop = false;
int ret = false;
int c; int c;
while ((c = getopt(argc, argv, "p:d:r:i:t:sh")) != -1) while ((c = getopt(argc, argv, "p:d:r:i:t:s:c:lh")) != -1)
switch (c) { switch (c) {
case 'p': case 'p':
client_addr.sin6_port = htons(atoi(optarg)); client_addr.sin6_port = htons(atoi(optarg));
@ -166,7 +181,17 @@ int main(int argc, char **argv) {
timeout = atof(optarg); timeout = atof(optarg);
break; break;
case 's': case 's':
sse = true; sse = optarg;
break;
case 'l':
loop = true;
break;
case 'c':
max_count = atoi(optarg);
if (max_count < 0) {
perror("Negative count not supported");
exit(EXIT_FAILURE);
}
break; break;
case 'h': case 'h':
usage(); usage();
@ -184,10 +209,12 @@ int main(int argc, char **argv) {
if (sse) if (sse)
fputs("Content-Type: text/event-stream\n\n", stdout); fputs("Content-Type: text/event-stream\n\n", stdout);
request(sock, &client_addr, request_string, sse, timeout); do {
ret = request(sock, &client_addr, request_string, sse, timeout, max_count);
} while(loop);
if (sse) if (sse)
fputs("event: eot\ndata: null\n\n", stdout); fputs("event: eot\ndata: null\n\n", stdout);
return EXIT_SUCCESS; return ret;
} }

View File

@ -4,4 +4,4 @@ echo 'Access-Control-Allow-Origin: *'
batctl if | cut -d: -f1 | grep -qxF "$QUERY_STRING" || exit 1 batctl if | cut -d: -f1 | grep -qxF "$QUERY_STRING" || exit 1
exec /usr/bin/gluon-neighbour-info -s -i "$QUERY_STRING" -d ff02::2:1001 -p 1001 -r nodeinfo exec /usr/bin/gluon-neighbour-info -s neighbour -i "$QUERY_STRING" -d ff02::2:1001 -p 1001 -r nodeinfo