mod_smacks/mod_smacks.lua
author Matthew Wild <mwild1@gmail.com>
Thu, 07 Oct 2010 16:19:09 +0100
changeset 258 36648205b10a
parent 257 08fa42e1ab06
child 263 41f1cac40560
permissions -rw-r--r--
mod_smacks: Add commented log statement for future debugging
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
local st = require "util.stanza";
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local t_insert, t_remove = table.insert, table.remove;
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
     4
local math_min = math.min;
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
local tonumber, tostring = tonumber, tostring;
201
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
     6
local add_filter = require "util.filters".add_filter;
257
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
     7
local timer = require "util.timer";
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
local xmlns_sm = "urn:xmpp:sm:2";
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local sm_attr = { xmlns = xmlns_sm };
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
257
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
    13
local resume_timeout = 300;
200
64a573203c20 mod_smacks: Better logic for deciding what is a stanza and what is not, and deciding when to send ack requests
Matthew Wild <mwild1@gmail.com>
parents: 160
diff changeset
    14
local max_unacked_stanzas = 0;
64a573203c20 mod_smacks: Better logic for deciding what is a stanza and what is not, and deciding when to send ack requests
Matthew Wild <mwild1@gmail.com>
parents: 160
diff changeset
    15
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
module:add_event_hook("stream-features",
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
		function (session, features)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
			features:tag("sm", sm_attr):tag("optional"):up():up();
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
		end);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
module:hook("s2s-stream-features",
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
		function (data)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
			data.features:tag("sm", sm_attr):tag("optional"):up():up();
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
		end);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
module:hook_stanza(xmlns_sm, "enable",
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
		function (session, stanza)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
			module:log("debug", "Enabling stream management");
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
			session.smacks = true;
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    30
			
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
			-- Overwrite process_stanza() and send()
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    32
			local queue = {};
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    33
			session.outgoing_stanza_queue = queue;
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    34
			session.last_acknowledged_stanza = 0;
255
9b9b089407b1 mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents: 220
diff changeset
    35
			local _send = session.sends2s or session.send;
9b9b089407b1 mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents: 220
diff changeset
    36
			local function new_send(stanza)
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
				local attr = stanza.attr;
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
				if attr and not attr.xmlns then -- Stanza in default stream namespace
256
57de4a7840ef mod_smacks: Fixes for storing the unacked stanzas so that they can be properly replayed to clients on stream resume
Matthew Wild <mwild1@gmail.com>
parents: 255
diff changeset
    39
					queue[#queue+1] = st.clone(stanza);
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
				end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
				local ok, err = _send(stanza);
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    42
				if ok and #queue > max_unacked_stanzas and not session.awaiting_ack then
200
64a573203c20 mod_smacks: Better logic for deciding what is a stanza and what is not, and deciding when to send ack requests
Matthew Wild <mwild1@gmail.com>
parents: 160
diff changeset
    43
					session.awaiting_ack = true;
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
					return _send(st.stanza("r", { xmlns = xmlns_sm }));
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
				end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
				return ok, err;
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
			end
201
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    48
			
255
9b9b089407b1 mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents: 220
diff changeset
    49
			if session.sends2s then
9b9b089407b1 mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents: 220
diff changeset
    50
				session.sends2s = new_send;
9b9b089407b1 mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents: 220
diff changeset
    51
			else
9b9b089407b1 mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents: 220
diff changeset
    52
				session.send = new_send;
9b9b089407b1 mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents: 220
diff changeset
    53
			end
9b9b089407b1 mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents: 220
diff changeset
    54
			
201
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    55
			session.handled_stanza_count = 0;
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    56
			add_filter(session, "stanzas/in", function (stanza)
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    57
				if not stanza.attr.xmlns then
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    58
					session.handled_stanza_count = session.handled_stanza_count + 1;
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    59
					session.log("debug", "Handled %d incoming stanzas", session.handled_stanza_count);
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    60
				end
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    61
				return stanza;
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    62
			end);
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    63
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    64
			if not stanza.attr.resume then -- FIXME: Resumption should be a different spec :/
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    65
				_send(st.stanza("enabled", sm_attr));
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    66
				return true;
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    67
			end
bc24f58a0d39 mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
Matthew Wild <mwild1@gmail.com>
parents: 200
diff changeset
    68
		end, 100);
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
module:hook_stanza(xmlns_sm, "r", function (origin, stanza)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
	if not origin.smacks then
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
		module:log("debug", "Received ack request from non-smack-enabled session");
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
		return;
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
	end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
	module:log("debug", "Received ack request, acking for %d", origin.handled_stanza_count);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
	-- Reply with <a>
255
9b9b089407b1 mod_smacks: Fix to reply to stream for s2s sessions
Matthew Wild <mwild1@gmail.com>
parents: 220
diff changeset
    77
	(origin.sends2s or origin.send)(st.stanza("a", { xmlns = xmlns_sm, h = tostring(origin.handled_stanza_count) }));
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
	return true;
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
end);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
module:hook_stanza(xmlns_sm, "a", function (origin, stanza)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
	if not origin.smacks then return; end
200
64a573203c20 mod_smacks: Better logic for deciding what is a stanza and what is not, and deciding when to send ack requests
Matthew Wild <mwild1@gmail.com>
parents: 160
diff changeset
    83
	origin.awaiting_ack = nil;
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
	-- Remove handled stanzas from outgoing_stanza_queue
258
36648205b10a mod_smacks: Add commented log statement for future debugging
Matthew Wild <mwild1@gmail.com>
parents: 257
diff changeset
    85
	--log("debug", "ACK: h=%s, last=%s", stanza.attr.h or "", origin.last_acknowledged_stanza or "");
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    86
	local handled_stanza_count = tonumber(stanza.attr.h)-origin.last_acknowledged_stanza;
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    87
	local queue = origin.outgoing_stanza_queue;
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    88
	if handled_stanza_count > #queue then
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    89
		module:log("warn", "The client says it handled %d new stanzas, but we only sent %d :)",
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    90
			handled_stanza_count, #queue);
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    91
		for i=1,#queue do
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    92
			module:log("debug", "Q item %d: %s", i, tostring(queue[i]));
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    93
		end
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    94
	end
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    95
	for i=1,math_min(handled_stanza_count,#queue) do
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
		t_remove(origin.outgoing_stanza_queue, 1);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
	end
220
263858d40ceb mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
Matthew Wild <mwild1@gmail.com>
parents: 202
diff changeset
    98
	origin.last_acknowledged_stanza = origin.last_acknowledged_stanza + handled_stanza_count;
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
	return true;
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
end);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
--TODO: Optimise... incoming stanzas should be handled by a per-session
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
-- function that has a counter as an upvalue (no table indexing for increments,
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
-- and won't slow non-198 sessions). We can also then remove the .handled flag
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
-- on stanzas
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
function handle_unacked_stanzas(session)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
	local queue = session.outgoing_stanza_queue;
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
	local error_attr = { type = "cancel" };
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
	if #queue > 0 then
202
d11478ae374e mod_smacks: Clean outgoing stanza queue correctly on session close
Matthew Wild <mwild1@gmail.com>
parents: 201
diff changeset
   111
		session.outgoing_stanza_queue = {};
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
		for i=1,#queue do
256
57de4a7840ef mod_smacks: Fixes for storing the unacked stanzas so that they can be properly replayed to clients on stream resume
Matthew Wild <mwild1@gmail.com>
parents: 255
diff changeset
   113
			local reply = st.reply(queue[i]);
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
			if reply.attr.to ~= session.full_jid then
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
				reply.attr.type = "error";
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
				reply:tag("error", error_attr)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
					:tag("recipient-unavailable", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"});
256
57de4a7840ef mod_smacks: Fixes for storing the unacked stanzas so that they can be properly replayed to clients on stream resume
Matthew Wild <mwild1@gmail.com>
parents: 255
diff changeset
   118
				core_process_stanza(session, reply);
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
			end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
		end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
	end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
local _destroy_session = sessionmanager.destroy_session;
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
function sessionmanager.destroy_session(session, err)
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
	if session.smacks then
257
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   127
		if not session.resumption_token then
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   128
			local queue = session.outgoing_stanza_queue;
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   129
			if #queue > 0 then
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   130
				module:log("warn", "Destroying session with %d unacked stanzas:", #queue);
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   131
				for i=1,#queue do
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   132
					module:log("warn", "::%s", tostring(queue[i]));
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   133
				end
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   134
				handle_unacked_stanzas(session);
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
			end
257
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   136
		else
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   137
			session.hibernating = true;
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   138
			timer.add_task(resume_timeout, function ()
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   139
				if session.hibernating then
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   140
					session.resumption_token = nil;
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   141
					sessionmanager.destroy_session(session); -- Re-destroy
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   142
				end
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   143
			end);
08fa42e1ab06 mod_smacks: Fixes for monkey-patched sessionmanager.destroy to handle stream resumption, and to fall back to stock destroy() if the session is not smacks-enabled.
Matthew Wild <mwild1@gmail.com>
parents: 256
diff changeset
   144
			return; -- Postpone destruction for now
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
		end
258
36648205b10a mod_smacks: Add commented log statement for future debugging
Matthew Wild <mwild1@gmail.com>
parents: 257
diff changeset
   146
		
160
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   147
	end
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
	return _destroy_session(session, err);
9a7671720dec mod_smacks: XEP-0198 Stream Management acks. Initial commit - very rough, useful mainly for testing at the moment, most certainly contains bugs :)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
end