コード例 #1
0
ファイル: mpd-async-request.c プロジェクト: emillon/gmpc
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);
}
コード例 #2
0
static gboolean
dmap_connection_do_something (DMAPConnection * connection)
{
	DMAPConnectionPrivate *priv = connection->priv;
	char *meta;
	char *path;

	g_debug ("Doing something for state: %d", priv->state);

	priv->do_something_id = 0;

	switch (priv->state) {
	case DMAP_GET_INFO:
		g_debug ("Getting DAAP server info");
		if (!http_get
		    (connection, "/server-info", FALSE, 0.0, 0, FALSE,
		     (DMAPResponseHandler) handle_server_info, NULL, FALSE)) {
			g_debug ("Could not get DAAP connection info");
			dmap_connection_state_done (connection, FALSE);
		}
		break;

	case DMAP_GET_PASSWORD:
		if (priv->password_protected) {
			/* FIXME this bit is still synchronous */
			g_debug ("Need a password for %s", priv->name);
			g_free (priv->password);
			priv->password = connection_get_password (connection);

			if (priv->password == NULL
			    || priv->password[0] == '\0') {
				g_debug ("Password entry cancelled");
				priv->result = FALSE;
				priv->state = DMAP_DONE;
				dmap_connection_do_something (connection);
				return FALSE;
			}

			/* If the share went away while we were asking for the password,
			 * don't bother trying to log in.
			 */
			if (priv->state != DMAP_GET_PASSWORD) {
				return FALSE;
			}
		}

		/* otherwise, fall through */
		priv->state = DMAP_LOGIN;

	case DMAP_LOGIN:
		g_debug ("Logging into DAAP server");
		if (!http_get (connection, "/login", FALSE, 0.0, 0, FALSE,
			       (DMAPResponseHandler) handle_login, NULL,
			       FALSE)) {
			g_debug ("Could not login to DAAP server");
			/* FIXME: set state back to GET_PASSWORD to try again */
			dmap_connection_state_done (connection, FALSE);
		}

		break;

	case DMAP_GET_REVISION_NUMBER:
		g_debug ("Getting DAAP server database revision number");
		path = g_strdup_printf
			("/update?session-id=%u&revision-number=1",
			 priv->session_id);
		if (!http_get
		    (connection, path, TRUE, priv->dmap_version, 0, FALSE,
		     (DMAPResponseHandler) handle_update, NULL, FALSE)) {
			g_debug ("Could not get server database revision number");
			dmap_connection_state_done (connection, FALSE);
		}
		g_free (path);
		break;

	case DMAP_GET_DB_INFO:
		g_debug ("Getting DAAP database info");
		path = g_strdup_printf
			("/databases?session-id=%u&revision-number=%d",
			 priv->session_id, priv->revision_number);
		if (!http_get
		    (connection, path, TRUE, priv->dmap_version, 0, FALSE,
		     (DMAPResponseHandler) handle_database_info, NULL,
		     FALSE)) {
			g_debug ("Could not get DAAP database info");
			dmap_connection_state_done (connection, FALSE);
		}
		g_free (path);
		break;

	case DMAP_GET_SONGS:
		g_debug ("Getting DAAP song listing");
		meta = DMAP_CONNECTION_GET_CLASS
			(connection)->get_query_metadata (connection);
		path = g_strdup_printf
			("/databases/%i/items?session-id=%u&revision-number=%i"
			 "&meta=%s", priv->database_id, priv->session_id,
			 priv->revision_number, meta);
		if (!http_get
		    (connection, path, TRUE, priv->dmap_version, 0, FALSE,
		     (DMAPResponseHandler) handle_song_listing, NULL, TRUE)) {
			g_debug ("Could not get DAAP song listing");
			dmap_connection_state_done (connection, FALSE);
		}
		g_free (path);
		g_free (meta);
		break;

	case DMAP_GET_PLAYLISTS:
		g_debug ("Getting DAAP playlists");
		path = g_strdup_printf
			("/databases/%d/containers?session-id=%u&revision-number=%d",
			 priv->database_id, priv->session_id,
			 priv->revision_number);
		if (!http_get
		    (connection, path, TRUE, priv->dmap_version, 0, FALSE,
		     (DMAPResponseHandler) handle_playlists, NULL, TRUE)) {
			g_debug ("Could not get DAAP playlists");
			dmap_connection_state_done (connection, FALSE);
		}
		g_free (path);
		break;

	case DMAP_GET_PLAYLIST_ENTRIES:
		{
			DMAPPlaylist *playlist =
				(DMAPPlaylist *)
				g_slist_nth_data (priv->playlists,
						  priv->reading_playlist);

			g_assert (playlist);
			g_debug ("Reading DAAP playlist %d entries",
				 priv->reading_playlist);
			path = g_strdup_printf
				("/databases/%d/containers/%d/items?session-id=%u&revision-number=%d&meta=dmap.itemid",
				 priv->database_id, playlist->id,
				 priv->session_id, priv->revision_number);
			if (!http_get
			    (connection, path, TRUE, priv->dmap_version, 0,
			     FALSE,
			     (DMAPResponseHandler) handle_playlist_entries,
			     NULL, TRUE)) {
				g_debug ("Could not get entries for DAAP playlist %d", priv->reading_playlist);
				dmap_connection_state_done (connection,
							    FALSE);
			}
			g_free (path);
		}
		break;

	case DMAP_LOGOUT:
		g_debug ("Logging out of DAAP server");
		path = g_strdup_printf ("/logout?session-id=%u",
					priv->session_id);
		if (!http_get
		    (connection, path, TRUE, priv->dmap_version, 0, FALSE,
		     (DMAPResponseHandler) handle_logout, NULL, FALSE)) {
			g_debug ("Could not log out of DAAP server");
			dmap_connection_state_done (connection, FALSE);
		}

		g_free (path);
		break;

	case DMAP_DONE:
		g_debug ("DAAP done");

		dmap_connection_finish (connection);

		break;
	}

	return FALSE;
}