author | Matthew Wild <mwild1@gmail.com> |
Mon, 19 Mar 2012 17:06:02 +0000 | |
changeset 625 | 2c07bcf56a36 |
parent 162 | fe9c4daee076 |
child 1485 | d8c50448d0e7 |
permissions | -rw-r--r-- |
162
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 |
local add_filter = require "util.filters".add_filter; |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
local add_task = require "util.timer".add_task; |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
|
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
local buffer_seconds = module:get_option_number("flush_presence_seconds"); |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
|
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
local function throttle_session(data) |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
local session = data.session; |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
local buffer, flushing = {}, false; |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
local timer_active = false; |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
local function flush_buffer() |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
module:log("debug", "Flushing buffer for %s", session.full_jid); |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
flushing = true; |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
for jid, presence in pairs(buffer) do |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
session.send(presence); |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
end |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
flushing = false; |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
end |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
local function throttle_presence(stanza) |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
if stanza.name ~= "presence" or (stanza.attr.type and stanza.attr.type ~= "unavailable") then |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
module:log("debug", "Non-presence stanza for %s: %s", session.full_jid, tostring(stanza)); |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 |
flush_buffer(); |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
elseif not flushing then |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
module:log("debug", "Buffering presence stanza from %s to %s", stanza.attr.from, session.full_jid); |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
buffer[stanza.attr.from] = stanza; |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
if not timer_active and buffer_seconds then |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
timer_active = true; |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
add_task(buffer_seconds, flush_buffer); |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
end |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
return nil; -- Drop this stanza (we've stored it for later) |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
end |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
return stanza; |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
end |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
add_filter(session, "stanzas/out", throttle_presence); |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
end |
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
|
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
|
fe9c4daee076
mod_throttle_presence: Buffer up presence for up to flush_presence_seconds and send latest presence stanzas for each contact at once
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
module:hook("resource-bind", throttle_session); |