Skip to content

Commit

Permalink
added timeouts, exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradeep Elankumaran committed Feb 18, 2009
1 parent 7ce6a3d commit 397c9eb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
5 changes: 5 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ This method allows you to hide authentication details for your users' XMPP accou

Tested on Rails 2.x with eJabberd 1.2+

References
==========
BOSH: http://xmpp.org/extensions/xep-0124.html
XMPP via BOSH: http://xmpp.org/extensions/xep-0206.html

Example
=======
In your Ruby app controller (or equivalent):
Expand Down
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
write basic tests

46 changes: 27 additions & 19 deletions lib/ruby_bosh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'rexml/document'
require 'base64'
require 'hpricot'
require 'system_timer'

# based on
# http://code.stanziq.com/svn/strophe/trunk/strophejs/examples/attach/boshclient.py
Expand All @@ -13,17 +14,33 @@ class RubyBOSHClient
BIND_XMLNS = 'urn:ietf:params:xml:ns:xmpp-bind'
SESSION_XMLNS = 'urn:ietf:params:xml:ns:xmpp-session'

class Timeout < StandardError; end

attr_accessor :jid, :rid, :sid, :success
def initialize(jid, pw, service_url)
def initialize(jid, pw, service_url, opts={})
@service_url = service_url
@jid, @pw = jid, pw
@host = jid.split("@").last
@success = false
@timeout = opts[:timeout] || 3 #seconds
@headers = {"Content-Type" => "text/xml; charset=utf-8",
"Accept" => "text/xml"}
connect
end

def initialize_bosh_session
response = deliver(construct_body(:wait => 5, :to => @host,
:hold => 3, :window => 5,
"xmpp:version" => '1.0'))
parse(response)
end

def success?
success == true
end

private

def connect
initialize_bosh_session
if send_auth_request
Expand All @@ -36,13 +53,6 @@ def connect
@rid+=1 #send this directly to the browser
end
end

def initialize_bosh_session
response = deliver(construct_body(:wait => 5, :to => @host,
:hold => 3, :window => 5,
"xmpp:version" => '1.0'))
parse(response)
end

def construct_body(params={}, &block)
@rid ? @rid+=1 : @rid=rand(100000)
Expand All @@ -65,7 +75,6 @@ def send_auth_request
end

response = deliver(request)
# TODO: make the following more robust
response.include?("success")
end

Expand Down Expand Up @@ -108,14 +117,13 @@ def parse(_response)
_response
end

def success?
success == true
end

private
def deliver(xml)
send(xml)
recv(RestClient.post(@service_url, xml, @headers))
SystemTimer.timeout(@timeout) do
send(xml)
recv(RestClient.post(@service_url, xml, @headers))
end
rescue SystemTimer::Error => e
raise RubyBOSHClient::Timeout, e.message
end

def send(msg)
Expand All @@ -129,8 +137,8 @@ def recv(msg)


class RubyBOSH
def self.initialize_session(jid, pw, service_url)
conn = RubyBOSHClient.new(jid, pw, service_url)
def self.initialize_session(jid, pw, service_url, opts={})
conn = RubyBOSHClient.new(jid, pw, service_url, opts)
if conn.success?
[conn.jid, conn.sid, conn.rid]
else
Expand All @@ -140,6 +148,6 @@ def self.initialize_session(jid, pw, service_url)
end

if __FILE__ == $0
p RubyBOSH.initialize_session("skyfallsin@localhost", "skyfallsin",
p RubyBOSH.initialize_session(ARGV[0], ARGV[1],
"http://localhost:5280/http-bind")
end

0 comments on commit 397c9eb

Please sign in to comment.