static zmsg_t * server_method (server_t *self, const char *method, zmsg_t *msg) { if (streq (method, "CLIENTLIST")) { zmsg_t *reply = zmsg_new (); zmsg_addstr (reply, "CLIENTLIST"); void *item = zhashx_first (self->clients); while (item) { zmsg_addstr (reply, (const char *) zhashx_cursor (self->clients)); item = (void *) zhashx_next (self->clients); } return reply; } if (streq (method, "STREAMLIST")) { zmsg_t *reply = zmsg_new(); zmsg_addstr (reply, "STREAMLIST"); stream_t *stream = (stream_t *) zhashx_first (self->streams); while (stream) { zmsg_addstr (reply, stream->name); stream = (stream_t *) zhashx_next (self->streams); } return reply; } if (streq (method, "SLOW_TEST_MODE")) { // selftest: Tell all stream engines to enable SLOW_TEST_MODE stream_t *stream = (stream_t *) zhashx_first (self->streams); while (stream) { zsock_send (stream->actor, "s", method); stream = (stream_t *) zhashx_next (self->streams); } return NULL; } return NULL; }
static void server_connect (server_t *self, const char *endpoint) { zsock_t *remote = zsock_new (ZMQ_DEALER); assert (remote); // No recovery if exhausted // Never block on sending; we use an infinite HWM and buffer as many // messages as needed in outgoing pipes. Note that the maximum number // is the overall tuple set size. zsock_set_unbounded (remote); if (zsock_connect (remote, "%s", endpoint)) { zsys_warning ("bad zgossip endpoint '%s'", endpoint); zsock_destroy (&remote); return; } // Send HELLO and then PUBLISH for each tuple we have zgossip_msg_t *gossip = zgossip_msg_new (); zgossip_msg_set_id (gossip, ZGOSSIP_MSG_HELLO); zgossip_msg_send (gossip, remote); tuple_t *tuple = (tuple_t *) zhashx_first (self->tuples); while (tuple) { zgossip_msg_set_id (gossip, ZGOSSIP_MSG_PUBLISH); zgossip_msg_set_key (gossip, tuple->key); zgossip_msg_set_value (gossip, tuple->value); zgossip_msg_send (gossip, remote); tuple = (tuple_t *) zhashx_next (self->tuples); } // Now monitor this remote for incoming messages zgossip_msg_destroy (&gossip); engine_handle_socket (self, remote, remote_handler); zlistx_add_end (self->remotes, remote); }
static void deregister_the_client (client_t *self) { // If the client never sent CONNECTION_OPEN then self->address was // never set, so avoid trying to dereference it. Nothing needs to // be cleaned up. if (self->address) { if (*self->address) zsys_info ("client %u address='%s' - de-registering", self->unique_id, self->address); // Cancel all stream subscriptions stream_t *stream = (stream_t *) zlistx_detach (self->readers, NULL); while (stream) { zsock_send (stream->actor, "sp", "CANCEL", self); stream = (stream_t *) zlistx_detach (self->readers, NULL); } // Cancel all service offerings service_t *service = (service_t *) zhashx_first (self->server->services); while (service) { offer_t *offer = (offer_t *) zlistx_first (service->offers); while (offer) { if (offer->client == self) zlistx_delete (service->offers, zlistx_cursor (service->offers)); offer = (offer_t *) zlistx_next (service->offers); } service = (service_t *) zhashx_next (self->server->services); } if (*self->address) zhashx_delete (self->server->clients, self->address); } mlm_proto_set_status_code (self->message, MLM_PROTO_SUCCESS); }
static void deregister_the_client (client_t *self) { if (*self->address) zsys_info ("client address='%s' - de-registering", self->address); // Cancel all stream subscriptions stream_t *stream = (stream_t *) zlistx_detach (self->readers, NULL); while (stream) { zsock_send (stream->actor, "sp", "CANCEL", self); stream = (stream_t *) zlistx_detach (self->readers, NULL); } // Cancel all service offerings service_t *service = (service_t *) zhashx_first (self->server->services); while (service) { offer_t *offer = (offer_t *) zlistx_first (service->offers); while (offer) { if (offer->client == self) zlistx_delete (service->offers, zlistx_cursor (service->offers)); offer = (offer_t *) zlistx_next (service->offers); } service = (service_t *) zhashx_next (self->server->services); } if (*self->address) zhashx_delete (self->server->clients, self->address); mlm_proto_set_status_code (self->message, MLM_PROTO_SUCCESS); }
static void get_first_tuple (client_t *self) { tuple_t *tuple = (tuple_t *) zhashx_first (self->server->tuples); if (tuple) { zgossip_msg_set_key (self->message, tuple->key); zgossip_msg_set_value (self->message, tuple->value); engine_set_next_event (self, ok_event); } else engine_set_next_event (self, finished_event); }
void zcertstore_fprint (zcertstore_t *self, FILE *file) { if (self->location) fprintf (file, "Certificate store at %s:\n", self->location); else fprintf (file, "Certificate store\n"); zcert_t *cert = (zcert_t *) zhashx_first (self->certs); while (cert) { zcert_fprint (cert, file); cert = (zcert_t *) zhashx_next (self->certs); } }
void zcertstore_print (zcertstore_t *self) { if (self->location) zsys_info ("zcertstore: certificates at location=%s:", self->location); else zsys_info ("zcertstore: certificates in memory"); zcert_t *cert = (zcert_t *) zhashx_first (self->certs); while (cert) { zcert_print (cert); cert = (zcert_t *) zhashx_next (self->certs); } }
server_connect (server_t *self, const char *endpoint) #endif { zsock_t *remote = zsock_new (ZMQ_DEALER); assert (remote); // No recovery if exhausted #ifdef CZMQ_BUILD_DRAFT_API // DRAFT-API: Security if (public_key){ zcert_t *cert = zcert_new_from_txt (self->public_key, self->secret_key); zcert_apply(cert, remote); zsock_set_curve_serverkey (remote, public_key); #ifndef ZMQ_CURVE // legacy ZMQ support // inline incase the underlying assert is removed bool ZMQ_CURVE = false; #endif assert (zsock_mechanism (remote) == ZMQ_CURVE); zcert_destroy(&cert); } #endif // Never block on sending; we use an infinite HWM and buffer as many // messages as needed in outgoing pipes. Note that the maximum number // is the overall tuple set size. zsock_set_unbounded (remote); if (zsock_connect (remote, "%s", endpoint)) { zsys_warning ("bad zgossip endpoint '%s'", endpoint); zsock_destroy (&remote); return; } // Send HELLO and then PUBLISH for each tuple we have zgossip_msg_t *gossip = zgossip_msg_new (); zgossip_msg_set_id (gossip, ZGOSSIP_MSG_HELLO); zgossip_msg_send (gossip, remote); tuple_t *tuple = (tuple_t *) zhashx_first (self->tuples); while (tuple) { zgossip_msg_set_id (gossip, ZGOSSIP_MSG_PUBLISH); zgossip_msg_set_key (gossip, tuple->key); zgossip_msg_set_value (gossip, tuple->value); zgossip_msg_send (gossip, remote); tuple = (tuple_t *) zhashx_next (self->tuples); } // Now monitor this remote for incoming messages zgossip_msg_destroy (&gossip); engine_handle_socket (self, remote, remote_handler); zlistx_add_end (self->remotes, remote); }
JNIEXPORT jlong JNICALL Java_org_zeromq_czmq_Zhashx__1_1first (JNIEnv *env, jclass c, jlong self) { jlong first_ = (jlong) (intptr_t) zhashx_first ((zhashx_t *) (intptr_t) self); return first_; }
void zhashx_test (int verbose) { printf (" * zhashx: "); // @selftest zhashx_t *hash = zhashx_new (); assert (hash); assert (zhashx_size (hash) == 0); assert (zhashx_first (hash) == NULL); assert (zhashx_cursor (hash) == NULL); // Insert some items int rc; rc = zhashx_insert (hash, "DEADBEEF", "dead beef"); char *item = (char *) zhashx_first (hash); assert (streq ((char *) zhashx_cursor (hash), "DEADBEEF")); assert (streq (item, "dead beef")); assert (rc == 0); rc = zhashx_insert (hash, "ABADCAFE", "a bad cafe"); assert (rc == 0); rc = zhashx_insert (hash, "C0DEDBAD", "coded bad"); assert (rc == 0); rc = zhashx_insert (hash, "DEADF00D", "dead food"); assert (rc == 0); assert (zhashx_size (hash) == 4); // Look for existing items item = (char *) zhashx_lookup (hash, "DEADBEEF"); assert (streq (item, "dead beef")); item = (char *) zhashx_lookup (hash, "ABADCAFE"); assert (streq (item, "a bad cafe")); item = (char *) zhashx_lookup (hash, "C0DEDBAD"); assert (streq (item, "coded bad")); item = (char *) zhashx_lookup (hash, "DEADF00D"); assert (streq (item, "dead food")); // Look for non-existent items item = (char *) zhashx_lookup (hash, "foo"); assert (item == NULL); // Try to insert duplicate items rc = zhashx_insert (hash, "DEADBEEF", "foo"); assert (rc == -1); item = (char *) zhashx_lookup (hash, "DEADBEEF"); assert (streq (item, "dead beef")); // Some rename tests // Valid rename, key is now LIVEBEEF rc = zhashx_rename (hash, "DEADBEEF", "LIVEBEEF"); assert (rc == 0); item = (char *) zhashx_lookup (hash, "LIVEBEEF"); assert (streq (item, "dead beef")); // Trying to rename an unknown item to a non-existent key rc = zhashx_rename (hash, "WHATBEEF", "NONESUCH"); assert (rc == -1); // Trying to rename an unknown item to an existing key rc = zhashx_rename (hash, "WHATBEEF", "LIVEBEEF"); assert (rc == -1); item = (char *) zhashx_lookup (hash, "LIVEBEEF"); assert (streq (item, "dead beef")); // Trying to rename an existing item to another existing item rc = zhashx_rename (hash, "LIVEBEEF", "ABADCAFE"); assert (rc == -1); item = (char *) zhashx_lookup (hash, "LIVEBEEF"); assert (streq (item, "dead beef")); item = (char *) zhashx_lookup (hash, "ABADCAFE"); assert (streq (item, "a bad cafe")); // Test keys method zlistx_t *keys = zhashx_keys (hash); assert (zlistx_size (keys) == 4); zlistx_destroy (&keys); zlistx_t *values = zhashx_values(hash); assert (zlistx_size (values) == 4); zlistx_destroy (&values); // Test dup method zhashx_t *copy = zhashx_dup (hash); assert (zhashx_size (copy) == 4); item = (char *) zhashx_lookup (copy, "LIVEBEEF"); assert (item); assert (streq (item, "dead beef")); zhashx_destroy (©); // Test pack/unpack methods zframe_t *frame = zhashx_pack (hash); copy = zhashx_unpack (frame); zframe_destroy (&frame); assert (zhashx_size (copy) == 4); item = (char *) zhashx_lookup (copy, "LIVEBEEF"); assert (item); assert (streq (item, "dead beef")); zhashx_destroy (©); // Test save and load zhashx_comment (hash, "This is a test file"); zhashx_comment (hash, "Created by %s", "czmq_selftest"); zhashx_save (hash, ".cache"); copy = zhashx_new (); assert (copy); zhashx_load (copy, ".cache"); item = (char *) zhashx_lookup (copy, "LIVEBEEF"); assert (item); assert (streq (item, "dead beef")); zhashx_destroy (©); zsys_file_delete (".cache"); // Delete a item zhashx_delete (hash, "LIVEBEEF"); item = (char *) zhashx_lookup (hash, "LIVEBEEF"); assert (item == NULL); assert (zhashx_size (hash) == 3); // Check that the queue is robust against random usage struct { char name [100]; bool exists; } testset [200]; memset (testset, 0, sizeof (testset)); int testmax = 200, testnbr, iteration; srandom ((unsigned) time (NULL)); for (iteration = 0; iteration < 25000; iteration++) { testnbr = randof (testmax); if (testset [testnbr].exists) { item = (char *) zhashx_lookup (hash, testset [testnbr].name); assert (item); zhashx_delete (hash, testset [testnbr].name); testset [testnbr].exists = false; } else { sprintf (testset [testnbr].name, "%x-%x", rand (), rand ()); if (zhashx_insert (hash, testset [testnbr].name, "") == 0) testset [testnbr].exists = true; } } // Test 10K lookups for (iteration = 0; iteration < 10000; iteration++) item = (char *) zhashx_lookup (hash, "DEADBEEFABADCAFE"); // Destructor should be safe to call twice zhashx_destroy (&hash); zhashx_destroy (&hash); assert (hash == NULL); // Test autofree; automatically copies and frees string values hash = zhashx_new (); assert (hash); zhashx_autofree (hash); char value [255]; strcpy (value, "This is a string"); rc = zhashx_insert (hash, "key1", value); assert (rc == 0); strcpy (value, "Ring a ding ding"); rc = zhashx_insert (hash, "key2", value); assert (rc == 0); assert (streq ((char *) zhashx_lookup (hash, "key1"), "This is a string")); assert (streq ((char *) zhashx_lookup (hash, "key2"), "Ring a ding ding")); zhashx_destroy (&hash); // @end printf ("OK\n"); }
/// // Simple iterator; returns first item in hash table, in no given order, // or NULL if the table is empty. This method is simpler to use than the // foreach() method, which is deprecated. To access the key for this item // use zhashx_cursor(). NOTE: do NOT modify the table while iterating. void * QZhashx::first () { void * rv = zhashx_first (self); return rv; }