/soc/2012/tomkiewicz/gg: 140aa3ad4cae: Gadu-Gadu: cancellable gg...
Tomasz Wasilczyk
tomkiewicz at cpw.pidgin.im
Thu Jul 5 13:31:55 EDT 2012
Changeset: 140aa3ad4cae89b4e9a6e5fd92fdfa1a8d1a9608
Author: Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date: 2012-06-30 19:56 +0200
Branch: soc.2012.gg
URL: http://hg.pidgin.im/soc/2012/tomkiewicz/gg/rev/140aa3ad4cae
Description:
Gadu-Gadu: cancellable gg_http watcher
diffstat:
libpurple/protocols/gg/account.c | 85 +++++++++++++-------------------------
libpurple/protocols/gg/libgaduw.c | 72 ++++++++++++++++++++++----------
libpurple/protocols/gg/libgaduw.h | 11 +++-
3 files changed, 88 insertions(+), 80 deletions(-)
diffs (282 lines):
diff --git a/libpurple/protocols/gg/account.c b/libpurple/protocols/gg/account.c
--- a/libpurple/protocols/gg/account.c
+++ b/libpurple/protocols/gg/account.c
@@ -19,16 +19,10 @@
PurpleConnection *gc;
void *user_data;
- gboolean cancelled;
ggp_libgaduw_http_req *req;
- ggp_purplew_request_processing_handle *req_processing;
} ggp_account_token_reqdata;
-static void ggp_account_token_response(struct gg_http *h, gboolean success, void *_reqdata);
-
-static void ggp_account_token_finish(ggp_account_token_reqdata *reqdata, ggp_account_token *token);
-
-static void ggp_account_token_request_cancel(PurpleConnection *gc, void *_reqdata);
+static void ggp_account_token_response(struct gg_http *h, gboolean success, gboolean cancelled, gpointer _reqdata);
/******************************************************************************/
@@ -43,14 +37,16 @@
{
struct gg_http *h;
ggp_account_token_reqdata *reqdata;
-
+
+ purple_debug_info("gg", "ggp_account_token_request: requesting token...\n");
+
if (!ggp_deprecated_setup_proxy(gc))
{
callback(gc, NULL, user_data);
return;
}
- h = gg_token(1);
+ h = gg_token(TRUE);
if (!h)
{
@@ -62,63 +58,42 @@
reqdata->callback = callback;
reqdata->gc = gc;
reqdata->user_data = user_data;
- reqdata->cancelled = FALSE;
- reqdata->req_processing = ggp_purplew_request_processing(gc, NULL, reqdata, ggp_account_token_request_cancel);
- reqdata->req = ggp_libgaduw_http_watch(h, ggp_account_token_response, reqdata);
+ reqdata->req = ggp_libgaduw_http_watch(gc, h, ggp_account_token_response, reqdata, TRUE);
}
-static void ggp_account_token_request_cancel(PurpleConnection *gc, void *_reqdata)
-{
- ggp_account_token_reqdata *reqdata = _reqdata;
-
- purple_debug_info("gg", "ggp_account_token_request_cancel\n");
- reqdata->cancelled = TRUE;
- reqdata->req_processing = NULL;
- ggp_libgaduw_http_cancel(reqdata->req);
-}
-
-static void ggp_account_token_response(struct gg_http *h, gboolean success, void *_reqdata)
+static void ggp_account_token_response(struct gg_http *h, gboolean success,
+ gboolean cancelled, gpointer _reqdata)
{
ggp_account_token_reqdata *reqdata = _reqdata;
struct gg_token *token_info;
- ggp_account_token *token;
+ ggp_account_token *token = NULL;
- if (!success)
+ g_assert(!(success && cancelled));
+
+ if (cancelled)
+ purple_debug_info("gg", "ggp_account_token_handler: cancelled\n");
+ else if (success)
{
- gg_token_free(h);
- ggp_account_token_finish(reqdata, NULL);
- return;
+ purple_debug_info("gg", "ggp_account_token_handler: got token\n");
+
+ token = g_new(ggp_account_token, 1);
+
+ token_info = h->data;
+ token->id = g_strdup(token_info->tokenid);
+ token->size = h->body_size;
+ token->data = g_memdup(h->body, token->size);
+ }
+ else
+ {
+ purple_debug_error("gg", "ggp_account_token_handler: error\n");
+ purple_notify_error(
+ purple_connection_get_account(reqdata->gc),
+ _("Token Error"),
+ _("Unable to fetch the token.\n"), NULL);
}
- purple_debug_info("gg", "ggp_account_token_handler: got token\n");
-
- token = g_new(ggp_account_token, 1);
-
- token_info = h->data;
- token->id = g_strdup(token_info->tokenid);
- token->size = h->body_size;
- token->data = g_memdup(h->body, token->size);
gg_token_free(h);
-
- ggp_account_token_finish(reqdata, token);
-}
-
-static void ggp_account_token_finish(ggp_account_token_reqdata *reqdata, ggp_account_token *token)
-{
- g_assert(!reqdata->cancelled || !token); // cancelled => not token
-
- if (reqdata->req_processing)
- {
- ggp_purplew_request_processing_done(reqdata->req_processing);
- reqdata->req_processing = NULL;
- }
reqdata->callback(reqdata->gc, token, reqdata->user_data);
-
- if (!token && !reqdata->cancelled)
- purple_notify_error(purple_connection_get_account(reqdata->gc),
- _("Token Error"),
- _("Unable to fetch the token.\n"), NULL);
-
g_free(reqdata);
}
diff --git a/libpurple/protocols/gg/libgaduw.c b/libpurple/protocols/gg/libgaduw.c
--- a/libpurple/protocols/gg/libgaduw.c
+++ b/libpurple/protocols/gg/libgaduw.c
@@ -8,29 +8,48 @@
* HTTP requests.
******************************************************************************/
+static void ggp_libgaduw_http_processing_cancel(PurpleConnection *gc,
+ void *_req);
+
static void ggp_libgaduw_http_handler(gpointer _req, gint fd,
PurpleInputCondition cond);
-static void ggp_libgaduw_http_cb_call(ggp_libgaduw_http_req *req,
+static void ggp_libgaduw_http_finish(ggp_libgaduw_http_req *req,
gboolean success);
/******************************************************************************/
-ggp_libgaduw_http_req * ggp_libgaduw_http_watch(struct gg_http *h,
- ggp_libgaduw_http_cb cb, gpointer user_data)
+ggp_libgaduw_http_req * ggp_libgaduw_http_watch(PurpleConnection *gc,
+ struct gg_http *h, ggp_libgaduw_http_cb cb,
+ gpointer user_data, gboolean show_processing)
{
- ggp_libgaduw_http_req *req = g_new(ggp_libgaduw_http_req, 1);
- purple_debug_info("gg", "ggp_libgaduw_http_watch\n");
+ ggp_libgaduw_http_req *req;
+ purple_debug_misc("gg", "ggp_libgaduw_http_watch(h=%x, show_processing=%d)\n",
+ (unsigned int)h, show_processing);
+ req = g_new(ggp_libgaduw_http_req, 1);
req->user_data = user_data;
req->cb = cb;
+ req->cancelled = FALSE;
req->h = h;
- req->inpa = purple_input_add(h->fd, PURPLE_INPUT_READ,
- ggp_libgaduw_http_handler, req);
+ req->processing = NULL;
+ if (show_processing)
+ req->processing = ggp_purplew_request_processing(gc, NULL,
+ req, ggp_libgaduw_http_processing_cancel);
+ req->inpa = ggp_purplew_http_input_add(h, ggp_libgaduw_http_handler,
+ req);
return req;
}
+static void ggp_libgaduw_http_processing_cancel(PurpleConnection *gc,
+ void *_req)
+{
+ ggp_libgaduw_http_req *req = _req;
+ req->processing = NULL;
+ ggp_libgaduw_http_cancel(req);
+}
+
static void ggp_libgaduw_http_handler(gpointer _req, gint fd,
PurpleInputCondition cond)
{
@@ -40,13 +59,13 @@
{
purple_debug_error("gg", "ggp_libgaduw_http_handler: failed to "
"make http request: %d\n", req->h->error);
- ggp_libgaduw_http_cb_call(req, FALSE);
+ ggp_libgaduw_http_finish(req, FALSE);
return;
}
//TODO: verbose mode
- purple_debug_misc("gg", "ggp_libgaduw_http_handler: got fd update "
- "[check=%d, state=%d]\n", req->h->check, req->h->state);
+ //purple_debug_misc("gg", "ggp_libgaduw_http_handler: got fd update "
+ // "[check=%d, state=%d]\n", req->h->check, req->h->state);
if (req->h->state != GG_STATE_DONE)
{
@@ -60,24 +79,33 @@
{
purple_debug_error("gg", "ggp_libgaduw_http_handler: got empty "
"http response: %d\n", req->h->error);
- ggp_libgaduw_http_cb_call(req, FALSE);
+ ggp_libgaduw_http_finish(req, FALSE);
return;
}
- ggp_libgaduw_http_cb_call(req, TRUE);
-}
-
-static void ggp_libgaduw_http_cb_call(ggp_libgaduw_http_req *req,
- gboolean success)
-{
- purple_input_remove(req->inpa);
- req->cb(req->h, success, req->user_data);
- g_free(req);
+ ggp_libgaduw_http_finish(req, TRUE);
}
void ggp_libgaduw_http_cancel(ggp_libgaduw_http_req *req)
{
- purple_debug_info("gg", "ggp_libgaduw_http_cancel\n");
+ purple_debug_misc("gg", "ggp_libgaduw_http_cancel\n");
+ req->cancelled = TRUE;
gg_http_stop(req->h);
- ggp_libgaduw_http_cb_call(req, FALSE);
+ ggp_libgaduw_http_finish(req, FALSE);
}
+
+static void ggp_libgaduw_http_finish(ggp_libgaduw_http_req *req,
+ gboolean success)
+{
+ purple_debug_misc("gg", "ggp_libgaduw_http_finish(h=%x, processing=%x):"
+ " success=%d\n", (unsigned int)req->h,
+ (unsigned int)req->processing, success);
+ if (req->processing)
+ {
+ ggp_purplew_request_processing_done(req->processing);
+ req->processing = NULL;
+ }
+ purple_input_remove(req->inpa);
+ req->cb(req->h, success, req->cancelled, req->user_data);
+ g_free(req);
+}
diff --git a/libpurple/protocols/gg/libgaduw.h b/libpurple/protocols/gg/libgaduw.h
--- a/libpurple/protocols/gg/libgaduw.h
+++ b/libpurple/protocols/gg/libgaduw.h
@@ -4,19 +4,24 @@
#include <internal.h>
#include <libgadu.h>
-typedef void (*ggp_libgaduw_http_cb)(struct gg_http *h, gboolean success, gpointer user_data);
+#include "purplew.h"
+
+typedef void (*ggp_libgaduw_http_cb)(struct gg_http *h, gboolean success,
+ gboolean cancelled, gpointer user_data);
typedef struct
{
gpointer user_data;
ggp_libgaduw_http_cb cb;
+ gboolean cancelled;
struct gg_http *h;
+ ggp_purplew_request_processing_handle *processing;
guint inpa;
} ggp_libgaduw_http_req;
-ggp_libgaduw_http_req * ggp_libgaduw_http_watch(struct gg_http *h,
- ggp_libgaduw_http_cb cb, gpointer user_data);
+ggp_libgaduw_http_req * ggp_libgaduw_http_watch(PurpleConnection *gc,
+ struct gg_http *h, ggp_libgaduw_http_cb cb, gpointer user_data, gboolean show_processing);
void ggp_libgaduw_http_cancel(ggp_libgaduw_http_req *req);
More information about the Commits
mailing list