Skip to content

Instantly share code, notes, and snippets.

@hintjens
Created September 7, 2010 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hintjens/567967 to your computer and use it in GitHub Desktop.
Save hintjens/567967 to your computer and use it in GitHub Desktop.
//
// Provoke assertion:
// Assertion failed: !engine (session.cpp:287)
// Caused by two clients with same identity
//
#include "zhelpers.h"
void *host_routine (void *context) {
// Socket to talk to dispatcher
void *host = zmq_socket (context, ZMQ_PULL);
assert (zmq_bind (host, "tcp://*:5555") == 0);
while (1) {
char *string = s_recv (host);
free (string);
}
return (NULL);
}
int main () {
void *context = zmq_init (1);
pthread_t thread;
pthread_create (&thread, NULL, host_routine, context);
char *string;
void *cli1 = zmq_socket (context, ZMQ_PUSH);
assert (zmq_setsockopt (cli1, ZMQ_IDENTITY, "Hello", 5) == 0);
assert (zmq_connect (cli1, "tcp://localhost:5555") == 0);
s_send (cli1, "request");
puts ("cli1 ok");
void *cli2 = zmq_socket (context, ZMQ_PUSH);
assert (zmq_setsockopt (cli2, ZMQ_IDENTITY, "Hello", 5) == 0);
assert (zmq_connect (cli2, "tcp://localhost:5555") == 0);
s_send (cli2, "request");
puts ("cli2 ok");
sleep (1);
return 0;
}
/* =========================================================================
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