Software /
code /
verse
Annotate
bosh.lua @ 87:d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 06 Aug 2010 16:28:50 +0100 |
child | 89:1752a9097e6b |
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"; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 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
|
6 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
|
7 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 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
|
9 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
|
10 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 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
|
12 local stream = { |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 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
|
14 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
|
15 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
|
16 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
|
17 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
|
18 conn = {}; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 }; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 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
|
21 self.bosh_need_restart = true; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 self:flush(true); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 end |
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.bosh_response_handler(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
|
25 return stream:_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
|
26 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 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
|
28 conn:add_plugin("http"); |
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 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 function stream_mt:flush(force) |
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 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 and (force or #self.bosh_outgoing_buffer > 0) then |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 local request = self.http.request(self.bosh_url, { body = tostring(payload) }, self.bosh_response_handler); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 table.insert(self.bosh_waiting_requests, request); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 else |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 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
|
57 end |
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 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 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
|
61 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
|
62 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 -- XEP-0124 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 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
|
65 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
|
66 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
|
67 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
|
68 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 -- XEP-0206 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 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
|
71 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
|
72 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
|
73 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 self.http.request(self.bosh_url, { body = tostring(body) }, function (response) |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 -- 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
|
76 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
|
77 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
|
78 self:warn("Invalid 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
|
79 self:event("disconnected"); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 return; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 self.bosh_sid = payload.attr.sid; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 self.bosh_wait = tonumber(payload.attr.wait); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 self.bosh_hold = tonumber(payload.attr.hold); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 self.bosh_max_requests = tonumber(payload.attr.requests) or self.bosh_hold; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 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
|
87 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
|
88 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
|
89 end); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 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
|
93 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
|
94 self:warn("Server replied to request that wasn't the oldest"); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 else |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 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
|
97 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 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
|
99 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
|
100 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
|
101 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 if #self.bosh_waiting_requests == 0 then |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 self:debug("We have no requests open, so forcing flush..."); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 self:flush(true); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 else |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 self:debug("We have %d requests open, so no need to force a flush", #self.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
|
107 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 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
|
116 else |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 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
|
118 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 }; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 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
|
129 self:debug("Parsing response: %s", response); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 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
|
131 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
|
132 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
|
133 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
|
134 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 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
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 }); |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 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
|
145 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
|
146 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
|
147 end |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 return body; |
d59073722924
verse.bosh: Use verse.new_bosh(logger, url) to make a BOSH connection
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 end |