示例#1
0
void wi_test_set_runtime_functions(void) {
    wi_set_t            *set1;
    wi_mutable_set_t    *set2;
    
    set1 = wi_set_with_data(WI_STR("foo"), WI_STR("bar"), NULL);
    set2 = wi_autorelease(wi_mutable_copy(set1));
    
    WI_TEST_ASSERT_EQUAL_INSTANCES(set1, set2, "");
    WI_TEST_ASSERT_EQUALS(wi_hash(set1), wi_hash(set2), "");
    WI_TEST_ASSERT_EQUALS(wi_runtime_id(set1), wi_set_runtime_id(), "");
    WI_TEST_ASSERT_EQUALS(wi_runtime_id(set2), wi_set_runtime_id(), "");
    WI_TEST_ASSERT_TRUE(wi_runtime_options(set1) & WI_RUNTIME_OPTION_IMMUTABLE, "");
    WI_TEST_ASSERT_TRUE(wi_runtime_options(set2) & WI_RUNTIME_OPTION_MUTABLE, "");
    
    wi_mutable_set_remove_data(set2, WI_STR("bar"));
    wi_mutable_set_add_data(set2, WI_STR("baz"));
    
    WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(set1, set2, "");
    
    wi_mutable_set_remove_data(set2, WI_STR("baz"));
    
    WI_TEST_ASSERT_NOT_EQUAL_INSTANCES(set1, set2, "");

    set2 = wi_autorelease(wi_set_init_with_capacity_and_callbacks(wi_mutable_set_alloc(), 0, false, wi_set_null_callbacks));
    
    wi_mutable_set_add_data(set2, (void *) 1);
    wi_mutable_set_add_data(set2, (void *) 2);
    
    WI_TEST_ASSERT_NOT_EQUALS(wi_string_index_of_string(wi_description(set1), WI_STR("foo"), 0), WI_NOT_FOUND, "");
    WI_TEST_ASSERT_NOT_EQUALS(wi_string_index_of_string(wi_description(set2), WI_STR("0x1"), 0), WI_NOT_FOUND, "");
}
示例#2
0
wi_config_t * wi_config_init_with_path(wi_config_t *config, wi_string_t *path, wi_dictionary_t *types, wi_dictionary_t *defaults) {
	config->lock		= wi_lock_init(wi_lock_alloc());
	config->path		= wi_retain(path);
	config->types		= wi_retain(types);
	config->changes		= wi_set_init(wi_mutable_set_alloc());
	config->defaults	= wi_retain(defaults);
	
	return config;
}
示例#3
0
void wi_test_set_mutation(void) {
    wi_mutable_set_t     *set1, *set2;
    
    set1 = wi_set_init_with_capacity_and_callbacks(wi_mutable_set_alloc(), 0, true, wi_set_null_callbacks);
    
    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 0U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 0U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 0U, "");
    
    wi_mutable_set_add_data(set1, WI_STR("foo"));
    wi_mutable_set_add_data(set1, WI_STR("bar"));
    
    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 2U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 1U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 1U, "");
    
    wi_mutable_set_add_data(set1, WI_STR("foo"));
    wi_mutable_set_add_data(set1, WI_STR("bar"));

    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 2U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 2U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 2U, "");
    
    wi_mutable_set_remove_data(set1, WI_STR("foo"));
    wi_mutable_set_remove_data(set1, WI_STR("bar"));
    
    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 2U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 1U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 1U, "");
    
    wi_mutable_set_remove_data(set1, WI_STR("foo"));
    wi_mutable_set_remove_data(set1, WI_STR("bar"));
    
    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 0U, "");

    wi_mutable_set_add_data(set1, WI_STR("foo"));
    wi_mutable_set_add_data(set1, WI_STR("bar"));
    
    set2 = wi_mutable_copy(set1);

    wi_mutable_set_add_data(set2, WI_STR("baz"));

    wi_mutable_set_set_set(set1, set2);

    WI_TEST_ASSERT_EQUAL_INSTANCES(set1, set2, "");
    
    wi_mutable_set_remove_all_data(set1);
    
    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 0U, "");
    
    wi_mutable_set_add_data_from_array(set1, wi_array_with_data(WI_STR("foo"), WI_STR("bar"), NULL));
    
    WI_TEST_ASSERT_EQUALS(wi_set_count(set1), 2U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("foo")), 1U, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count_for_data(set1, WI_STR("bar")), 1U, "");
}
示例#4
0
void wi_test_set_scalars(void) {
    wi_mutable_set_t    *set;
    wi_array_t          *array;
    
    set = wi_set_init_with_capacity_and_callbacks(wi_mutable_set_alloc(), 0, false, wi_set_null_callbacks);
    
    wi_mutable_set_add_data(set, "foo");
    wi_mutable_set_add_data(set, "bar");
    
    WI_TEST_ASSERT_NOT_NULL(set, "");
    WI_TEST_ASSERT_EQUALS(wi_set_count(set), 2U, "");
    WI_TEST_ASSERT_TRUE(wi_set_contains_data(set, "foo"), "");
    WI_TEST_ASSERT_TRUE(wi_set_contains_data(set, "bar"), "");
    
    array = wi_set_all_data(set);
    
    WI_TEST_ASSERT_EQUALS(wi_array_count(array), 2U, "");
    WI_TEST_ASSERT_TRUE(wi_array_contains_data(array, "foo"), "");
    WI_TEST_ASSERT_TRUE(wi_array_contains_data(array, "bar"), "");
}
示例#5
0
文件: users.c 项目: ProfDrLuigi/wired
static wd_user_t * wd_user_init_with_socket(wd_user_t *user, wi_socket_t *socket) {
	wi_address_t	*address;

	user->id						= wd_user_next_id();
	user->socket					= wi_retain(socket);
	user->state						= WD_USER_CONNECTED;
	user->login_time				= wi_date_init(wi_date_alloc());
	user->idle_time					= wi_date_init(wi_date_alloc());
	
	address							= wi_socket_address(socket);
	user->ip						= wi_retain(wi_address_string(address));
	user->host						= wi_retain(wi_address_hostname(address));
	
	user->user_lock					= wi_recursive_lock_init(wi_recursive_lock_alloc());
	user->socket_lock				= wi_recursive_lock_init(wi_recursive_lock_alloc());
	
	user->subscribed_paths			= wi_set_init_with_capacity(wi_mutable_set_alloc(), 0, true);
	user->subscribed_virtualpaths	= wi_dictionary_init(wi_mutable_dictionary_alloc());
	
	return user;
}
示例#6
0
static void wd_transfers_update_queue(void) {
	wi_mutable_set_t		*users;
	wi_mutable_array_t		*sorted_users, *transfers_queue, *failed_transfers;
	wd_transfer_t			*transfer;
	wd_user_t				*user;
	wi_uinteger_t			position;
	wi_uinteger_t			i, count;
	wi_uinteger_t			total_downloads, total_uploads, user_downloads, user_uploads, active_downloads, active_uploads;
	wi_boolean_t			queue;
	
	wi_array_rdlock(wd_transfers);
	
	total_downloads		= wd_settings.totaldownloads;
	user_downloads		= wd_settings.clientdownloads;
	total_uploads		= wd_settings.totaluploads;
	user_uploads		= wd_settings.clientuploads;
	active_downloads	= 0;
	active_uploads		= 0;
	
	failed_transfers	= wi_array_init(wi_mutable_array_alloc());

	users				= wi_set_init(wi_mutable_set_alloc());
	count				= wi_array_count(wd_transfers);
	
	for(i = 0; i < count; i++) {
		transfer = WI_ARRAY(wd_transfers, i);
		
		if(wd_transfer_state(transfer) == WD_TRANSFER_QUEUED) {
			wi_mutable_array_add_data(wd_user_transfers_queue(transfer->user), transfer);
			wi_mutable_set_add_data(users, transfer->user);
		}
		
		wd_user_clear_downloads(transfer->user);
		wd_user_clear_uploads(transfer->user);
	}
	
	for(i = 0; i < count; i++) {
		transfer = WI_ARRAY(wd_transfers, i);
		
		if(wd_transfer_state(transfer) == WD_TRANSFER_RUNNING) {
			if(transfer->type == WD_TRANSFER_DOWNLOAD) {
				active_downloads++;
				wd_user_increase_downloads(transfer->user);
			} else {
				active_uploads++;
				wd_user_increase_uploads(transfer->user);
			}
		}
	}

	count = wi_set_count(users);
	
	if(count > 0) {
		sorted_users = wi_autorelease(wi_mutable_copy(wi_set_all_data(users)));
		
		wi_mutable_array_sort(sorted_users, wd_transfers_compare_user);
		
		position = 1;
		
		while(count > 0) {
			for(i = 0; i < count; i++) {
				user = WI_ARRAY(sorted_users, i);
				transfers_queue = wd_user_transfers_queue(user);
				transfer = WI_ARRAY(transfers_queue, 0);
				
				if(transfer->type == WD_TRANSFER_DOWNLOAD) {
					queue = ((total_downloads > 0 && active_downloads >= total_downloads) ||
							 (user_downloads > 0 && wd_user_downloads(transfer->user) >= user_downloads));
				} else {
					queue = ((total_uploads > 0 && active_uploads >= total_uploads) ||
							 (user_uploads > 0 && wd_user_uploads(transfer->user) >= user_uploads));
				}
				
				if(queue) {
					if(transfer->queue != position) {
						transfer->queue = position;
						
						wd_user_lock_socket(transfer->user);
						wd_sreply(wd_user_socket(transfer->user), 401, WI_STR("%#@%c%u"),
								  transfer->path,	WD_FIELD_SEPARATOR,
								  transfer->queue);
						wd_user_unlock_socket(transfer->user);
					}

					position++;
				} else {
					transfer->queue = 0;
					transfer->waiting_time = wi_time_interval();
					
					wd_transfer_set_state(transfer, WD_TRANSFER_WAITING);
					
					if(wd_transfer_open(transfer)) {
						wd_transfer_create_timer(transfer);
						
						wd_user_lock_socket(transfer->user);
						wd_sreply(wd_user_socket(transfer->user), 400, WI_STR("%#@%c%llu%c%#@"),
								  transfer->path,		WD_FIELD_SEPARATOR,
								  transfer->offset,		WD_FIELD_SEPARATOR,
								  transfer->hash);
						wd_user_unlock_socket(transfer->user);
					} else {
						wd_user_lock_socket(transfer->user);
						wd_sreply(wd_user_socket(transfer->user), 500, WI_STR("Command Failed"));
						wd_user_unlock_socket(transfer->user);
						
						wi_mutable_array_add_data(failed_transfers, transfer);
					}
				}
				
				wi_mutable_array_remove_data_at_index(transfers_queue, 0);
				
				if(wi_array_count(transfers_queue) == 0) {
					wi_mutable_array_remove_data_at_index(sorted_users, i);
					
					i--;
					count--;
				}
			}
		}
	}
	
	wi_mutable_array_remove_data_in_array(wd_transfers, failed_transfers);
	wi_array_unlock(wd_transfers);
	
	wi_release(users);
	wi_release(failed_transfers);
}