|
#include "polling_utils.h" |
|
|
|
static int count = 0; |
|
void on_socket_polled(uv_poll_t* poller, int status, int events); |
|
void send_server_message(void* socket); |
|
|
|
void on_socket_polled(uv_poll_t* poller, int status, int events) |
|
{ |
|
assert(poller); |
|
count++; |
|
|
|
send_server_message(poller->data); |
|
if (count >= MAX_MSG_COUNT) |
|
{ |
|
echo("client", "Killed after %d messages.\n", MAX_MSG_COUNT); |
|
uv_poll_stop(poller); |
|
} |
|
|
|
} |
|
|
|
void send_server_message(void* socket) |
|
{ |
|
assert(socket); |
|
|
|
char buffer [BUFFER_SIZE]; |
|
int notTheSame = 0; |
|
zmq_send (socket, CLIENT_MSG, BUFFER_SIZE, 0); |
|
while (notTheSame < 0) |
|
{ |
|
zmq_recv (socket, buffer, BUFFER_SIZE, 0); |
|
notTheSame = strcmp(buffer, SERVER_MSG); |
|
} |
|
|
|
echo("client", "Received '%s' from server; sent %s.\n", buffer, CLIENT_MSG); |
|
sleep(SLEEP_INTERVAL / 3); |
|
} |
|
|
|
int main(void) |
|
{ |
|
// djj |
|
int zmq_fd = 0; |
|
size_t zmq_fd_len = 0; |
|
void* context = NULL; |
|
void* socket = NULL; |
|
uv_loop_t* loop = NULL; |
|
uv_poll_t poller; |
|
|
|
loop = uv_default_loop(); |
|
assert(loop != NULL); |
|
|
|
context = zmq_ctx_new(); |
|
assert(context); |
|
socket = zmq_socket(context, ZMQ_REQ); |
|
assert(socket); |
|
zmq_connect(socket, CONNECTION_STR); |
|
assert(zmq_getsockopt(socket, ZMQ_FD, &zmq_fd, &zmq_fd_len)); |
|
|
|
uv_poll_init(loop, &poller, zmq_fd); |
|
uv_poll_start(&poller, UV_READABLE, &on_socket_polled); |
|
uv_run(loop, UV_RUN_DEFAULT); |
|
zmq_close(socket); |
|
zmq_ctx_destroy(context); |
|
return 0; |
|
} |