Beispiel #1
0
/*
 * This is called at the beginning of a cubist run to clear out
 * all "files" generated on the previous run and to prepare it
 * for the next run.
 */
void rbm_removeall()
{
    /* Check if there actually is anything to remove */
    if (strbufv != NULL) {
        /*
         * Destroy all STRBUF's in the hash table.
         * Note that this loop leaves the hash table full of
         * pointers to deallocated STRBUF's until ht_destroy
         * is called below.
         */
        ht_reset(strbufv);  /* just in case */
        while (1) {
            void *e = ht_next(strbufv);
            if (e == NULL)
                break;
            strbuf_destroy((STRBUF *) ht_value(e));
        }

        /* Destroy the hash table itself */
        ht_destroy(strbufv);
    }

    /* Create/recreate the hash table for subsequent use */
    strbufv = ht_new(HASH_LEN);
}
// remove expired items from connection_list
void _hp_clean_expired(hashtable_t *ht, time_t max_age) {

	hashtable_item_t *i;
	_connection_info_t *ci;
	_connection_list_t *cl;
	strlist_t *rl = 0;
	strlist_iterator_t *rli;
	char *tip;


	for (ht_reset(ht); ht_hasnext(ht); ) {
		i = ht_next(ht);

		ci = (_connection_info_t*) i->value;

		// remove expired connection times
		ci->connects = _hp_cl_remove(ci->connects, max_age);

		// if there are none left, remove the ip from the table.
		if (_hp_cl_size(ci->connects) < 1)
			rl = str_add(rl, ci->ip);
	}

	// remove ips with no connection-times from hashtable.
	for (rli = str_iterator(rl); str_iterator_hasnext(rli); ) {
		tip = str_iterator_next(rli);

		// printf("DEBUG: expiring %s\n", tip);

		ht_remove(ht, tip);
	}

	str_close(rl);
	free(rli);
}
// shows state.
void hp_show_state(hashtable_t *ht, int timeout_seconds, int (*prnf)(char *fmt, ...)) {

	time_t now;
	hashtable_item_t *i;
	_connection_info_t *ci;
	_connection_list_t *cl;

	now = time(0);
	now -= timeout_seconds;

	for (ht_reset(ht); ht_hasnext(ht); ) {
		i = ht_next(ht);

		ci = (_connection_info_t*) i->value;

		prnf("info: %s\n", ci->ip);

		for (cl = ci->connects; cl; cl = cl->next)
			prnf("        %d, ttl = %d\n", cl->time, cl->time - now);

		prnf("\n");

	}


}
Beispiel #4
0
/* Initialize the hash table */
int ht_init(struct hashtable *t)
{
	ht_reset(t);
	t->hashf = ht_hash_pointer;
	t->key_destructor = ht_no_destructor;
	t->val_destructor = ht_no_destructor;
	t->key_compare = ht_compare_ptr;
	return HT_OK;
}
Beispiel #5
0
/* Destroy an entire hash table */
int ht_destroy(struct hashtable *t)
{
	unsigned int i;

	/* Free all the elements */
	for (i = 0; i < t->size && t->used > 0; i++) {
		if (t->table[i] != NULL && t->table[i] != ht_free_element) {
			if (t->key_destructor)
				t->key_destructor(t->table[i]->key);
			if (t->val_destructor)
				t->val_destructor(t->table[i]->data);
			free(t->table[i]);
			t->used--;
		}
	}
	/* Free the table and the allocated cache structure */
	free(t->table);
	/* Re-initialize the table */
	ht_reset(t);
	return HT_OK; /* Actually ht_destroy never fails */
}
Beispiel #6
0
/*
 * Returns the list of pre groups from config.
 *
 */
strlist_t * groups_find_all() {
	strlist_t * l = 0;
	char *tmp, buf[300], *grp;
	hashtable_t *cfg = get_config();
	hashtable_t *env = get_context();
	hashtable_item_t *htn;

	// check if we have made it already.
	if (ht_get(env, "ALLGROUPS"))
		return (strlist_t*)ht_get(env, "ALLGROUPS");

	ht_reset(cfg);

	// look through all properties in config.
	while (htn = ht_next(cfg)) {
		tmp = htn->key;

		if (strcmp(tmp, "group."))
			continue;

		grp = strdup(tmp + 6);

		tmp = strchr(grp, '.');

		if (!tmp) {
			free(grp);
			continue;
		}

		*tmp = 0;

		if (!str_search(l, grp, 0)) {
			l = str_add(l, grp);
		}
	}

	ht_put_obj(env, "ALLGROUPS", l);

	return l;
}