/soc/2015/koosha/main: 26fbd7c72d8c: Stream Management: move all...

Koosha Khajehmoogahi koosha at posteo.de
Fri Aug 14 21:23:22 EDT 2015


Changeset: 26fbd7c72d8c3e14f95b6f9503747cf2406bd936
Author:	 Koosha Khajehmoogahi <koosha at posteo.de>
Date:	 2015-08-15 03:08 +0200
Branch:	 default
URL: https://hg.pidgin.im/soc/2015/koosha/main/rev/26fbd7c72d8c

Description:

Stream Management: move all the code to a new files, add handler for <a/> and request acks with <r/>

diffstat:

 libpurple/protocols/jabber/Makefile.am |    2 +
 libpurple/protocols/jabber/jabber.c    |   94 +-------------
 libpurple/protocols/jabber/jabber.h    |    5 +-
 libpurple/protocols/jabber/sm.c        |  205 +++++++++++++++++++++++++++++++++
 libpurple/protocols/jabber/sm.h        |   50 ++++++++
 5 files changed, 269 insertions(+), 87 deletions(-)

diffs (truncated from 468 to 300 lines):

diff --git a/libpurple/protocols/jabber/Makefile.am b/libpurple/protocols/jabber/Makefile.am
--- a/libpurple/protocols/jabber/Makefile.am
+++ b/libpurple/protocols/jabber/Makefile.am
@@ -88,6 +88,8 @@ JABBERSOURCES = \
 			  roster.h \
 			  si.c \
 			  si.h \
+			  sm.c \
+			  sm.h \
 			  useravatar.c \
 			  useravatar.h \
 			  usermood.c \
diff --git a/libpurple/protocols/jabber/jabber.c b/libpurple/protocols/jabber/jabber.c
--- a/libpurple/protocols/jabber/jabber.c
+++ b/libpurple/protocols/jabber/jabber.c
@@ -64,6 +64,7 @@
 #include "roster.h"
 #include "ping.h"
 #include "si.h"
+#include "sm.h"
 #include "usermood.h"
 #include "xdata.h"
 #include "pep.h"
@@ -138,7 +139,6 @@ jabber_session_initialized_cb(JabberStre
 
 static void jabber_session_init(JabberStream *js)
 {
-	char *sm;
 	JabberIq *iq = jabber_iq_new(js, JABBER_IQ_SET);
 	PurpleXmlNode *session;
 
@@ -148,16 +148,7 @@ static void jabber_session_init(JabberSt
 	purple_xmlnode_set_namespace(session, NS_XMPP_SESSION);
 
 	jabber_iq_send(iq);
-
-	/* Enable stream management with resumptions */
-	if (js->server_caps & JABBER_CAP_SM) {
-		sm = g_strdup_printf("<enable xmlns='" NS_XMPP_SM "' resume='true' />");
-		jabber_send_raw(js, sm, -1);
-		g_free(sm);
-		/* From now on, count the number of stanzas sent and received */
-		js->stanzas_recvd = 0;
-		js->stanzas_sent  = 0;
-	}
+	jabber_stream_management_enable(js);
 }
 
 static void jabber_bind_result_cb(JabberStream *js, const char *from,
@@ -351,36 +342,6 @@ static void jabber_stream_handle_error(J
 	g_free(msg);
 }
 
-/*
- * TODO: parse the 'location' attribute as well
- */
-void jabber_stream_management_parse(JabberStream *js, PurpleXmlNode *packet)
-{
-	const char *resume, *id, *max;
-	PurpleAccount *account;
-	gboolean resume_enabled;
-	resume = purple_xmlnode_get_attrib(packet, "resume");
-	account = purple_connection_get_account(js->gc);
-
-	js->has_sm = TRUE;
-
-	resume_enabled = resume && purple_xmlnode_is_attrib_true(resume);
-
-	purple_debug_info( "jabber", "Stream management %s resumption is enabled.\n",
-				resume_enabled ? "with" : "without" );
-
-	if (resume_enabled) {
-		id = purple_xmlnode_get_attrib(packet, "id");
-		purple_account_set_string(account, "sm_id", id);
-
-		/* max means the maximum possible resumption time in seconds
-		 * supported by server */
-		max = purple_xmlnode_get_attrib(packet, "max");
-		if (max)
-			purple_account_set_string(account, "sm_max", max);
-	}
-}
-
 static void tls_init(JabberStream *js);
 
 void jabber_process_packet(JabberStream *js, PurpleXmlNode **packet)
@@ -397,19 +358,7 @@ void jabber_process_packet(JabberStream 
 	name = (*packet)->name;
 	xmlns = purple_xmlnode_get_namespace(*packet);
 
-	/* Count received stanzas to include when acking
-	 * via <a/> (stream management) to the server */
-	if (js->server_caps & JABBER_CAP_SM) {
-		if (purple_strequal(name, "iq") ||
-		    purple_strequal(name, "presence") ||
-		    purple_strequal(name, "message"))
-		{
-			if (G_UNLIKELY(G_MAXUINT32 == js->stanzas_recvd))
-				js->stanzas_recvd = 0;
-			else
-				js->stanzas_recvd++;
-		}
-	}
+	jabber_stream_management_incr_inbound_counter(js, *packet);
 
 	if (purple_strequal(name, "iq")) {
 		jabber_iq_parse(js, *packet);
@@ -444,7 +393,9 @@ void jabber_process_packet(JabberStream 
 			/* TODO: Handle <failure/>, I guess? */
 		}
 	} else if (purple_strequal(name, "r") && purple_strequal(xmlns, NS_XMPP_SM)) {
-		jabber_send_stream_ack(js);
+		jabber_stream_management_send_ack(js);
+	} else if (purple_strequal(name, "a") && purple_strequal(xmlns, NS_XMPP_SM)) {
+		jabber_stream_management_recv_ack(js, *packet);
 	} else {
 		purple_debug_warning("jabber", "Unknown packet: %s\n", name);
 	}
@@ -688,35 +639,9 @@ void jabber_send_signal_cb(PurpleConnect
 	g_free(txt);
 }
 
-void jabber_send_stream_ack(JabberStream *js)
-{
-	PurpleXmlNode *ack = purple_xmlnode_new("a");
-	gchar *h = g_strdup_printf("%d", js->stanzas_recvd);
-	purple_xmlnode_set_attrib(ack, "h", h);
-	purple_xmlnode_set_namespace(ack, NS_XMPP_SM);
-
-	purple_debug_info("jabber", "Sending ack (<a/>) with h='%d'\n", js->stanzas_recvd);
-
-	jabber_send(js, ack);
-	g_free(h);
-	purple_xmlnode_free(ack);
-}
-
 void jabber_send(JabberStream *js, PurpleXmlNode *packet)
 {
-	const char *name = packet->name;
-	if (js->server_caps & JABBER_CAP_SM) {
-		if (purple_strequal(name, "message") ||
-		    purple_strequal(name, "presence") ||
-		    purple_strequal(name, "iq"))
-		{
-			if (G_UNLIKELY(G_MAXUINT32 == js->stanzas_sent))
-				js->stanzas_sent = 0;
-			else
-				js->stanzas_sent++;
-		}
-	}
-
+	jabber_stream_management_incr_outbound_counter(js, packet);
 	purple_signal_emit(purple_connection_get_protocol(js->gc), "jabber-sending-xmlnode", js->gc, &packet);
 }
 
@@ -1088,6 +1013,7 @@ jabber_stream_new(PurpleAccount *account
 			g_free, (GDestroyNotify)jabber_iq_callbackdata_free);
 	js->chats = g_hash_table_new_full(g_str_hash, g_str_equal,
 			g_free, (GDestroyNotify)jabber_chat_free);
+	js->sm = jabber_stream_management_init();
 	js->next_id = g_random_int();
 	js->write_buffer = purple_circular_buffer_new(512);
 	js->old_length = 0;
@@ -1098,8 +1024,6 @@ jabber_stream_new(PurpleAccount *account
 	js->protocol_version.minor = 0;
 	js->sessions = NULL;
 	js->has_sm = FALSE;
-	js->stanzas_sent = 0;
-	js->stanzas_recvd = 0;
 	js->stun_ip = NULL;
 	js->stun_port = 0;
 	js->stun_query = NULL;
@@ -1777,6 +1701,8 @@ void jabber_close(PurpleConnection *gc)
 		g_free(cmd);
 		js->commands = g_list_delete_link(js->commands, js->commands);
 	}
+	g_hash_table_destroy(js->sm->packets_buffer);
+	g_free(js->sm);
 	g_free(js->server_name);
 	g_free(js->certificate_CN);
 	g_free(js->gmail_last_time);
diff --git a/libpurple/protocols/jabber/jabber.h b/libpurple/protocols/jabber/jabber.h
--- a/libpurple/protocols/jabber/jabber.h
+++ b/libpurple/protocols/jabber/jabber.h
@@ -80,6 +80,7 @@ typedef struct _JabberStream JabberStrea
 #include "xmlnode.h"
 #include "buddy.h"
 #include "bosh.h"
+#include "sm.h"
 
 #ifdef HAVE_CYRUS_SASL
 #include <sasl/sasl.h>
@@ -197,9 +198,7 @@ struct _JabberStream
 
 	/* Whether stream management is enabled or not */
 	gboolean has_sm;
-	/* Number of stanzas sent and received after enabling stream management */
-	guint32 stanzas_sent;
-	guint32 stanzas_recvd;
+	JabberSM* sm;
 
 	/** When we last pinged the server, so we don't ping more
 	 *  often than once every minute.
diff --git a/libpurple/protocols/jabber/sm.c b/libpurple/protocols/jabber/sm.c
new file mode 100644
--- /dev/null
+++ b/libpurple/protocols/jabber/sm.c
@@ -0,0 +1,205 @@
+/**
+ * purple
+ *
+ * Purple is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
+ *
+ */
+#include "internal.h"
+
+#include "namespaces.h"
+#include "jabber.h"
+#include "sm.h"
+#include "xmlnode.h"
+
+static gboolean sent_enable_packet = FALSE;
+
+JabberSM *jabber_stream_management_init()
+{
+	JabberSM *sm;
+
+	sm = g_new(JabberSM, 1);
+	sm->stanzas_sent   = 0;
+	sm->stanzas_recvd  = 0;
+	sm->last_acked_stanza = 0;
+	sm->packets_buffer = g_hash_table_new_full(g_int_hash,
+			g_int_equal, g_free, (GDestroyNotify)purple_xmlnode_free);
+
+	return sm;
+}
+
+void jabber_stream_management_enable(JabberStream *js)
+{
+	char *sm;
+	g_return_if_fail(js->server_caps & JABBER_CAP_SM);
+
+	/* Enable stream management with resumptions */
+	sm = g_strdup_printf("<enable xmlns='" NS_XMPP_SM "' resume='true' />");
+	jabber_send_raw(js, sm, -1);
+	g_free(sm);
+	/* From now on, count the number of stanzas sent and received */
+	js->sm->stanzas_recvd = 0;
+	js->sm->stanzas_sent  = 0;
+	sent_enable_packet = TRUE;
+}
+
+/*
+ * TODO: parse the 'location' attribute as well
+ */
+void jabber_stream_management_parse(JabberStream *js, PurpleXmlNode *packet)
+{
+	const char *resume, *id, *max;
+	PurpleAccount *account;
+	gboolean resume_enabled;
+
+	resume = purple_xmlnode_get_attrib(packet, "resume");
+	account = purple_connection_get_account(js->gc);
+
+	js->has_sm = TRUE;
+
+	resume_enabled = resume && purple_xmlnode_is_attrib_true(resume);
+
+	purple_debug_info( "jabber", "Stream management %s resumption is enabled.\n",
+				resume_enabled ? "with" : "without" );
+
+	if (resume_enabled) {
+		id = purple_xmlnode_get_attrib(packet, "id");
+		purple_account_set_string(account, "sm_id", id);
+
+		/* max means the maximum possible resumption time in seconds
+		 * supported by server */
+		max = purple_xmlnode_get_attrib(packet, "max");
+		if (max)
+			purple_account_set_string(account, "sm_max", max);
+	}
+}
+
+void jabber_stream_management_send_ack(JabberStream *js)



More information about the Commits mailing list