diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-05-17 14:28:20 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-05-17 14:28:20 +0200 |
commit | 0f31d194025df5d63fcee65e32baa90cc5aa0ad6 (patch) | |
tree | 83ff9e158ba9df44dc8bb1a3c4ff6304ceb86a29 /doc | |
parent | implemented timed write, read and poll (diff) | |
download | bus-0f31d194025df5d63fcee65e32baa90cc5aa0ad6.tar.gz bus-0f31d194025df5d63fcee65e32baa90cc5aa0ad6.tar.bz2 bus-0f31d194025df5d63fcee65e32baa90cc5aa0ad6.tar.xz |
add timed example and fix timed polling
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/examples/nonblocking/poll.c | 4 | ||||
-rw-r--r-- | doc/examples/timed/.gitignore | 5 | ||||
-rw-r--r-- | doc/examples/timed/Makefile | 13 | ||||
-rw-r--r-- | doc/examples/timed/README | 18 | ||||
-rw-r--r-- | doc/examples/timed/cleanup.c | 8 | ||||
-rw-r--r-- | doc/examples/timed/init.c | 8 | ||||
-rw-r--r-- | doc/examples/timed/poll.c | 45 | ||||
-rw-r--r-- | doc/examples/timed/write.c | 32 |
8 files changed, 131 insertions, 2 deletions
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 <bus.h> +#include <stdio.h> + +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 <bus.h> +#include <stdio.h> + +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 <bus.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> + +#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 <bus.h> +#include <stdio.h> +#include <unistd.h> +#include <stdint.h> + +#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; +} + |