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/.gitignore | 1 + doc/examples/timed/Makefile | 2 +- doc/examples/timed/README | 5 ++++- doc/examples/timed/cleanup.c | 3 ++- doc/examples/timed/init.c | 3 ++- doc/examples/timed/poll.c | 3 ++- doc/examples/timed/read.c | 45 +++++++++++++++++++++++++++++++++++++++++++ doc/examples/timed/write.c | 3 ++- 8 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 doc/examples/timed/read.c (limited to 'doc/examples/timed') diff --git a/doc/examples/timed/.gitignore b/doc/examples/timed/.gitignore index e5c1856..b1076f0 100644 --- a/doc/examples/timed/.gitignore +++ b/doc/examples/timed/.gitignore @@ -2,4 +2,5 @@ cleanup init write poll +read diff --git a/doc/examples/timed/Makefile b/doc/examples/timed/Makefile index 1b1cbd3..fe1285c 100644 --- a/doc/examples/timed/Makefile +++ b/doc/examples/timed/Makefile @@ -1,4 +1,4 @@ -COMMANDS = init cleanup write poll +COMMANDS = init cleanup write poll read all: ${COMMANDS} diff --git a/doc/examples/timed/README b/doc/examples/timed/README index b0989bf..db34fa0 100644 --- a/doc/examples/timed/README +++ b/doc/examples/timed/README @@ -12,7 +12,10 @@ 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 +Running instances of ./read will read for ten seconds +then time out. + +./poll, ./read, ./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 index 00f07bc..bf54226 100644 --- a/doc/examples/timed/cleanup.c +++ b/doc/examples/timed/cleanup.c @@ -1,7 +1,8 @@ #include #include -int main() +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 index 870e10d..d1325fb 100644 --- a/doc/examples/timed/init.c +++ b/doc/examples/timed/init.c @@ -1,7 +1,8 @@ #include #include -int main() +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 index d8c5aac..d57d4ea 100644 --- a/doc/examples/timed/poll.c +++ b/doc/examples/timed/poll.c @@ -8,7 +8,8 @@ -int main() +int +main() { bus_t bus; const char *message; 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; +} + diff --git a/doc/examples/timed/write.c b/doc/examples/timed/write.c index b6b4c16..250ba14 100644 --- a/doc/examples/timed/write.c +++ b/doc/examples/timed/write.c @@ -11,7 +11,8 @@ static char message[BUS_MEMORY_SIZE]; -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { bus_t bus; if (argc < 2) { -- cgit v1.2.3-70-g09d2