Comparison

core/sessionmanager.lua @ 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
parent 49:1cd2a8db392d
child 57:126b25079399
comparison
equal deleted inserted replaced
52:93e468eb2ffb 53:14ea0fe6ca86
1 1
2 local tonumber, tostring = tonumber, tostring; 2 local tonumber, tostring = tonumber, tostring;
3 local ipairs, print= ipairs, print; 3 local ipairs, pairs, print= ipairs, pairs, print;
4 4 local collectgarbage = collectgarbage;
5 local m_random = import("math", "random"); 5 local m_random = import("math", "random");
6 local format = import("string", "format"); 6 local format = import("string", "format");
7 7
8 local hosts = hosts; 8 local hosts = hosts;
9 local sessions = sessions;
9 10
10 local modulemanager = require "core.modulemanager"; 11 local modulemanager = require "core.modulemanager";
11 local log = require "util.logger".init("sessionmanager"); 12 local log = require "util.logger".init("sessionmanager");
12 local error = error; 13 local error = error;
13 local uuid_generate = require "util.uuid".uuid_generate; 14 local uuid_generate = require "util.uuid".uuid_generate;
15
16 local newproxy = newproxy;
17 local getmetatable = getmetatable;
18
14 module "sessionmanager" 19 module "sessionmanager"
15 20
16 function new_session(conn) 21 function new_session(conn)
17 local session = { conn = conn, notopen = true, priority = 0, type = "c2s_unauthed" }; 22 local session = { conn = conn, notopen = true, priority = 0, type = "c2s_unauthed" };
23 if true then
24 session.trace = newproxy(true);
25 getmetatable(session.trace).__gc = function () print("Session got collected") end;
26 end
18 local w = conn.write; 27 local w = conn.write;
19 session.send = function (t) w(tostring(t)); end 28 session.send = function (t) w(tostring(t)); end
20 return session; 29 return session;
21 end 30 end
22 31
23 function destroy_session(session) 32 function destroy_session(session)
33 if not (session and session.disconnect) then return; end
34 log("debug", "Destroying session...");
35 session.disconnect();
36 if session.username then
37 if session.resource then
38 hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
39 end
40 local nomore = true;
41 for res, ssn in pairs(hosts[session.host].sessions[session.username]) do
42 nomore = false;
43 end
44 if nomore then
45 hosts[session.host].sessions[session.username] = nil;
46 end
47 end
48 session.conn = nil;
49 session.disconnect = nil;
50 for k in pairs(session) do
51 if k ~= "trace" then
52 session[k] = nil;
53 end
54 end
55 collectgarbage("collect");
56 collectgarbage("collect");
57 collectgarbage("collect");
58 collectgarbage("collect");
59 collectgarbage("collect");
24 end 60 end
25 61
26 function send_to_session(session, data) 62 function send_to_session(session, data)
27 log("debug", "Sending: %s", tostring(data)); 63 log("debug", "Sending: %s", tostring(data));
28 session.conn.write(tostring(data)); 64 session.conn.write(tostring(data));
32 session.username = username; 68 session.username = username;
33 session.resource = resource; 69 session.resource = resource;
34 if session.type == "c2s_unauthed" then 70 if session.type == "c2s_unauthed" then
35 session.type = "c2s"; 71 session.type = "c2s";
36 end 72 end
73 return true;
37 end 74 end
38 75
39 function bind_resource(session, resource) 76 function bind_resource(session, resource)
40 if not session.username then return false, "auth"; end 77 if not session.username then return false, "auth"; end
41 if session.resource then return false, "constraint"; end -- We don't support binding multiple resources 78 if session.resource then return false, "constraint"; end -- We don't support binding multiple resources