Software /
code /
prosody
Annotate
net/xmppserver_listener.lua @ 298:985710ea308b
Small fix for mod_version
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 15 Nov 2008 22:07:31 +0000 |
parent | 232:20745f8f4cf1 |
child | 330:d9d4c1de16ce |
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) | |
226
ba4711c4e8d2
Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents:
163
diff
changeset
|
65 |
ba4711c4e8d2
Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents:
163
diff
changeset
|
66 -- Debug version -- |
232
20745f8f4cf1
Actually show error and position when we show a traceback :)
Matthew Wild <mwild1@gmail.com>
parents:
226
diff
changeset
|
67 local function handleerr(err) print("Traceback:", err, debug.traceback()); end |
226
ba4711c4e8d2
Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents:
163
diff
changeset
|
68 session.stanza_dispatch = function (stanza) return select(2, xpcall(function () return core_process_stanza(session, stanza); end, handleerr)); end |
ba4711c4e8d2
Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents:
163
diff
changeset
|
69 |
ba4711c4e8d2
Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents:
163
diff
changeset
|
70 -- session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end |
148 | 71 |
72 end | |
73 if data then | |
74 session.data(conn, data); | |
75 end | |
76 end | |
77 | |
78 function xmppserver.disconnect(conn) | |
163
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
79 local session = sessions[conn]; |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
80 if session then |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
81 s2s_destroy_session(session); |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
82 sessions[conn] = nil; |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
83 session = nil; |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
84 collectgarbage("collect"); |
3fec9b512d4e
Clean up session when s2s connections are disconnected
Matthew Wild <mwild1@gmail.com>
parents:
148
diff
changeset
|
85 end |
148 | 86 end |
87 | |
88 function xmppserver.register_outgoing(conn, session) | |
89 session.direction = "outgoing"; | |
90 sessions[conn] = session; | |
91 | |
92 session.reset_stream = session_reset_stream; | |
93 session_reset_stream(session); -- Initialise, ready for use | |
94 | |
95 -- FIXME: Below function should be session,stanza - and xmlhandlers should use :method() notation to call, | |
96 -- this will avoid the useless indirection we have atm | |
97 -- (I'm on a mission, no time to fix now) | |
98 session.stanza_dispatch = function (stanza) return core_process_stanza(session, stanza); end | |
99 end | |
100 | |
101 connlisteners_register("xmppserver", xmppserver); | |
102 | |
103 | |
104 -- We need to perform some initialisation when a connection is created | |
105 -- We also need to perform that same initialisation at other points (SASL, TLS, ...) | |
106 | |
107 -- ...and we need to handle data | |
226
ba4711c4e8d2
Committing code to get nicer tracebacks for errors, also we no longer consider such errors fatal (probably a bad thing, I know...)
Matthew Wild <mwild1@gmail.com>
parents:
163
diff
changeset
|
108 -- ...and record all sessions associated with connections |