예제 #1
0
int main()
{
	int i, n;
	char *a = "AaAa";
	char *b = "BbBb";
	char *c = "CcCc";
	char *p;
	
	cp_vector *v = cp_vector_create(1);

	for (i = 0; i < 30; i++)
	{
		char buf[0x20];
		sprintf(buf, "%s %d", a, i);
		cp_vector_add_element(v, strdup(buf));
		sprintf(buf, "%s %d", b, i);
		cp_vector_add_element(v, strdup(buf));
		sprintf(buf, "%s %d", c, i);
		cp_vector_add_element(v, strdup(buf));
	}

	n = cp_vector_size(v);

	for (i = 0; i < n; i++)
	{
		p = cp_vector_element_at(v, i);
		printf("%d: %s\n", i, p);
	}

	cp_vector_destroy_custom(v, free);
	
	return 0;
}
예제 #2
0
void cp_pooled_thread_client_negociate(cp_pooled_thread_client_interface *c)
{
	int curr_load = (*c->report_load)(c);
	cp_pooled_thread_scheduler *scheduler = c->owner;
	Vector *clients = scheduler->client_list;
	int i, min, imin, max, imax, cval, max_count;
	cp_pooled_thread_client_interface *other;

	int clen = cp_vector_size(clients);

	if (clen == 0) return; //~~ warning for bad usage

	min = INT_MAX;
	max = -1;
	max_count = -1;

	for (i = 0; i < clen; i++)
	{
		other = (cp_pooled_thread_client_interface *) cp_vector_element_at(clients, i);
		cval = (*other->report_load)(other);

		if (other->count < other->min) //~~ what's with the switching
			cp_thread_pool_get_nb(other->owner->pool, other->action, other->prm);

		DEBUGMSG("negociate: pool %d: load = %d, %d <= %d <= %d", i, cval, other->min, other->count, other->max);
		if (cval > max) { max = cval; imax = i; }
		if (cval < min || (cval == min && other->count > max_count)) { min = cval; imin = i; max_count = other->count; }
	}
	DEBUGMSG("negociate: min = %d, max = %d", min, max);
	if (abs(max - min) > SCHEDULER_THRESHOLD)
	{
		cp_pooled_thread_client_interface *maxc = cp_vector_element_at(clients, imax);
		cp_pooled_thread_client_interface *minc = cp_vector_element_at(clients, imin);

		if (cp_thread_pool_count_available(scheduler->pool) == 0 &&
			minc->count > minc->min)
		{
			DEBUGMSG("negociate: shrinking min pool (%d)", imin);
			(*minc->shrink)(minc);
		}
			
		DEBUGMSG("negociate: get_nb for max pool (%d)", imax);
		if (cp_thread_pool_get_nb(maxc->owner->pool, maxc->action, maxc->prm))
			maxc->count++;
	}
}
예제 #3
0
cp_pooled_thread_client_interface *
	choose_random_client(cp_pooled_thread_scheduler *scheduler)
{
	int index = 
#ifdef CP_HAS_RANDOM
		random() 
#else
		rand()
#endif
		% (cp_vector_size(scheduler->client_list));
	return (cp_pooled_thread_client_interface *) 
		cp_vector_element_at(scheduler->client_list, index);
}
예제 #4
0
파일: wf.c 프로젝트: dulton/tinybus
/* output callback */
int print_word_freq(void *entry, void *ordp)
{
	cp_avlnode *n = entry;
	int *count = n->key;
	cp_vector *res = n->value;
	int *ord = ordp;
	int i;

	for (i = 0; i < cp_vector_size(res); i++)
		printf("(%d) [%d] %s\n", *ord, *count, 
				(char *) cp_vector_element_at(res, i));
	(*ord)++;

	return 0;
}
예제 #5
0
void cp_avlnode_multi_print(cp_avlnode *node, int level)
{
	int i;
	cp_vector *v = node->value;
	if (node->right) cp_avlnode_multi_print(node->right, level + 1);
	
	for (i = 0; i < level; i++) printf("  . ");
	printf("(%d) [%s => ", node->balance, (char *) node->key);

	for (i = 0; i < cp_vector_size(v); i++)
		printf("%s; ", (char *) cp_vector_element_at(v, i));

	printf("]\n");

	if (node->left) cp_avlnode_multi_print(node->left, level + 1);
}
예제 #6
0
int print_Employee_entry(void *entry, void *multiple)
{
	cp_index_map_node *node = entry;

	if (multiple)
	{
		int i;
		cp_vector *v = node->entry;
		for (i = 0; i < cp_vector_size(v); i++)
			Employee_dump(cp_vector_element_at(v, i));
	}
	else
		Employee_dump(node->entry);

	return 0;
}
예제 #7
0
파일: test_rtrie.c 프로젝트: dulton/tinybus
int main(int argc, char *argv[])
{
    char *bstr1 = "0011100011110000011111";
    char *bstr2 = "0011100011110010011111";
    char *bstr3 = "0011100011110000011111001";
    char *bstr4 = "01";
    char *bstr5 = "011001";

    char *l1 = "001110001111000001111";
    char *l2 = "0011100011110000011110";
    char *l3 = "00111000111100000111111";

    void *x;
    cp_vector *v;
    int len;
    char *tmp;

    cp_bstr *a, *b, *c, *d, *e;
    cp_bstr *bl1, *bl2, *bl3;

    cp_rtrie *grp;

    a = cstr_to_bstr(bstr1);
    b = cstr_to_bstr(bstr2);
    c = cstr_to_bstr(bstr3);
    d = cstr_to_bstr(bstr4);
    e = cstr_to_bstr(bstr5);

    grp = cp_rtrie_create_rtrie(COLLECTION_MODE_NOSYNC |
                                COLLECTION_MODE_COPY | COLLECTION_MODE_DEEP,
                                (cp_copy_fn) strdup, free);

    printf("empty trie\n");
    cp_rtrie_dump(grp);

    cp_rtrie_add(grp, a, "aaa");
    printf("adding aaa\n");
    cp_rtrie_dump(grp);

    cp_rtrie_add(grp, b, "bbb");
    printf("adding bbb\n");
    cp_rtrie_dump(grp);

    cp_rtrie_add(grp, c, "ccc");
    printf("adding ccc\n");
    cp_rtrie_dump(grp);

    cp_rtrie_add(grp, d, "ddd");
    printf("adding ddd\n");
    cp_rtrie_dump(grp);

    cp_rtrie_add(grp, e, "eee");
    printf("adding eee\n");
    cp_rtrie_dump(grp);

    bl1 = cstr_to_bstr(l1);
    printf("looking up [%s]\n", l1);
    x = cp_rtrie_exact_match(grp, bl1);
    printf("expected: 0, actual: %p [%s] - %s\n", x, x != 0 ? (char *) x : "NULL", x == NULL ? "success" : "error");
    cp_bstr_destroy(bl1);

    bl2 = cstr_to_bstr(l2);
    printf("looking up [%s]\n", l2);
    x = cp_rtrie_exact_match(grp, bl2);
    printf("expected: 0, actual: %p [%s] - %s\n", x, x != 0 ? (char *) x : "NULL", x == NULL ? "success" : "error");
    cp_bstr_destroy(bl2);

    bl3 = cstr_to_bstr(l3);
    printf("looking up [%s]\n", l3);
    x = cp_rtrie_exact_match(grp, bl3);
    printf("expected: 0, actual: %p [%s] - %s\n", x, x != 0 ? (char *) x : "NULL", x == NULL ? "success" : "error");
    cp_bstr_destroy(bl3);

    printf("looking up [%s]\n", bstr1);
    x = cp_rtrie_exact_match(grp, a);
    printf("expected: [aaa], actual: %p [%s] - %s\n", x, x != 0 ? (char *) x : "NULL", ((x != NULL) && (strcmp((char *) x, "aaa") == 0)) ? "success" : "error");

    printf("testing cp_rtrie_fetch_matches\n");
    printf("looking up [%s]\n", bstr3);
    v = cp_rtrie_fetch_matches(grp, c);
    if (v != NULL)
    {
        int i;
        char *s;
        i = cp_vector_size(v);
        if (i != 5)
            printf("expected 5 entries, got %d - error\n", i);
        else
        {
            s = cp_vector_element_at(v, 0);
            if (s != NULL)
                printf("expected NULL element 0, got %p [%s] - error\n", s, s);
            else
                printf("[0] [NULL]\n");

            s = cp_vector_element_at(v, 1);
            if (s != NULL)
                printf("expected NULL element 1, got %p [%s] - error\n", s, s);
            else
                printf("[1] [NULL]\n");

            s = cp_vector_element_at(v, 2);
            if (s != NULL)
                printf("expected NULL element 2, got %p [%s] - error\n", s, s);
            else
                printf("[2] [NULL]\n");

            s = cp_vector_element_at(v, 3);
            if (strcmp(s, "aaa") != 0)
                printf("expected element 3 [aaa], got %p [%s] - error\n", s, s);
            else
                printf("[3] [aaa]\n");

            s = cp_vector_element_at(v, 4);
            if (strcmp(s, "ccc") != 0)
                printf("expected element 4 [ccc], got %p [%s] - error\n", s, s);
            else
                printf("[4] [ccc]\n");
        }
    }
    else
        printf("lookup failed, error\n");
    cp_vector_destroy(v);

    tmp = strdup(bstr3);
    len = strlen(tmp);
    tmp[len - 1] = '\0';
    c->length--;
    printf("looking up [%s]\n", tmp);
    v = cp_rtrie_fetch_matches(grp, c);
    if (v != NULL)
    {
        int i;
        char *s;
        i = cp_vector_size(v);
        if (i != 4)
            printf("expected 4 entries, got %d - error\n", i);
        else
        {
            s = cp_vector_element_at(v, 0);
            if (s != NULL)
                printf("expected NULL element 0, got %p [%s] - error\n", s, s);
            else
                printf("[0] [NULL]\n");

            s = cp_vector_element_at(v, 1);
            if (s != NULL)
                printf("expected NULL element 1, got %p [%s] - error\n", s, s);
            else
                printf("[1] [NULL]\n");

            s = cp_vector_element_at(v, 2);
            if (s != NULL)
                printf("expected NULL element 2, got %p [%s] - error\n", s, s);
            else
                printf("[2] [NULL]\n");

            s = cp_vector_element_at(v, 3);
            if (strcmp(s, "aaa") != 0)
                printf("expected element 3 [aaa], got %p [%s] - error\n", s, s);
            else
                printf("[3] [aaa]\n");
        }
    }
    else
        printf("lookup failed, error\n");
    c->length++;
    cp_vector_destroy(v);

    printf("\ntesting cp_rtrie_submatch\n");
    v = cp_rtrie_submatch(grp, a);
    if (v == NULL)
        printf("got no result for [%s], error\n", bstr1);
    else if (cp_vector_size(v) != 2)
        printf("expected 2 entries for [%s], got %d, error\n", bstr1, cp_vector_size(v));
    else
    {
        char *s = cp_vector_element_at(v, 0);
        if (strcmp(s, "aaa") != 0)
            printf("expected element 0 [aaa], got %p [%s] - error\n", s, s);
        else
            printf("[0] [aaa]\n");

        s = cp_vector_element_at(v, 1);
        if (strcmp(s, "ccc") != 0)
            printf("expected element 1 [ccc], got %p [%s] - error\n", s, s);
        else
            printf("[1] [ccc]\n");
    }

    printf("\ntesting_cp_rtrie_prefix_match\n");

    len = cp_rtrie_prefix_match(grp, c, &x);
    if (len != 2)
        printf("expected 2 nodes, got %d - error\n", len);
    if (strcmp((char *) x, "ccc") != 0)
        printf("expected [ccc], got [%s], error\n", (char *) x);

    c->length--;
    len = cp_rtrie_prefix_match(grp, c, &x);
    if (len != 1)
        printf("expected 1 node, got %d - error\n", len);
    if (strcmp((char *) x, "aaa") != 0)
        printf("expected [aaa], got [%s], error\n", (char *) x);
    c->length++;

    printf("\nremoving aaa\n");
    cp_rtrie_remove(grp, a, NULL);
    cp_rtrie_dump(grp);

    printf("removing bbb\n");
    cp_rtrie_remove(grp, b, NULL);
    cp_rtrie_dump(grp);

    printf("removing ccc\n");
    cp_rtrie_remove(grp, c, NULL);
    cp_rtrie_dump(grp);

    printf("removing ddd\n");
    cp_rtrie_remove(grp, d, NULL);
    cp_rtrie_dump(grp);

    printf("removing eee\n");
    cp_rtrie_remove(grp, e, NULL);
    cp_rtrie_dump(grp);

    cp_bstr_destroy(a);
    cp_bstr_destroy(b);
    cp_bstr_destroy(c);
    cp_bstr_destroy(d);
    cp_bstr_destroy(e);

    cp_rtrie_destroy(grp);

    return 0;
}
예제 #8
0
/** execute a prepared statement */
int cp_db_connection_execute_statement(cp_db_connection *connection,
		                               cp_db_statement *statement, 
									   cp_vector *prm,
									   cp_result_set **results)
{
	if (connection->data_source->act->execute_statement)
	{
		int rc;
		int *lengths = NULL;
		void **sprm = NULL;
		char *cstr;
		if (statement->prm_count)
		{
			int i;
//			cp_field_type *type;
			cp_string *str;
			lengths = calloc(statement->prm_count, sizeof(int));
			sprm = calloc(statement->prm_count, sizeof(void *));
			for (i = 0; i < statement->prm_count; i++)
			{
				sprm[i] = cp_vector_element_at(prm, i);
				switch (statement->types[i])
				{
					case CP_FIELD_TYPE_BOOLEAN: 
					case CP_FIELD_TYPE_SHORT:     
						lengths[i] = sizeof(short); break;
					case CP_FIELD_TYPE_INT:       
						lengths[i] = sizeof(int); break;
					case CP_FIELD_TYPE_LONG:      
						lengths[i] = sizeof(long); break;
					case CP_FIELD_TYPE_LONG_LONG: 
#ifdef CP_HAS_LONG_LONG
						lengths[i] = sizeof(long long); break;
#else
						lengths[i] = sizeof(__int64); break;
#endif /* CP_HAS_LONG_LONG */
					case CP_FIELD_TYPE_FLOAT:     
						lengths[i] = sizeof(float); break;
					case CP_FIELD_TYPE_DOUBLE:    
						lengths[i] = sizeof(double); break;
					case CP_FIELD_TYPE_CHAR:
					case CP_FIELD_TYPE_VARCHAR:
						cstr = cp_vector_element_at(prm, i);
						lengths[i] = strlen(cstr);
						break;
					case CP_FIELD_TYPE_BLOB:      
						str = cp_vector_element_at(prm, i);
						lengths[i] = str->len;
						sprm[i] = str->data;
						break;
					case CP_FIELD_TYPE_DATE:      
					case CP_FIELD_TYPE_TIME:      
					case CP_FIELD_TYPE_TIMESTAMP:
					default:
						;
				}
			}
		}
		rc =  (*connection->data_source->act->execute_statement)(connection, 
																 statement, 
																 results, 
																 lengths, 
																 sprm);
		if (statement->prm_count)
		{
			free(lengths);
			free(sprm);
		}

		return rc;
	}

	cp_error(CP_METHOD_NOT_IMPLEMENTED, 
			"%s driver does not implement execute_statement", 
			connection->data_source->act->dbms_lit);

	return -1;
}
예제 #9
0
int hitcount_service(cp_http_request *request, cp_http_response *response)
{
    char buf[BUFLEN];
    char sbuf[BUFLEN / 2];
    char **key;
    int i;
    int *pcount;

    cp_http_session *session = cp_http_request_get_session(request, 1);
    if (cp_http_session_is_fresh(session))
    {
        pcount = malloc(sizeof(int *));
        *pcount = 1;
        cp_http_session_set_dtr(session, "pcount", pcount, free);
    }
    else
    {
        pcount = cp_http_session_get(session, "pcount");
        (*pcount)++;
    }

    cp_http_response_set_content_type(response, HTML);
#ifdef CP_HAS_SNPRINTF
    snprintf(buf, BUFLEN, 
        "<html>\n<head>\n<title>cp_httpsocket test </title>\n</head>\n"
        "<body><h1>hit count: %d</h1>\n"
        "you've visited this page %d times.<p>", hitcount++, *pcount);
#else
    sprintf(buf, 
        "<html>\n<head>\n<title>cp_httpsocket test </title>\n</head>\n"
        "<body><h1>hit count: %d</h1>\n"
        "you've visited this page %d times.<p>", hitcount++, *pcount);
#endif /* CP_HAS_SNPRINTF */

    if (cp_hashtable_count(request->header))
    {
        strlcat(buf, "<hr>\n<h3> your headers: </h3>\n<ul>\n", BUFLEN);
        key = (char **) cp_hashtable_get_keys(request->header);
        for (i = 0; i < cp_hashtable_count(request->header); i++)
        {
#ifdef CP_HAS_SNPRINTF
            snprintf(sbuf, BUFLEN / 2, "<li> %s: %s\n", 
                    key[i], cp_http_request_get_header(request, key[i]));
#else
            sprintf(sbuf, "<li> %s: %s\n", 
                    key[i], cp_http_request_get_header(request, key[i]));
#endif /* CP_HAS_SNPRINTF */
            strlcat(buf, sbuf, BUFLEN);
        }
        if (key) free(key);
        strlcat(buf, "</ul>\n", BUFLEN);
    }
    
    if (request->cookie)
    {
        int n = cp_vector_size(request->cookie);
        strlcat(buf, "<hr>\n<h3> cookies: </h3>\n<ul>\n", BUFLEN);
        for (i = 0; i < n; i++)
        {
#ifdef CP_HAS_SNPRINTF
            snprintf(sbuf, BUFLEN / 2, "<li> %s\n", (char *) cp_vector_element_at(request->cookie, i));
#else
            sprintf(sbuf, "<li> %s\n", (char *) cp_vector_element_at(request->cookie, i));
#endif /* CP_HAS_SNPRINTF */
            strlcat(buf, sbuf, BUFLEN);
        }
        strlcat(buf, "</ul>\n", BUFLEN);
    }
        
    if (request->prm)
    {
        strlcat(buf, "<hr>\n<h3> cgi variables: </h3>\n<ul>\n", BUFLEN);
        key = (char **) cp_hashtable_get_keys(request->prm);
        for (i = 0; i < cp_hashtable_count(request->prm); i++)
        {
#ifdef CP_HAS_SNPRINTF
            snprintf(sbuf, BUFLEN / 2, "<li> %s: %s\n", 
                    key[i], cp_http_request_get_parameter(request, key[i]));
#else
            sprintf(sbuf, "<li> %s: %s\n", 
                    key[i], cp_http_request_get_parameter(request, key[i]));
#endif /* CP_HAS_SNPRINTF */
            strlcat(buf, sbuf, BUFLEN);
        }
        if (key) free(key);
        strlcat(buf, "</ul>\n", BUFLEN);
    }

    strlcat(buf, "<hr>\nfree image: <img src=\"/img/free_image.jpg\">\n", BUFLEN);
    strlcat(buf, "</body>\n</html>\n", BUFLEN);
    response->body = strdup(buf);
    
    DEBUGMSG("hitcount_service: dumping request");
    cp_http_request_dump(request);

    return HTTP_CONNECTION_POLICY_KEEP_ALIVE;
}