This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sessionManager.lua
297 lines (256 loc) · 8.66 KB
/
sessionManager.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
local M = {};
local Jingle = require "basejingle";
local global = {
c = {},
v = {},
conf = {},
sessions = {},
peers = {},
iceServers = {},
prepareSession = {},
};
local features = {
"urn:xmpp:jingle:1",
"urn:xmpp:jingle:apps:rtp:1",
"urn:xmpp:jingle:apps:rtp:audio",
"urn:xmpp:jingle:apps:rtp:video",
"urn:xmpp:jingle:apps:rtp:rtcb-fb:0",
"urn:xmpp:jingle:apps:rtp:rtp-hdrext:0",
"urn:xmpp:jingle:apps:rtp:ssma:0",
"urn:xmpp:jingle:apps:dtls:0",
"urn:xmpp:jingle:apps:grouping:0",
"urn:xmpp:jingle:apps:file-transfer:3",
"urn:xmpp:jingle:transports:ice-udp:1",
"urn:xmpp:jingle:transports.dtls-sctp:1",
"urn:ietf:rfc:3264",
"urn:ietf:rfc:5576",
"urn:ietf:rfc:5888",
"http://jitsi.org/protocol/colibri"
};
local xmlns_jingle = "urn:xmpp:jingle:1";
local xmlns_jingle_error = "urn:xmpp:jingle:errors:1";
M.newSession = function (o)
o.client = global.c;
o.verse = global.v;
local session = {};
local peer = o.peer;
o.local_state = {};
o.remote_state = {};
if global.conf["createSession"] then
session = global.conf["createSession"](o);
else
session = Jingle:new(o);
end
if not global.peers[peer] then
global.peers[peer] = {};
end
table.insert(global.peers[peer], session);
global.sessions[o.sid] = session;
return session;
end
M.closeSession = function (id)
end
M.addICEServer = function (server)
table.insert(global.iceServers, server);
end
M.endPeerSessions = function (peer, reason, silent)
end
M.getSessionsByJID = function (jid)
local sessions = global.peers[jid]
if not sessions then
return nil
end
local copy = {}
for orig_key, orig_session in pairs(sessions) do
copy[orig_key] = orig_session
end
return copy
end
M.getSessionBySID = function (sid)
return global.sessions[sid];
end
M.removeSessionFromPeers = function (session)
local jid = session.peer
local sessions = global.peers[jid]
for i=#sessions,1,-1 do
if session.sid == sessions[i].sid then
table.remove(sessions, i)
end
end
if #sessions == 0 then
global.peers[jid] = nil
end
end
M.endSessionBySID = function (sid, reason, notify)
local sess = global.sessions[sid]
if sess then
global.sessions[sid] = nil
M.removeSessionFromPeers(sess)
if notify then
sess:terminate(reason)
end
end
global.c:event("jingle/session-terminate", sid)
end
M.onSessionTerminate = function (req, sid)
global.c:send(verse.reply(req));
M.endSessionBySID(sid, nil, false)
return true
end
M.endSessionsForJID = function (jid, reason, notify)
local sessions = M.getSessionsByJID(jid);
if not sessions then
return
end
for _, session in ipairs(sessions) do
M.endSessionBySID(session.sid, reason, notify)
end
end
M.handle = function (req)
local tag = req:get_child('jingle', xmlns_jingle);
local sid = tag.attr.sid;
local session = global.sessions[sid];
local rid = req.attr.id;
local type = req.attr.type;
local sender = req.attr.from;
if type == "error" then
local error_tag = req:get_child('error');
local tie_break = false;
if (error_tag) then
local tie_break_tag = error_tag:get_child('tie-break', xmlns_jingle_error);
if (tie_break_tag) then
tie_break = true;
end
end
if (session and tie_break and self.isPending) then
session:close("alternative-session");
else
if (session) then
session.pendingAction = false;
end
end
return global.c:event("jingle-error", req);
end
if type == "result" then
if session then
session.pendingAction = false;
end
return;
end
local action = tag.attr.action;
local descriptionTypes = {};
local dTCount = 0;
for child_tag in tag:children() do
if child_tag.name == "content" then
local description_tag = child_tag:child_with_name("description");
if description_tag and description_tag.attr.media then
dTCount = dTCount + 1;
descriptionTypes[description_tag.attr.media.."/"..description_tag.attr.xmlns] = true;
end
end
if child_tag.name == "transport" then
--something something transport type
end
end
if action ~= "session-initiate" then
if not session then
--print("Unknown jingle session "..sid);
local error_stanza = global.v.error_reply(req, 'cancel', 'item-not-found'):tag("unknown-session", { xmlns = xmlns_jingle_error }):up();
global.c:send(error_stanza);
return true;
end
if session.peerID ~= sender or session:check('ended') then
--print("Session has ended or action has the wrong sender");
local error_stanza = req:error_reply('cancel', 'conflict'):tag("tie-break", {xmlns = xmlns_jingle_error}):up();
global.c:send(error_stanza);
return true;
end
if action == "session-accept" and not session.isPending then
--print("Tried to accept the session more than once");
local error_stanza = req:error_reply('cancel', 'unexpected-request'):tag("out-of-order", {xmlns = xmlns_jingle_error}):up();
global.c:send(error_stanza);
return true;
end
if action ~= "session-terminate" and action == session.pendingAction then
--print("Tie break during pending request");
local error_stanza = req:error_reply('cancel', 'conflict'):tag("tie-break", {xmlns = xmlns_jingle_error}):up();
global.c:send(error_stanza);
return true;
end
elseif (session) then
if session.peerID ~= sender then
--print("Duplicate sid from new sender");
local error_stanza = req:error_reply('cancel', "service-unavailable");
global.c:send(error_stanza);
return true;
end
if session.isPending then
if c.bound > session.peerID then
--print("Tie break new session because of duplicate sids");
local error_stanza = req:error_reply('cancel', 'conflict'):tag("tie-break", {xmlns = xmlns_jingle_error}):up();
global.c:send(error_stanza);
return true;
end
else
--print("Someone is doing this wrong");
local error_stanza = req:error_reply('cancel', 'unexpected-request'):tag("out-of-order", {xmlns = xmlns_jingle_error}):up();
global.c:send(error_stanza);
return true;
end
--[[
elseif (global.peers[sender] and #global.peers[sender] > 0) then
--it's a tie!
for sess_idx, sess in ipairs(global.peers[sender]) do
if sess and sess.isPending then
local conflict = false;
for desc_idx, desc_bool in pairs(descriptionTypes) do
if session.pendingDescriptionTypes[desc_idx] then
conflict = true;
break;
end
end
if conflict and sess.sid > sid then
--we won the tie breaker
print("Tie break");
local error_stanza = req:error_reply('cancel', 'conflict'):tag("tie-break", {xmlns = xmlns_jingle_error}):up();
global.c:send(error_stanza);
return true;
end
end
end
--]]
end
if action == "session-initiate" then
--for reals this time
if dTCount == 0 then
--print("session initiate has nothing we need");
local error_stanza = global.v.error_reply(req, 'cancel', 'bad-reqeust');
global.c:send(error_stanza);
return true;
end
local new_session = M.newSession({
sid = sid,
peer = req.attr.from,
peerID = sender,
initiator = false,
descriptionTypes = descriptionTypes,
transportTypes = transportTypes,
req = req,
});
return new_session:process(action, req);
elseif action == "session-terminate" then
M.onSessionTerminate(req, sid)
elseif session then
return session:process(action, req);
end
end;
M.init = function (verse, client, conf)
global.v = verse;
global.c = client;
global.conf = conf;
global.c:hook("iq/"..xmlns_jingle, M.handle);
for key, feature in pairs(features) do
global.c:add_disco_feature(feature);
end
end
return M;