static zyre_node_t * zyre_node_new (zsock_t *pipe, void *args) { zyre_node_t *self = (zyre_node_t *) zmalloc (sizeof (zyre_node_t)); self->inbox = zsock_new (ZMQ_ROUTER); if (self->inbox == NULL) { free (self); return NULL; // Could not create new socket } // Use ZMQ_ROUTER_HANDOVER so that when a peer disconnects and // then reconnects, the new client connection is treated as the // canonical one, and any old trailing commands are discarded. zsock_set_router_handover (self->inbox, 1); self->pipe = pipe; self->outbox = (zsock_t *) args; self->poller = zpoller_new (self->pipe, NULL); self->beacon_port = ZRE_DISCOVERY_PORT; self->evasive_timeout = 5000; self->expired_timeout = 30000; self->interval = 0; // Use default self->uuid = zuuid_new (); self->peers = zhash_new (); self->peer_groups = zhash_new (); self->own_groups = zlist_new (); zlist_autofree (self->own_groups); zlist_comparefn (self->own_groups, s_string_compare); self->headers = zhash_new (); zhash_autofree (self->headers); // Default name for node is first 6 characters of UUID: // the shorter string is more readable in logs self->name = (char *) zmalloc (7); memcpy (self->name, zuuid_str (self->uuid), 6); return self; }
JNIEXPORT void JNICALL Java_zlist__1_1comparefn (JNIEnv *env, jclass c, jlong self, jlong fn) { zlist_comparefn ((zlist_t *) self, (zlist_compare_fn *) fn); }
void zlist_test (bool verbose) { printf (" * zlist: "); // @selftest zlist_t *list = zlist_new (); assert (list); assert (zlist_size (list) == 0); // Three items we'll use as test data // List items are void *, not particularly strings char *cheese = "boursin"; char *bread = "baguette"; char *wine = "bordeaux"; zlist_append (list, cheese); assert (zlist_size (list) == 1); assert ( zlist_exists (list, cheese)); assert (!zlist_exists (list, bread)); assert (!zlist_exists (list, wine)); zlist_append (list, bread); assert (zlist_size (list) == 2); assert ( zlist_exists (list, cheese)); assert ( zlist_exists (list, bread)); assert (!zlist_exists (list, wine)); zlist_append (list, wine); assert (zlist_size (list) == 3); assert ( zlist_exists (list, cheese)); assert ( zlist_exists (list, bread)); assert ( zlist_exists (list, wine)); assert (zlist_head (list) == cheese); assert (zlist_next (list) == cheese); assert (zlist_first (list) == cheese); assert (zlist_tail (list) == wine); assert (zlist_next (list) == bread); assert (zlist_first (list) == cheese); assert (zlist_next (list) == bread); assert (zlist_next (list) == wine); assert (zlist_next (list) == NULL); // After we reach end of list, next wraps around assert (zlist_next (list) == cheese); assert (zlist_size (list) == 3); zlist_remove (list, wine); assert (zlist_size (list) == 2); assert (zlist_first (list) == cheese); zlist_remove (list, cheese); assert (zlist_size (list) == 1); assert (zlist_first (list) == bread); zlist_remove (list, bread); assert (zlist_size (list) == 0); zlist_append (list, cheese); zlist_append (list, bread); assert (zlist_last (list) == bread); zlist_remove (list, bread); assert (zlist_last (list) == cheese); zlist_remove (list, cheese); assert (zlist_last (list) == NULL); zlist_push (list, cheese); assert (zlist_size (list) == 1); assert (zlist_first (list) == cheese); zlist_push (list, bread); assert (zlist_size (list) == 2); assert (zlist_first (list) == bread); assert (zlist_item (list) == bread); zlist_append (list, wine); assert (zlist_size (list) == 3); assert (zlist_first (list) == bread); zlist_t *sub_list = zlist_dup (list); assert (sub_list); assert (zlist_size (sub_list) == 3); zlist_sort (list, s_compare); char *item; item = (char *) zlist_pop (list); assert (item == bread); item = (char *) zlist_pop (list); assert (item == wine); item = (char *) zlist_pop (list); assert (item == cheese); assert (zlist_size (list) == 0); assert (zlist_size (sub_list) == 3); zlist_push (list, sub_list); zlist_t *sub_list_2 = zlist_dup (sub_list); zlist_append (list, sub_list_2); assert (zlist_freefn (list, sub_list, &s_zlist_free, false) == sub_list); assert (zlist_freefn (list, sub_list_2, &s_zlist_free, true) == sub_list_2); zlist_destroy (&list); // Test autofree functionality list = zlist_new (); assert (list); zlist_autofree (list); // Set equals function otherwise equals will not work as autofree copies strings zlist_comparefn (list, s_compare); zlist_push (list, bread); zlist_append (list, cheese); assert (zlist_size (list) == 2); zlist_append (list, wine); assert (zlist_exists (list, wine)); zlist_remove (list, wine); assert (!zlist_exists (list, wine)); assert (streq ((const char *) zlist_first (list), bread)); item = (char *) zlist_pop (list); assert (streq (item, bread)); free (item); item = (char *) zlist_pop (list); assert (streq (item, cheese)); free (item); zlist_destroy (&list); assert (list == NULL); // @end printf ("OK\n"); }
/// // Sets a compare function for this list. The function compares two items. // It returns an integer less than, equal to, or greater than zero if the // first item is found, respectively, to be less than, to match, or be // greater than the second item. // This function is used for sorting, removal and exists checking. void QZlist::comparefn (zlist_compare_fn fn) { zlist_comparefn (self, fn); }