void mdp_client_set_timeout (mdp_client_t *self, int timeout) { assert (self); self->timeout = timeout; zsock_set_rcvtimeo(self->client,self->timeout); }
void mdp_broker_test (bool verbose) { printf (" * mdp_broker: "); if (verbose) printf ("\n"); // @selftest zactor_t *server = zactor_new (mdp_broker, "server"); if (verbose) zstr_send (server, "VERBOSE"); zstr_sendx (server, "BIND", "ipc://@/mdp_broker", NULL); zsock_t *client = zsock_new (ZMQ_DEALER); assert (client); zsock_set_rcvtimeo (client, 2000); zsock_connect (client, "ipc://@/mdp_broker"); // TODO: fill this out mdp_msg_t *request = mdp_msg_new (); mdp_msg_destroy (&request); zsock_destroy (&client); zactor_destroy (&server); // @end printf ("OK\n"); }
int main (int argc, char** argv) { zsock_t *client = zsock_new (ZMQ_DEALER); int i; for (i = 0; i < 255; i++) { int port = 5556; // printf ("Seeking to server at '%s.%d:%d'\n", argv [1], i, port); zsock_connect (client, "tcp://%s.%d:%d", argv [1], i, port); } zsock_set_rcvtimeo (client, 2000); while ( 1 ) { char *reply = zstr_recv (client); if (reply) { puts (reply); free (reply); } else { puts ("-"); break; } } zsock_destroy (&client); return 0; }
void s_mdp_client_connect_to_broker (mdp_client_t *self) { if(self->client) zsock_destroy (&self->client); self->client = zsock_new (ZMQ_DEALER); assert(0==zsock_connect (self->client, "%s", self->broker)); if (self->verbose) zclock_log ("I: connecting to broker at %s...", self->broker); zsock_set_rcvtimeo(self->client,self->timeout); }
void zmailer_server_test (bool verbose) { printf (" * zmailer_server: "); if (verbose) printf ("\n"); // @selftest zactor_t *server = zactor_new (zmailer_server, "server"); if (verbose) zstr_send (server, "VERBOSE"); zstr_sendx (server, "LOAD", ".config/zmailer.cfg", NULL); //zstr_sendx (server, "BIND", "ipc://@/zmailer_server", NULL); zsock_t *client = zsock_new (ZMQ_DEALER); assert (client); zsock_set_rcvtimeo (client, 2000); zsock_connect (client, "ipc://@/zmailer_server"); // TODO: fill this out zmailer_msg_t *request = zmailer_msg_new (); zmailer_msg_set_id (request, ZMAILER_MSG_MAIL); // PLEASE CHANGE zmailer_msg_set_from (request, "YOUROWNEMAIL"); // PLEASE CHANGE zmailer_msg_set_to (request, "YOUROWNEMAIL"); zmailer_msg_set_subject (request, "Test"); zmailer_msg_set_request (request, "This is a test email for you !"); zmailer_msg_send (request, client); sleep (1); zmailer_msg_destroy (&request); zsock_destroy (&client); zactor_destroy (&server); // @end printf ("OK\n"); }
void zgossip_test (bool verbose) { printf (" * zgossip: "); if (verbose) printf ("\n"); // @selftest // Test basic client-to-server operation of the protocol zactor_t *server = zactor_new (zgossip, "server"); assert (server); if (verbose) zstr_send (server, "VERBOSE"); zstr_sendx (server, "BIND", "inproc://zgossip", NULL); zsock_t *client = zsock_new (ZMQ_DEALER); assert (client); zsock_set_rcvtimeo (client, 2000); int rc = zsock_connect (client, "inproc://zgossip"); assert (rc == 0); // Send HELLO, which gets no message zgossip_msg_t *message = zgossip_msg_new (); zgossip_msg_set_id (message, ZGOSSIP_MSG_HELLO); zgossip_msg_send (message, client); // Send PING, expect PONG back zgossip_msg_set_id (message, ZGOSSIP_MSG_PING); zgossip_msg_send (message, client); zgossip_msg_recv (message, client); assert (zgossip_msg_id (message) == ZGOSSIP_MSG_PONG); zgossip_msg_destroy (&message); zactor_destroy (&server); zsock_destroy (&client); // Test peer-to-peer operations zactor_t *base = zactor_new (zgossip, "base"); assert (base); if (verbose) zstr_send (base, "VERBOSE"); // Set a 100msec timeout on clients so we can test expiry zstr_sendx (base, "SET", "server/timeout", "100", NULL); zstr_sendx (base, "BIND", "inproc://base", NULL); zactor_t *alpha = zactor_new (zgossip, "alpha"); assert (alpha); zstr_sendx (alpha, "CONNECT", "inproc://base", NULL); zstr_sendx (alpha, "PUBLISH", "inproc://alpha-1", "service1", NULL); zstr_sendx (alpha, "PUBLISH", "inproc://alpha-2", "service2", NULL); zactor_t *beta = zactor_new (zgossip, "beta"); assert (beta); zstr_sendx (beta, "CONNECT", "inproc://base", NULL); zstr_sendx (beta, "PUBLISH", "inproc://beta-1", "service1", NULL); zstr_sendx (beta, "PUBLISH", "inproc://beta-2", "service2", NULL); // got nothing zclock_sleep (200); zactor_destroy (&base); zactor_destroy (&alpha); zactor_destroy (&beta); // @end printf ("OK\n"); }
void zbeacon_test (bool verbose) { printf (" * zbeacon: "); if (verbose) printf ("\n"); // @selftest // Test 1 - two beacons, one speaking, one listening // Create speaker beacon to broadcast our service zactor_t *speaker = zactor_new (zbeacon, NULL); assert (speaker); if (verbose) zstr_sendx (speaker, "VERBOSE", NULL); zsock_send (speaker, "si", "CONFIGURE", 9999); char *hostname = zstr_recv (speaker); if (!*hostname) { printf ("OK (skipping test, no UDP broadcasting)\n"); zactor_destroy (&speaker); free (hostname); return; } free (hostname); // Create listener beacon on port 9999 to lookup service zactor_t *listener = zactor_new (zbeacon, NULL); assert (listener); if (verbose) zstr_sendx (listener, "VERBOSE", NULL); zsock_send (listener, "si", "CONFIGURE", 9999); hostname = zstr_recv (listener); assert (*hostname); free (hostname); // We will broadcast the magic value 0xCAFE byte announcement [2] = { 0xCA, 0xFE }; zsock_send (speaker, "sbi", "PUBLISH", announcement, 2, 100); // We will listen to anything (empty subscription) zsock_send (listener, "sb", "SUBSCRIBE", "", 0); // Wait for at most 1/2 second if there's no broadcasting zsock_set_rcvtimeo (listener, 500); char *ipaddress = zstr_recv (listener); if (ipaddress) { zframe_t *content = zframe_recv (listener); assert (zframe_size (content) == 2); assert (zframe_data (content) [0] == 0xCA); assert (zframe_data (content) [1] == 0xFE); zframe_destroy (&content); zstr_free (&ipaddress); zstr_sendx (speaker, "SILENCE", NULL); } zactor_destroy (&listener); zactor_destroy (&speaker); // Test subscription filter using a 3-node setup zactor_t *node1 = zactor_new (zbeacon, NULL); assert (node1); zsock_send (node1, "si", "CONFIGURE", 5670); hostname = zstr_recv (node1); assert (*hostname); free (hostname); zactor_t *node2 = zactor_new (zbeacon, NULL); assert (node2); zsock_send (node2, "si", "CONFIGURE", 5670); hostname = zstr_recv (node2); assert (*hostname); free (hostname); zactor_t *node3 = zactor_new (zbeacon, NULL); assert (node3); zsock_send (node3, "si", "CONFIGURE", 5670); hostname = zstr_recv (node3); assert (*hostname); free (hostname); zsock_send (node1, "sbi", "PUBLISH", "NODE/1", 6, 250); zsock_send (node2, "sbi", "PUBLISH", "NODE/2", 6, 250); zsock_send (node3, "sbi", "PUBLISH", "RANDOM", 6, 250); zsock_send (node1, "sb", "SUBSCRIBE", "NODE", 4); // Poll on three API sockets at once zpoller_t *poller = zpoller_new (node1, node2, node3, NULL); assert (poller); int64_t stop_at = zclock_mono () + 1000; while (zclock_mono () < stop_at) { long timeout = (long) (stop_at - zclock_mono ()); if (timeout < 0) timeout = 0; void *which = zpoller_wait (poller, timeout * ZMQ_POLL_MSEC); if (which) { assert (which == node1); char *ipaddress, *received; zstr_recvx (node1, &ipaddress, &received, NULL); assert (streq (received, "NODE/2")); zstr_free (&ipaddress); zstr_free (&received); } } zpoller_destroy (&poller); // Stop listening zstr_sendx (node1, "UNSUBSCRIBE", NULL); // Stop all node broadcasts zstr_sendx (node1, "SILENCE", NULL); zstr_sendx (node2, "SILENCE", NULL); zstr_sendx (node3, "SILENCE", NULL); // Destroy the test nodes zactor_destroy (&node1); zactor_destroy (&node2); zactor_destroy (&node3); // @end printf ("OK\n"); }
void mlm_server_test (bool verbose) { printf (" * mlm_server: "); if (verbose) printf ("\n"); // @selftest zactor_t *server = zactor_new (mlm_server, "mlm_server_test"); if (verbose) zstr_send (server, "VERBOSE"); zstr_sendx (server, "BIND", "tcp://127.0.0.1:*", NULL); zstr_sendx (server, "PORT", NULL); char *command, *port; int rc = zstr_recvx (server, &command, &port, NULL); assert (rc == 2); assert (streq (command, "PORT")); assert (strlen (port) > 0 && strlen (port) < 6); assert (!streq (port, "-1")); zsock_t *reader = zsock_new (ZMQ_DEALER); assert (reader); zsock_connect (reader, "tcp://127.0.0.1:%s", port); zsock_set_rcvtimeo (reader, 500); mlm_proto_t *proto = mlm_proto_new (); // Server insists that connection starts properly mlm_proto_set_id (proto, MLM_PROTO_STREAM_WRITE); mlm_proto_send (proto, reader); zclock_sleep (500); // to calm things down && make memcheck pass. Thanks @malanka mlm_proto_recv (proto, reader); zclock_sleep (500); // detto as above assert (mlm_proto_id (proto) == MLM_PROTO_ERROR); assert (mlm_proto_status_code (proto) == MLM_PROTO_COMMAND_INVALID); // Now do a stream publish-subscribe test zsock_t *writer = zsock_new (ZMQ_DEALER); assert (writer); zsock_connect (writer, "tcp://127.0.0.1:%s", port); zsock_set_rcvtimeo (reader, 500); // Open connections from both reader and writer mlm_proto_set_id (proto, MLM_PROTO_CONNECTION_OPEN); mlm_proto_send (proto, reader); mlm_proto_recv (proto, reader); assert (mlm_proto_id (proto) == MLM_PROTO_OK); mlm_proto_set_id (proto, MLM_PROTO_CONNECTION_OPEN); mlm_proto_send (proto, writer); mlm_proto_recv (proto, writer); assert (mlm_proto_id (proto) == MLM_PROTO_OK); // Prepare to write and read a "weather" stream mlm_proto_set_id (proto, MLM_PROTO_STREAM_WRITE); mlm_proto_set_stream (proto, "weather"); mlm_proto_send (proto, writer); mlm_proto_recv (proto, writer); assert (mlm_proto_id (proto) == MLM_PROTO_OK); mlm_proto_set_id (proto, MLM_PROTO_STREAM_READ); mlm_proto_set_pattern (proto, "temp.*"); mlm_proto_send (proto, reader); mlm_proto_recv (proto, reader); assert (mlm_proto_id (proto) == MLM_PROTO_OK); // Now send some weather data, with null contents mlm_proto_set_id (proto, MLM_PROTO_STREAM_SEND); mlm_proto_set_subject (proto, "temp.moscow"); mlm_proto_send (proto, writer); mlm_proto_set_subject (proto, "rain.moscow"); mlm_proto_send (proto, writer); mlm_proto_set_subject (proto, "temp.chicago"); mlm_proto_send (proto, writer); mlm_proto_set_subject (proto, "rain.chicago"); mlm_proto_send (proto, writer); mlm_proto_set_subject (proto, "temp.london"); mlm_proto_send (proto, writer); mlm_proto_set_subject (proto, "rain.london"); mlm_proto_send (proto, writer); // We should receive exactly three deliveries, in order mlm_proto_recv (proto, reader); assert (mlm_proto_id (proto) == MLM_PROTO_STREAM_DELIVER); assert (streq (mlm_proto_subject (proto), "temp.moscow")); mlm_proto_recv (proto, reader); assert (mlm_proto_id (proto) == MLM_PROTO_STREAM_DELIVER); assert (streq (mlm_proto_subject (proto), "temp.chicago")); mlm_proto_recv (proto, reader); assert (mlm_proto_id (proto) == MLM_PROTO_STREAM_DELIVER); assert (streq (mlm_proto_subject (proto), "temp.london")); mlm_proto_destroy (&proto); // Finished, we can clean up zsock_destroy (&writer); zsock_destroy (&reader); zactor_destroy (&server); zstr_free (&port); zstr_free (&command); // Test Case: // CLIENTLIST command { const char *endpoint = "inproc://mlm_server_clientlist_test"; zactor_t *server = zactor_new (mlm_server, "mlm_server_clientlist_test"); if (verbose) zstr_send (server, "VERBOSE"); zstr_sendx (server, "BIND", endpoint, NULL); mlm_client_t *client_1 = mlm_client_new (); int rv = mlm_client_connect (client_1, endpoint, 1000, "Karol"); assert (rv >= 0); mlm_client_t *client_2 = mlm_client_new (); rv = mlm_client_connect (client_2, endpoint, 1000, "Tomas"); assert (rv >= 0); mlm_client_t *client_3 = mlm_client_new (); rv = mlm_client_connect (client_3, endpoint, 1000, "Alenka"); assert (rv >= 0); zclock_sleep (500); zstr_sendx (server, "CLIENTLIST", NULL); zmsg_t *message = zmsg_recv (server); assert (message); assert (zmsg_size (message) == 4); char *pop = zmsg_popstr (message); assert (streq (pop, "CLIENTLIST")); zstr_free (&pop); zlistx_t *expected_names = zlistx_new (); assert (expected_names); zlistx_set_destructor (expected_names, (czmq_destructor *) zstr_free); zlistx_set_duplicator (expected_names, (czmq_duplicator *) strdup); zlistx_set_comparator (expected_names, (czmq_comparator *) strcmp); zlistx_add_end (expected_names, (void *) "Karol"); zlistx_add_end (expected_names, (void *) "Tomas"); zlistx_add_end (expected_names, (void *) "Alenka"); pop = zmsg_popstr (message); assert (pop); void *handle = zlistx_find (expected_names, pop); assert (handle); rv = zlistx_delete (expected_names, handle); assert (rv == 0); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop); handle = zlistx_find (expected_names, pop); assert (handle); rv = zlistx_delete (expected_names, handle); assert (rv == 0); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop); handle = zlistx_find (expected_names, pop); assert (handle); rv = zlistx_delete (expected_names, handle); assert (rv == 0); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop == NULL); assert (zlistx_size (expected_names) == 0); zmsg_destroy (&message); // remove a client Karol mlm_client_destroy (&client_1); zlistx_add_end (expected_names, (void *) "Tomas"); zlistx_add_end (expected_names, (void *) "Alenka"); zstr_sendx (server, "CLIENTLIST", NULL); zclock_sleep (100); message = zmsg_recv (server); assert (message); assert (zmsg_size (message) == 3); pop = zmsg_popstr (message); assert (streq (pop, "CLIENTLIST")); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop); handle = zlistx_find (expected_names, pop); assert (handle); rv = zlistx_delete (expected_names, handle); assert (rv == 0); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop); handle = zlistx_find (expected_names, pop); assert (handle); rv = zlistx_delete (expected_names, handle); assert (rv == 0); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop == NULL); assert (zlistx_size (expected_names) == 0); zlistx_destroy (&expected_names); zmsg_destroy (&message); mlm_client_destroy (&client_2); mlm_client_destroy (&client_3); zactor_destroy (&server); } // Test Case: // STREAMLIST command { const char *endpoint = "inproc://mlm_server_streamlist_test"; zactor_t *server = zactor_new (mlm_server, "mlm_server_streamlist_test"); if (verbose) zstr_send (server, "VERBOSE"); zstr_sendx (server, "BIND", endpoint, NULL); mlm_client_t *client_1 = mlm_client_new (); int rv = mlm_client_connect (client_1, endpoint, 1000, "Karol"); assert (rv != -1); rv = mlm_client_set_producer (client_1, "STREAM_1"); assert (rv != -1); mlm_client_t *client_2 = mlm_client_new (); rv = mlm_client_connect (client_2, endpoint, 1000, "Tomas"); assert (rv != -1); rv = mlm_client_set_producer (client_2, "STREAM_2"); assert (rv != -1); mlm_client_t *client_3 = mlm_client_new (); rv = mlm_client_connect (client_3, endpoint, 1000, "Alenka"); assert (rv != -1); rv = mlm_client_set_consumer (client_3, "STREAM_2", ".*"); assert (rv != -1); zclock_sleep (100); zlistx_t *expected_streams = zlistx_new (); assert (expected_streams); zlistx_set_destructor (expected_streams, (czmq_destructor *) zstr_free); zlistx_set_duplicator (expected_streams, (czmq_duplicator *) strdup); zlistx_set_comparator (expected_streams, (czmq_comparator *) strcmp); zlistx_add_end (expected_streams, (void *) "STREAM_1"); zlistx_add_end (expected_streams, (void *) "STREAM_2"); zstr_sendx (server, "STREAMLIST", NULL); zmsg_t *message = zmsg_recv (server); assert (message); assert (zmsg_size (message) == 3); char *pop = zmsg_popstr (message); assert (streq (pop, "STREAMLIST")); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop); void *handle = zlistx_find (expected_streams, pop); assert (handle); rv = zlistx_delete (expected_streams, handle); assert (rv == 0); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop); handle = zlistx_find (expected_streams, pop); assert (handle); rv = zlistx_delete (expected_streams, handle); assert (rv == 0); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop == NULL); assert (zlistx_size (expected_streams) == 0); zmsg_destroy (&message); // NOTE: Currently when producer disconnects, malamute does not destroy the stream // Therefore it doesn't make sense to test removal of streams, but addition mlm_client_t *client_4 = mlm_client_new (); rv = mlm_client_connect (client_4, endpoint, 1000, "Michal"); assert (rv >= 0); rv = mlm_client_set_producer (client_4, "New stream"); assert (rv >= 0); zlistx_add_end (expected_streams, (void *) "STREAM_1"); zlistx_add_end (expected_streams, (void *) "STREAM_2"); zlistx_add_end (expected_streams, (void *) "New stream"); zclock_sleep (100); zstr_sendx (server, "STREAMLIST", NULL); zclock_sleep (100); message = zmsg_recv (server); assert (message); assert (zmsg_size (message) == 4); pop = zmsg_popstr (message); assert (streq (pop, "STREAMLIST")); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop); handle = zlistx_find (expected_streams, pop); assert (handle); rv = zlistx_delete (expected_streams, handle); assert (rv == 0); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop); handle = zlistx_find (expected_streams, pop); assert (handle); rv = zlistx_delete (expected_streams, handle); assert (rv == 0); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop); handle = zlistx_find (expected_streams, pop); assert (handle); rv = zlistx_delete (expected_streams, handle); assert (rv == 0); zstr_free (&pop); pop = zmsg_popstr (message); assert (pop == NULL); assert (zlistx_size (expected_streams) == 0); zlistx_destroy (&expected_streams); zmsg_destroy (&message); mlm_client_destroy (&client_1); mlm_client_destroy (&client_2); mlm_client_destroy (&client_3); mlm_client_destroy (&client_4); zactor_destroy (&server); } // Regression Test Case: // Segfault from deregistering zombie connection { const char *endpoint = "inproc://mlm_server_deregister_zombie_connection_test"; zactor_t *server = zactor_new (mlm_server, "mlm_server_deregister_zombie_connection_test"); if (verbose) zstr_send (server, "VERBOSE"); zstr_sendx (server, "BIND", endpoint, NULL); zstr_sendx (server, "SET", "server/timeout", "3000", NULL); // 3 second client timeout zsock_t *reader = zsock_new (ZMQ_DEALER); assert (reader); zsock_connect (reader, "inproc://mlm_server_deregister_zombie_connection_test"); zsock_set_rcvtimeo (reader, 500); mlm_proto_t *proto = mlm_proto_new (); // If the malamute server is restarted and clients have queued // up ping messages, the'll be sent before any // CONNECTION_OPEN. The server eventually tries to deregister // this and (previously) would derefence a null pointer for // the client address. mlm_proto_set_id (proto, MLM_PROTO_CONNECTION_PING); mlm_proto_send (proto, reader); printf("Regression test for segfault due to leftover client messages after restart...\n"); // Give the server more than 3 seconds to time out the client... zclock_sleep (3100); printf("passed\n"); mlm_proto_destroy (&proto); zsock_destroy (&reader); zactor_destroy (&server); } { const char *endpoint = "inproc://mlm_server_disconnect_pending_stream_traffic"; zactor_t *server = zactor_new (mlm_server, "mlm_server_disconnect_pending_stream_traffic"); if (verbose) { zstr_send (server, "VERBOSE"); printf("Regression test for use-after-free with pending stream traffic after disconnect\n"); } zstr_sendx (server, "BIND", endpoint, NULL); mlm_client_t *producer = mlm_client_new (); assert (mlm_client_connect (producer, endpoint, 1000, "producer") >= 0); assert (mlm_client_set_producer (producer, "STREAM_TEST") >= 0); zstr_sendx (server, "SLOW_TEST_MODE", NULL); mlm_client_t *consumer = mlm_client_new (); assert (mlm_client_connect (consumer, endpoint, 1000, "consumer") >= 0); assert (mlm_client_set_consumer (consumer, "STREAM_TEST", ".*") >= 0); zmsg_t *msg = zmsg_new (); zmsg_addstr (msg, "world"); assert (mlm_client_send (producer, "hello", &msg) >= 0); mlm_client_destroy (&consumer); zclock_sleep (2000); mlm_client_destroy (&producer); zactor_destroy (&server); } // @end printf ("OK\n"); }
void mlm_server_test (bool verbose) { printf (" * mlm_server: "); if (verbose) printf ("\n"); // @selftest zactor_t *server = zactor_new (mlm_server, "mlm_server_test"); if (verbose) zstr_send (server, "VERBOSE"); zstr_sendx (server, "BIND", "tcp://127.0.0.1:9999", NULL); zsock_t *reader = zsock_new (ZMQ_DEALER); assert (reader); zsock_connect (reader, "tcp://127.0.0.1:9999"); zsock_set_rcvtimeo (reader, 500); mlm_proto_t *proto = mlm_proto_new (); // Server insists that connection starts properly mlm_proto_set_id (proto, MLM_PROTO_STREAM_WRITE); mlm_proto_send (proto, reader); mlm_proto_recv (proto, reader); assert (mlm_proto_id (proto) == MLM_PROTO_ERROR); assert (mlm_proto_status_code (proto) == MLM_PROTO_COMMAND_INVALID); // Now do a stream publish-subscribe test zsock_t *writer = zsock_new (ZMQ_DEALER); assert (writer); zsock_connect (writer, "tcp://127.0.0.1:9999"); zsock_set_rcvtimeo (reader, 500); // Open connections from both reader and writer mlm_proto_set_id (proto, MLM_PROTO_CONNECTION_OPEN); mlm_proto_send (proto, reader); mlm_proto_recv (proto, reader); assert (mlm_proto_id (proto) == MLM_PROTO_OK); mlm_proto_set_id (proto, MLM_PROTO_CONNECTION_OPEN); mlm_proto_send (proto, writer); mlm_proto_recv (proto, writer); assert (mlm_proto_id (proto) == MLM_PROTO_OK); // Prepare to write and read a "weather" stream mlm_proto_set_id (proto, MLM_PROTO_STREAM_WRITE); mlm_proto_set_stream (proto, "weather"); mlm_proto_send (proto, writer); mlm_proto_recv (proto, writer); assert (mlm_proto_id (proto) == MLM_PROTO_OK); mlm_proto_set_id (proto, MLM_PROTO_STREAM_READ); mlm_proto_set_pattern (proto, "temp.*"); mlm_proto_send (proto, reader); mlm_proto_recv (proto, reader); assert (mlm_proto_id (proto) == MLM_PROTO_OK); // Now send some weather data, with null contents mlm_proto_set_id (proto, MLM_PROTO_STREAM_SEND); mlm_proto_set_subject (proto, "temp.moscow"); mlm_proto_send (proto, writer); mlm_proto_set_subject (proto, "rain.moscow"); mlm_proto_send (proto, writer); mlm_proto_set_subject (proto, "temp.chicago"); mlm_proto_send (proto, writer); mlm_proto_set_subject (proto, "rain.chicago"); mlm_proto_send (proto, writer); mlm_proto_set_subject (proto, "temp.london"); mlm_proto_send (proto, writer); mlm_proto_set_subject (proto, "rain.london"); mlm_proto_send (proto, writer); // We should receive exactly three deliveries, in order mlm_proto_recv (proto, reader); assert (mlm_proto_id (proto) == MLM_PROTO_STREAM_DELIVER); assert (streq (mlm_proto_subject (proto), "temp.moscow")); mlm_proto_recv (proto, reader); assert (mlm_proto_id (proto) == MLM_PROTO_STREAM_DELIVER); assert (streq (mlm_proto_subject (proto), "temp.chicago")); mlm_proto_recv (proto, reader); assert (mlm_proto_id (proto) == MLM_PROTO_STREAM_DELIVER); assert (streq (mlm_proto_subject (proto), "temp.london")); mlm_proto_destroy (&proto); // Finished, we can clean up zsock_destroy (&writer); zsock_destroy (&reader); zactor_destroy (&server); // @end printf ("OK\n"); }
/// // Set socket option `rcvtimeo`. void QZsock::setRcvtimeo (int rcvtimeo) { zsock_set_rcvtimeo (self, rcvtimeo); }
void zproxy_test (bool verbose) { printf (" * zproxy: "); if (verbose) printf ("\n"); // @selftest // Create and configure our proxy zactor_t *proxy = zactor_new (zproxy, NULL); assert (proxy); if (verbose) { zstr_sendx (proxy, "VERBOSE", NULL); zsock_wait (proxy); } zstr_sendx (proxy, "FRONTEND", "PULL", "inproc://frontend", NULL); zsock_wait (proxy); zstr_sendx (proxy, "BACKEND", "PUSH", "inproc://backend", NULL); zsock_wait (proxy); // Connect application sockets to proxy zsock_t *faucet = zsock_new_push (">inproc://frontend"); assert (faucet); zsock_t *sink = zsock_new_pull (">inproc://backend"); assert (sink); // Send some messages and check they arrived char *hello, *world; zstr_sendx (faucet, "Hello", "World", NULL); zstr_recvx (sink, &hello, &world, NULL); assert (streq (hello, "Hello")); assert (streq (world, "World")); zstr_free (&hello); zstr_free (&world); // Test pause/resume functionality zstr_sendx (proxy, "PAUSE", NULL); zsock_wait (proxy); zstr_sendx (faucet, "Hello", "World", NULL); zsock_set_rcvtimeo (sink, 100); zstr_recvx (sink, &hello, &world, NULL); assert (!hello && !world); zstr_sendx (proxy, "RESUME", NULL); zsock_wait (proxy); zstr_recvx (sink, &hello, &world, NULL); assert (streq (hello, "Hello")); assert (streq (world, "World")); zstr_free (&hello); zstr_free (&world); // Test capture functionality zsock_t *capture = zsock_new_pull ("inproc://capture"); assert (capture); // Switch on capturing, check that it works zstr_sendx (proxy, "CAPTURE", "inproc://capture", NULL); zsock_wait (proxy); zstr_sendx (faucet, "Hello", "World", NULL); zstr_recvx (sink, &hello, &world, NULL); assert (streq (hello, "Hello")); assert (streq (world, "World")); zstr_free (&hello); zstr_free (&world); zstr_recvx (capture, &hello, &world, NULL); assert (streq (hello, "Hello")); assert (streq (world, "World")); zstr_free (&hello); zstr_free (&world); zsock_destroy (&faucet); zsock_destroy (&sink); zsock_destroy (&capture); zactor_destroy (&proxy); // @end printf ("OK\n"); }
void zgossip_test (bool verbose) { printf (" * zgossip: "); if (verbose) printf ("\n"); // @selftest // Test basic client-to-server operation of the protocol zactor_t *server = zactor_new (zgossip, "server"); assert (server); if (verbose) zstr_send (server, "VERBOSE"); zstr_sendx (server, "BIND", "inproc://zgossip", NULL); zsock_t *client = zsock_new (ZMQ_DEALER); assert (client); zsock_set_rcvtimeo (client, 2000); int rc = zsock_connect (client, "inproc://zgossip"); assert (rc == 0); // Send HELLO, which gets no message zgossip_msg_t *message = zgossip_msg_new (); zgossip_msg_set_id (message, ZGOSSIP_MSG_HELLO); zgossip_msg_send (message, client); // Send PING, expect PONG back zgossip_msg_set_id (message, ZGOSSIP_MSG_PING); zgossip_msg_send (message, client); zgossip_msg_recv (message, client); assert (zgossip_msg_id (message) == ZGOSSIP_MSG_PONG); zgossip_msg_destroy (&message); zactor_destroy (&server); zsock_destroy (&client); // Test peer-to-peer operations zactor_t *base = zactor_new (zgossip, "base"); assert (base); if (verbose) zstr_send (base, "VERBOSE"); // Set a 100msec timeout on clients so we can test expiry zstr_sendx (base, "SET", "server/timeout", "100", NULL); zstr_sendx (base, "BIND", "inproc://base", NULL); zactor_t *alpha = zactor_new (zgossip, "alpha"); assert (alpha); zstr_sendx (alpha, "CONNECT", "inproc://base", NULL); zstr_sendx (alpha, "PUBLISH", "inproc://alpha-1", "service1", NULL); zstr_sendx (alpha, "PUBLISH", "inproc://alpha-2", "service2", NULL); zactor_t *beta = zactor_new (zgossip, "beta"); assert (beta); zstr_sendx (beta, "CONNECT", "inproc://base", NULL); zstr_sendx (beta, "PUBLISH", "inproc://beta-1", "service1", NULL); zstr_sendx (beta, "PUBLISH", "inproc://beta-2", "service2", NULL); // got nothing zclock_sleep (200); zstr_send (alpha, "STATUS"); char *command, *status, *key, *value; zstr_recvx (alpha, &command, &key, &value, NULL); assert (streq (command, "DELIVER")); assert (streq (key, "inproc://alpha-1")); assert (streq (value, "service1")); zstr_free (&command); zstr_free (&key); zstr_free (&value); zstr_recvx (alpha, &command, &key, &value, NULL); assert (streq (command, "DELIVER")); assert (streq (key, "inproc://alpha-2")); assert (streq (value, "service2")); zstr_free (&command); zstr_free (&key); zstr_free (&value); zstr_recvx (alpha, &command, &key, &value, NULL); assert (streq (command, "DELIVER")); assert (streq (key, "inproc://beta-1")); assert (streq (value, "service1")); zstr_free (&command); zstr_free (&key); zstr_free (&value); zstr_recvx (alpha, &command, &key, &value, NULL); assert (streq (command, "DELIVER")); assert (streq (key, "inproc://beta-2")); assert (streq (value, "service2")); zstr_free (&command); zstr_free (&key); zstr_free (&value); zstr_recvx (alpha, &command, &status, NULL); assert (streq (command, "STATUS")); assert (atoi (status) == 4); zstr_free (&command); zstr_free (&status); zactor_destroy (&base); zactor_destroy (&alpha); zactor_destroy (&beta); #ifdef CZMQ_BUILD_DRAFT_API // DRAFT-API: Security // curve if (zsys_has_curve()) { if (verbose) printf("testing CURVE support"); zclock_sleep (2000); zactor_t *auth = zactor_new(zauth, NULL); assert (auth); if (verbose) { zstr_sendx (auth, "VERBOSE", NULL); zsock_wait (auth); } zstr_sendx(auth,"ALLOW","127.0.0.1",NULL); zsock_wait(auth); zstr_sendx (auth, "CURVE", CURVE_ALLOW_ANY, NULL); zsock_wait (auth); server = zactor_new (zgossip, "server"); if (verbose) zstr_send (server, "VERBOSE"); assert (server); zcert_t *client1_cert = zcert_new (); zcert_t *server_cert = zcert_new (); zstr_sendx (server, "SET PUBLICKEY", zcert_public_txt (server_cert), NULL); zstr_sendx (server, "SET SECRETKEY", zcert_secret_txt (server_cert), NULL); zstr_sendx (server, "ZAP DOMAIN", "TEST", NULL); zstr_sendx (server, "BIND", "tcp://127.0.0.1:*", NULL); zstr_sendx (server, "PORT", NULL); zstr_recvx (server, &command, &value, NULL); assert (streq (command, "PORT")); int port = atoi (value); zstr_free (&command); zstr_free (&value); char endpoint [32]; sprintf (endpoint, "tcp://127.0.0.1:%d", port); zactor_t *client1 = zactor_new (zgossip, "client"); if (verbose) zstr_send (client1, "VERBOSE"); assert (client1); zstr_sendx (client1, "SET PUBLICKEY", zcert_public_txt (client1_cert), NULL); zstr_sendx (client1, "SET SECRETKEY", zcert_secret_txt (client1_cert), NULL); zstr_sendx (client1, "ZAP DOMAIN", "TEST", NULL); const char *public_txt = zcert_public_txt (server_cert); zstr_sendx (client1, "CONNECT", endpoint, public_txt, NULL); zstr_sendx (client1, "PUBLISH", "tcp://127.0.0.1:9001", "service1", NULL); zclock_sleep (500); zstr_send (server, "STATUS"); zclock_sleep (500); zstr_recvx (server, &command, &key, &value, NULL); assert (streq (command, "DELIVER")); assert (streq (value, "service1")); zstr_free (&command); zstr_free (&key); zstr_free (&value); zstr_sendx (client1, "$TERM", NULL); zstr_sendx (server, "$TERM", NULL); zclock_sleep(500); zcert_destroy (&client1_cert); zcert_destroy (&server_cert); zactor_destroy (&client1); zactor_destroy (&server); zactor_destroy (&auth); } #endif #if defined (__WINDOWS__) zsys_shutdown(); #endif // @end printf ("OK\n"); }
void citizen_server_test (bool verbose) { printf (" * citizen_server: "); if (verbose) printf ("\n"); // @selftest zactor_t *server = zactor_new (citizen_server, "server"); if (verbose) zstr_send (server, "VERBOSE"); zstr_sendx (server, "BIND", "ipc://@/citizen_server", NULL); // Connect a client zsock_t *client = zsock_new (ZMQ_DEALER); assert (client); zsock_set_rcvtimeo (client, 2000); zsock_connect (client, "ipc://@/citizen_server"); // Message holders citizen_msg_t *request, *reply; // Check initial server status request = citizen_msg_new (CITIZEN_MSG_HOWAREYOU); citizen_msg_send (&request, client); reply = citizen_msg_recv (client); assert (reply); assert (citizen_msg_id (reply) == CITIZEN_MSG_STATUS); assert (citizen_msg_follower_count (reply) == 0); citizen_msg_destroy (&reply); // Solicit a follow request request = citizen_msg_new (CITIZEN_MSG_IMLOST); citizen_msg_send (&request, client); reply = citizen_msg_recv (client); assert (reply); assert (citizen_msg_id (reply) == CITIZEN_MSG_FOLLOWME); citizen_msg_destroy (&reply); // Solicit a follow request request = citizen_msg_new (CITIZEN_MSG_LEADME); citizen_msg_send (&request, client); // Check that status now shows one follower request = citizen_msg_new (CITIZEN_MSG_HOWAREYOU); citizen_msg_send (&request, client); reply = citizen_msg_recv (client); assert (reply); assert (citizen_msg_id (reply) == CITIZEN_MSG_STATUS); assert (citizen_msg_follower_count (reply) == 1); citizen_msg_destroy (&reply); // Force an expiration of the client with very short timeout, // a message to reapply the timeout time, and a too-long sleep; // then reset timeout to default time of 60 seconds. zstr_sendx (server, "SET", "server/timeout", "10", NULL); citizen_msg_send_howareyou (client); reply = citizen_msg_recv (client); citizen_msg_destroy (&reply); zclock_sleep(20); // client connection should time out zstr_sendx (server, "SET", "server/timeout", "60000", NULL); // Check that status now shows zero followers request = citizen_msg_new (CITIZEN_MSG_HOWAREYOU); citizen_msg_send (&request, client); reply = citizen_msg_recv (client); assert (reply); assert (citizen_msg_id (reply) == CITIZEN_MSG_STATUS); assert (citizen_msg_follower_count (reply) == 0); citizen_msg_destroy (&reply); // Cleanup zsock_destroy (&client); zactor_destroy (&server); // @end printf ("OK\n"); }
void zyre_test (bool verbose) { printf (" * zyre: "); if (verbose) printf ("\n"); // @selftest // We'll use inproc gossip discovery so that this works without networking uint64_t version = zyre_version (); assert ((version / 10000) % 100 == ZYRE_VERSION_MAJOR); assert ((version / 100) % 100 == ZYRE_VERSION_MINOR); assert (version % 100 == ZYRE_VERSION_PATCH); // Create two nodes zyre_t *node1 = zyre_new ("node1"); assert (node1); assert (streq (zyre_name (node1), "node1")); zyre_set_header (node1, "X-HELLO", "World"); if (verbose) zyre_set_verbose (node1); // Set inproc endpoint for this node int rc = zyre_set_endpoint (node1, "inproc://zyre-node1"); assert (rc == 0); // Set up gossip network for this node zyre_gossip_bind (node1, "inproc://gossip-hub"); rc = zyre_start (node1); assert (rc == 0); zyre_t *node2 = zyre_new ("node2"); assert (node2); assert (streq (zyre_name (node2), "node2")); if (verbose) zyre_set_verbose (node2); // Set inproc endpoint for this node // First, try to use existing name, it'll fail rc = zyre_set_endpoint (node2, "inproc://zyre-node1"); assert (rc == -1); // Now use available name and confirm that it succeeds rc = zyre_set_endpoint (node2, "inproc://zyre-node2"); assert (rc == 0); // Set up gossip network for this node zyre_gossip_connect (node2, "inproc://gossip-hub"); rc = zyre_start (node2); assert (rc == 0); assert (strneq (zyre_uuid (node1), zyre_uuid (node2))); zyre_join (node1, "GLOBAL"); zyre_join (node2, "GLOBAL"); // Give time for them to interconnect zclock_sleep (250); if (verbose) zyre_dump (node1); zlist_t *peers = zyre_peers (node1); assert (peers); assert (zlist_size (peers) == 1); zlist_destroy (&peers); zyre_join (node1, "node1 group of one"); zyre_join (node2, "node2 group of one"); // Give them time to join their groups zclock_sleep (250); zlist_t *own_groups = zyre_own_groups (node1); assert (own_groups); assert (zlist_size (own_groups) == 2); zlist_destroy (&own_groups); zlist_t *peer_groups = zyre_peer_groups (node1); assert (peer_groups); assert (zlist_size (peer_groups) == 2); zlist_destroy (&peer_groups); char *value = zyre_peer_header_value (node2, zyre_uuid (node1), "X-HELLO"); assert (streq (value, "World")); zstr_free (&value); // One node shouts to GLOBAL zyre_shouts (node1, "GLOBAL", "Hello, World"); // Second node should receive ENTER, JOIN, and SHOUT zmsg_t *msg = zyre_recv (node2); assert (msg); char *command = zmsg_popstr (msg); assert (streq (command, "ENTER")); zstr_free (&command); assert (zmsg_size (msg) == 4); char *peerid = zmsg_popstr (msg); char *name = zmsg_popstr (msg); assert (streq (name, "node1")); zstr_free (&name); zframe_t *headers_packed = zmsg_pop (msg); char *address = zmsg_popstr (msg); char *endpoint = zyre_peer_address (node2, peerid); assert (streq (address, endpoint)); zstr_free (&peerid); zstr_free (&endpoint); zstr_free (&address); assert (headers_packed); zhash_t *headers = zhash_unpack (headers_packed); assert (headers); zframe_destroy (&headers_packed); assert (streq ((char *) zhash_lookup (headers, "X-HELLO"), "World")); zhash_destroy (&headers); zmsg_destroy (&msg); msg = zyre_recv (node2); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "JOIN")); zstr_free (&command); assert (zmsg_size (msg) == 3); zmsg_destroy (&msg); msg = zyre_recv (node2); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "JOIN")); zstr_free (&command); assert (zmsg_size (msg) == 3); zmsg_destroy (&msg); msg = zyre_recv (node2); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "SHOUT")); zstr_free (&command); zmsg_destroy (&msg); zyre_stop (node2); msg = zyre_recv (node2); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "STOP")); zstr_free (&command); zmsg_destroy (&msg); zyre_stop (node1); zyre_destroy (&node1); zyre_destroy (&node2); printf ("OK\n"); #ifdef ZYRE_BUILD_DRAFT_API if (zsys_has_curve()){ printf (" * zyre-curve: "); if (verbose) printf ("\n"); if (verbose) zsys_debug("----------------TESTING CURVE --------------"); zactor_t *speaker = zactor_new (zbeacon, NULL); assert (speaker); if (verbose) zstr_sendx (speaker, "VERBOSE", NULL); // ensuring we have a broadcast address zsock_send (speaker, "si", "CONFIGURE", 9999); char *hostname = zstr_recv (speaker); if (!*hostname) { printf ("OK (skipping test, no UDP broadcasting)\n"); zactor_destroy (&speaker); freen (hostname); return; } freen (hostname); zactor_destroy (&speaker); // zap setup zactor_t *auth = zactor_new(zauth, NULL); assert (auth); if (verbose) { zstr_sendx(auth, "VERBOSE", NULL); zsock_wait(auth); } zstr_sendx (auth, "CURVE", CURVE_ALLOW_ANY, NULL); zsock_wait (auth); zyre_t *node3 = zyre_new ("node3"); zyre_t *node4 = zyre_new ("node4"); assert (node3); assert (node4); zyre_set_verbose (node3); zyre_set_verbose (node4); zyre_set_zap_domain(node3, "TEST"); zyre_set_zap_domain(node4, "TEST"); zsock_set_rcvtimeo(node3->inbox, 10000); zsock_set_rcvtimeo(node4->inbox, 10000); zcert_t *node3_cert = zcert_new (); zcert_t *node4_cert = zcert_new (); assert (node3_cert); assert (node4_cert); zyre_set_zcert(node3, node3_cert); zyre_set_zcert(node4, node4_cert); zyre_set_header(node3, "X-PUBLICKEY", "%s", zcert_public_txt(node3_cert)); zyre_set_header(node4, "X-PUBLICKEY", "%s", zcert_public_txt(node4_cert)); // test beacon if (verbose) zsys_debug ("----------------TESTING BEACON----------------"); rc = zyre_start(node3); assert (rc == 0); rc = zyre_start(node4); assert (rc == 0); zyre_join (node3, "GLOBAL"); zyre_join (node4, "GLOBAL"); zclock_sleep (1500); if (verbose) { zyre_dump (node3); zyre_dump (node4); } zyre_shouts (node3, "GLOBAL", "Hello, World"); // Second node should receive ENTER, JOIN, and SHOUT msg = zyre_recv (node4); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "ENTER")); zstr_free (&command); char *peerid = zmsg_popstr (msg); assert (peerid); name = zmsg_popstr (msg); assert (streq (name, "node3")); zmsg_destroy (&msg); msg = zyre_recv (node4); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "JOIN")); zstr_free (&command); zmsg_destroy(&msg); msg = zyre_recv (node4); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "SHOUT")); zstr_free (&command); zmsg_destroy(&msg); zyre_leave(node3, "GLOBAL"); zyre_leave(node4, "GLOBAL"); zstr_free (&name); zstr_free (&peerid); zstr_free (&command); zyre_stop (node3); zyre_stop (node4); // give things a chance to settle... zclock_sleep (250); zyre_destroy(&node3); zyre_destroy(&node4); zcert_destroy(&node3_cert); zcert_destroy(&node4_cert); // test gossip if (verbose) zsys_debug ("----------------TESTING GOSSIP----------------"); zyre_t *node5 = zyre_new ("node5"); zyre_t *node6 = zyre_new ("node6"); assert (node5); assert (node6); if (verbose) { zyre_set_verbose (node5); zyre_set_verbose (node6); } // if it takes more than 10s, something probably went terribly wrong zsock_set_rcvtimeo(node5->inbox, 10000); zsock_set_rcvtimeo(node6->inbox, 10000); zcert_t *node5_cert = zcert_new (); zcert_t *node6_cert = zcert_new (); assert (node5_cert); assert (node6_cert); zyre_set_zcert(node5, node5_cert); zyre_set_zcert(node6, node6_cert); zyre_set_header(node5, "X-PUBLICKEY", "%s", zcert_public_txt(node5_cert)); zyre_set_header(node6, "X-PUBLICKEY", "%s", zcert_public_txt(node6_cert)); const char *gossip_cert = zcert_public_txt (node5_cert); // TODO- need to add zyre_gossip_port functions to get port from gossip bind(?) zyre_gossip_bind(node5, "tcp://127.0.0.1:9001"); zyre_gossip_connect_curve(node6, gossip_cert, "tcp://127.0.0.1:9001"); zyre_start(node5); zsock_wait(node5); zyre_start(node6); zsock_wait(node6); zyre_join (node5, "GLOBAL"); zyre_join (node6, "GLOBAL"); // give things a chance to settle... zclock_sleep (1500); if (verbose) { zyre_dump (node5); zyre_dump (node6); } zyre_shouts (node5, "GLOBAL", "Hello, World"); // Second node should receive ENTER, JOIN, and SHOUT msg = zyre_recv (node6); assert (msg); command = zmsg_popstr (msg); zsys_info(command); assert (streq (command, "ENTER")); zstr_free (&command); peerid = zmsg_popstr (msg); assert (peerid); name = zmsg_popstr (msg); zmsg_destroy (&msg); assert (streq (name, "node5")); zstr_free (&name); zyre_leave(node5, "GLOBAL"); zyre_leave(node6, "GLOBAL"); zyre_stop (node5); zyre_stop (node6); // give things a chance to settle... zclock_sleep (250); zstr_free (&peerid); zcert_destroy (&node5_cert); zcert_destroy (&node6_cert); zyre_destroy(&node5); zyre_destroy(&node6); zactor_destroy(&auth); printf ("OK\n"); } #endif }
JNIEXPORT void JNICALL Java_org_zeromq_czmq_Zsock__1_1setRcvtimeo (JNIEnv *env, jclass c, jlong self, jint rcvtimeo) { zsock_set_rcvtimeo ((zsock_t *) (intptr_t) self, (int) rcvtimeo); }