Permalink
Browse files

added timeouts, exceptions

  • Loading branch information...
1 parent 7ce6a3d commit 397c9eb3a43318b2f1070c7be87b2048ff04958c Pradeep Elankumaran committed Feb 18, 2009
Showing with 34 additions and 19 deletions.
  1. +5 −0 README
  2. +2 −0 TODO
  3. +27 −19 lib/ruby_bosh.rb
View
5 README
@@ -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):
View
2 TODO
@@ -0,0 +1,2 @@
+write basic tests
+
View
@@ -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
@@ -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
@@ -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)
@@ -65,7 +75,6 @@ def send_auth_request
end
response = deliver(request)
- # TODO: make the following more robust
response.include?("success")
end
@@ -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)
@@ -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
@@ -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.