Example #1
0
void wr_chats_init(void) {
	wr_chat_runtime_id = wi_runtime_register_class(&wr_chat_runtime_class);

	wr_chats = wi_dictionary_init(wi_mutable_dictionary_alloc());
	
	wr_public_chat = wr_chat_init_public_chat(wr_chat_alloc());
	wr_chats_add_chat(wr_public_chat);
}
Example #2
0
void wi_dictionary_initialize(void) {
    _wi_dictionary_buckets_per_page = wi_page_size() / sizeof(_wi_dictionary_bucket_t);

#ifndef _WI_DICTIONARY_USE_QSORT_R
    _wi_dictionary_sort_lock = wi_lock_init(wi_lock_alloc());
#endif

    _wi_dictionary0 = wi_dictionary_init(wi_dictionary_alloc());
}
Example #3
0
wi_dictionary_t * wi_thread_dictionary(void) {
#ifdef WI_PTHREADS
	wi_dictionary_t			*dictionary;
	
	dictionary = pthread_getspecific(_wi_thread_dictionary_key);
	
	if(!dictionary) {
		dictionary = wi_dictionary_init(wi_dictionary_alloc());
		
		pthread_setspecific(_wi_thread_dictionary_key, dictionary);
	}
	
	return dictionary;
#else
	if(!_wi_thread_dictionary)
		_wi_thread_dictionary = wi_dictionary_init(wi_dictionary_alloc());
	
	return _wi_thread_dictionary;
#endif
}
Example #4
0
void wd_users_init(void) {
	wd_user_runtime_id = wi_runtime_register_class(&wd_user_runtime_class);

	wd_users = wi_dictionary_init(wi_dictionary_alloc());
	
	wd_users_uid_lock = wi_lock_init(wi_lock_alloc());
		
	wd_users_timer = wi_timer_init_with_function(wi_timer_alloc(),
												 wd_users_update_idle,
												 WD_USERS_TIMER_INTERVAL,
												 true);
}
Example #5
0
void wt_servers_init(void) {
	wt_server_runtime_id = wi_runtime_register_class(&wt_server_runtime_class);

	wt_servers = wi_dictionary_init(wi_dictionary_alloc());
	
	wt_servers_lock = wi_lock_init(wi_lock_alloc());

	wt_servers_timer = wi_timer_init_with_function(wi_timer_alloc(),
												   wt_update_servers,
												   WT_SERVERS_UPDATE_INTERVAL,
												   true);
}
Example #6
0
wi_mutable_dictionary_t * wi_mutable_dictionary_with_data_and_keys(void *data0, void *key0, ...) {
    wi_dictionary_t     *dictionary;
    void                *data, *key;
    va_list             ap;

    dictionary = wi_dictionary_init(wi_mutable_dictionary_alloc());

    _wi_dictionary_set_data_for_key(dictionary, data0, key0);

    va_start(ap, key0);
    while((data = va_arg(ap, void *))) {
        key = va_arg(ap, void *);

        _wi_dictionary_set_data_for_key(dictionary, data, key);
    }
    va_end(ap);

    return wi_autorelease(dictionary);
}
Example #7
0
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;
}
Example #8
0
wi_dictionary_t * wi_p7_message_fields(wi_p7_message_t *p7_message) {
	wi_p7_spec_field_t			*field;
	wi_mutable_dictionary_t		*fields;
	wi_string_t					*field_name, *field_value;
	unsigned char				*buffer, *start;
	uint32_t					message_size, field_id, field_size;
	
	fields = wi_dictionary_init(wi_mutable_dictionary_alloc());

	message_size = p7_message->binary_size - WI_P7_MESSAGE_BINARY_HEADER_SIZE;
	buffer = start = p7_message->binary_buffer + WI_P7_MESSAGE_BINARY_HEADER_SIZE;
	
	while((uint32_t) (buffer - start) < message_size) {
		field_id	= wi_read_swap_big_to_host_int32(buffer, 0);
		buffer		+= sizeof(field_id);
		field		= wi_p7_spec_field_with_id(p7_message->spec, field_id);

		if(!field)
			continue;
		
		field_size	= wi_p7_spec_field_size(field);
		
		if(field_size == 0) {
			field_size = wi_read_swap_big_to_host_int32(buffer, 0);
			
			buffer += sizeof(field_size);
		}
		
		field_name		= wi_p7_spec_field_name(field);
		field_value		= _wi_p7_message_field_string_value(p7_message, field);

		wi_mutable_dictionary_set_data_for_key(fields, field_value, field_name);

		buffer += field_size;
	}
	
	return wi_autorelease(fields);
}
Example #9
0
wi_mutable_dictionary_t * wi_mutable_dictionary(void) {
    return wi_autorelease(wi_dictionary_init(wi_mutable_dictionary_alloc()));
}
Example #10
0
static void wd_transfers_queue_thread(wi_runtime_instance_t *argument) {
	wi_pool_t					*pool;
	wi_enumerator_t				*enumerator;
	wi_mutable_dictionary_t		*key_queues;
	wi_mutable_array_t			*key_queue, *keys;
	wi_string_t					*key;
	wd_transfer_t				*transfer;
	wd_account_t				*account;
	wi_uinteger_t				user_downloads, user_uploads, user_transfers;
	wi_uinteger_t				new_position, position, queue, i, count, longest_queue;
	
	pool = wi_pool_init(wi_pool_alloc());
	
	key_queues = wi_dictionary_init(wi_mutable_dictionary_alloc());
	
	while(true) {
		wi_mutable_dictionary_remove_all_data(key_queues);

		wi_condition_lock_lock_when_condition(wd_transfers_queue_lock, 1, 0.0);
		wi_array_rdlock(wd_transfers);
		
		longest_queue	= 0;
		enumerator		= wi_array_data_enumerator(wd_transfers);
		
		while((transfer = wi_enumerator_next_data(enumerator))) {
			wi_condition_lock_lock(transfer->queue_lock);
			
			if(transfer->state == WD_TRANSFER_QUEUED && transfer->queue != 0) {
				key_queue = wi_dictionary_data_for_key(key_queues, transfer->key);
				
				if(!key_queue) {
					key_queue = wi_mutable_array();
					
					wi_mutable_dictionary_set_data_for_key(key_queues, key_queue, transfer->key);
				}
				
				wi_mutable_array_add_data(key_queue, transfer);
				
				if(wi_array_count(key_queue) > longest_queue)
					longest_queue = wi_array_count(key_queue);
			}
			
			wi_condition_lock_unlock(transfer->queue_lock);
		}
		
		keys		= wi_autorelease(wi_mutable_copy(wi_dictionary_keys_sorted_by_value(key_queues, wd_transfers_queue_compare)));
		position	= 1;
		count		= wi_array_count(keys);
		
		while(longest_queue > 0) {
			for(i = 0; i < count; i++) {
				key			= WI_ARRAY(keys, i);
				key_queue	= wi_dictionary_data_for_key(key_queues, key);
				
				if(wi_array_count(key_queue) > 0) {
					transfer	= WI_ARRAY(key_queue, 0);
					account		= wd_user_account(transfer->user);

					wi_lock_lock(wd_transfers_status_lock);
					
					if(transfer->type == WD_TRANSFER_DOWNLOAD) {
						wi_dictionary_rdlock(wd_transfers_user_downloads);
						
						user_downloads	= wd_account_transfer_download_limit(account);
						user_transfers	= (wi_integer_t) wi_dictionary_data_for_key(wd_transfers_user_downloads, transfer->key);
						queue			= ((wd_transfers_total_downloads > 0 && wd_transfers_active_downloads >= wd_transfers_total_downloads) ||
										   (user_downloads > 0 && user_transfers >= user_downloads));

						wi_dictionary_unlock(wd_transfers_user_downloads);
					} else {
						wi_dictionary_rdlock(wd_transfers_user_uploads);
						
						user_uploads	= wd_account_transfer_upload_limit(account);
						user_transfers	= (wi_integer_t) wi_dictionary_data_for_key(wd_transfers_user_uploads, transfer->key);
						queue			= ((wd_transfers_total_uploads > 0 && wd_transfers_active_uploads >= wd_transfers_total_uploads) ||
										   (user_uploads > 0 && user_transfers >= user_uploads));

						wi_dictionary_unlock(wd_transfers_user_uploads);
					}
					
					wi_lock_unlock(wd_transfers_status_lock);
					
					if(queue)
						new_position = position++;
					else
						new_position = 0;
					
					if(new_position != (wi_uinteger_t) transfer->queue) {
						if(new_position == 0)
							wd_transfers_add_or_remove_transfer(transfer, true);
						
						wi_condition_lock_lock(transfer->queue_lock);
						transfer->queue = new_position;
						wi_condition_lock_unlock_with_condition(transfer->queue_lock, 1);
					}
					
					wi_mutable_array_remove_data_at_index(key_queue, 0);
				}
			}
				
			longest_queue--;
		}
		
		wi_array_unlock(wd_transfers);
		wi_condition_lock_unlock_with_condition(wd_transfers_queue_lock, 0);
		
		wi_pool_drain(pool);
	}
	
	wi_release(key_queues);
	wi_release(pool);
}
Example #11
0
static wi_boolean_t _wi_plist_read_node_to_instance(xmlNodePtr content_node, wi_runtime_instance_t *collection) {
	xmlNodePtr				node;
	wi_string_t				*name, *key = NULL;
	wi_runtime_instance_t	*instance = NULL;
	wi_boolean_t			dictionary;
	
	dictionary = (wi_runtime_id(collection) == wi_dictionary_runtime_id());
	
	for(node = content_node->children; node != NULL; node = node->next) {
		if(node->type == XML_ELEMENT_NODE) {
			name = wi_xml_node_name(node);
			
			if(wi_is_equal(name, WI_STR("key")))
				key = wi_xml_node_content(node);
			else if(wi_is_equal(name, WI_STR("string")))
				instance = wi_xml_node_content(node);
			else if(wi_is_equal(name, WI_STR("integer")))
				instance = wi_number_with_integer(wi_string_integer(wi_xml_node_content(node)));
			else if(wi_is_equal(name, WI_STR("real")))
				instance = wi_number_with_double(wi_string_double(wi_xml_node_content(node)));
			else if(wi_is_equal(name, WI_STR("true")))
				instance = wi_number_with_bool(true);
			else if(wi_is_equal(name, WI_STR("false")))
				instance = wi_number_with_bool(false);
			else if(wi_is_equal(name, WI_STR("date")))
				instance = wi_date_with_rfc3339_string(wi_xml_node_content(node));
			else if(wi_is_equal(name, WI_STR("data")))
				instance = wi_data_with_base64(wi_xml_node_content(node));
			else if(wi_is_equal(name, WI_STR("dict"))) {
				instance = wi_autorelease(wi_dictionary_init(wi_dictionary_alloc()));
				
				if(!_wi_plist_read_node_to_instance(node, instance))
					return false;
			}
			else if(wi_is_equal(name, WI_STR("array"))) {
				instance = wi_autorelease(wi_array_init(wi_array_alloc()));
				
				if(!_wi_plist_read_node_to_instance(node, instance))
					return false;
			}
			else {
				wi_error_set_libwired_error_with_format(WI_ERROR_PLIST_READFAILED,
					WI_STR("Unhandled node \"%@\""), name);
				
				return false;
			}
		}
			
		if(instance) {
			if(dictionary)
				wi_dictionary_set_data_for_key(collection, instance, key);
			else
				wi_array_add_data(collection, instance);
			
			instance = NULL;
			key = NULL;
		}
	}
	
	return true;
}
Example #12
0
wi_boolean_t wi_config_read_file(wi_config_t *config) {
	wi_enumerator_t				*enumerator;
	wi_runtime_instance_t		*instance;
	wi_file_t					*file;
	wi_mutable_array_t			*array;
	wi_mutable_dictionary_t		*previous_values;
	wi_string_t					*string, *name, *value;
	wi_config_type_t			type;
	
	file = wi_file_for_reading(config->path);
	
	if(!file) {
		wi_log_err(WI_STR("Could not open %@: %m"),
			config->path);
		
		return false;
	}
	
	wi_log_info(WI_STR("Reading %@"), config->path);
	
	config->line = 0;
	
	wi_lock_lock(config->lock);
	
	previous_values = config->values;
	
	config->values = wi_dictionary_init(wi_mutable_dictionary_alloc());
	
	if(config->defaults) {
		enumerator = wi_dictionary_key_enumerator(config->defaults);
		
		while((name = wi_enumerator_next_data(enumerator))) {
//			instance = wi_mutable_copy(wi_dictionary_data_for_key(config->defaults, name));
			instance = wi_dictionary_data_for_key(config->defaults, name);
			if(wi_runtime_id(instance) == wi_array_runtime_id())
				instance = wi_autorelease(wi_mutable_copy(instance));
			wi_mutable_dictionary_set_data_for_key(config->values, instance, name);
//			wi_release(instance);
		}
	}
	
	while((string = wi_file_read_line(file))) {
		config->line++;

		if(wi_string_length(string) > 0 && !wi_string_has_prefix(string, WI_STR("#"))) {
			if(_wi_config_parse_string(config, string, &name, &value)) {
				instance = _wi_config_instance_for_setting(config, name, value, &type);
				
				if(instance) {
					wi_log_debug(WI_STR("  %@ = %@"), name, value);
					
					if(type == WI_CONFIG_STRINGLIST) {
						array = wi_dictionary_data_for_key(config->values, name);
						
						if(!array) {
							array = wi_mutable_array();
							
							wi_mutable_dictionary_set_data_for_key(config->values, array, name);
						}
						
						wi_mutable_array_add_data(array, instance);
					} else {
						wi_mutable_dictionary_set_data_for_key(config->values, instance, name);
					}
				} else {
					_wi_config_log_error(config, name);
				}
			} else {
				wi_error_set_libwired_error(WI_ERROR_SETTINGS_SYNTAXERROR);
				
				_wi_config_log_error(config, string);
			}
		}
	}
	
	enumerator = wi_dictionary_key_enumerator(config->values);
	
	while((name = wi_enumerator_next_data(enumerator))) {
		instance = wi_dictionary_data_for_key(config->values, name);
		
		if(!previous_values || !wi_is_equal(instance, wi_dictionary_data_for_key(previous_values, name)))
			wi_mutable_set_add_data(config->changes, name);
	}
	
	wi_release(previous_values);

	wi_lock_unlock(config->lock);

	return true;
}
Example #13
0
void wd_banlist_init(void) {
	wd_tempban_runtime_id = wi_runtime_register_class(&wd_tempban_runtime_class);

	wd_tempbans = wi_dictionary_init(wi_mutable_dictionary_alloc());
}
Example #14
0
void wr_commands_initialize(void) {
	wr_commands_transactions = wi_dictionary_init(wi_mutable_dictionary_alloc());
}
Example #15
0
wr_chat_t * wr_chat_init(wr_chat_t *chat) {
	chat->users_array			= wi_array_init(wi_mutable_array_alloc());
	chat->users_dictionary		= wi_dictionary_init(wi_mutable_dictionary_alloc());
	
	return chat;
}