From 0d44f16f6738c0776edf60b84f49f0c6f875a8fe Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 17 May 2015 14:40:46 +0200 Subject: style + add timed read example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- doc/examples/timed/read.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 doc/examples/timed/read.c (limited to 'doc/examples/timed/read.c') diff --git a/doc/examples/timed/read.c b/doc/examples/timed/read.c new file mode 100644 index 0000000..abe733f --- /dev/null +++ b/doc/examples/timed/read.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include + +#define t(stmt) if (stmt) goto fail + + + +static int +callback(const char *message, void *user_data) +{ + (void) user_data; + + if (message == NULL) + return 1; + + message = strchr(message, ' ') + 1; + if (!strcmp(message, "stop")) + return 0; + printf("%s\n", message); + return 1; +} + + +int +main() +{ + bus_t bus; + struct timespec timeout; + t(bus_open(&bus, "/tmp/example-bus", BUS_RDONLY)); + t(clock_gettime(CLOCK_MONOTONIC, &timeout)); + timeout.tv_sec += 10; + t(bus_read_timed(&bus, callback, NULL, &timeout, CLOCK_MONOTONIC)); + bus_close(&bus); + return 0; + +fail: + perror("poll"); + bus_poll_stop(&bus); + bus_close(&bus); + return 1; +} + -- cgit v1.2.3-70-g09d2