Created
September 7, 2010 06:42
-
-
Save hintjens/567969 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Demonstrate identities as used by the request-reply | |
// pattern. Run this program by itself. | |
// (provokes assertion failure in xrep.cpp) | |
// | |
#include "zhelpers.h" | |
// Receives all message parts from socket, prints neatly | |
// | |
static void | |
s_dump_input (void *socket) | |
{ | |
puts ("----------------------------------------"); | |
while (1) { | |
// Process all parts of the message | |
zmq_msg_t message; | |
zmq_msg_init (&message); | |
zmq_recv (socket, &message, 0); | |
// Dump the message as text or binary | |
char *data = zmq_msg_data (&message); | |
int size = zmq_msg_size (&message); | |
int is_text = 1; | |
int char_nbr; | |
for (char_nbr = 0; char_nbr < size; char_nbr++) | |
if (data [char_nbr] < 32 || data [char_nbr] > 127) | |
is_text = 0; | |
printf ("[%03d] ", size); | |
for (char_nbr = 0; char_nbr < size; char_nbr++) { | |
if (is_text) | |
printf ("%c", data [char_nbr]); | |
else | |
printf ("%02X", (unsigned char) data [char_nbr]); | |
} | |
printf ("\n"); | |
int64_t more; // Multipart detection | |
size_t more_size = sizeof (more); | |
zmq_getsockopt (socket, ZMQ_RCVMORE, &more, &more_size); | |
zmq_msg_close (&message); | |
if (!more) | |
break; // Last message part | |
} | |
} | |
int main () { | |
void *context = zmq_init (1); | |
// First allow 0MQ to set the identity | |
void *sink = zmq_socket (context, ZMQ_XREP); | |
zmq_bind (sink, "inproc://example"); | |
void *automatic = zmq_socket (context, ZMQ_REQ); | |
zmq_setsockopt (automatic, ZMQ_IDENTITY, "Hello", 5); | |
zmq_connect (automatic, "inproc://example"); | |
s_send (automatic, "XREP socket uses a generated UUID"); | |
s_dump_input (sink); | |
void *manual = zmq_socket (context, ZMQ_REQ); | |
zmq_setsockopt (manual, ZMQ_IDENTITY, "Hello", 5); | |
zmq_connect (manual, "inproc://example"); | |
s_send (manual, "XREP socket uses REQ's socket identity"); | |
s_dump_input (sink); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ========================================================================= | |
zhelpers.h - ZeroMQ helpers for example applications | |
Copyright (c) 1991-2010 iMatix Corporation and contributors | |
This is free software; you can redistribute it and/or modify it under | |
the terms of the Lesser GNU General Public License as published by | |
the Free Software Foundation; either version 3 of the License, or | |
(at your option) any later version. | |
This software is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
Lesser GNU General Public License for more details. | |
You should have received a copy of the Lesser GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
========================================================================= | |
*/ | |
#ifndef __ZHELPERS_H_INCLUDED__ | |
#define __ZHELPERS_H_INCLUDED__ | |
// Include a bunch of headers that we will need in the examples | |
#include <zmq.h> | |
#include <pthread.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/time.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include <assert.h> | |
// Receive 0MQ string from socket and convert into C string | |
static char * | |
s_recv (void *socket) { | |
zmq_msg_t message; | |
zmq_msg_init (&message); | |
assert (zmq_recv (socket, &message, 0) == 0); | |
int size = zmq_msg_size (&message); | |
char *string = malloc (size + 1); | |
memcpy (string, zmq_msg_data (&message), size); | |
zmq_msg_close (&message); | |
string [size] = 0; | |
return (string); | |
} | |
// Convert C string to 0MQ string and send to socket | |
static int | |
s_send (void *socket, char *string) { | |
int rc; | |
zmq_msg_t message; | |
zmq_msg_init_size (&message, strlen (string)); | |
memcpy (zmq_msg_data (&message), string, strlen (string)); | |
rc = zmq_send (socket, &message, 0); | |
assert (!rc); | |
zmq_msg_close (&message); | |
return (rc); | |
} | |
// Sends string as 0MQ string, as multipart non-terminal | |
static int | |
s_sendmore (void *socket, char *string) { | |
int rc; | |
zmq_msg_t message; | |
zmq_msg_init_data (&message, string, strlen (string), NULL, NULL); | |
rc = zmq_send (socket, &message, ZMQ_SNDMORE); | |
zmq_msg_close (&message); | |
assert (!rc); | |
return (rc); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment