Created
April 19, 2013 20:32
-
-
Save deepcube/5423034 to your computer and use it in GitHub Desktop.
Rewrite of https://gist.github.com/deepcube/5417288 in czmq. To run download all files in a directory and run: sh czmq_disconnect_test.sh
This file contains hidden or 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
#include <czmq.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <zmq.h> | |
#define zcheck_err(eval, condition, fmt, args...) \ | |
((void)({ \ | |
errno = 0; \ | |
if (!(condition)) { \ | |
fprintf(stderr, "%s: " fmt "%s%s\n", prog_name, ##args, errno ? ": " : "", errno ? zmq_strerror(errno) : ""); \ | |
exit(eval); \ | |
} \ | |
})) | |
char *prog_name; | |
/* | |
* Connect to a socket, send a single empty message, exit. | |
*/ | |
int main(int argc, char **argv) | |
{ | |
zctx_t *ctx; | |
void *soc; | |
zmsg_t *msg; | |
prog_name = argv[0]; | |
zcheck_err(EXIT_FAILURE, argc <= 3 , "Supply endpoint then ID as arguments"); | |
zcheck_err(EXIT_FAILURE, ctx = zctx_new() , "Error creating context" ); | |
zcheck_err(EXIT_FAILURE, soc = zsocket_new(ctx, ZMQ_DEALER), "Error creating socket" ); | |
if (argc == 3) | |
zsocket_set_identity(soc, argv[2]); | |
zcheck_err(EXIT_FAILURE, 0 <= zsocket_connect(soc, argv[1]), "Error connecting to %s", argv[1]); | |
zcheck_err(EXIT_FAILURE, msg = zmsg_new() , "Error creating message" ); | |
zcheck_err(EXIT_FAILURE, 0 == zmsg_pushmem(msg, "", 0) , "Error adding empty frame" ); | |
zcheck_err(EXIT_FAILURE, 0 == zmsg_send(&msg, soc) , "Error sending message" ); | |
zcheck_err(EXIT_FAILURE, msg = zmsg_recv(soc) , "Error receiving message" ); | |
// zctx_destroy(&ctx); | |
return 0; | |
} |
This file contains hidden or 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
#!/bin/sh | |
# | |
# Create a ZMQ_ROUTER, bind to $BIND, echo messages | |
# Output PID of router so you may monitor the process | |
# Continually connect to $CONNECT, send a message, then die | |
# Print relevant memory information for router (recommend a wide terminal) | |
# | |
# Should work with any sh, went through and got rid of bashisms | |
trap die INT TERM | |
die() { | |
[ -n "$router_pid" ] && kill $router_pid | |
printf "$0: $@\n" 1>&2 | |
exit | |
} | |
BIND="tcp://lo:12345" | |
CONNECT="tcp://localhost:12345" | |
gcc -lzmq -lczmq -Wall -Werror -o czmq_router_echo czmq_router_echo.c || die "Error compiling czmq_router_echo.c" | |
gcc -lzmq -lczmq -Wall -Werror -o czmq_connect_die czmq_connect_die.c || die "Error compiling czmq_connect_die.c" | |
./czmq_router_echo "$BIND" & | |
router_pid=$! | |
ps $router_pid > /dev/null 2>&1 || die "Error starting router_echo" | |
echo "Router pid is $router_pid. Press [Enter] to continue." | |
read unused | |
unset i | |
while true; do | |
i=$((i + 1)) | |
./czmq_connect_die "$CONNECT" client_$i || die "Error starting a client" | |
egrep "Vm" /proc/$router_pid/status | sed ':l; N; $!bl; s/[\n\t ]\+/ /g;' | |
done | |
wait |
This file contains hidden or 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
#include <czmq.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <zmq.h> | |
#define zcheck_err(eval, condition, fmt, args...) \ | |
((void)({ \ | |
errno = 0; \ | |
if (!(condition)) { \ | |
fprintf(stderr, "%s: " fmt "%s%s\n", prog_name, ##args, errno ? ": " : "", errno ? zmq_strerror(errno) : ""); \ | |
exit(eval); \ | |
} \ | |
})) | |
char *prog_name; | |
int echo(zloop_t *loop, zmq_pollitem_t *item, void *arg) | |
{ | |
zmsg_t *msg; | |
static int con = 0; | |
zcheck_err(EXIT_FAILURE, msg = zmsg_recv(item->socket) , "Error receiving message"); | |
zcheck_err(EXIT_FAILURE, 0 == zmsg_send(&msg, item->socket), "Error sending message" ); | |
printf("Received: %d ", ++con); | |
return 0; | |
} | |
/* | |
* Create ZMQ_ROUTER socket, bind, echo messages. | |
*/ | |
int main(int argc, char **argv) | |
{ | |
zctx_t *ctx; | |
void *soc; | |
zloop_t *loop; | |
zmq_pollitem_t poller; | |
prog_name = argv[0]; | |
zcheck_err(EXIT_FAILURE, argc == 2 , "Supply endpoint as only argument" ); | |
zcheck_err(EXIT_FAILURE, ctx = zctx_new() , "Error creating context" ); | |
zcheck_err(EXIT_FAILURE, soc = zsocket_new(ctx, ZMQ_ROUTER), "Error creating socket" ); | |
zcheck_err(EXIT_FAILURE, 0 <= zsocket_bind(soc, argv[1]) , "Error binding socket to %s", argv[1]); | |
zcheck_err(EXIT_FAILURE, loop = zloop_new() , "Error creating zloop" ); | |
poller = (zmq_pollitem_t){ .socket = soc, .fd = 0, .events = ZMQ_POLLIN, .revents = 0 }; | |
zcheck_err(EXIT_FAILURE, 0 == zloop_poller(loop, &poller, echo, NULL), "Error registering poller"); | |
setbuf(stdout, NULL); | |
zloop_start(loop); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment