comparison mod_smacks/mod_smacks.lua @ 2394:4c27ebcf4cbd

mod_smacks: added new event "smacks-ack-delayed" used by mod_cloud_notify and extended the readme file accordingly (also mention mod_smacks_offline and mod_smacks_noerror in readme file)
author tmolitor <thilo@eightysoft.de>
date Thu, 24 Nov 2016 00:47:32 +0100
parents 48c3d64a3fc1
children 5e7badecf7fe
comparison
equal deleted inserted replaced
2393:3b2c94ea0c2e 2394:4c27ebcf4cbd
3 -- Copyright (C) 2010-2015 Matthew Wild 3 -- Copyright (C) 2010-2015 Matthew Wild
4 -- Copyright (C) 2010 Waqas Hussain 4 -- Copyright (C) 2010 Waqas Hussain
5 -- Copyright (C) 2012-2015 Kim Alvefur 5 -- Copyright (C) 2012-2015 Kim Alvefur
6 -- Copyright (C) 2012 Thijs Alkemade 6 -- Copyright (C) 2012 Thijs Alkemade
7 -- Copyright (C) 2014 Florian Zeitz 7 -- Copyright (C) 2014 Florian Zeitz
8 -- Copyright (C) 2016 Thilo Molitor
8 -- 9 --
9 -- This project is MIT/X11 licensed. Please see the 10 -- This project is MIT/X11 licensed. Please see the
10 -- COPYING file in the source package for more information. 11 -- COPYING file in the source package for more information.
11 -- 12 --
12 13
31 32
32 local resume_timeout = module:get_option_number("smacks_hibernation_time", 300); 33 local resume_timeout = module:get_option_number("smacks_hibernation_time", 300);
33 local s2s_smacks = module:get_option_boolean("smacks_enabled_s2s", false); 34 local s2s_smacks = module:get_option_boolean("smacks_enabled_s2s", false);
34 local s2s_resend = module:get_option_boolean("smacks_s2s_resend", false); 35 local s2s_resend = module:get_option_boolean("smacks_s2s_resend", false);
35 local max_unacked_stanzas = module:get_option_number("smacks_max_unacked_stanzas", 0); 36 local max_unacked_stanzas = module:get_option_number("smacks_max_unacked_stanzas", 0);
37 local delayed_ack_timeout = module:get_option_number("smacks_max_ack_delay", 60);
36 local core_process_stanza = prosody.core_process_stanza; 38 local core_process_stanza = prosody.core_process_stanza;
37 local sessionmanager = require"core.sessionmanager"; 39 local sessionmanager = require"core.sessionmanager";
38 40
39 local c2s_sessions = module:shared("/*/c2s/sessions"); 41 local c2s_sessions = module:shared("/*/c2s/sessions");
40 local session_registry = {}; 42 local session_registry = {};
43
44 local function delayed_ack_function(session)
45 -- fire event only when configured to do so
46 if delayed_ack_timeout > 0 and session.awaiting_ack and not session.outgoing_stanza_queue == nil then
47 session.log("debug", "Firing event 'smacks-ack-delayed', queue = %d", #session.outgoing_stanza_queue);
48 module:fire_event("smacks-ack-delayed", {origin = session, queue = session.outgoing_stanza_queue});
49 end
50 end
41 51
42 local function can_do_smacks(session, advertise_only) 52 local function can_do_smacks(session, advertise_only)
43 if session.smacks then return false, "unexpected-request", "Stream management is already enabled"; end 53 if session.smacks then return false, "unexpected-request", "Stream management is already enabled"; end
44 54
45 local session_type = session.type; 55 local session_type = session.type;
90 if #queue > max_unacked_stanzas and session.awaiting_ack == nil then 100 if #queue > max_unacked_stanzas and session.awaiting_ack == nil then
91 session.log("debug", "Queuing <r> (in a moment)"); 101 session.log("debug", "Queuing <r> (in a moment)");
92 session.awaiting_ack = false; 102 session.awaiting_ack = false;
93 session.awaiting_ack_timer = module:add_timer(1e-06, function () 103 session.awaiting_ack_timer = module:add_timer(1e-06, function ()
94 if not session.awaiting_ack then 104 if not session.awaiting_ack then
105 session.log("debug", "Sending <r> (before send)");
106 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks }))
95 session.log("debug", "Sending <r> (after send)"); 107 session.log("debug", "Sending <r> (after send)");
96 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks }))
97 session.awaiting_ack = true; 108 session.awaiting_ack = true;
109 session.delayed_ack_timer = module:add_timer(delayed_ack_timeout, function()
110 delayed_ack_function(session);
111 end);
98 end 112 end
99 end); 113 end);
100 end 114 end
101 end 115 end
102 return stanza; 116 return stanza;
217 function handle_a(origin, stanza) 231 function handle_a(origin, stanza)
218 if not origin.smacks then return; end 232 if not origin.smacks then return; end
219 origin.awaiting_ack = nil; 233 origin.awaiting_ack = nil;
220 if origin.awaiting_ack_timer then 234 if origin.awaiting_ack_timer then
221 origin.awaiting_ack_timer:stop(); 235 origin.awaiting_ack_timer:stop();
236 end
237 if origin.delayed_ack_timer then
238 origin.delayed_ack_timer:stop();
222 end 239 end
223 -- Remove handled stanzas from outgoing_stanza_queue 240 -- Remove handled stanzas from outgoing_stanza_queue
224 --log("debug", "ACK: h=%s, last=%s", stanza.attr.h or "", origin.last_acknowledged_stanza or ""); 241 --log("debug", "ACK: h=%s, last=%s", stanza.attr.h or "", origin.last_acknowledged_stanza or "");
225 local h = tonumber(stanza.attr.h); 242 local h = tonumber(stanza.attr.h);
226 if not h then 243 if not h then
403 if session.smacks then 420 if session.smacks then
404 if session.awaiting_ack then 421 if session.awaiting_ack then
405 if session.awaiting_ack_timer then 422 if session.awaiting_ack_timer then
406 session.awaiting_ack_timer:stop(); 423 session.awaiting_ack_timer:stop();
407 end 424 end
425 if session.delayed_ack_timer then
426 session.delayed_ack_timer:stop();
427 end
408 return false; -- Kick the session 428 return false; -- Kick the session
409 end 429 end
410 session.log("debug", "Sending <r> (read timeout)"); 430 session.log("debug", "Sending <r> (read timeout)");
411 session.awaiting_ack = false; 431 session.awaiting_ack = false;
412 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks })); 432 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks }));
413 session.awaiting_ack = true; 433 session.awaiting_ack = true;
434 session.delayed_ack_timer = module:add_timer(delayed_ack_timeout, function()
435 delayed_ack_function(session);
436 end);
414 return true; 437 return true;
415 end 438 end
416 end 439 end
417 440
418 module:hook("s2s-read-timeout", handle_read_timeout); 441 module:hook("s2s-read-timeout", handle_read_timeout);