Permalink
Browse files

Added new zbeacon class

  • Loading branch information...
1 parent eb5fde2 commit 4c4bc96da659f400d7adf14af29ea7543b795013 @hintjens hintjens committed Feb 22, 2013
Showing with 1,162 additions and 237 deletions.
  1. +17 −2 configure.ac
  2. +1 −0 doc/Makefile.am
  3. +135 −0 doc/zbeacon.txt
  4. +0 −9 doc/zclock.txt
  5. +1 −3 doc/zctx.txt
  6. +22 −10 doc/zfile.txt
  7. +47 −14 doc/zhash.txt
  8. +1 −1 doc/zloop.txt
  9. +1 −1 doc/zsocket.txt
  10. +7 −0 doc/zsockopt.txt
  11. +5 −13 doc/zstr.txt
  12. +0 −20 doc/zsys.txt
  13. +6 −4 doc/zthread.txt
  14. +4 −3 include/czmq.h
  15. +87 −0 include/zbeacon.h
  16. +2 −2 include/zhash.h
  17. +2 −2 include/zlist.h
  18. +2 −2 include/zmutex.h
  19. +2 −2 include/zsys.h
  20. +2 −0 src/Makefile.am
  21. +1 −0 src/czmq_selftest.c
  22. +755 −0 src/zbeacon.c
  23. +2 −0 src/zsockopt.c
  24. +60 −149 win32/czmq.vcproj
View
@@ -90,8 +90,9 @@ AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <zmq.h>],
[AC_MSG_RESULT([yes])],
[AC_MSG_ERROR([no. Please specify libzmq installation prefix using --with-libzmq])])
-#Whether we are on mingw or not.
+# Platform specific checks
czmq_on_mingw32="no"
+czmq_on_android="no"
# Host speciffic checks
AC_CANONICAL_HOST
@@ -126,6 +127,13 @@ case "${host_os}" in
# Define on Linux to enable all library features
CPPFLAGS="-D_GNU_SOURCE -DLINUX $CPPFLAGS"
AC_DEFINE(CZMQ_HAVE_LINUX, 1, [Have Linux OS])
+
+ case "${host_os}" in
+ *android*)
+ AC_DEFINE(CZMQ_HAVE_ANDROID, 1, [Have Android OS])
+ czmq_on_android="yes"
+ ;;
+ esac
;;
*solaris*)
# Define on Solaris to enable all library features
@@ -184,6 +192,12 @@ esac
AC_HEADER_STDC
AC_CHECK_HEADERS(errno.h arpa/inet.h netinet/tcp.h netinet/in.h stddef.h \
stdlib.h string.h sys/socket.h sys/time.h unistd.h limits.h)
+AC_CHECK_HEADERS([net/if.h net/if_media.h linux/wireless.h], [], [],
+[
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
+])
# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
@@ -204,12 +218,13 @@ if test "x$GCC" = "xyes"; then
fi
AM_CONDITIONAL(ON_MINGW, test "x$czmq_on_mingw32" = "xyes")
+AM_CONDITIONAL(ON_ANDROID, test "x$czmq_on_android" = "xyes")
AM_CONDITIONAL(INSTALL_MAN, test "x$czmq_install_man" = "xyes")
AM_CONDITIONAL(BUILD_DOC, test "x$czmq_build_doc" = "xyes")
# Checks for library functions.
AC_TYPE_SIGNAL
-AC_CHECK_FUNCS(perror gettimeofday memset)
+AC_CHECK_FUNCS(perror gettimeofday memset getifaddrs freeifaddrs)
AC_OUTPUT(Makefile src/Makefile doc/Makefile src/libczmq.pc)
View
@@ -1,6 +1,7 @@
MAN1 =
MAN3 =
MAN7 = czmq.7 \
+ zbeacon.7 \
zclock.7 \
zctx.7 \
zfile.7 \
View
@@ -0,0 +1,135 @@
+zbeacon(7)
+==========
+
+NAME
+----
+zbeacon - LAN service announcement and discovery
+
+SYNOPSIS
+--------
+----
+// Create a new beacon
+CZMQ_EXPORT zbeacon_t *
+ zbeacon_new (int port_nbr);
+
+// Destroy a beacon
+CZMQ_EXPORT void
+ zbeacon_destroy (zbeacon_t **self_p);
+
+// Set broadcast interval in milliseconds (default is 1000 msec)
+CZMQ_EXPORT void
+ zbeacon_set_interval (zbeacon_t *self, int interval);
+
+// Filter out any beacon that looks exactly like ours
+CZMQ_EXPORT void
+ zbeacon_noecho (zbeacon_t *self);
+
+// Start broadcasting beacon to peers at the specified interval
+CZMQ_EXPORT void
+ zbeacon_publish (zbeacon_t *self, byte *transmit, size_t size);
+
+// Stop broadcasting beacons
+CZMQ_EXPORT void
+ zbeacon_silence (zbeacon_t *self);
+
+// Start listening to other peers; zero-sized filter means get everything
+CZMQ_EXPORT void
+ zbeacon_subscribe (zbeacon_t *self, byte *filter, size_t size);
+
+// Stop listening to other peers
+CZMQ_EXPORT void
+ zbeacon_unsubscribe (zbeacon_t *self);
+
+// Get beacon pipe, for polling or receiving messages off
+CZMQ_EXPORT void *
+ zbeacon_pipe (zbeacon_t *self);
+
+// Return our own IP address as printable string
+CZMQ_EXPORT char *
+ zbeacon_hostname (zbeacon_t *self);
+
+// Self test of this class
+void
+ zbeacon_test (bool verbose);
+----
+
+DESCRIPTION
+-----------
+
+The zbeacon module implements a peer-to-peer discovery service for local
+networks. A beacon can broadcast and/or capture service announcements
+using UDP messages on the local area network. This implementation uses
+IPv4 UDP broadcasts. You can define the format of your outgoing beacons,
+and set a filter that validates incoming beacons. Beacons are sent and
+received asynchronously in the background. The zbeacon API provides a
+incoming beacons on a ZeroMQ socket (the pipe) that you can configure,
+poll on, and receive messages on. Incoming beacons are always delivered
+as two frames: the ipaddress of the sender (a string), and the beacon
+data itself (binary, as published).
+
+
+EXAMPLE
+-------
+.From zbeacon_test method
+----
+ zbeacon_t *node1 = zbeacon_new (5670);
+ zbeacon_t *node2 = zbeacon_new (5670);
+ zbeacon_t *node3 = zbeacon_new (5670);
+
+ assert (*zbeacon_hostname (node1));
+ assert (*zbeacon_hostname (node2));
+ assert (*zbeacon_hostname (node3));
+
+ zbeacon_set_interval (node1, 250);
+ zbeacon_set_interval (node2, 250);
+ zbeacon_set_interval (node3, 250);
+ zbeacon_noecho (node1);
+ zbeacon_publish (node1, (byte *) "NODE/1", 6);
+ zbeacon_publish (node2, (byte *) "NODE/2", 6);
+ zbeacon_publish (node3, (byte *) "GARBAGE", 7);
+ zbeacon_subscribe (node1, (byte *) "NODE", 4);
+
+ // Poll on API pipe and on UDP socket
+ zmq_pollitem_t pollitems [] = {
+ { zbeacon_pipe (node1), 0, ZMQ_POLLIN, 0 },
+ { zbeacon_pipe (node2), 0, ZMQ_POLLIN, 0 },
+ { zbeacon_pipe (node3), 0, ZMQ_POLLIN, 0 }
+ };
+ long stop_at = zclock_time () + 1000;
+ while (zclock_time () < stop_at) {
+ long timeout = (long) (stop_at - zclock_time ());
+ if (timeout < 0)
+ timeout = 0;
+ if (zmq_poll (pollitems, 3, timeout * ZMQ_POLL_MSEC) == -1)
+ break; // Interrupted
+
+ // We cannot get messages on nodes 2 and 3
+ assert ((pollitems [1].revents & ZMQ_POLLIN) == 0);
+ assert ((pollitems [2].revents & ZMQ_POLLIN) == 0);
+
+ // If we get a message on node 1, it must be NODE/2
+ if (pollitems [0].revents & ZMQ_POLLIN) {
+ char *ipaddress = zstr_recv (zbeacon_pipe (node1));
+ char *beacon = zstr_recv (zbeacon_pipe (node1));
+ assert (streq (beacon, "NODE/2"));
+ free (ipaddress);
+ free (beacon);
+ }
+ }
+ // Stop listening
+ zbeacon_unsubscribe (node1);
+
+ // Stop all node broadcasts
+ zbeacon_silence (node1);
+ zbeacon_silence (node2);
+ zbeacon_silence (node3);
+
+ // Destroy the test nodes
+ zbeacon_destroy (&node1);
+ zbeacon_destroy (&node2);
+ zbeacon_destroy (&node3);
+----
+
+SEE ALSO
+--------
+linkczmq:czmq[7]
View
@@ -33,15 +33,6 @@ The zclock class provides essential sleep and system time functions, used
to slow down threads for testing, and calculate timers for polling. Wraps
the non-portable system calls in a simple portable API.
-This class contains some small surprises. Most amazing, win32 did an API
-better than POSIX. The win32 Sleep() call is not only a neat 1-liner, it
-also sleeps for milliseconds, whereas the POSIX call asks us to think in
-terms of nanoseconds, which is insane. I've decided every single man page
-for this library will say "insane" at least once. Anyhow, milliseconds
-are a concept we can deal with. Seconds are too fat, nanoseconds too
-tiny, but milliseconds are just right for slices of time we want to work
-with at the 0MQ scale. zclock doesn't give you objects to work with, we
-like the czmq class model but we're not insane. There, got it in again.
The Win32 Sleep() call defaults to 16ms resolution unless the system timer
resolution is increased with a call to timeBeginPeriod() permitting 1ms
granularity.
View
@@ -52,7 +52,7 @@ DESCRIPTION
The zctx class wraps 0MQ contexts. It manages open sockets in the context
and automatically closes these before terminating the context. It provides
a simple way to set the linger timeout on sockets, and configure contexts
-for number of I/O threads. Sets-up signal (interrrupt) handling for the
+for number of I/O threads. Sets-up signal (interrupt) handling for the
process.
The zctx class has these main features:
@@ -103,8 +103,6 @@ EXAMPLE
zsocket_connect (s5, "tcp://127.0.0.1:5555");
zsocket_connect (s6, "tcp://127.0.0.1:5555");
assert (zctx_underlying (ctx));
-
- // Everything should be cleanly closed now
zctx_destroy (&ctx);
----
View
@@ -8,21 +8,33 @@ zfile - helper functions for working with files.
SYNOPSIS
--------
----
+// Return 1 if file exists, else zero
+CZMQ_EXPORT bool
+ zfile_exists (const char *filename);
+
+// Return size of file, or -1 if not found
+CZMQ_EXPORT ssize_t
+ zfile_size (const char *filename);
+
+// Return file mode
+CZMQ_EXPORT mode_t
+ zfile_mode (const char *filename);
+
// Delete file. Does not complain if the file is absent
CZMQ_EXPORT int
zfile_delete (const char *filename);
-// Make directory (maximum one level depending on OS)
-CZMQ_EXPORT int
- zfile_mkdir (const char *dirname);
+// Check if file is 'stable'
+CZMQ_EXPORT bool
+ zfile_stable (const char *filename);
-// Return 1 if file exists, else zero
+// Create a file path if it doesn't exit
CZMQ_EXPORT int
- zfile_exists (const char *filename);
+ zfile_mkdir (const char *pathname);
-// Return size of file, or -1 if not found
-CZMQ_EXPORT ssize_t
- zfile_size (const char *filename);
+// Remove a file path if empty
+CZMQ_EXPORT int
+ zfile_rmdir (const char *pathname);
// Self test of this class
int
@@ -32,7 +44,7 @@ int
DESCRIPTION
-----------
-The zfile class provides methods to work with files.
+The zfile class provides methods to work with files and directories.
EXAMPLE
@@ -43,7 +55,7 @@ EXAMPLE
assert (rc == -1);
rc = zfile_exists ("nosuchfile");
- assert (rc == FALSE);
+ assert (rc != true);
rc = (int) zfile_size ("nosuchfile");
assert (rc == -1);
Oops, something went wrong.

0 comments on commit 4c4bc96

Please sign in to comment.