From 5f5316a034d68f8825a16e7b866c8e8b2b6c6bd8 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Mon, 4 Jun 2018 00:09:02 +1200 Subject: Initial dump of prototype --- alarm-tools/Makefile | 4 + alarm-tools/alarmd_reference_client.c | 224 ++++++++++++++++++++++++++++++++++ alarm-tools/alarms-show.c | 109 +++++++++++++++++ 3 files changed, 337 insertions(+) create mode 100644 alarm-tools/Makefile create mode 100644 alarm-tools/alarmd_reference_client.c create mode 100644 alarm-tools/alarms-show.c (limited to 'alarm-tools') diff --git a/alarm-tools/Makefile b/alarm-tools/Makefile new file mode 100644 index 0000000..75f3c5b --- /dev/null +++ b/alarm-tools/Makefile @@ -0,0 +1,4 @@ +LDFLAGS += -L../lib -lalarm +CFLAGS += -I../inc + +all: alarms-show diff --git a/alarm-tools/alarmd_reference_client.c b/alarm-tools/alarmd_reference_client.c new file mode 100644 index 0000000..d200e04 --- /dev/null +++ b/alarm-tools/alarmd_reference_client.c @@ -0,0 +1,224 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "alarmd_proto.h" + +int8_t read_string(int sock, char (*buffer)[128]) +{ + uint8_t length = 0; + if (recv(sock, &length, sizeof(uint8_t), 0) < sizeof(uint8_t)) { + return -1; + } + if (length >= 128) { + return -1; + } + if (recv(sock, buffer, length, 0) < 0) { + return -1; + } + (*buffer)[127] = '\0'; + return length; +} + +int alarmd_dump_alarms(int sock) +{ + ssize_t i = 0; + ssize_t nread = 0; + size_t length = 0; + uint32_t count = 0; + uint32_t packet_type = 0; + uint8_t is_raised = 0; + char buffer[128]; + + packet_type = htonl(ALARMD_PACKET_TYPE_QUERY); + + if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { + perror("send"); + return 1; + } + + nread = recv(sock, &count, sizeof(count), 0); + if (nread < 0) { + perror("recv"); + return 1; + } else if (nread < sizeof(count)) { + fprintf(stderr, "Alarm count too small\n"); + return 1; + } + + count = ntohl(count); + + printf("Alarms\n" + "------\n"); + + for (i = 0; i < count; i++) { + if ((length = read_string(sock, &buffer)) < 0) { + perror("recv"); + break; + } + if (recv(sock, &is_raised, sizeof(is_raised), 0) != sizeof(is_raised)) { + perror("recv"); + break; + } + printf("[%s] %s\n", is_raised ? "\x1b[1;31mRAISE\x1b[0m" : "\x1b[1;32mCLEAR\x1b[0m", buffer); + } + + if (i < count) { + fprintf(stderr, "Not all alarms received, output must be missing some\n"); + return 1; + } + + printf("\n"); + + return 0; +} + +int alarmd_register(int sock, char *desc, char (*uuid)[16]) +{ + uint8_t length = 0; + uint32_t packet_type = 0; + + packet_type = htonl(ALARMD_PACKET_TYPE_REGISTER); + if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { + perror("send"); + return 1; + } + + length = strlen(desc); + if (send(sock, &length, sizeof(length), 0) != sizeof(length)) { + perror("send"); + return 1; + } + + if (send(sock, desc, strlen(desc), 0) != strlen(desc)) { + perror("send"); + return 1; + } + if (recv(sock, uuid, 16, 0) != 16) { + perror("recv"); + return 1; + } + return 0; +} + +int alarmd_deregister(int sock, char uuid[16]) +{ + uint32_t packet_type = 0; + + packet_type = htonl(ALARMD_PACKET_TYPE_DEREGISTER); + if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { + perror("send"); + return 1; + } + + /* FIXME 16 magic */ + if (send(sock, uuid, 16, 0) != 16) { + perror("send"); + return 1; + } + return 0; +} + +int alarmd_raise(int sock, char uuid[16]) +{ + uint32_t packet_type = 0; + + packet_type = htonl(ALARMD_PACKET_TYPE_RAISE); + if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { + perror("send"); + return 1; + } + + /* FIXME 16 magic */ + if (send(sock, uuid, 16, 0) != 16) { + perror("send"); + return 1; + } + return 0; +} + +int alarmd_clear(int sock, char uuid[16]) +{ + uint32_t packet_type = 0; + + packet_type = htonl(ALARMD_PACKET_TYPE_CLEAR); + if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { + perror("send"); + return 1; + } + + /* FIXME 16 magic */ + if (send(sock, uuid, 16, 0) != 16) { + perror("send"); + return 1; + } + return 0; +} + +int main(void) +{ + int sock = 0; + char buffer[128]; + struct addrinfo hints, *s_info, *p; + + bzero(&hints, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + /* FIXME spec custom hostname on cmd line */ + if (getaddrinfo("localhost", ALARMD_PORT, &hints, &s_info) != 0) { + perror("getaddrinfo"); + return 1; + } + + for (p = s_info; p != NULL; p = p->ai_next) { + if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) { + perror("socket"); + continue; + } + if (connect(sock, p->ai_addr, p->ai_addrlen) < 0) { + close(sock); + perror("connect"); + continue; + } + break; + } + + if (!p) { + fprintf(stderr, "Connection to server failed\n"); + return 1; + } + + + /**/ + char *name = "Some test alarm"; + char uuid[16]; + /**/ + + freeaddrinfo(s_info); + + printf(">>>Register\n"); + int i, j; + for (i = 0; i < 10000; i++) { + for (j = 0; j < 100; j++) { + alarmd_register(sock, name, &uuid); + } + fprintf(stderr, "%d", i); + } + +// alarmd_dump_alarms(sock); +// printf(">>>Raise\n"); +// alarmd_raise(sock, uuid); +// alarmd_dump_alarms(sock); +// printf(">>>Clear\n"); +// alarmd_clear(sock, uuid); +// alarmd_dump_alarms(sock); +// printf(">>>Deregister\n"); +// alarmd_deregister(sock, uuid); + close(sock); +} diff --git a/alarm-tools/alarms-show.c b/alarm-tools/alarms-show.c new file mode 100644 index 0000000..0d63d4a --- /dev/null +++ b/alarm-tools/alarms-show.c @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "alarmd_proto.h" + +int dump_alarms(int sock) +{ + ssize_t i = 0; + ssize_t nread = 0; + size_t length = 0; + uint32_t count = 0; + uint32_t packet_type = 0; + uint8_t is_raised = 0; + char buffer[128]; + + packet_type = htonl(ALARMD_PACKET_TYPE_QUERY); + + if (send(sock, &packet_type, sizeof(packet_type), 0) != sizeof(packet_type)) { + perror("send"); + return 1; + } + + nread = recv(sock, &count, sizeof(count), 0); + if (nread < 0) { + perror("recv"); + return 1; + } else if (nread < sizeof(count)) { + fprintf(stderr, "Alarm count too small\n"); + return 1; + } + + count = ntohl(count); + + printf("Alarms\n" + "------\n"); + + for (i = 0; i < count; i++) { + if ((length = recv_string(sock, &buffer)) < 0) { + perror("recv"); + break; + } + if (recv(sock, &is_raised, sizeof(is_raised), 0) != sizeof(is_raised)) { + perror("recv"); + break; + } + printf("[%s] %s\n", is_raised ? "\x1b[1;31mRAISE\x1b[0m" : "\x1b[1;32mCLEAR\x1b[0m", buffer); + } + + if (i < count) { + fprintf(stderr, "Not all alarms received, output must be missing some\n"); + return 1; + } + + printf("\n"); + + return 0; +} + +int main(void) +{ + int sock = 0; + char buffer[128]; + struct addrinfo hints, *s_info, *p; + + bzero(&hints, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + /* FIXME spec custom hostname on cmd line */ + if (getaddrinfo("localhost", ALARMD_PORT, &hints, &s_info) != 0) { + perror("getaddrinfo"); + return 1; + } + + for (p = s_info; p != NULL; p = p->ai_next) { + if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) { + perror("socket"); + continue; + } + if (connect(sock, p->ai_addr, p->ai_addrlen) < 0) { + close(sock); + perror("connect"); + continue; + } + break; + } + + if (!p) { + fprintf(stderr, "Connection to server failed\n"); + return 1; + } + + + /**/ + char *name = "Some test alarm"; + char uuid[16]; + /**/ + + freeaddrinfo(s_info); + + dump_alarms(sock); + close(sock); +} -- cgit v1.1