This commit is contained in:
Jan-Philipp Litza 2015-10-13 20:05:13 +00:00
commit 1b17b93bcb
8 changed files with 57 additions and 57 deletions

View File

@ -11,7 +11,7 @@ include $(INCLUDE_DIR)/package.mk
define Package/gluon-alfred define Package/gluon-alfred
SECTION:=gluon SECTION:=gluon
CATEGORY:=Gluon CATEGORY:=Gluon
DEPENDS:=+gluon-core +gluon-announce +gluon-cron +alfred DEPENDS:=+gluon-core +gluon-announced +gluon-neighbour-info +gluon-cron +alfred
TITLE:=Configure alfred TITLE:=Configure alfred
endef endef

View File

@ -1 +1 @@
* * * * * /lib/gluon/announce/collect.lua nodeinfo | gzip | alfred -s 158; /lib/gluon/announce/collect.lua statistics | gzip | alfred -s 159; /lib/gluon/announce/collect.lua neighbours | gzip | alfred -s 160 * * * * * gluon-neighbour-info -d ::1 -p 1001 -t 1 -c 1 -r nodeinfo | gzip | alfred -s 158; gluon-neighbour-info -d ::1 -p 1001 -t 1 -c 1 -r statistics | gzip | alfred -s 159; gluon-neighbour-info -d ::1 -p 1001 -t 1 -c 1 -r neighbours | gzip | alfred -s 160

View File

@ -1,8 +0,0 @@
#!/usr/bin/lua
local announce = require 'gluon.announce'
local json = require 'luci.jsonc'
local announce_dir = '/lib/gluon/announce/' .. arg[1] .. '.d'
print(json.stringify(announce.collect_dir(announce_dir)))

View File

@ -11,10 +11,6 @@ end
module('gluon.announced', package.seeall) module('gluon.announced', package.seeall)
function handle_request(query) function handle_request(query)
if query:match('^nodeinfo$') then
return json.stringify(collect('nodeinfo'))
end
local m = query:match('^GET ([a-z ]+)$') local m = query:match('^GET ([a-z ]+)$')
if m then if m then
local data = {} local data = {}
@ -29,5 +25,10 @@ function handle_request(query)
if next(data) then if next(data) then
return deflate.compress(json.stringify(data)) return deflate.compress(json.stringify(data))
end end
elseif query:match('^[a-z]+$') then
local ok, data = pcall(collect, query)
if ok then
return json.stringify(data)
end
end end
end end

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

View File

@ -1,18 +1,5 @@
#!/usr/bin/lua #!/bin/sh
local announce = require 'gluon.announce' echo "Access-Control-Allow-Origin: *"
local json = require 'luci.jsonc'
local util = require 'luci.util'
local nixio = require 'nixio'
local announce_dir = '/lib/gluon/announce/statistics.d/' exec gluon-neighbour-info -s "" -l -d ::1 -p 1001 -t 1 -r statistics
io.write("Access-Control-Allow-Origin: *\n")
io.write("Content-type: text/event-stream\n\n")
while true do
local data = json.stringify(announce.collect_dir(announce_dir))
io.write("data: " .. data .. "\n\n")
io.flush()
nixio.nanosleep(1, 0)
end

View File

@ -1,15 +1,8 @@
#!/usr/bin/lua #!/bin/sh
local announce = require 'gluon.announce' cat <<EOF
local json = require 'luci.jsonc' Access-Control-Allow-Origin: *
local util = require 'luci.util' Content-type: application/json
local nixio = require 'nixio'
local announce_dir = '/lib/gluon/announce/nodeinfo.d/' EOF
exec gluon-neighbour-info -d ::1 -p 1001 -t 1 -c 1 -r nodeinfo
io.write("Access-Control-Allow-Origin: *\n")
io.write("Content-type: application/json\n\n")
local data = json.stringify(announce.collect_dir(announce_dir))
io.write(data)
io.flush()