Comparison

mod_smacks/mod_smacks.lua @ 220:263858d40ceb

mod_smacks: Fix the logic for handling outgoing stanzas and ack requests
author Matthew Wild <mwild1@gmail.com>
date Sat, 24 Jul 2010 11:07:38 +0100
parent 202:d11478ae374e
child 255:9b9b089407b1
comparison
equal deleted inserted replaced
219:6cdc313cf0eb 220:263858d40ceb
1 local st = require "util.stanza"; 1 local st = require "util.stanza";
2 2
3 local t_insert, t_remove = table.insert, table.remove; 3 local t_insert, t_remove = table.insert, table.remove;
4 local math_min = math.min;
4 local tonumber, tostring = tonumber, tostring; 5 local tonumber, tostring = tonumber, tostring;
5 local add_filter = require "util.filters".add_filter; 6 local add_filter = require "util.filters".add_filter;
6 7
7 local xmlns_sm = "urn:xmpp:sm:2"; 8 local xmlns_sm = "urn:xmpp:sm:2";
8 9
22 23
23 module:hook_stanza(xmlns_sm, "enable", 24 module:hook_stanza(xmlns_sm, "enable",
24 function (session, stanza) 25 function (session, stanza)
25 module:log("debug", "Enabling stream management"); 26 module:log("debug", "Enabling stream management");
26 session.smacks = true; 27 session.smacks = true;
27 session.handled_stanza_count = 0; 28
28 -- Overwrite process_stanza() and send() 29 -- Overwrite process_stanza() and send()
29 local queue, queue_length = {}, 0; 30 local queue = {};
30 session.outgoing_stanza_queue, session.outgoing_stanza_count = queue, queue_length; 31 session.outgoing_stanza_queue = queue;
32 session.last_acknowledged_stanza = 0;
31 local _send = session.send; 33 local _send = session.send;
32 function session.send(stanza) 34 function session.send(stanza)
33 local attr = stanza.attr; 35 local attr = stanza.attr;
34 if attr and not attr.xmlns then -- Stanza in default stream namespace 36 if attr and not attr.xmlns then -- Stanza in default stream namespace
35 queue_length = queue_length + 1; 37 queue[#queue+1] = st.reply(stanza);
36 session.outgoing_stanza_count = queue_length;
37 queue[queue_length] = st.reply(stanza);
38 end 38 end
39 local ok, err = _send(stanza); 39 local ok, err = _send(stanza);
40 if ok and queue_length > max_unacked_stanzas and not session.awaiting_ack then 40 if ok and #queue > max_unacked_stanzas and not session.awaiting_ack then
41 session.awaiting_ack = true; 41 session.awaiting_ack = true;
42 return _send(st.stanza("r", { xmlns = xmlns_sm })); 42 return _send(st.stanza("r", { xmlns = xmlns_sm }));
43 end 43 end
44 return ok, err; 44 return ok, err;
45 end 45 end
72 72
73 module:hook_stanza(xmlns_sm, "a", function (origin, stanza) 73 module:hook_stanza(xmlns_sm, "a", function (origin, stanza)
74 if not origin.smacks then return; end 74 if not origin.smacks then return; end
75 origin.awaiting_ack = nil; 75 origin.awaiting_ack = nil;
76 -- Remove handled stanzas from outgoing_stanza_queue 76 -- Remove handled stanzas from outgoing_stanza_queue
77 local handled_stanza_count = tonumber(stanza.attr.h)+1; 77 local handled_stanza_count = tonumber(stanza.attr.h)-origin.last_acknowledged_stanza;
78 for i=1,handled_stanza_count do 78 local queue = origin.outgoing_stanza_queue;
79 if handled_stanza_count > #queue then
80 module:log("warn", "The client says it handled %d new stanzas, but we only sent %d :)",
81 handled_stanza_count, #queue);
82 for i=1,#queue do
83 module:log("debug", "Q item %d: %s", i, tostring(queue[i]));
84 end
85 end
86 for i=1,math_min(handled_stanza_count,#queue) do
79 t_remove(origin.outgoing_stanza_queue, 1); 87 t_remove(origin.outgoing_stanza_queue, 1);
80 end 88 end
89 origin.last_acknowledged_stanza = origin.last_acknowledged_stanza + handled_stanza_count;
81 return true; 90 return true;
82 end); 91 end);
83 92
84 --TODO: Optimise... incoming stanzas should be handled by a per-session 93 --TODO: Optimise... incoming stanzas should be handled by a per-session
85 -- function that has a counter as an upvalue (no table indexing for increments, 94 -- function that has a counter as an upvalue (no table indexing for increments,