Annotate

core/s2smanager.lua @ 9949:a39f3681d685

net.http.files: Make into standalone library
author Kim Alvefur <zash@zash.se>
date Fri, 05 Apr 2019 17:09:03 +0200
parent 9938:4e8ba156738b
child 10102:77a21eef243b
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;
5459
3a821511b9ec sessionmanager, s2smanager: Remove unused imports
Matthew Wild <mwild1@gmail.com>
parents: 5447
diff changeset
12 local tostring, pairs, setmetatable
3a821511b9ec sessionmanager, s2smanager: Remove unused imports
Matthew Wild <mwild1@gmail.com>
parents: 5447
diff changeset
13 = tostring, pairs, setmetatable;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 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
16 local sessionlib = require "util.session";
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local log = logger_init("s2smanager");
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
3476
193bb0936a4e s2smanager: Fire s2s{in,out}-established when new s2s connections are ready
Matthew Wild <mwild1@gmail.com>
parents: 3459
diff changeset
20 local prosody = _G.prosody;
8675
d3d74e923e4e s2smanager: Explicitly export the incoming_s2s table [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8555
diff changeset
21 local incoming_s2s = {};
d3d74e923e4e s2smanager: Explicitly export the incoming_s2s table [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8555
diff changeset
22 _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
23 prosody.incoming_s2s = incoming_s2s;
5349
0d11e393201f s2smanager: Use unused local, reduce table indexing
Kim Alvefur <zash@zash.se>
parents: 5306
diff changeset
24 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
25
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
26 local _ENV = nil;
8555
4f0f5b49bb03 vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7950
diff changeset
27 -- luacheck: std none
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
29 local function new_incoming(conn)
9936
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
30 local host_session = sessionlib.new("s2sin");
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
31 sessionlib.set_id(host_session);
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
32 sessionlib.set_logger(host_session);
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
33 sessionlib.set_conn(host_session, conn);
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
34 host_session.direction = "incoming";
9938
4e8ba156738b core.s2smanager: Fix previous commit (Thanks Martin)
Kim Alvefur <zash@zash.se>
parents: 9936
diff changeset
35 host_session.hosts = {};
9934
69982753fe4b core.s2smanager: Rename variable to be same in two functions
Kim Alvefur <zash@zash.se>
parents: 9787
diff changeset
36 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
37 return host_session;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
40 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
41 local host_session = sessionlib.new("s2sout");
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
42 sessionlib.set_id(host_session);
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
43 sessionlib.set_logger(host_session);
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
44 host_session.to_host = to_host;
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
45 host_session.from_host = from_host;
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
46 host_session.host = from_host;
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
47 host_session.notopen = true;
8695b50368cd core.s2smanager: Use util.session to create sessions
Kim Alvefur <zash@zash.se>
parents: 9935
diff changeset
48 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
49 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
50 return host_session;
148
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
4c0dcd245d34 s2s works! \o/ \o/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
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
53 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
54 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
55 type = "s2s_destroyed";
2748
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
56 open_stream = function (session)
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
57 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
58 end;
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
59 close = function (session)
85a242cd1bc4 s2smanager: Add open_stream and close methods to resting sessions
Matthew Wild <mwild1@gmail.com>
parents: 2747
diff changeset
60 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
61 end;
9787
6625efab91e2 core.s2smanager: Add stub reset_stream method to destroyed sessions
Kim Alvefur <zash@zash.se>
parents: 8675
diff changeset
62 reset_stream = function (session)
6625efab91e2 core.s2smanager: Add stub reset_stream method to destroyed sessions
Kim Alvefur <zash@zash.se>
parents: 8675
diff changeset
63 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
64 end;
6663
d3023dd07cb6 portmanager, s2smanager, sessionmanager, stanza_router, storagemanager, usermanager, util.xml: Add luacheck annotations
Matthew Wild <mwild1@gmail.com>
parents: 5776
diff changeset
65 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
66 }; 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
67
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
68 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
69 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
70 for k in pairs(session) do
5447
92b88476873a sessionmanager, s2smanager: Remove open_session tracing
Matthew Wild <mwild1@gmail.com>
parents: 5367
diff changeset
71 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
72 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
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 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
75
4018
5061c8d41d89 s2smanager: retire_session(): Add a 'reason' parameter
Matthew Wild <mwild1@gmail.com>
parents: 4017
diff changeset
76 session.destruction_reason = reason;
5061c8d41d89 s2smanager: retire_session(): Add a 'reason' parameter
Matthew Wild <mwild1@gmail.com>
parents: 4017
diff changeset
77
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
78 function session.send(data) log("debug", "Discarding data sent to resting session: %s", tostring(data)); 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
79 function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end
7452
d916703d5e18 s2smanager: Include a stub thread on destroyed sessions (thanks Link Mauve)
Kim Alvefur <zash@zash.se>
parents: 6779
diff changeset
80 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
81 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
82 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
83 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
84
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
85 local function destroy_session(session, reason)
2749
8dc5f3651501 s2smanager: Don't re-destroy destroyed sessions
Matthew Wild <mwild1@gmail.com>
parents: 2748
diff changeset
86 if session.destroyed then return; end
7947
24170d74b00b core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6779
diff changeset
87 (session.log or log)("debug", "Destroying "..tostring(session.direction)
24170d74b00b core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6779
diff changeset
88 .." session "..tostring(session.from_host).."->"..tostring(session.to_host)
24170d74b00b core: Split some very long lines [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6779
diff changeset
89 ..(reason and (": "..reason) or ""));
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5459
diff changeset
90
164
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
91 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
92 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
93 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
94 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
95 incoming_s2s[session] = nil;
164
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
96 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5459
diff changeset
97
3488
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
98 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
99 if session.type == "s2sout" then
5349
0d11e393201f s2smanager: Use unused local, reduce table indexing
Kim Alvefur <zash@zash.se>
parents: 5306
diff changeset
100 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
101 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
102 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
103 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
104 elseif session.type == "s2sin" then
5349
0d11e393201f s2smanager: Use unused local, reduce table indexing
Kim Alvefur <zash@zash.se>
parents: 5306
diff changeset
105 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
106 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
107 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
108 end
4f3fc5f9d944 s2smanager: Fire s2s{in,out}-destroyed when s2s connections are destroyed
Matthew Wild <mwild1@gmail.com>
parents: 3476
diff changeset
109 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5459
diff changeset
110
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
111 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
112 return true;
164
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
113 end
8dc1faa5b1df other half of previous commit
Matthew Wild <mwild1@gmail.com>
parents: 162
diff changeset
114
6779
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
115 return {
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
116 incoming_s2s = incoming_s2s;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
117 new_incoming = new_incoming;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
118 new_outgoing = new_outgoing;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
119 retire_session = retire_session;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
120 destroy_session = destroy_session;
6236668da30a core.*: Remove use of module() function
Kim Alvefur <zash@zash.se>
parents: 6692
diff changeset
121 };