Annotate

core/s2smanager.lua @ 10210:9fdda9fafc3c

Merge mod-installer (2019 GSoC by João Duarte)
author Matthew Wild <mwild1@gmail.com>
date Mon, 19 Aug 2019 12:17:17 +0100
parent 10103:18cab054364a
child 10243:b36765ab0ae3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1492
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2889
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2889
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5459
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 739
diff changeset
6 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
7 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 451
diff changeset
9
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
5366
c1357b7fbca3 s2smanager: Access prosody.hosts instead of hosts global directly
Matthew Wild <mwild1@gmail.com>
parents: 5362
diff changeset
11 local hosts = prosody.hosts;
10103
18cab054364a core.s2smanager: Rewrite log line to use formatting instead of concatenation
Kim Alvefur <zash@zash.se>
parents: 10102
diff changeset
12 local pairs, setmetatable = pairs, setmetatable;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local logger_init = require "util.logger".init;
9936
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
15 local sessionlib = require "util.session";
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local log = logger_init("s2smanager");
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
3476
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
19 local prosody = _G.prosody;
8675
d3d74e923e4e s2smanager: Explicitly export the incoming_s2s table [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8555
diff changeset
20 local incoming_s2s = {};
d3d74e923e4e s2smanager: Explicitly export the incoming_s2s table [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8555
diff changeset
21 _G.incoming_s2s = incoming_s2s;
3476
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
22 prosody.incoming_s2s = incoming_s2s;
5349
0d11e393201f s2smanager: Use unused local, reduce table indexing
Kim Alvefur <zash@zash.se>
parents: 5306
diff changeset
23 local fire_event = prosody.events.fire_event;
621
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
24
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
25 local _ENV = nil;
8555
4f0f5b49bb03 vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7950
diff changeset
26 -- luacheck: std none
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
28 local function new_incoming(conn)
9936
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
29 local host_session = sessionlib.new("s2sin");
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
30 sessionlib.set_id(host_session);
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
31 sessionlib.set_logger(host_session);
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
32 sessionlib.set_conn(host_session, conn);
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
33 host_session.direction = "incoming";
9938
4e8ba156738b core.s2smanager: Fix previous commit (Thanks Martin)
Kim Alvefur <zash@zash.se>
parents: 9936
diff changeset
34 host_session.hosts = {};
9934
69982753fe4b core.s2smanager: Rename variable to be same in two functions
Kim Alvefur <zash@zash.se>
parents: 9787
diff changeset
35 incoming_s2s[host_session] = true;
69982753fe4b core.s2smanager: Rename variable to be same in two functions
Kim Alvefur <zash@zash.se>
parents: 9787
diff changeset
36 return host_session;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
39 local function new_outgoing(from_host, to_host)
9936
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
40 local host_session = sessionlib.new("s2sout");
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
41 sessionlib.set_id(host_session);
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
42 sessionlib.set_logger(host_session);
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
43 host_session.to_host = to_host;
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
44 host_session.from_host = from_host;
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
45 host_session.host = from_host;
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
46 host_session.notopen = true;
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
47 host_session.direction = "outgoing";
4555
3dce04129693 s2smanager, mod_s2s, mod_s2s/s2sout: Split connection handling out of s2smanager into mod_s2s, and further split connection logic for s2sout to a module lib, s2sout.lib.lua
Matthew Wild <mwild1@gmail.com>
parents: 4461
diff changeset
48 hosts[from_host].s2sout[to_host] = host_session;
3dce04129693 s2smanager, mod_s2s, mod_s2s/s2sout: Split connection handling out of s2smanager into mod_s2s, and further split connection logic for s2sout to a module lib, s2sout.lib.lua
Matthew Wild <mwild1@gmail.com>
parents: 4461
diff changeset
49 return host_session;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
2746
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
52 local resting_session = { -- Resting, not dead
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
53 destroyed = true;
2915
f47bd0f7e2e6 sessionmanager, s2smanager: Add type of ?2s_destroyed to resting sessions (fixes a logging traceback, thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2892
diff changeset
54 type = "s2s_destroyed";
2748
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
55 open_stream = function (session)
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
56 session.log("debug", "Attempt to open stream on resting session");
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
57 end;
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
58 close = function (session)
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
59 session.log("debug", "Attempt to close already-closed session");
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
60 end;
9787
6625efab91e2 core.s2smanager: Add stub reset_stream method to destroyed sessions
Kim Alvefur <zash@zash.se>
parents: 8675
diff changeset
61 reset_stream = function (session)
6625efab91e2 core.s2smanager: Add stub reset_stream method to destroyed sessions
Kim Alvefur <zash@zash.se>
parents: 8675
diff changeset
62 session.log("debug", "Attempt to reset stream of already-closed session");
6625efab91e2 core.s2smanager: Add stub reset_stream method to destroyed sessions
Kim Alvefur <zash@zash.se>
parents: 8675
diff changeset
63 end;
6663
d3023dd07cb6 portmanager, s2smanager, sessionmanager, stanza_router, storagemanager, usermanager, util.xml: Add luacheck annotations
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
64 filter = function (type, data) return data; end; --luacheck: ignore 212/type
2746
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
65 }; resting_session.__index = resting_session;
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
66
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
67 local function retire_session(session, reason)
6663
d3023dd07cb6 portmanager, s2smanager, sessionmanager, stanza_router, storagemanager, usermanager, util.xml: Add luacheck annotations
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
68 local log = session.log or log; --luacheck: ignore 431/log
2746
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
69 for k in pairs(session) do
5447
92b88476873a sessionmanager, s2smanager: Remove open_session tracing
Matthew Wild <mwild1@gmail.com>
parents: 5367
diff changeset
70 if k ~= "log" and k ~= "id" and k ~= "conn" then
2746
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
71 session[k] = nil;
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
72 end
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
73 end
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
74
4018
5061c8d41d89 s2smanager: retire_session(): Add a 'reason' parameter
Matthew Wild <mwild1@gmail.com>
parents: 4017
diff changeset
75 session.destruction_reason = reason;
5061c8d41d89 s2smanager: retire_session(): Add a 'reason' parameter
Matthew Wild <mwild1@gmail.com>
parents: 4017
diff changeset
76
10102
77a21eef243b core.s2smanager: Remove use of tostring in logging
Kim Alvefur <zash@zash.se>
parents: 9938
diff changeset
77 function session.send(data) log("debug", "Discarding data sent to resting session: %s", data); end
77a21eef243b core.s2smanager: Remove use of tostring in logging
Kim Alvefur <zash@zash.se>
parents: 9938
diff changeset
78 function session.data(data) log("debug", "Discarding data received from resting session: %s", data); end
7452
d916703d5e18 s2smanager: Include a stub thread on destroyed sessions (thanks Link Mauve)
Kim Alvefur <zash@zash.se>
parents: 6779
diff changeset
79 session.thread = { run = function (_, data) return session.data(data) end };
6691
c6c996410064 s2smanager: Make sure destroyed sessions have a sends2s method
Kim Alvefur <zash@zash.se>
parents: 5459
diff changeset
80 session.sends2s = session.send;
2746
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
81 return setmetatable(session, resting_session);
3b9547fc0bed sessionmanager, s2smanager: Destroyed sessions are now simply resting (not dead) until they are collected - prevents a whole class of tracebacks
Matthew Wild <mwild1@gmail.com>
parents: 2714
diff changeset
82 end
2857
6036c4b75235 sessionmanager, s2smanager: Give sessions dummy data handlers that log when data is received by a destroyed session
Matthew Wild <mwild1@gmail.com>
parents: 2712
diff changeset
83
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
84 local function destroy_session(session, reason)
2749
8dc5f3651501 s2smanager: Don't re-destroy destroyed sessions
Matthew Wild <mwild1@gmail.com>
parents: 2748
diff changeset
85 if session.destroyed then return; end
10103
18cab054364a core.s2smanager: Rewrite log line to use formatting instead of concatenation
Kim Alvefur <zash@zash.se>
parents: 10102
diff changeset
86 local log = session.log or log;
18cab054364a core.s2smanager: Rewrite log line to use formatting instead of concatenation
Kim Alvefur <zash@zash.se>
parents: 10102
diff changeset
87 log("debug", "Destroying %s session %s->%s%s%s", session.direction, session.from_host, session.to_host, reason and ": " or "", reason or "");
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5459
diff changeset
88
164
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
89 if session.direction == "outgoing" then
260
182f0c895676 Now outgoing s2s sessions are associated with their from_host, fixes #15
Matthew Wild <mwild1@gmail.com>
parents: 259
diff changeset
90 hosts[session.from_host].s2sout[session.to_host] = nil;
4555
3dce04129693 s2smanager, mod_s2s, mod_s2s/s2sout: Split connection handling out of s2smanager into mod_s2s, and further split connection logic for s2sout to a module lib, s2sout.lib.lua
Matthew Wild <mwild1@gmail.com>
parents: 4461
diff changeset
91 session:bounce_sendq(reason);
621
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
92 elseif session.direction == "incoming" then
cd2cab5400fc Add support for dialback piggy-backing. Fixes #37. Thanks to CShadowRun for helping me test :)
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
93 incoming_s2s[session] = nil;
164
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
94 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5459
diff changeset
95
3488
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
96 local event_data = { session = session, reason = reason };
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
97 if session.type == "s2sout" then
5349
0d11e393201f s2smanager: Use unused local, reduce table indexing
Kim Alvefur <zash@zash.se>
parents: 5306
diff changeset
98 fire_event("s2sout-destroyed", event_data);
3488
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
99 if hosts[session.from_host] then
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
100 hosts[session.from_host].events.fire_event("s2sout-destroyed", event_data);
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
101 end
3489
1b76d18e8045 s2smanager: Don't fire s2sin-destroyed for sessions that were never fully established (thanks Thomas)
Matthew Wild <mwild1@gmail.com>
parents: 3488
diff changeset
102 elseif session.type == "s2sin" then
5349
0d11e393201f s2smanager: Use unused local, reduce table indexing
Kim Alvefur <zash@zash.se>
parents: 5306
diff changeset
103 fire_event("s2sin-destroyed", event_data);
3488
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
104 if hosts[session.to_host] then
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
105 hosts[session.to_host].events.fire_event("s2sin-destroyed", event_data);
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
106 end
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
107 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5459
diff changeset
108
4019
80aa47c009f0 s2smanager: destroy_session(): Pass reason to retire_session() and return true on successful destruction
Matthew Wild <mwild1@gmail.com>
parents: 4018
diff changeset
109 retire_session(session, reason); -- Clean session until it is GC'd
80aa47c009f0 s2smanager: destroy_session(): Pass reason to retire_session() and return true on successful destruction
Matthew Wild <mwild1@gmail.com>
parents: 4018
diff changeset
110 return true;
164
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
111 end
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
112
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
113 return {
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
114 incoming_s2s = incoming_s2s;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
115 new_incoming = new_incoming;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
116 new_outgoing = new_outgoing;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
117 retire_session = retire_session;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
118 destroy_session = destroy_session;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
119 };