Changeset

1418:d14de6cb8b5b

mod_message, mod_presence: Maintain list of top resources. Less work in routing messages to bare JIDs. - #optimization
author Waqas Hussain <waqas20@gmail.com>
date Fri, 26 Jun 2009 08:54:22 +0500
parents 1417:b02b22f77326
children 1419:1ac8bcc63a9d
files plugins/mod_message.lua plugins/mod_presence.lua
diffstat 2 files changed, 34 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_message.lua	Fri Jun 26 08:52:26 2009 +0500
+++ b/plugins/mod_message.lua	Fri Jun 26 08:54:22 2009 +0500
@@ -9,24 +9,6 @@
 local offlinemanager = require "core.offlinemanager";
 local t_insert = table.insert;
 
-local function select_top_resources(user)
-	local priority = 0;
-	local recipients = {};
-	for _, session in pairs(user.sessions) do -- find resource with greatest priority
-		if session.presence then
-			-- TODO check active privacy list for session
-			local p = session.priority;
-			if p > priority then
-				priority = p;
-				recipients = {session};
-			elseif p == priority then
-				t_insert(recipients, session);
-			end
-		end
-	end
-	return recipients;
-end
-
 local function process_to_bare(bare, origin, stanza)
 	local user = bare_sessions[bare];
 	
@@ -45,8 +27,8 @@
 		end  -- current policy is to discard headlines if no recipient is available
 	else -- chat or normal message
 		if user then -- some resources are connected
-			local recipients = select_top_resources(user);
-			if #recipients > 0 then
+			local recipients = user.top_resources;
+			if recipients then
 				for i=1,#recipients do
 					recipients[i].send(stanza);
 				end
--- a/plugins/mod_presence.lua	Fri Jun 26 08:52:26 2009 +0500
+++ b/plugins/mod_presence.lua	Fri Jun 26 08:54:22 2009 +0500
@@ -52,6 +52,29 @@
 	end
 end
 
+local function select_top_resources(user)
+	local priority = 0;
+	local recipients = {};
+	for _, session in pairs(user.sessions) do -- find resource with greatest priority
+		if session.presence then
+			-- TODO check active privacy list for session
+			local p = session.priority;
+			if p > priority then
+				priority = p;
+				recipients = {session};
+			elseif p == priority then
+				t_insert(recipients, session);
+			end
+		end
+	end
+	return recipients;
+end
+local function recalc_resource_map(origin)
+	local user = hosts[origin.host].sessions[origin.username];
+	user.top_resources = select_top_resources(user);
+	if #user.top_resources == 0 then user.top_resources = nil; end
+end
+
 function handle_normal_presence(origin, stanza, core_route_stanza)
 	if origin.roster then
 		for jid in pairs(origin.roster) do -- broadcast to all interested contacts
@@ -104,9 +127,12 @@
 				offlinemanager.deleteAll(node, host);
 			end
 		end
-		origin.priority = 0;
 		if stanza.attr.type == "unavailable" then
 			origin.presence = nil;
+			if origin.priority then
+				origin.priority = nil;
+				recalc_resource_map(origin);
+			end
 			if origin.directed then
 				local old_from = stanza.attr.from;
 				stanza.attr.from = origin.full_jid;
@@ -126,8 +152,11 @@
 					priority = tonumber(priority);
 					if priority < -128 then priority = -128 end
 					if priority > 127 then priority = 127 end
-					origin.priority = priority;
-				end
+				else priority = 0; end
+			else priority = 0; end
+			if origin.priority ~= priority then
+				origin.priority = priority;
+				recalc_resource_map(origin);
 			end
 		end
 		stanza.attr.to = nil; -- reset it