Mercurial > prosody-hg
comparison core/sessionmanager.lua @ 135:bf10d42dd41b
Merged from Matthew
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Thu, 23 Oct 2008 21:51:00 +0500 |
| parents | 6b6b8f4ab7e3 |
| children | 63863534b1f1 |
comparison
equal
deleted
inserted
replaced
| 134:b772317ea1ee | 135:bf10d42dd41b |
|---|---|
| 17 local newproxy = newproxy; | 17 local newproxy = newproxy; |
| 18 local getmetatable = getmetatable; | 18 local getmetatable = getmetatable; |
| 19 | 19 |
| 20 module "sessionmanager" | 20 module "sessionmanager" |
| 21 | 21 |
| 22 local open_sessions = 0; | |
| 23 | |
| 22 function new_session(conn) | 24 function new_session(conn) |
| 23 local session = { conn = conn, priority = 0, type = "c2s_unauthed" }; | 25 local session = { conn = conn, priority = 0, type = "c2s_unauthed" }; |
| 24 if true then | 26 if true then |
| 25 session.trace = newproxy(true); | 27 session.trace = newproxy(true); |
| 26 getmetatable(session.trace).__gc = function () print("Session got collected") end; | 28 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("Session got collected, now "..open_sessions.." sessions are allocated") end; |
| 27 end | 29 end |
| 30 open_sessions = open_sessions + 1; | |
| 28 local w = conn.write; | 31 local w = conn.write; |
| 29 session.send = function (t) w(tostring(t)); end | 32 session.send = function (t) w(tostring(t)); end |
| 30 return session; | 33 return session; |
| 31 end | 34 end |
| 32 | 35 |
| 33 function destroy_session(session) | 36 function destroy_session(session) |
| 34 if not (session and session.disconnect) then return; end | 37 session.log("info", "Destroying session"); |
| 35 log("debug", "Destroying session..."); | |
| 36 session.disconnect(); | |
| 37 if session.username then | 38 if session.username then |
| 38 if session.resource then | 39 if session.resource then |
| 39 hosts[session.host].sessions[session.username].sessions[session.resource] = nil; | 40 hosts[session.host].sessions[session.username].sessions[session.resource] = nil; |
| 40 end | 41 end |
| 41 local nomore = true; | 42 if not next(hosts[session.host].sessions[session.username], nil) then |
| 42 for res, ssn in pairs(hosts[session.host].sessions[session.username]) do | |
| 43 nomore = false; | |
| 44 end | |
| 45 if nomore then | |
| 46 hosts[session.host].sessions[session.username] = nil; | 43 hosts[session.host].sessions[session.username] = nil; |
| 47 end | 44 end |
| 48 end | 45 end |
| 49 session.conn = nil; | 46 session.conn = nil; |
| 50 session.disconnect = nil; | 47 session.disconnect = nil; |
| 51 for k in pairs(session) do | 48 for k in pairs(session) do |
| 52 if k ~= "trace" then | 49 if k ~= "trace" then |
| 53 session[k] = nil; | 50 session[k] = nil; |
| 54 end | 51 end |
| 55 end | 52 end |
| 56 collectgarbage("collect"); | |
| 57 collectgarbage("collect"); | |
| 58 collectgarbage("collect"); | |
| 59 collectgarbage("collect"); | |
| 60 collectgarbage("collect"); | |
| 61 end | 53 end |
| 62 | 54 |
| 63 function send_to_session(session, data) | 55 function send_to_session(session, data) |
| 64 log("debug", "Sending: %s", tostring(data)); | 56 log("debug", "Sending: %s", tostring(data)); |
| 65 session.conn.write(tostring(data)); | 57 session.conn.write(tostring(data)); |
