From 66ae6ceaa874b6359957322718d4a9c9862d262d Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 2 Nov 2008 06:36:42 +0500 Subject: Added resource priority handling, etc --- core/s2smanager.lua | 2 +- core/stanza_router.lua | 39 ++++++++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/s2smanager.lua b/core/s2smanager.lua index b2e498bd..7855181e 100644 --- a/core/s2smanager.lua +++ b/core/s2smanager.lua @@ -57,7 +57,7 @@ end local open_sessions = 0; function new_incoming(conn) - local session = { conn = conn, priority = 0, type = "s2sin_unauthed", direction = "incoming" }; + local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming" }; if true then session.trace = newproxy(true); getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("s2s session got collected, now "..open_sessions.." s2s sessions are allocated") end; diff --git a/core/stanza_router.lua b/core/stanza_router.lua index 01a46961..41e89f31 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -21,6 +21,9 @@ local modules_handle_stanza = require "core.modulemanager".handle_stanza; local format = string.format; local tostring = tostring; +local t_concat = table.concat; +local tonumber = tonumber; +local s_find = string.find; local jid_split = require "util.jid".split; local print = print; @@ -91,7 +94,7 @@ function core_handle_stanza(origin, stanza) core_route_stanza(origin, stanza); end end - if not origin.presence then -- presence probes on initial presence -- FIXME does unavailable qualify as initial presence? + if stanza.attr.type == nil and not origin.presence then -- initial presence local probe = st.presence({from = origin.full_jid, type = "probe"}); for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to local subscription = origin.roster[jid].subscription; @@ -100,16 +103,42 @@ function core_handle_stanza(origin, stanza) core_route_stanza(origin, probe); end end - for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all resources - if res ~= origin and stanza.attr.type ~= "unavailable" and res.presence then -- FIXME does unavailable qualify as initial presence? + for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources + if res ~= origin and res.presence then res.presence.attr.to = origin.full_jid; core_route_stanza(res, res.presence); res.presence.attr.to = nil; end end - -- TODO resend subscription requests + if origin.roster.pending then -- resend incoming subscription requests + for jid in pairs(origin.roster.pending) do + origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original? + end + end + local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host}); + for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests + if item.ask then + request.attr.to = jid; + core_route_stanza(origin, request); + end + end + end + origin.priority = 0; + if stanza.attr.type == "unavailable" then + origin.presence = nil; + else + origin.presence = stanza; + local priority = stanza:child_with_name("priority"); + if priority and #priority > 0 then + priority = t_concat(priority); + if s_find(priority, "^[+-]?[0-9]+$") then + priority = tonumber(priority); + if priority < -128 then priority = -128 end + if priority > 127 then priority = 127 end + origin.priority = priority; + end + end end - origin.presence = stanza; stanza.attr.to = nil; -- reset it else -- TODO error, bad type -- cgit v1.2.3 From 02b083a7471024fa6f01afaf80ed0817c3bfd061 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sun, 2 Nov 2008 06:53:31 +0500 Subject: Priority based message routing, etc --- core/stanza_router.lua | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'core') diff --git a/core/stanza_router.lua b/core/stanza_router.lua index 41e89f31..03018c8b 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -22,6 +22,7 @@ local modules_handle_stanza = require "core.modulemanager".handle_stanza; local format = string.format; local tostring = tostring; local t_concat = table.concat; +local t_insert = table.insert; local tonumber = tonumber; local s_find = string.find; @@ -218,6 +219,7 @@ function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_b local st_from, st_to = stanza.attr.from, stanza.attr.to; stanza.attr.from, stanza.attr.to = from_bare, to_bare; if stanza.attr.type == "probe" then + log("debug", "inbound probe from "..from_bare.." for "..to_bare); if rostermanager.is_contact_subscribed(node, host, from_bare) then if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too) @@ -281,22 +283,28 @@ function core_route_stanza(origin, stanza) if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare); else -- sender is available or unavailable - for k in pairs(user.sessions) do -- presence broadcast to all user resources. FIXME should this be just for available resources? Do we need to check subscription? - if user.sessions[k].full_jid then - stanza.attr.to = user.sessions[k].full_jid; -- reset at the end of function - user.sessions[k].send(stanza); + for _, session in pairs(user.sessions) do -- presence broadcast to all user resources. + if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription? + stanza.attr.to = session.full_jid; -- reset at the end of function + session.send(stanza); end end end elseif stanza.name == "message" then -- select a resource to recieve message - for k in pairs(user.sessions) do - if user.sessions[k].full_jid then - res = user.sessions[k]; - break; + local priority = 0; + local recipients = {}; + for _, session in pairs(user.sessions) do -- find resource with greatest priority + local p = session.priority; + if p > priority then + priority = p; + recipients = {session}; + elseif p == priority then + t_insert(recipients, session); end end - -- TODO find resource with greatest priority - res.send(stanza); + for _, session in pairs(recipient) do + session.send(stanza); + end else -- TODO send IQ error end -- cgit v1.2.3