Software /
code /
prosody
Annotate
net/xmppserver_listener.lua @ 182:f5cb6b5a6eb7
Datamanager Fixes and improvements
- Pretty printing
- Fixed bug causing a nil concatenation error when saving a datastore for nil user or host
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sun, 26 Oct 2008 21:19:04 +0500 |
parent | 163:3fec9b512d4e |
child | 226:ba4711c4e8d2 |
rev | line source |
---|---|
148 | 1 |
2 local logger = require "logger"; | |
3 local lxp = require "lxp" | |
4 local init_xmlhandlers = require "core.xmlhandlers" | |
5 local sm_new_session = require "core.sessionmanager".new_session; | |
6 local s2s_new_incoming = require "core.s2smanager".new_incoming; | |
7 local s2s_streamopened = require "core.s2smanager".streamopened; | |
163
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
8 local s2s_destroy_session = require "core.s2smanager".destroy_session; |
148 | 9 |
10 local connlisteners_register = require "net.connlisteners".register; | |
11 | |
12 local t_insert = table.insert; | |
13 local t_concat = table.concat; | |
14 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 | |
15 local m_random = math.random; | |
16 local format = string.format; | |
17 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session"); | |
18 local st = stanza; | |
19 | |
20 local sessions = {}; | |
21 local xmppserver = { default_port = 5269 }; | |
22 | |
23 -- These are session methods -- | |
24 | |
25 local function session_reset_stream(session) | |
26 -- Reset stream | |
27 local parser = lxp.new(init_xmlhandlers(session, s2s_streamopened), "|"); | |
28 session.parser = parser; | |
29 | |
30 session.notopen = true; | |
31 | |
32 function session.data(conn, data) | |
33 parser:parse(data); | |
34 end | |
35 return true; | |
36 end | |
37 | |
38 -- End of session methods -- | |
39 | |
40 function xmppserver.listener(conn, data) | |
41 local session = sessions[conn]; | |
42 if not session then | |
43 session = s2s_new_incoming(conn); | |
44 sessions[conn] = session; | |
45 | |
46 -- Logging functions -- | |
47 | |
48 local mainlog, log = log; | |
49 do | |
50 local conn_name = "s2sin"..tostring(conn):match("[a-f0-9]+$"); | |
51 log = logger.init(conn_name); | |
52 end | |
53 local print = function (...) log("info", t_concatall({...}, "\t")); end | |
54 session.log = log; | |
55 | |
56 print("Incoming s2s connection"); | |
57 | |
58 session.reset_stream = session_reset_stream; | |
59 | |
60 session_reset_stream(session); -- Initialise, ready for use | |
61 | |
62 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call, | |
63 -- this will avoid the useless indirection we have atm | |
64 -- (I'm on a mission, no time to fix now) | |
65 session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end | |
66 | |
67 end | |
68 if data then | |
69 session.data(conn, data); | |
70 end | |
71 end | |
72 | |
73 function xmppserver.disconnect(conn) | |
163
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
74 local session = sessions[conn]; |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
75 if session then |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
76 s2s_destroy_session(session); |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
77 sessions[conn] = nil; |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
78 session = nil; |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
79 collectgarbage("collect"); |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
80 end |
148 | 81 end |
82 | |
83 function xmppserver.register_outgoing(conn, session) | |
84 session.direction = "outgoing"; | |
85 sessions[conn] = session; | |
86 | |
87 session.reset_stream = session_reset_stream; | |
88 session_reset_stream(session); -- Initialise, ready for use | |
89 | |
90 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call, | |
91 -- this will avoid the useless indirection we have atm | |
92 -- (I'm on a mission, no time to fix now) | |
93 session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end | |
94 end | |
95 | |
96 connlisteners_register("xmppserver", xmppserver); | |
97 | |
98 | |
99 -- We need to perform some initialisation when a connection is created | |
100 -- We also need to perform that same initialisation at other points (SASL, TLS, ...) | |
101 | |
102 -- ...and we need to handle data | |
103 -- ...and record all sessions associated with connections |