/soc/2012/tomkiewicz/gg: 45147502ceab: Gadu-Gadu: refactoring - ...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Thu Jul 5 13:31:52 EDT 2012


Changeset: 45147502ceabeb371dfbc438af9e01e793d8c7bb
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-06-30 13:04 +0200
Branch:	 soc.2012.gg
URL: http://hg.pidgin.im/soc/2012/tomkiewicz/gg/rev/45147502ceab

Description:

Gadu-Gadu: refactoring - gg_http watcher

diffstat:

 libpurple/protocols/gg/Makefile.am |   4 +-
 libpurple/protocols/gg/account.c   |  98 ++++++++++++++-----------------------
 libpurple/protocols/gg/libgaduw.c  |  83 ++++++++++++++++++++++++++++++++
 libpurple/protocols/gg/libgaduw.h  |  23 ++++++++
 4 files changed, 147 insertions(+), 61 deletions(-)

diffs (289 lines):

diff --git a/libpurple/protocols/gg/Makefile.am b/libpurple/protocols/gg/Makefile.am
--- a/libpurple/protocols/gg/Makefile.am
+++ b/libpurple/protocols/gg/Makefile.am
@@ -65,7 +65,9 @@
 	deprecated.h \
 	deprecated.c \
 	purplew.h \
-	purplew.c
+	purplew.c \
+	libgaduw.h \
+	libgaduw.c
 
 AM_CFLAGS = $(st)
 
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
@@ -7,6 +7,7 @@
 #include "deprecated.h"
 #include "purplew.h"
 #include "utils.h"
+#include "libgaduw.h"
 
 /*******************************************************************************
  * Token requesting.
@@ -17,16 +18,15 @@
 	ggp_account_token_cb callback;
 	PurpleConnection *gc;
 	void *user_data;
-	
-	struct gg_http *h;
+
+	gboolean cancelled;
+	ggp_libgaduw_http_req *req;
 	ggp_purplew_request_processing_handle *req_processing;
-	guint inpa;
 } ggp_account_token_reqdata;
 
-static void ggp_account_token_handler(gpointer _reqdata, gint fd, PurpleInputCondition cond);
+static void ggp_account_token_response(struct gg_http *h, gboolean success, void *_reqdata);
 
-/* user cancel isn't a failure */
-static void ggp_account_token_cb_call(ggp_account_token_reqdata *reqdata, ggp_account_token *token, gboolean failure);
+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);
 
@@ -39,26 +39,6 @@
 	g_free(token);
 }
 
-static void ggp_account_token_cb_call(ggp_account_token_reqdata *reqdata, ggp_account_token *token, gboolean failure)
-{
-	g_assert(!failure || !token); // failure => 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);
-	purple_input_remove(reqdata->inpa);
-	gg_token_free(reqdata->h);
-	g_free(reqdata);
-	
-	if (failure)
-		purple_notify_error(purple_connection_get_account(reqdata->gc),
-			_("Token Error"),
-			_("Unable to fetch the token.\n"), NULL);
-}
-
 void ggp_account_token_request(PurpleConnection *gc, ggp_account_token_cb callback, void *user_data)
 {
 	struct gg_http *h;
@@ -82,9 +62,9 @@
 	reqdata->callback = callback;
 	reqdata->gc = gc;
 	reqdata->user_data = user_data;
-	reqdata->h = h;
+	reqdata->cancelled = FALSE;
 	reqdata->req_processing = ggp_purplew_request_processing(gc, NULL, reqdata, ggp_account_token_request_cancel);
-	reqdata->inpa = purple_input_add(h->fd, PURPLE_INPUT_READ, ggp_account_token_handler, reqdata);
+	reqdata->req = ggp_libgaduw_http_watch(h, ggp_account_token_response, reqdata);
 }
 
 static void ggp_account_token_request_cancel(PurpleConnection *gc, void *_reqdata)
@@ -92,43 +72,21 @@
 	ggp_account_token_reqdata *reqdata = _reqdata;
 
 	purple_debug_info("gg", "ggp_account_token_request_cancel\n");
+	reqdata->cancelled = TRUE;
 	reqdata->req_processing = NULL;
-	gg_http_stop(reqdata->h);
-	ggp_account_token_cb_call(reqdata, NULL, FALSE);
+	ggp_libgaduw_http_cancel(reqdata->req);
 }
 
-static void ggp_account_token_handler(gpointer _reqdata, gint fd, PurpleInputCondition cond)
+static void ggp_account_token_response(struct gg_http *h, gboolean success, void *_reqdata)
 {
 	ggp_account_token_reqdata *reqdata = _reqdata;
 	struct gg_token *token_info;
 	ggp_account_token *token;
 	
-	//TODO: verbose mode
-	purple_debug_misc("gg", "ggp_account_token_handler: got fd update [check=%d, state=%d]\n",
-		reqdata->h->check, reqdata->h->state);
-	
-	if (gg_token_watch_fd(reqdata->h) == -1 || reqdata->h->state == GG_STATE_ERROR)
+	if (!success)
 	{
-		purple_debug_error("gg", "ggp_account_token_handler: failed to request token: %d\n",
-			reqdata->h->error);
-		ggp_account_token_cb_call(reqdata, NULL, TRUE);
-		return;
-	}
-	
-	if (reqdata->h->state != GG_STATE_DONE)
-	{
-		purple_input_remove(reqdata->inpa);
-		reqdata->inpa = purple_input_add(reqdata->h->fd,
-			(reqdata->h->check == 1) ? PURPLE_INPUT_WRITE : PURPLE_INPUT_READ,
-			ggp_account_token_handler, reqdata);
-		return;
-	}
-	
-	if (!reqdata->h->data || !reqdata->h->body)
-	{
-		purple_debug_error("gg", "ggp_account_token_handler: failed to fetch token: %d\n",
-			reqdata->h->error); // TODO: will anything be here in that case?
-		ggp_account_token_cb_call(reqdata, NULL, TRUE);
+		gg_token_free(h);
+		ggp_account_token_finish(reqdata, NULL);
 		return;
 	}
 	
@@ -136,12 +94,32 @@
 	
 	token = g_new(ggp_account_token, 1);
 	
-	token_info = reqdata->h->data;
+	token_info = h->data;
 	token->id = g_strdup(token_info->tokenid);
-	token->size = reqdata->h->body_size;
-	token->data = g_memdup(reqdata->h->body, token->size);
+	token->size = h->body_size;
+	token->data = g_memdup(h->body, token->size);
+	gg_token_free(h);
 	
-	ggp_account_token_cb_call(reqdata, token, FALSE);
+	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
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/gg/libgaduw.c
@@ -0,0 +1,83 @@
+#include "libgaduw.h"
+
+#include <debug.h>
+
+#include "purplew.h"
+
+/*******************************************************************************
+ * HTTP requests.
+ ******************************************************************************/
+
+static void ggp_libgaduw_http_handler(gpointer _req, gint fd,
+	PurpleInputCondition cond);
+
+static void ggp_libgaduw_http_cb_call(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 *req = g_new(ggp_libgaduw_http_req, 1);
+	purple_debug_info("gg", "ggp_libgaduw_http_watch\n");
+	
+	req->user_data = user_data;
+	req->cb = cb;
+	req->h = h;
+	req->inpa = purple_input_add(h->fd, PURPLE_INPUT_READ,
+		ggp_libgaduw_http_handler, req);
+	
+	return req;
+}
+
+static void ggp_libgaduw_http_handler(gpointer _req, gint fd,
+	PurpleInputCondition cond)
+{
+	ggp_libgaduw_http_req *req = _req;
+	
+	if (gg_token_watch_fd(req->h) == -1 || req->h->state == GG_STATE_ERROR)
+	{
+		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);
+		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);
+	
+	if (req->h->state != GG_STATE_DONE)
+	{
+		purple_input_remove(req->inpa);
+		req->inpa = ggp_purplew_http_input_add(req->h,
+			ggp_libgaduw_http_handler, req);
+		return;
+	}
+	
+	if (!req->h->data || !req->h->body)
+	{
+		purple_debug_error("gg", "ggp_libgaduw_http_handler: got empty "
+			"http response: %d\n", req->h->error);
+		ggp_libgaduw_http_cb_call(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);
+}
+
+void ggp_libgaduw_http_cancel(ggp_libgaduw_http_req *req)
+{
+	purple_debug_info("gg", "ggp_libgaduw_http_cancel\n");
+	gg_http_stop(req->h);
+	ggp_libgaduw_http_cb_call(req, FALSE);
+}
diff --git a/libpurple/protocols/gg/libgaduw.h b/libpurple/protocols/gg/libgaduw.h
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/gg/libgaduw.h
@@ -0,0 +1,23 @@
+#ifndef _GGP_LIBGADUW_H
+#define _GGP_LIBGADUW_H
+
+#include <internal.h>
+#include <libgadu.h>
+
+typedef void (*ggp_libgaduw_http_cb)(struct gg_http *h, gboolean success, gpointer user_data);
+
+typedef struct
+{
+	gpointer user_data;
+	ggp_libgaduw_http_cb cb;
+	
+	struct gg_http *h;
+	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);
+void ggp_libgaduw_http_cancel(ggp_libgaduw_http_req *req);
+
+
+#endif /* _GGP_LIBGADUW_H */



More information about the Commits mailing list