Skip to content

Commit

Permalink
C code to illustrate issue LIBZMQ-256 problem with IDENTITY
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuck Remes committed Sep 17, 2011
1 parent a634396 commit a172a19
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions 256/3-0/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
To avoid loading a system-wide 0mq library, place
the C libraries here. This let's you run your Ruby
code with a 0mq C library build that is different
from the system-wide one. This can be handy for
rolling upgrades or testing.
65 changes: 65 additions & 0 deletions 256/3-0/issue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Custom IDENTITY values are not preserved as
// they are passed from one socket to another.
//
// gcc -lzmq issue.c
// chmod +x a.out
// ./a.out
//
#include <zmq.h>


// several of these are probably unnecessary...
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
#include <inttypes.h>



int main (void)
{
int rc;
void *context = zmq_init (1);
void *sender = zmq_socket (context, ZMQ_REQ);
void *receiver = zmq_socket (context, ZMQ_XREP);

// set a custom IDENTITY
assert (0 == zmq_setsockopt (sender, ZMQ_IDENTITY, "Custom", 6));
assert (0 == zmq_bind (receiver, "tcp://127.0.0.1:7557"));
assert (0 == zmq_connect (sender, "tcp://127.0.0.1:7557"));

zmq_msg_t m1, m2;
assert (0 == zmq_msg_init_data (&m1, "Hello world", 11, NULL, NULL));
assert (11 == zmq_sendmsg(sender, &m1, 0));

assert (0 == zmq_msg_init (&m2) );
assert (-1 != zmq_recvmsg (receiver, &m2, 0));

int label, more;
size_t size = sizeof (label);

do {
assert (0 == zmq_msg_init (&m2));
assert (-1 != zmq_recvmsg (receiver, &m2, 0));
assert (0 == zmq_getsockopt (receiver, ZMQ_RCVLABEL, &label, &size));
assert (0 == zmq_getsockopt (receiver, ZMQ_RCVMORE, &more, &size));

if (label)
printf ("LABEL (should be 'Custom'): %s\n", (char*) zmq_msg_data (&m2));
else
printf ("Contents: %s\n", (char*) zmq_msg_data (&m2));
}
while ( (label == 1) || (more == 1));

zmq_close (sender);
zmq_close (receiver);
zmq_term (context);
return (0);
}

0 comments on commit a172a19

Please sign in to comment.