Last active
December 16, 2015 09:59
-
-
Save deepcube/5417288 to your computer and use it in GitHub Desktop.
Test whether ZMQ_ROUTER sockets grow as peers connect and disconnect. It looks like they don't. To run, download all three files into the same directory and run: sh 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 <errno.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.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. | |
* | |
* comment out zmq_close() and zmq_ctx_destory() to simulate dirty disconnect | |
* must then uncomment zmq_recvmsg() otherwise we die before the message makes it to the wire | |
*/ | |
int main(int argc, char **argv) | |
{ | |
void *ctx, *soc; | |
zmq_msg_t msg; | |
prog_name = argv[0]; | |
zcheck_err(EXIT_FAILURE, 2 <= argc , "Supply endpoint then ID as arguments"); | |
zcheck_err(EXIT_FAILURE, ctx = zmq_ctx_new() , "Error creating context" ); | |
zcheck_err(EXIT_FAILURE, soc = zmq_socket(ctx, ZMQ_DEALER), "Error creating socket" ); | |
if (argc == 3) | |
zcheck_err(EXIT_FAILURE, 0 == zmq_setsockopt(soc, ZMQ_IDENTITY, argv[2], strlen(argv[2])), "Error identifying as %s", argv[2]); | |
zcheck_err(EXIT_FAILURE, 0 == zmq_connect(soc, argv[1]), "Error connecting to endpoint %s", argv[1]); | |
zcheck_err(EXIT_FAILURE, 0 == zmq_msg_init(&msg) , "Error initialising message" ); | |
zcheck_err(EXIT_FAILURE, -1 != zmq_sendmsg(soc, &msg, 0), "Error sending message" ); | |
zcheck_err(EXIT_FAILURE, 0 == zmq_close(soc) , "Error closing socket" ); | |
zcheck_err(EXIT_FAILURE, 0 == zmq_ctx_destroy(ctx) , "Error destroying context" ); | |
// zcheck_err(EXIT_FAILURE, -1 != zmq_recvmsg(soc, &msg, 0), "Error receiving ack" ); | |
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 -Wall -Wextra -Werror -o router_echo router_echo.c || die "Could not compile router_echo.c" | |
gcc -lzmq -Wall -Wextra -Werror -o connect_die connect_die.c || die "Could not compile connect_die.c" | |
./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)) | |
./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 <errno.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; | |
/* | |
* Create ZMQ_ROUTER socket, bind, echo messages. | |
*/ | |
int main(int argc, char **argv) | |
{ | |
int con; | |
void *ctx, *soc; | |
zmq_msg_t msg; | |
prog_name = argv[0]; | |
zcheck_err(EXIT_FAILURE, 2 == argc , "Supply endpoint as only argument" ); | |
zcheck_err(EXIT_FAILURE, ctx = zmq_ctx_new() , "Error creating context" ); | |
zcheck_err(EXIT_FAILURE, soc = zmq_socket(ctx, ZMQ_ROUTER), "Error creating socket" ); | |
zcheck_err(EXIT_FAILURE, 0 == zmq_bind(soc, argv[1]) , "Error binding to endpoint %s", argv[1]); | |
zcheck_err(EXIT_FAILURE, 0 == zmq_msg_init(&msg) , "Error initialise mesage" ); | |
setbuf(stdout, NULL); | |
for (con = 0;;) { | |
int more; | |
size_t size = sizeof(more); | |
zcheck_err(EXIT_FAILURE, -1 != zmq_recvmsg(soc, &msg, 0) , "Error receiving message" ); | |
zcheck_err(EXIT_FAILURE, 0 == zmq_getsockopt(soc, ZMQ_RCVMORE, &more, &size), "Error checking for ZMQ_RCVMORE"); | |
zcheck_err(EXIT_FAILURE, -1 != zmq_sendmsg(soc, &msg, more ? ZMQ_SNDMORE : 0), "Error echoing message" ); | |
if (!more) | |
printf("Received: %d ", ++con); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment