From 0f31d194025df5d63fcee65e32baa90cc5aa0ad6 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 17 May 2015 14:28:20 +0200 Subject: add timed example and fix timed polling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- doc/examples/nonblocking/poll.c | 4 ++-- doc/examples/timed/.gitignore | 5 +++++ doc/examples/timed/Makefile | 13 ++++++++++++ doc/examples/timed/README | 18 +++++++++++++++++ doc/examples/timed/cleanup.c | 8 ++++++++ doc/examples/timed/init.c | 8 ++++++++ doc/examples/timed/poll.c | 45 +++++++++++++++++++++++++++++++++++++++++ doc/examples/timed/write.c | 32 +++++++++++++++++++++++++++++ 8 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 doc/examples/timed/.gitignore create mode 100644 doc/examples/timed/Makefile create mode 100644 doc/examples/timed/README create mode 100644 doc/examples/timed/cleanup.c create mode 100644 doc/examples/timed/init.c create mode 100644 doc/examples/timed/poll.c create mode 100644 doc/examples/timed/write.c (limited to 'doc') diff --git a/doc/examples/nonblocking/poll.c b/doc/examples/nonblocking/poll.c index c918b5b..2b0c266 100644 --- a/doc/examples/nonblocking/poll.c +++ b/doc/examples/nonblocking/poll.c @@ -14,9 +14,9 @@ int main() const char *message; long long tick = 0; t(bus_open(&bus, "/tmp/example-bus", BUS_RDONLY)); - t(bus_poll_start(&bus, BUS_NOWAIT)); + t(bus_poll_start(&bus)); for (;;) { - message = bus_poll(&bus); + message = bus_poll(&bus, BUS_NOWAIT); if (message == NULL) { t(errno != EAGAIN); printf("waiting... %lli\n", ++tick); diff --git a/doc/examples/timed/.gitignore b/doc/examples/timed/.gitignore new file mode 100644 index 0000000..e5c1856 --- /dev/null +++ b/doc/examples/timed/.gitignore @@ -0,0 +1,5 @@ +cleanup +init +write +poll + diff --git a/doc/examples/timed/Makefile b/doc/examples/timed/Makefile new file mode 100644 index 0000000..1b1cbd3 --- /dev/null +++ b/doc/examples/timed/Makefile @@ -0,0 +1,13 @@ +COMMANDS = init cleanup write poll + +all: ${COMMANDS} + +%: %.c + ${CC} -Wall -Wextra -pedantic -std=c99 -lbus -o $@ $< + +clean: + -rm ${COMMANDS} + + +.PHONY: all clean + diff --git a/doc/examples/timed/README b/doc/examples/timed/README new file mode 100644 index 0000000..b0989bf --- /dev/null +++ b/doc/examples/timed/README @@ -0,0 +1,18 @@ +API usage example + +This example shows how to use timed operations. + + + +First of, run make to build this example. + +To start the example run ./init. +When you are done run ./cleanup. + +Running instances of ./poll will wait for new messages +continuously, but with one second timeouts. + +./poll, ./init and ./cleanup are run without any +additional arguments. ./write is run with the message +as the second argument. + diff --git a/doc/examples/timed/cleanup.c b/doc/examples/timed/cleanup.c new file mode 100644 index 0000000..00f07bc --- /dev/null +++ b/doc/examples/timed/cleanup.c @@ -0,0 +1,8 @@ +#include +#include + +int main() +{ + return bus_unlink("/tmp/example-bus") && (perror("cleanup"), 1); +} + diff --git a/doc/examples/timed/init.c b/doc/examples/timed/init.c new file mode 100644 index 0000000..870e10d --- /dev/null +++ b/doc/examples/timed/init.c @@ -0,0 +1,8 @@ +#include +#include + +int main() +{ + return bus_create("/tmp/example-bus", 0, NULL) && (perror("init"), 1); +} + diff --git a/doc/examples/timed/poll.c b/doc/examples/timed/poll.c new file mode 100644 index 0000000..d8c5aac --- /dev/null +++ b/doc/examples/timed/poll.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + + +int main() +{ + bus_t bus; + const char *message; + long long tick = 0; + struct timespec timeout; + t(bus_open(&bus, "/tmp/example-bus", BUS_RDONLY)); + t(bus_poll_start(&bus)); + for (;;) { + t(clock_gettime(CLOCK_MONOTONIC, &timeout)); + timeout.tv_sec += 1; + message = bus_poll_timed(&bus, &timeout, CLOCK_MONOTONIC); + if (message == NULL) { + t(errno != EAGAIN); + printf("waiting... %lli\n", ++tick); + sleep(1); + continue; + } + tick = 0; + message = strchr(message, ' ') + 1; + if (!strcmp(message, "stop")) + break; + printf("\033[01m%s\033[21m\n", message); + } + t(bus_poll_stop(&bus)); + bus_close(&bus); + return 0; + +fail: + perror("poll"); + bus_poll_stop(&bus); + bus_close(&bus); + return 1; +} + diff --git a/doc/examples/timed/write.c b/doc/examples/timed/write.c new file mode 100644 index 0000000..b6b4c16 --- /dev/null +++ b/doc/examples/timed/write.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + + +static char message[BUS_MEMORY_SIZE]; + + + +int main(int argc, char *argv[]) +{ + bus_t bus; + if (argc < 2) { + fprintf(stderr, "%s: USAGE: %s message\n", argv[0], argv[0]); + return 2; + } + sprintf(message, "0 %s", argv[1]); + t(bus_open(&bus, "/tmp/example-bus", BUS_WRONLY)); + t(bus_write(&bus, message, 0)); + bus_close(&bus); + return 0; + +fail: + perror("write"); + bus_close(&bus); + return 1; +} + -- cgit v1.2.3-70-g09d2