static void store_client_subscription (server_t *self, client_t *client) { // Find mount point with longest match to subscription char *path = fmq_msg_path (client->request); mount_t *check = (mount_t *) zlist_first (self->mounts); mount_t *mount = check; while (check) { // If check->alias is prefix of path and alias is // longer than current mount then we have a new mount if (strncmp (path, check->alias, strlen (check->alias)) == 0 && strlen (check->alias) > strlen (mount->alias)) mount = check; check = (mount_t *) zlist_next (self->mounts); } mount_sub_store (mount, client, client->request); }
static void mount_sub_store (mount_t *self, client_t *client, fmq_msg_t *request) { assert (self); assert (self->subs); // Store subscription along with any previous ones // Coalesce subscriptions that are on same path char *path = fmq_msg_path (request); sub_t *sub = (sub_t *) zlist_first (self->subs); while (sub) { if (client == sub->client) { // If old subscription is superset/same as new, ignore new if (strncmp (path, sub->path, strlen (sub->path)) == 0) return; else // If new subscription is superset of old one, remove old if (strncmp (sub->path, path, strlen (path)) == 0) { zlist_remove (self->subs, sub); sub_destroy (&sub); sub = (sub_t *) zlist_first (self->subs); } else sub = (sub_t *) zlist_next (self->subs); } else sub = (sub_t *) zlist_next (self->subs); } // New subscription for this client, append to our list sub = sub_new (client, path, fmq_msg_cache (request)); zlist_append (self->subs, sub); // If client requested resync, send full mount contents now if (fmq_msg_options_number (client->request, "RESYNC", 0) == 1) { zlist_t *patches = zdir_resync (self->dir, self->alias); while (zlist_size (patches)) { zdir_patch_t *patch = (zdir_patch_t *) zlist_pop (patches); sub_patch_add (sub, patch); zdir_patch_destroy (&patch); } zlist_destroy (&patches); } }
void fmq_msg_test (bool verbose) { printf (" * fmq_msg:"); if (verbose) printf ("\n"); // @selftest // Simple create/destroy test fmq_msg_t *self = fmq_msg_new (); assert (self); fmq_msg_destroy (&self); // Create pair of sockets we can send through // We must bind before connect if we wish to remain compatible with ZeroMQ < v4 zsock_t *output = zsock_new (ZMQ_DEALER); assert (output); int rc = zsock_bind (output, "inproc://selftest-fmq_msg"); assert (rc == 0); zsock_t *input = zsock_new (ZMQ_ROUTER); assert (input); rc = zsock_connect (input, "inproc://selftest-fmq_msg"); assert (rc == 0); // Encode/send/decode and verify each message type int instance; self = fmq_msg_new (); fmq_msg_set_id (self, FMQ_MSG_OHAI); // Send twice fmq_msg_send (self, output); fmq_msg_send (self, output); for (instance = 0; instance < 2; instance++) { fmq_msg_recv (self, input); assert (fmq_msg_routing_id (self)); } fmq_msg_set_id (self, FMQ_MSG_OHAI_OK); // Send twice fmq_msg_send (self, output); fmq_msg_send (self, output); for (instance = 0; instance < 2; instance++) { fmq_msg_recv (self, input); assert (fmq_msg_routing_id (self)); } fmq_msg_set_id (self, FMQ_MSG_ICANHAZ); fmq_msg_set_path (self, "Life is short but Now lasts for ever"); zhash_t *icanhaz_options = zhash_new (); zhash_insert (icanhaz_options, "Name", "Brutus"); fmq_msg_set_options (self, &icanhaz_options); zhash_t *icanhaz_cache = zhash_new (); zhash_insert (icanhaz_cache, "Name", "Brutus"); fmq_msg_set_cache (self, &icanhaz_cache); // Send twice fmq_msg_send (self, output); fmq_msg_send (self, output); for (instance = 0; instance < 2; instance++) { fmq_msg_recv (self, input); assert (fmq_msg_routing_id (self)); assert (streq (fmq_msg_path (self), "Life is short but Now lasts for ever")); zhash_t *options = fmq_msg_get_options (self); assert (zhash_size (options) == 2); assert (streq ((char *) zhash_first (options), "Brutus")); assert (streq ((char *) zhash_cursor (options), "Name")); zhash_destroy (&options); if (instance == 1) zhash_destroy (&icanhaz_options); zhash_t *cache = fmq_msg_get_cache (self); assert (zhash_size (cache) == 2); assert (streq ((char *) zhash_first (cache), "Brutus")); assert (streq ((char *) zhash_cursor (cache), "Name")); zhash_destroy (&cache); if (instance == 1) zhash_destroy (&icanhaz_cache); } fmq_msg_set_id (self, FMQ_MSG_ICANHAZ_OK); // Send twice fmq_msg_send (self, output); fmq_msg_send (self, output); for (instance = 0; instance < 2; instance++) { fmq_msg_recv (self, input); assert (fmq_msg_routing_id (self)); } fmq_msg_set_id (self, FMQ_MSG_NOM); fmq_msg_set_credit (self, 123); fmq_msg_set_sequence (self, 123); // Send twice fmq_msg_send (self, output); fmq_msg_send (self, output); for (instance = 0; instance < 2; instance++) { fmq_msg_recv (self, input); assert (fmq_msg_routing_id (self)); assert (fmq_msg_credit (self) == 123); assert (fmq_msg_sequence (self) == 123); } fmq_msg_set_id (self, FMQ_MSG_CHEEZBURGER); fmq_msg_set_sequence (self, 123); fmq_msg_set_operation (self, 123); fmq_msg_set_filename (self, "Life is short but Now lasts for ever"); fmq_msg_set_offset (self, 123); fmq_msg_set_eof (self, 123); zhash_t *cheezburger_headers = zhash_new (); zhash_insert (cheezburger_headers, "Name", "Brutus"); fmq_msg_set_headers (self, &cheezburger_headers); zchunk_t *cheezburger_chunk = zchunk_new ("Captcha Diem", 12); fmq_msg_set_chunk (self, &cheezburger_chunk); // Send twice fmq_msg_send (self, output); fmq_msg_send (self, output); for (instance = 0; instance < 2; instance++) { fmq_msg_recv (self, input); assert (fmq_msg_routing_id (self)); assert (fmq_msg_sequence (self) == 123); assert (fmq_msg_operation (self) == 123); assert (streq (fmq_msg_filename (self), "Life is short but Now lasts for ever")); assert (fmq_msg_offset (self) == 123); assert (fmq_msg_eof (self) == 123); zhash_t *headers = fmq_msg_get_headers (self); assert (zhash_size (headers) == 2); assert (streq ((char *) zhash_first (headers), "Brutus")); assert (streq ((char *) zhash_cursor (headers), "Name")); zhash_destroy (&headers); if (instance == 1) zhash_destroy (&cheezburger_headers); assert (memcmp (zchunk_data (fmq_msg_chunk (self)), "Captcha Diem", 12) == 0); if (instance == 1) zchunk_destroy (&cheezburger_chunk); } fmq_msg_set_id (self, FMQ_MSG_HUGZ); // Send twice fmq_msg_send (self, output); fmq_msg_send (self, output); for (instance = 0; instance < 2; instance++) { fmq_msg_recv (self, input); assert (fmq_msg_routing_id (self)); } fmq_msg_set_id (self, FMQ_MSG_HUGZ_OK); // Send twice fmq_msg_send (self, output); fmq_msg_send (self, output); for (instance = 0; instance < 2; instance++) { fmq_msg_recv (self, input); assert (fmq_msg_routing_id (self)); } fmq_msg_set_id (self, FMQ_MSG_KTHXBAI); // Send twice fmq_msg_send (self, output); fmq_msg_send (self, output); for (instance = 0; instance < 2; instance++) { fmq_msg_recv (self, input); assert (fmq_msg_routing_id (self)); } fmq_msg_set_id (self, FMQ_MSG_SRSLY); fmq_msg_set_reason (self, "Life is short but Now lasts for ever"); // Send twice fmq_msg_send (self, output); fmq_msg_send (self, output); for (instance = 0; instance < 2; instance++) { fmq_msg_recv (self, input); assert (fmq_msg_routing_id (self)); assert (streq (fmq_msg_reason (self), "Life is short but Now lasts for ever")); } fmq_msg_set_id (self, FMQ_MSG_RTFM); fmq_msg_set_reason (self, "Life is short but Now lasts for ever"); // Send twice fmq_msg_send (self, output); fmq_msg_send (self, output); for (instance = 0; instance < 2; instance++) { fmq_msg_recv (self, input); assert (fmq_msg_routing_id (self)); assert (streq (fmq_msg_reason (self), "Life is short but Now lasts for ever")); } fmq_msg_destroy (&self); zsock_destroy (&input); zsock_destroy (&output); // @end printf ("OK\n"); }