Esempio n. 1
0
void mpd_async_request(MpdAsyncCallback * callback, gpointer callback_data, MpdAsyncFunction * function,
					   gpointer function_data)
{
	gchar *string = NULL;
	GSimpleAsyncResult *res = NULL;
	MpdAsyncData *data = g_new0(MpdAsyncData, 1);
	data->mi = mpd_new_default();
	data->function = function;
	data->function_data = function_data;
	data->callback = callback;
	data->callback_data = callback_data;
	/**
     * Set Hostname
     */
	string = connection_get_hostname();
	mpd_set_hostname(data->mi, string);
	/**
	 * Set port
	 */
	mpd_set_port(data->mi, connection_get_port());
	/**
	 * Timeout
	 */
	mpd_set_connection_timeout(data->mi,
							   cfg_get_single_value_as_float_with_default(config, "connection", "timeout",
																		  DEFAULT_TIMEOUT));

	if (connection_use_auth())
	{
		string = connection_get_password();
		mpd_set_password(data->mi, string);
	} else
	{
		mpd_set_password(data->mi, "");
	}

	res = g_simple_async_result_new(NULL, __mpd_async_simple_callback, NULL, mpd_async_request);
	g_simple_async_result_set_op_res_gpointer(G_SIMPLE_ASYNC_RESULT(res), data, __mpd_async_result_free_data);
	g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Start async thread %p\n", data);
	g_simple_async_result_run_in_thread(res, __async_handler_function, G_PRIORITY_LOW, NULL);
}
Esempio n. 2
0
MpdObj *mpd_new(char *hostname,  int port, char *password)
{
	MpdObj *mi = mpd_create();
	if(mi == NULL)
	{
		return NULL;
	}
	if(hostname != NULL)
	{
		mpd_set_hostname(mi, hostname);
	}
	if(port != 0)
	{
		mpd_set_port(mi, port);
	}
	if(password != NULL)
	{
		mpd_set_password(mi, password);
	}
	return mi;
}
Esempio n. 3
0
int mpd_connect_real(MpdObj *mi,mpd_Connection *connection)
{
    int retv;
    if(mi == NULL)
	{
		/* should return some spiffy error here */
		debug_printf(DEBUG_ERROR, "mi != NULL failed");
		return MPD_ARGS_ERROR;
	}
	/* reset errors */
	mi->error = 0;
	mi->error_mpd_code = 0;
	if(mi->error_msg != NULL)
	{
		free(mi->error_msg);
	}
	mi->error_msg = NULL;

	debug_printf(DEBUG_INFO, "connecting\n");
	mpd_init_MpdServerState(&(mi->CurrentState));

	memcpy(&(mi->OldState), &(mi->CurrentState), sizeof(MpdServerState));

	if(mi->connected)
	{
		/* disconnect */
		mpd_disconnect(mi);
	}

	if(mi->hostname == NULL)
	{
		mpd_set_hostname(mi, "localhost");
	}
	/* make sure this is locked */
	if(!mi->connection_lock)
	{
		mpd_lock_conn(mi);
	}
	if(connection) {
		mi->connection = connection;
	} else {
		/* make timeout configurable */
		mi->connection = mpd_newConnection(mi->hostname,mi->port,mi->connection_timeout);
	}
	if(mi->connection == NULL)
	{
		/* TODO: make seperate error message? */
		return MPD_NOT_CONNECTED;
	}
	if(mpd_check_error(mi) != MPD_OK)
	{
		/* TODO: make seperate error message? */
		return MPD_NOT_CONNECTED;
	}

	/* set connected state */
	mi->connected = TRUE;
	if(mpd_unlock_conn(mi))
	{
		return MPD_LOCK_FAILED;
	}

	/* get the commands we are allowed to use */
	retv = mpd_server_get_allowed_commands(mi);
    if(retv!= MPD_OK)
    {
        return retv;
    }
    if(mi->the_connection_changed_callback != NULL)
	{
		mi->the_connection_changed_callback( mi, TRUE, mi->the_connection_changed_signal_userdata );
	}

	debug_printf(DEBUG_INFO, "Connected to mpd");
	return MPD_OK;
}