Changeset

53:14ea0fe6ca86

Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
author Matthew Wild <mwild1@gmail.com>
date Sat, 04 Oct 2008 15:25:54 +0100
parents 52:93e468eb2ffb
children 54:2f2c2375bf3e
files core/sessionmanager.lua core/usermanager.lua core/xmlhandlers.lua main.lua util/logger.lua
diffstat 5 files changed, 56 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/core/sessionmanager.lua	Sat Oct 04 15:24:52 2008 +0100
+++ b/core/sessionmanager.lua	Sat Oct 04 15:25:54 2008 +0100
@@ -1,26 +1,62 @@
 
 local tonumber, tostring = tonumber, tostring;
-local ipairs, print= ipairs, print;
-
+local ipairs, pairs, print= ipairs, pairs, print;
+local collectgarbage = collectgarbage;
 local m_random = import("math", "random");
 local format = import("string", "format");
 
 local hosts = hosts;
+local sessions = sessions;
 
 local modulemanager = require "core.modulemanager";
 local log = require "util.logger".init("sessionmanager");
 local error = error;
 local uuid_generate = require "util.uuid".uuid_generate;
+
+local newproxy = newproxy;
+local getmetatable = getmetatable;
+
 module "sessionmanager"
 
 function new_session(conn)
 	local session = { conn = conn, notopen = true, priority = 0, type = "c2s_unauthed" };
+	if true then
+		session.trace = newproxy(true);
+		getmetatable(session.trace).__gc = function () print("Session got collected") end;
+	end
 	local w = conn.write;
 	session.send = function (t) w(tostring(t)); end
 	return session;
 end
 
 function destroy_session(session)
+	if not (session and session.disconnect) then return; end 
+	log("debug", "Destroying session...");
+	session.disconnect();
+	if session.username then
+		if session.resource then
+			hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
+		end
+		local nomore = true;
+		for res, ssn in pairs(hosts[session.host].sessions[session.username]) do
+			nomore = false;
+		end
+		if nomore then
+			hosts[session.host].sessions[session.username] = nil;
+		end
+	end
+	session.conn = nil;
+	session.disconnect = nil;
+	for k in pairs(session) do
+		if k ~= "trace" then
+			session[k] = nil;
+		end
+	end
+	collectgarbage("collect");
+	collectgarbage("collect");
+	collectgarbage("collect");
+	collectgarbage("collect");
+	collectgarbage("collect");
 end
 
 function send_to_session(session, data)
@@ -34,6 +70,7 @@
 	if session.type == "c2s_unauthed" then
 		session.type = "c2s";
 	end
+	return true;
 end
 
 function bind_resource(session, resource)
--- a/core/usermanager.lua	Sat Oct 04 15:24:52 2008 +0100
+++ b/core/usermanager.lua	Sat Oct 04 15:25:54 2008 +0100
@@ -1,10 +1,12 @@
 
 require "util.datamanager"
 local datamanager = datamanager;
+local log = require "util.logger".init("usermanager");
 
 module "usermanager"
 
 function validate_credentials(host, username, password)
+	log("debug", "User '%s' is being validated", username);
 	local credentials = datamanager.load(username, host, "accounts") or {};
 	if password == credentials.password then return true; end
 	return false;
--- a/core/xmlhandlers.lua	Sat Oct 04 15:24:52 2008 +0100
+++ b/core/xmlhandlers.lua	Sat Oct 04 15:25:54 2008 +0100
@@ -10,6 +10,7 @@
 local t_remove = table.remove;
 local t_concat = table.concat;
 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end
+local sm_destroy_session = import("core.sessionmanager", "destroy_session");
 
 local error = error;
 
@@ -60,7 +61,15 @@
 		end
 		function xml_handlers:EndElement(name)
 			curr_ns,name = name:match("^(.+):(%w+)$");
-			if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then error("XML parse error in client stream"); end
+			if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then 
+				if name == "stream" then
+					log("debug", "Stream closed");
+					sm_destroy_session(session);
+					return;
+				else
+					error("XML parse error in client stream");
+				end
+			end
 			if stanza and #chardata > 0 then
 				-- We have some character data in the buffer
 				stanza:text(t_concat(chardata));
--- a/main.lua	Sat Oct 04 15:24:52 2008 +0100
+++ b/main.lua	Sat Oct 04 15:25:54 2008 +0100
@@ -33,6 +33,7 @@
 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end
 local m_random = math.random;
 local format = string.format;
+local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session");
 local st = stanza;
 ------------------------------
 
@@ -48,7 +49,7 @@
 	local session = sessions[conn];
 
 	if not session then
-		sessions[conn] = sessionmanager.new_session(conn);
+		sessions[conn] = sm_new_session(conn);
 		session = sessions[conn];
 
 		-- Logging functions --
@@ -75,11 +76,8 @@
 				pres:tag("status"):text("Disconnected: "..err);
 				session.stanza_dispatch(pres);
 			end
-			if session.username then
-				hosts[session.host].sessions[session.username] = nil;
-			end
 			session = nil;
-			print("Disconnected: "..err);
+			print("Disconnected: "..tostring(err));
 			collectgarbage("collect");
 		end
 	end
@@ -91,7 +89,7 @@
 end
 
 function disconnect(conn, err)
-	sessions[conn].disconnect(err);
+	sm_destroy_session(sessions[conn]);
 	sessions[conn] = nil;
 end
 
--- a/util/logger.lua	Sat Oct 04 15:24:52 2008 +0100
+++ b/util/logger.lua	Sat Oct 04 15:25:54 2008 +0100
@@ -9,7 +9,7 @@
 	name = nil; -- While this line is not commented, will automatically fill in file/line number info
 	return 	function (level, message, ...)
 				if not name then
-					local inf = debug.getinfo(2, 'Snl');
+					local inf = debug.getinfo(3, 'Snl');
 					level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline;
 				end
 				if ... then