aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/timed
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2015-05-17 14:40:46 +0200
committerMattias Andrée <maandree@operamail.com>2015-05-17 14:40:46 +0200
commit0d44f16f6738c0776edf60b84f49f0c6f875a8fe (patch)
treeacc2a01cea1f7c7458c8ccc6ec4b37fa5ccb5d0f /doc/examples/timed
parentupdate documentation for bus_poll (diff)
downloadbus-0d44f16f6738c0776edf60b84f49f0c6f875a8fe.tar.gz
bus-0d44f16f6738c0776edf60b84f49f0c6f875a8fe.tar.bz2
bus-0d44f16f6738c0776edf60b84f49f0c6f875a8fe.tar.xz
style + add timed read example
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'doc/examples/timed')
-rw-r--r--doc/examples/timed/.gitignore1
-rw-r--r--doc/examples/timed/Makefile2
-rw-r--r--doc/examples/timed/README5
-rw-r--r--doc/examples/timed/cleanup.c3
-rw-r--r--doc/examples/timed/init.c3
-rw-r--r--doc/examples/timed/poll.c3
-rw-r--r--doc/examples/timed/read.c45
-rw-r--r--doc/examples/timed/write.c3
8 files changed, 59 insertions, 6 deletions
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 <bus.h>
#include <stdio.h>
-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 <bus.h>
#include <stdio.h>
-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 <bus.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+#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) {