Skip to content

Commit

Permalink
Added new zbeacon class
Browse files Browse the repository at this point in the history
  • Loading branch information
hintjens committed Feb 22, 2013
1 parent eb5fde2 commit 4c4bc96
Show file tree
Hide file tree
Showing 24 changed files with 1,162 additions and 237 deletions.
19 changes: 17 additions & 2 deletions configure.ac
Expand Up @@ -90,8 +90,9 @@ AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <zmq.h>],
[AC_MSG_RESULT([yes])], [AC_MSG_RESULT([yes])],
[AC_MSG_ERROR([no. Please specify libzmq installation prefix using --with-libzmq])]) [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_mingw32="no"
czmq_on_android="no"


# Host speciffic checks # Host speciffic checks
AC_CANONICAL_HOST AC_CANONICAL_HOST
Expand Down Expand Up @@ -126,6 +127,13 @@ case "${host_os}" in
# Define on Linux to enable all library features # Define on Linux to enable all library features
CPPFLAGS="-D_GNU_SOURCE -DLINUX $CPPFLAGS" CPPFLAGS="-D_GNU_SOURCE -DLINUX $CPPFLAGS"
AC_DEFINE(CZMQ_HAVE_LINUX, 1, [Have Linux OS]) 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*) *solaris*)
# Define on Solaris to enable all library features # Define on Solaris to enable all library features
Expand Down Expand Up @@ -184,6 +192,12 @@ esac
AC_HEADER_STDC AC_HEADER_STDC
AC_CHECK_HEADERS(errno.h arpa/inet.h netinet/tcp.h netinet/in.h stddef.h \ 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) 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. # Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL AC_HEADER_STDBOOL
Expand All @@ -204,12 +218,13 @@ if test "x$GCC" = "xyes"; then
fi fi


AM_CONDITIONAL(ON_MINGW, test "x$czmq_on_mingw32" = "xyes") 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(INSTALL_MAN, test "x$czmq_install_man" = "xyes")
AM_CONDITIONAL(BUILD_DOC, test "x$czmq_build_doc" = "xyes") AM_CONDITIONAL(BUILD_DOC, test "x$czmq_build_doc" = "xyes")


# Checks for library functions. # Checks for library functions.
AC_TYPE_SIGNAL 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) AC_OUTPUT(Makefile src/Makefile doc/Makefile src/libczmq.pc)


1 change: 1 addition & 0 deletions doc/Makefile.am
@@ -1,6 +1,7 @@
MAN1 = MAN1 =
MAN3 = MAN3 =
MAN7 = czmq.7 \ MAN7 = czmq.7 \
zbeacon.7 \
zclock.7 \ zclock.7 \
zctx.7 \ zctx.7 \
zfile.7 \ zfile.7 \
Expand Down
135 changes: 135 additions & 0 deletions doc/zbeacon.txt
@@ -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]
9 changes: 0 additions & 9 deletions doc/zclock.txt
Expand Up @@ -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 to slow down threads for testing, and calculate timers for polling. Wraps
the non-portable system calls in a simple portable API. 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 The Win32 Sleep() call defaults to 16ms resolution unless the system timer
resolution is increased with a call to timeBeginPeriod() permitting 1ms resolution is increased with a call to timeBeginPeriod() permitting 1ms
granularity. granularity.
Expand Down
4 changes: 1 addition & 3 deletions doc/zctx.txt
Expand Up @@ -52,7 +52,7 @@ DESCRIPTION
The zctx class wraps 0MQ contexts. It manages open sockets in the context The zctx class wraps 0MQ contexts. It manages open sockets in the context
and automatically closes these before terminating the context. It provides and automatically closes these before terminating the context. It provides
a simple way to set the linger timeout on sockets, and configure contexts 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. process.


The zctx class has these main features: The zctx class has these main features:
Expand Down Expand Up @@ -103,8 +103,6 @@ EXAMPLE
zsocket_connect (s5, "tcp://127.0.0.1:5555"); zsocket_connect (s5, "tcp://127.0.0.1:5555");
zsocket_connect (s6, "tcp://127.0.0.1:5555"); zsocket_connect (s6, "tcp://127.0.0.1:5555");
assert (zctx_underlying (ctx)); assert (zctx_underlying (ctx));

// Everything should be cleanly closed now
zctx_destroy (&ctx); zctx_destroy (&ctx);
---- ----


Expand Down
32 changes: 22 additions & 10 deletions doc/zfile.txt
Expand Up @@ -8,21 +8,33 @@ zfile - helper functions for working with files.
SYNOPSIS 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 // Delete file. Does not complain if the file is absent
CZMQ_EXPORT int CZMQ_EXPORT int
zfile_delete (const char *filename); zfile_delete (const char *filename);


// Make directory (maximum one level depending on OS) // Check if file is 'stable'
CZMQ_EXPORT int CZMQ_EXPORT bool
zfile_mkdir (const char *dirname); zfile_stable (const char *filename);


// Return 1 if file exists, else zero // Create a file path if it doesn't exit
CZMQ_EXPORT int CZMQ_EXPORT int
zfile_exists (const char *filename); zfile_mkdir (const char *pathname);


// Return size of file, or -1 if not found // Remove a file path if empty
CZMQ_EXPORT ssize_t CZMQ_EXPORT int
zfile_size (const char *filename); zfile_rmdir (const char *pathname);


// Self test of this class // Self test of this class
int int
Expand All @@ -32,7 +44,7 @@ int
DESCRIPTION DESCRIPTION
----------- -----------


The zfile class provides methods to work with files. The zfile class provides methods to work with files and directories.




EXAMPLE EXAMPLE
Expand All @@ -43,7 +55,7 @@ EXAMPLE
assert (rc == -1); assert (rc == -1);


rc = zfile_exists ("nosuchfile"); rc = zfile_exists ("nosuchfile");
assert (rc == FALSE); assert (rc != true);


rc = (int) zfile_size ("nosuchfile"); rc = (int) zfile_size ("nosuchfile");
assert (rc == -1); assert (rc == -1);
Expand Down

0 comments on commit 4c4bc96

Please sign in to comment.