zhash_t * zhash_dup (zhash_t *self) { if (!self) return NULL; zhash_t *copy = zhash_new (); if (copy) { copy->destructor = self->destructor; copy->duplicator = self->duplicator; uint index; size_t limit = primes [self->prime_index]; for (index = 0; index < limit; index++) { item_t *item = self->items [index]; while (item) { if (zhash_insert (copy, item->key, item->value)) { zhash_destroy (©); break; } item = item->next; } } } return copy; }
void zcertstore_insert (zcertstore_t *self, zcert_t **cert_p) { zlist_append (self->cert_list, *cert_p); zhash_insert (self->cert_hash, zcert_public_txt (*cert_p), *cert_p); *cert_p = NULL; // We own this now }
void agent_control_message (agent_t *self) { zmsg_t *msg = zmsg_recv (self->control); char *command = zmsg_pop (msg); if (strcmp (command, "CONNECT") == 0) { char *endpoint = zmsg_pop (msg); printf ("I: connecting to %s...\n", endpoint); int rc = zmq_connect (self->router, endpoint); assert (rc == 0); server_t *server = server_new (endpoint); zhash_insert (self->servers, endpoint, server); zhash_freefn (self->servers, endpoint, s_server_free); zlist_append (self->actives, server); server->ping_at = s_clock () + PING_INTERVAL; server->expires = s_clock () + SERVER_TTL; free (endpoint); } else if (strcmp (command, "REQUEST") == 0) { assert (!self->request); // Strict request-reply cycle // Prefix request with sequence number and empty envelope char sequence_text [10]; sprintf (sequence_text, "%u", ++self->sequence); zmsg_push (msg, sequence_text); // Take ownership of request message self->request = msg; msg = NULL; // Request expires after global timeout self->expires = s_clock () + GLOBAL_TIMEOUT; } free (command); zmsg_destroy (&msg); }
static void s_zdir_watch_subscribe (zdir_watch_t *watch, const char *path) { if (watch->verbose) zsys_info ("zdir_watch: Subscribing to directory path: %s", path); zdir_watch_sub_t *sub = (zdir_watch_sub_t *) zmalloc (sizeof (zdir_watch_sub_t)); sub->dir = zdir_new (path, NULL); if (!sub->dir) { if (watch->verbose) zsys_error ("zdir_watch: Unable to create zdir for path: %s", path); zsock_signal (watch->pipe, 1); return; } int rc = zhash_insert (watch->subs, path, sub); if (rc) { if (watch->verbose) zsys_error ("zdir_watch: Unable to insert path '%s' into subscription list", path); zsock_signal (watch->pipe, 1); return; } void *item = zhash_freefn (watch->subs, path, s_sub_free); if (item != sub) { if (watch->verbose) zsys_error ("zdir_watch: Unable to set free fn for path %s", path); zsock_signal (watch->pipe, 1); return; } if (watch->verbose) zsys_info ("zdir_watch: Successfully subscribed to %s", path); zsock_signal (watch->pipe, 0); }
static void fixup_newjob_event (flux_t *h, int64_t nj) { json_object *ss = NULL; json_object *jcb = NULL; int64_t js = J_NULL; char *key = xasprintf ("%"PRId64, nj); jscctx_t *ctx = getctx (h); /* We fix up ordering problem only when new job event hasn't been reported through a kvs watch */ jcb = Jnew (); ss = Jnew (); Jadd_int64 (jcb, JSC_JOBID, nj); Jadd_int64 (ss, JSC_STATE_PAIR_OSTATE , (int64_t) js); Jadd_int64 (ss, JSC_STATE_PAIR_NSTATE, (int64_t) js); json_object_object_add (jcb, JSC_STATE_PAIR, ss); if (zhash_insert (ctx->active_jobs, key, (void *)(intptr_t)js) < 0) { flux_log (h, LOG_ERR, "new_job_cb: inserting a job to hash failed"); goto done; } if (invoke_cbs (h, nj, jcb, 0) < 0) { flux_log (h, LOG_ERR, "makeup_newjob_event: failed to invoke callbacks"); goto done; } done: Jput (jcb); free (key); return; }
static zhash_t *determine_all_min_bandwidth (struct rdl *rdl, zlist_t *running_jobs) { double root_bw; double *curr_value = NULL; struct resource *root = NULL; job_t *curr_job = NULL; char job_id_str[100]; zhash_t *job_hash = zhash_new (); root = rdl_resource_get (rdl, "default"); root_bw = get_max_bandwidth (root); curr_job = zlist_first (running_jobs); while (curr_job != NULL) { curr_value = (double *)malloc (sizeof (double)); *curr_value = root_bw; sprintf (job_id_str, "%d", curr_job->id); zhash_insert (job_hash, job_id_str, curr_value); zhash_freefn (job_hash, job_id_str, free); curr_job = zlist_next (running_jobs); } determine_all_min_bandwidth_helper (root, root_bw, job_hash); return job_hash; }
static halutils_err_e _disp_table_insert (disp_table_t *self, uint32_t key, disp_table_func_fp func_fp) { if (func_fp == NULL) { return HALUTILS_ERR_NULL_POINTER; } DBE_DEBUG (DBG_HAL_UTILS | DBG_LVL_TRACE, "[halutils:disp_table] Registering function (%p) opcode (%u) into dispatch table\n", func_fp, key); func_fp_wrapper_t *func_fp_wrapper = zmalloc (sizeof *func_fp_wrapper); ASSERT_ALLOC (func_fp_wrapper, err_func_wrapper_alloc); /* Initialize func_p_wrapper struct */ func_fp_wrapper->func_fp = func_fp; char *key_c = halutils_stringify_key (key); ASSERT_ALLOC (key_c, err_key_c_alloc); int zerr = zhash_insert (self->table_h, key_c, func_fp_wrapper); ASSERT_TEST(zerr == 0, "Could not insert item into dispatch table", err_insert_hash); /* Setup free function */ zhash_freefn (self->table_h, key_c, _disp_table_free_item); free (key_c); return HALUTILS_SUCCESS; err_insert_hash: free (key_c); err_key_c_alloc: free (func_fp_wrapper); err_func_wrapper_alloc: return HALUTILS_ERR_ALLOC; }
static void sub_patch_add (sub_t *self, fmq_patch_t *patch) { // Skip file creation if client already has identical file fmq_patch_digest_set (patch); if (fmq_patch_op (patch) == patch_create) { char *digest = zhash_lookup (self->cache, fmq_patch_virtual (patch)); if (digest && strcasecmp (digest, fmq_patch_digest (patch)) == 0) return; // Just skip patch for this client } // Remove any previous patches for the same file fmq_patch_t *existing = (fmq_patch_t *) zlist_first (self->client->patches); while (existing) { if (streq (fmq_patch_virtual (patch), fmq_patch_virtual (existing))) { zlist_remove (self->client->patches, existing); fmq_patch_destroy (&existing); break; } existing = (fmq_patch_t *) zlist_next (self->client->patches); } if (fmq_patch_op (patch) == patch_create) zhash_insert (self->cache, fmq_patch_digest (patch), fmq_patch_virtual (patch)); // Track that we've queued patch for client, so we don't do it twice zlist_append (self->client->patches, fmq_patch_dup (patch)); }
static const char* processor_setup_module(processor_state_t *self, const char *page) { int max_mod_len = strlen(page); char module_str[max_mod_len+1]; char *mod_ptr = strchr(page, ':'); strcpy(module_str, "::"); if (mod_ptr != NULL){ if (mod_ptr != page) { int mod_len = mod_ptr - page; memcpy(module_str+2, page, mod_len); module_str[mod_len+2] = '\0'; } } else { char *action_ptr = strchr(page, '#'); if (action_ptr != NULL) { int mod_len = action_ptr - page; memcpy(module_str+2, page, mod_len); module_str[mod_len+2] = '\0'; } } char *module = zhash_lookup(self->modules, module_str); if (module == NULL) { module = strdup(module_str); int rc = zhash_insert(self->modules, module, module); assert(rc == 0); zhash_freefn(self->modules, module, free); } // printf("[D] page: %s\n", page); // printf("[D] module: %s\n", module); return module; }
static service_t * s_service_require (broker_t *self, zframe_t *service_frame) { assert (service_frame); char *name = zframe_strdup (service_frame); service_t *service = (service_t *) zhash_lookup (self->services, name); if (service == NULL) { service = (service_t *) zmalloc (sizeof (service_t)); service->broker = self; service->name = name; service->requests = zlist_new (); service->waiting = zlist_new (); service->blacklist = zlist_new (); zhash_insert (self->services, name, service); zhash_freefn (self->services, name, s_service_destroy); if (self->verbose) zclock_log ("I: added service: %s", name); } else free (name); return service; }
void zyre_group_join (zyre_group_t *self, zyre_peer_t *peer) { assert (self); assert (peer); zhash_insert (self->peers, zyre_peer_identity (peer), peer); zyre_peer_set_status (peer, zyre_peer_status (peer) + 1); }
static int barrier_add_client (barrier_t *b, char *sender, zmsg_t **zmsg) { if (zhash_insert (b->clients, sender, *zmsg) < 0) return -1; zhash_freefn (b->clients, sender, (zhash_free_fn *)free_zmsg); *zmsg = NULL; /* list owns it now */ return 0; }
static int barrier_add_client (barrier_t *b, char *sender, const flux_msg_t *msg) { flux_msg_t *cpy = flux_msg_copy (msg, true); if (!cpy || zhash_insert (b->clients, sender, cpy) < 0) return -1; zhash_freefn (b->clients, sender, (zhash_free_fn *)flux_msg_destroy); return 0; }
void zcert_set_meta (zcert_t *self, const char *name, const char *format, ...) { va_list argptr; va_start (argptr, format); char *value = zsys_vprintf (format, argptr); va_end (argptr); zhash_insert (self->metadata, name, value); zstr_free (&value); }
zargs_t * zargs_new (int argc, char **argv) { assert (argc > 0); assert (argv); zargs_t *self = (zargs_t *) zmalloc (sizeof (zargs_t)); assert (self); // Initialize class properties here self->progname = argv [0]; assert (self->progname); self->arguments = zlist_new (); assert (self->arguments); self->parameters = zhash_new (); assert (self->parameters); if (argc == 1) return self; int idx = 1; bool params_only = false; while (argv [idx]) { if (params_only || argv [idx][0] != '-') zlist_append (self->arguments, argv [idx]); else { if (streq (argv [idx], "--")) { params_only = true; idx ++; continue; } else if (argv [idx+1] && argv [idx+1][0] != '-') { zhash_insert (self->parameters, argv [idx], argv [idx+1]); idx ++; } else { zhash_insert (self->parameters, argv [idx], ZARG_PARAM_EMPTY); } } idx ++; } return self; }
static pipe_t * pipe_new (server_t *server, const char *name) { pipe_t *self = (pipe_t *) zmalloc (sizeof (pipe_t)); self->name = strdup (name); self->server = server; zhash_insert (server->pipes, name, self); zhash_freefn (server->pipes, name, s_delete_pipe); return self; }
static int new_job_cb (const char *key, int64_t val, void *arg, int errnum) { int64_t nj = 0; int64_t js = 0; JSON ss = NULL; JSON jcb = NULL; char k[20] = {'\0'}; char path[20] = {'\0'}; flux_t h = (flux_t) arg; jscctx_t *ctx = getctx (h); if (ctx->first_time == 1) { /* watch is invoked immediately and we shouldn't * rely on that event at all. */ ctx->first_time = 0; return 0; } if (chk_errnum (h, errnum) < 0) return 0; flux_log (h, LOG_DEBUG, "new_job_cb invoked: key(%s), val(%ld)", key, val); js = J_NULL; nj = val-1; snprintf (k, 20, "%ld", nj); snprintf (path, 20, "lwj.%ld", nj); if (zhash_insert (ctx->active_jobs, k, (void *)(intptr_t)js) < 0) { flux_log (h, LOG_ERR, "new_job_cb: inserting a job to hash failed"); goto done; } flux_log (h, LOG_DEBUG, "jobstate_hdlr registered"); jcb = Jnew (); ss = Jnew (); Jadd_int64 (jcb, JSC_JOBID, nj); Jadd_int64 (ss, JSC_STATE_PAIR_OSTATE , (int64_t) js); Jadd_int64 (ss, JSC_STATE_PAIR_NSTATE, (int64_t) js); json_object_object_add (jcb, JSC_STATE_PAIR, ss); if (invoke_cbs (h, nj, jcb, errnum) < 0) { flux_log (h, LOG_ERR, "new_job_cb: failed to invoke callbacks"); } if (reg_jobstate_hdlr (h, path, job_state_cb) == -1) { flux_log (h, LOG_ERR, "new_job_cb: reg_jobstate_hdlr: %s", strerror (errno)); } done: /* always return 0 so that reactor won't return */ return 0; }
int store_payload(zhash_t *object_hash, char *obj_name, Tag tag, zframe_t *payload, enum INSERT_DATA_POLICY policy) { char tag_str[BUFSIZE]; zhash_t *single_object_hash = (zhash_t *)zhash_lookup(object_hash, obj_name); tag_to_string(tag, tag_str); if( single_object_hash==NULL) { single_object_hash = zhash_new(); zhash_insert(object_hash, obj_name, (void *)single_object_hash); } zframe_t *payload_frame = (zframe_t *)zhash_lookup(single_object_hash, tag_str); if( policy == yield) { if(payload_frame!=NULL) return -1; } else if(policy==force) { if(payload_frame!=NULL) { zframe_destroy(&payload_frame); zhash_delete(single_object_hash, tag_str); } } return(zhash_insert(single_object_hash, tag_str, (void *)payload)); }
void CSetAdd(cset_t *self, const uint8_t *srcip, const uint8_t *srcport, const uint8_t *dstip, const uint8_t *dstport, void *value) { assert(self); assert(srcip); assert(srcport); assert(dstip); assert(dstport); const char *key = CSetCreateKey(srcip, srcport, dstip, dstport); zhash_insert(self->hash, key, value); zhash_freefn (self->hash, key, _cset_free_fn); }
static int build_name_array (zhash_t *ha, const char *k, json_object *ns) { int i = (intptr_t) zhash_lookup (ha, k); if ((void *)((intptr_t)i) == NULL) { char *t = xstrdup (k); i = json_object_array_length (ns); Jadd_ar_str (ns, t); zhash_insert (ha, k, (void *)(intptr_t)i+1); free (t); } else i--; return i; }
bool adminCmdInit(void) { // initialize the admin commands hashtable if (!(adminCommands = zhash_new())) { error ("Cannot initialize admin commands hashtable correctly."); return false; } zhash_insert(adminCommands, "spawn", adminCmdSpawnPc); zhash_insert(adminCommands, "jump", adminCmdJump); zhash_insert(adminCommands, "additem", adminCmdAddItem); zhash_insert(adminCommands, "test", adminCmdTest); zhash_insert(adminCommands, "where", adminCmdWhere); zhash_insert(adminCommands, "changeCamera", adminCmdChangeCamera); zhash_insert(adminCommands, "setStamina", adminCmdSetStamina); zhash_insert(adminCommands, "setSP", adminCmdSetSP); zhash_insert(adminCommands, "setLevel", adminCmdSetLevel); return true; }
zyre_group_t * zyre_group_new (const char *name, zhash_t *container) { zyre_group_t *self = (zyre_group_t *) zmalloc (sizeof (zyre_group_t)); self->name = strdup (name); self->peers = zhash_new (); // Insert into container if requested if (container) { zhash_insert (container, name, self); zhash_freefn (container, name, s_delete_group); } return self; }
static int64_t * seq_create (seqhash_t *s, const char *name) { int rc; int64_t *v; if (zhash_lookup (s->vhash, name)) { errno = EEXIST; return (NULL); } v = seq_new (); rc = zhash_insert (s->vhash, xstrdup (name), v); assert (rc >= 0); zhash_freefn (s->vhash, name, free); return (v); }
static barrier_t *barrier_create (ctx_t *ctx, const char *name, int nprocs) { barrier_t *b; b = xzmalloc (sizeof (barrier_t)); b->name = xstrdup (name); b->nprocs = nprocs; if (!(b->clients = zhash_new ())) oom (); b->ctx = ctx; zhash_insert (ctx->barriers, b->name, b); zhash_freefn (ctx->barriers, b->name, barrier_destroy); return b; }
zhash_t * zhash_unpack (zframe_t *frame) { zhash_t *self = zhash_new (); if (!self) return NULL; assert (frame); if (zframe_size (frame) < 4) return self; // Arguable... byte *needle = zframe_data (frame); byte *ceiling = needle + zframe_size (frame); size_t nbr_items = ntohl (*(uint32_t *) needle); needle += 4; while (nbr_items && needle < ceiling) { // Get key as string size_t key_size = *needle++; if (needle + key_size <= ceiling) { char key [256]; memcpy (key, needle, key_size); key [key_size] = 0; needle += key_size; // Get value as longstr if (needle + 4 <= ceiling) { size_t value_size = ntohl (*(uint32_t *) needle); needle += 4; // Be wary of malformed frames if (needle + value_size <= ceiling) { char *value = (char *) malloc (value_size + 1); memcpy (value, needle, value_size); value [value_size] = 0; needle += value_size; // Hash takes ownership of value if (zhash_insert (self, key, value)) { zhash_destroy (&self); break; } } } } } // Hash will free values in destructor if (self) zhash_autofree (self); return self; }
static int command_list_read (zhash_t *h, const char *path) { int i; int rc = -1; int n = 0; json_t *o = NULL; if (!(o = command_list_file_read (path))) goto out; n = json_array_size (o); for (i = 0; i < n; i++) { const char *category; const char *command; const char *description; json_t *entry; zlist_t *zl; if (!(entry = json_array_get (o, i))) { log_msg ("%s: entry %d is not an object", path, i); goto out; } if (json_unpack (entry, "{s:s s:s s:s}", "category", &category, "command", &command, "description", &description) < 0) { log_msg ("%s: Missing element in JSON entry %d", path, i); goto out; } if (!(zl = zhash_lookup (h, category))) { char *s = strdup (category); if (s == NULL) goto out; zl = zlist_new (); //zlist_set_destructor (zl, (czmq_destructor *) cmdhelp_destroy); zhash_insert (h, s, (void *) zl); zhash_freefn (h, s, (zhash_free_fn *) cmd_list_destroy); free (s); } zlist_append (zl, cmdhelp_create (command, description)); } rc = 0; out: json_decref (o); return (rc); }
// somewhat counterintuitively, this returns NULL if everything's ok. char * create_trigger(zhash_t * triggers, char * rule_id, zctx_t * context, triggerconfig_t * tconf) { void * trigger_pipe = zthread_fork(context, trigger, tconf); send_sync("ping", trigger_pipe); char * pipe_resp = zstr_recv(trigger_pipe); if(strcmp(pipe_resp, "pong") == 0) { zhash_insert(triggers, rule_id, trigger_pipe); free(pipe_resp); return NULL; } else { zclock_log("something went wrong creating a trigger: %s", pipe_resp); return pipe_resp; } }
static service_t * s_service_require(server_t *self, const char *service_name) { char *name = strdup(service_name); service_t *service = (service_t *) zhash_lookup(self->services, name); if (service == NULL) { service = (service_t *) zmalloc(sizeof(service_t)); service->broker = self; service->name = name; service->requests = zlist_new(); service->waiting = zlist_new(); zhash_insert(self->services, name, service); zhash_freefn(self->services, name, s_service_destroy); } else zstr_free(&name); return service; }
void zhash_update (zhash_t *self, char *key, void *value) { item_t *item; assert (self); assert (key); item = s_item_lookup (self, key); if (item) { if (item->free_fn) (item->free_fn) (item->value); item->value = value; } else zhash_insert (self, key, value); }
bool staticDataAdd(StaticData *self, StaticDataId id, void *object) { if (self->locked) { sdError(self, "StaticData has been locked and cannot be modified anymore."); return false; } StaticDataKey key; staticDataGenKey(self, id, key); if (zhash_insert(self->hashtable, key, object) != 0) { sdError(self, "Cannot insert the object '%s'", key); return false; } return true; }