Session destruction fixes, some debugging code while we fix the rest. Also change...
authorMatthew Wild <mwild1@gmail.com>
Sat, 4 Oct 2008 14:25:54 +0000 (15:25 +0100)
committerMatthew Wild <mwild1@gmail.com>
Sat, 4 Oct 2008 14:25:54 +0000 (15:25 +0100)
core/sessionmanager.lua
core/usermanager.lua
core/xmlhandlers.lua
main.lua
util/logger.lua

index b257e532a3fe2f8ba9947f05410196b8f496378f..3366120e4edf2461f9392c567b25adeb2b451228 100644 (file)
@@ -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 @@ function make_authenticated(session, username)
        if session.type == "c2s_unauthed" then
                session.type = "c2s";
        end
+       return true;
 end
 
 function bind_resource(session, resource)
index a67ad368338495abb5903d88f34c2bebf5a7ae7f..0f303b24af7d06d3f965445a505c2d184371fa59 100644 (file)
@@ -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;
index 1ce7527dd12367000d6b46fbcfab1cc29a644e5a..ebc8f91db969a962d64a158bb469904ffb98de78 100644 (file)
@@ -10,6 +10,7 @@ local t_insert = table.insert;
 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 @@ function init_xmlhandlers(session)
                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));
index 027a472cf58e630d9195bf6afa276754dd72c37c..821b3273bd6b0fa69285c33da8362a2925b75316 100644 (file)
--- a/main.lua
+++ b/main.lua
@@ -33,6 +33,7 @@ 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 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 @@ function handler(conn, data, err)
        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 @@ function handler(conn, data, err)
                                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 @@ function handler(conn, data, err)
 end
 
 function disconnect(conn, err)
-       sessions[conn].disconnect(err);
+       sm_destroy_session(sessions[conn]);
        sessions[conn] = nil;
 end
 
index 3d672e942805272345486c6d94e49f8bbf90d395..623ceb674abf8d054de14d0e850f880a426e04a5 100644 (file)
@@ -9,7 +9,7 @@ function init(name)
        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