comparison mod_bidi/mod_bidi.lua @ 893:602e4c509095

mod_bidi: Close conflicting outgoing sessions when bidi is initiated, not requested.
author Kim Alvefur <zash@zash.se>
date Sat, 12 Jan 2013 02:36:30 +0100
parents 148865199003
children d066987e00b7
comparison
equal deleted inserted replaced
892:148865199003 893:602e4c509095
26 end 26 end
27 27
28 local function new_bidi(origin) 28 local function new_bidi(origin)
29 local bidi_session, remote_host; 29 local bidi_session, remote_host;
30 origin.log("debug", "Creating bidirectional session wrapper"); 30 origin.log("debug", "Creating bidirectional session wrapper");
31 if origin.direction == "incoming" then 31 if origin.direction == "incoming" then -- then we create an "outgoing" bidirectional session
32 local conflicting_session = hosts[origin.to_host].s2sout[origin.from_host]
33 if conflicting_session then
34 conflicting_session.log("warn", "We already have an outgoing connection to %s, closing it...", origin.from_host);
35 conflicting_session:close{ condition = "conflict", text = "Replaced by bidirectional stream" }
36 s2smanager.destroy_session(conflicting_session);
37 end
32 remote_host = origin.from_host; 38 remote_host = origin.from_host;
33 bidi_session = s2smanager.new_outgoing(origin.to_host, origin.from_host) 39 bidi_session = s2smanager.new_outgoing(origin.to_host, origin.from_host)
34 else -- outgoing 40 else -- outgoing -- then we create an "incoming" bidirectional session
35 remote_host = origin.to_host; 41 remote_host = origin.to_host;
36 bidi_session = s2smanager.new_incoming(origin.conn) 42 bidi_session = s2smanager.new_incoming(origin.conn)
37 bidi_session.to_host = origin.from_host; 43 bidi_session.to_host = origin.from_host;
38 bidi_session.from_host = origin.to_host; 44 bidi_session.from_host = origin.to_host;
39 add_filter(origin, "stanzas/in", function(stanza) 45 add_filter(origin, "stanzas/in", function(stanza)
73 79
74 module:hook("stanza/urn:xmpp:bidi:bidi", function(event) 80 module:hook("stanza/urn:xmpp:bidi:bidi", function(event)
75 local origin = event.session or event.origin; 81 local origin = event.session or event.origin;
76 if not origin.is_bidi and not origin.bidi_session then 82 if not origin.is_bidi and not origin.bidi_session then
77 module:log("debug", "%s requested bidirectional stream", origin.from_host); 83 module:log("debug", "%s requested bidirectional stream", origin.from_host);
78 if hosts[module.host].s2sout[origin.from_host] then
79 local conflicting_session = hosts[module.host].s2sout[origin.from_host]
80 conflicting_session.log("warn", "We already have an outgoing connection to %s, closing it...", origin.from_host);
81 conflicting_session:close{ condition = "conflict", text = "Replaced by bidirectional stream" }
82 s2smanager.destroy_session(conflicting_session);
83 end
84 origin.do_bidi = true; 84 origin.do_bidi = true;
85 return true; 85 return true;
86 end 86 end
87 end); 87 end);
88 88