Software /
code /
verse
Annotate
bosh.lua @ 143:641c0a1bb330
plugins.jingle_ft: Change content 'name' field to 'type' to avoid confusion with the 'name' attribute of the <content> tag in Jingle
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 17 Sep 2010 16:40:07 +0100 |
parent | 93:2442e751f3cb |
child | 161:b177bcea2006 |
rev | line source |
---|---|
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local init_xmlhandlers = require "core.xmlhandlers"; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local st = require "util.stanza"; |
89
1752a9097e6b
verse.bosh: Update to use net.http instead of verse.plugins.http
Matthew Wild <mwild1@gmail.com>
parents:
87
diff
changeset
|
4 require "net.httpclient_listener"; -- Required for net.http to work |
1752a9097e6b
verse.bosh: Update to use net.http instead of verse.plugins.http
Matthew Wild <mwild1@gmail.com>
parents:
87
diff
changeset
|
5 local http = require "net.http"; |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local stream_mt = setmetatable({}, { __index = verse.stream_mt }); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 stream_mt.__index = stream_mt; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local xmlns_stream = "http://etherx.jabber.org/streams"; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
13 local reconnect_timeout = 5; |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
14 |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 function verse.new_bosh(logger, url) |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local stream = { |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 bosh_conn_pool = {}; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 bosh_waiting_requests = {}; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 bosh_rid = math.random(1,999999); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 bosh_outgoing_buffer = {}; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 bosh_url = url; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 conn = {}; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 }; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 function stream:reopen() |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 self.bosh_need_restart = true; |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
26 self:flush(); |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local conn = verse.new(logger, stream); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 return setmetatable(conn, stream_mt); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 function stream_mt:connect() |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 self:_send_session_request(); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 function stream_mt:send(data) |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 self:debug("Putting into BOSH send buffer: %s", tostring(data)); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 self.bosh_outgoing_buffer[#self.bosh_outgoing_buffer+1] = st.clone(data); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 self:flush(); --TODO: Optimize by doing this on next tick (give a chance for data to buffer) |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
42 function stream_mt:flush() |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 if self.connected |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 and #self.bosh_waiting_requests < self.bosh_max_requests |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
45 and (#self.bosh_waiting_requests == 0 |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
46 or #self.bosh_outgoing_buffer > 0 |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
47 or self.bosh_need_restart) then |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 self:debug("Flushing..."); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 local payload = self:_make_body(); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 local buffer = self.bosh_outgoing_buffer; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 for i, stanza in ipairs(buffer) do |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 payload:add_child(stanza); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 buffer[i] = nil; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 end |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
55 self:_make_request(payload); |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 else |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 self:debug("Decided not to flush."); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
61 function stream_mt:_make_request(payload) |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
62 local request, err = http.request(self.bosh_url, { body = tostring(payload) }, function (response, code, request) |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
63 if code ~= 0 then |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
64 self.inactive_since = nil; |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
65 return self:_handle_response(response, code, request); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
66 end |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
67 |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
68 -- Connection issues, we need to retry this request |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
69 local time = os.time(); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
70 if not self.inactive_since then |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
71 self.inactive_since = time; -- So we know when it is time to give up |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
72 elseif time - self.inactive_since > self.bosh_max_inactivity then |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
73 return self:_disconnected(); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
74 else |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
75 self:debug("%d seconds left to reconnect, retrying in %d seconds...", |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
76 self.bosh_max_inactivity - (time - self.inactive_since), reconnect_timeout); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
77 end |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
78 |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
79 -- Set up reconnect timer |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
80 timer.add_task(reconnect_timeout, function () |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
81 self:debug("Retrying request..."); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
82 -- Remove old request |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
83 for i, waiting_request in ipairs(self.bosh_waiting_requests) do |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
84 if waiting_request == request then |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
85 table.remove(self.bosh_waiting_requests, i); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
86 break; |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
87 end |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
88 end |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
89 self:_make_request(payload); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
90 end); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
91 end); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
92 if request then |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
93 table.insert(self.bosh_waiting_requests, request); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
94 else |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
95 self:warn("Request failed instantly: %s", err); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
96 end |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
97 end |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
98 |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
99 function stream_mt:_disconnected() |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
100 self.connected = nil; |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
101 self:event("disconnected"); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
102 end |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
103 |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 function stream_mt:_send_session_request() |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 local body = self:_make_body(); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 -- XEP-0124 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 body.attr.hold = "1"; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 body.attr.wait = "60"; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 body.attr["xml:lang"] = "en"; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 body.attr.ver = "1.6"; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 -- XEP-0206 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 body.attr.from = self.jid; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 body.attr.to = self.host; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 body.attr.secure = 'true'; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
118 http.request(self.bosh_url, { body = tostring(body) }, function (response, code) |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
119 if code == 0 then |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
120 -- Failed to connect |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
121 return self:_disconnected(); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
122 end |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 -- Handle session creation response |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 local payload = self:_parse_response(response) |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 if not payload then |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 self:warn("Invalid session creation response"); |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
127 self:_disconnected(); |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 return; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 end |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
130 self.bosh_sid = payload.attr.sid; -- Session id |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
131 self.bosh_wait = tonumber(payload.attr.wait); -- How long the server may hold connections for |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
132 self.bosh_hold = tonumber(payload.attr.hold); -- How many connections the server may hold |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
133 self.bosh_max_inactivity = tonumber(payload.attr.inactivity); -- Max amount of time with no connections |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
134 self.bosh_max_requests = tonumber(payload.attr.requests) or self.bosh_hold; -- Max simultaneous requests we can make |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 self.connected = true; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 self:event("connected"); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 self:_handle_response_payload(payload); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 end); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 function stream_mt:_handle_response(response, code, request) |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 if self.bosh_waiting_requests[1] ~= request then |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 self:warn("Server replied to request that wasn't the oldest"); |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
144 for i, waiting_request in ipairs(self.bosh_waiting_requests) do |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
145 if waiting_request == request then |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
146 self.bosh_waiting_requests[i] = nil; |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
147 break; |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
148 end |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
149 end |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 else |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 table.remove(self.bosh_waiting_requests, 1); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 local payload = self:_parse_response(response); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 if payload then |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 self:_handle_response_payload(payload); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 end |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
157 self:flush(); |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 function stream_mt:_handle_response_payload(payload) |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 for stanza in payload:childtags() do |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 if stanza.attr.xmlns == xmlns_stream then |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 self:event("stream-"..stanza.name, stanza); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 elseif stanza.attr.xmlns then |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 self:event("stream/"..stanza.attr.xmlns, stanza); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 else |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 self:event("stanza", stanza); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 end |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
170 if payload.attr.type == "terminate" then |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
171 self:_disconnected({reason = payload.attr.condition}); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
172 end |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 local stream_callbacks = { |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 stream_ns = "http://jabber.org/protocol/httpbind", stream_tag = "body", |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 default_ns = "jabber:client", |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 streamopened = function (session, attr) session.notopen = nil; session.payload = verse.stanza("body", attr); return true; end; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 handlestanza = function (session, stanza) session.payload:add_child(stanza); end; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 }; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 function stream_mt:_parse_response(response) |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 self:debug("Parsing response: %s", response); |
93
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
183 if response == nil then |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
184 self:debug("%s", debug.traceback()); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
185 self:_disconnected(); |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
186 return; |
2442e751f3cb
verse.bosh: Implemented retry/reconnect logic, and handling of disconnects (either CM-intiated or due to connection failures)
Matthew Wild <mwild1@gmail.com>
parents:
89
diff
changeset
|
187 end |
87
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 local session = { notopen = true, log = self.log }; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "\1"); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 parser:parse(response); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 return session.payload; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 function stream_mt:_make_body() |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 self.bosh_rid = self.bosh_rid + 1; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 local body = verse.stanza("body", { |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 xmlns = xmlns_bosh; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 content = "text/xml; charset=utf-8"; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 sid = self.bosh_sid; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 rid = self.bosh_rid; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 }); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 if self.bosh_need_restart then |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 self.bosh_need_restart = nil; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 body.attr.restart = 'true'; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 return body; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 end |