diff --git a/Rakefile b/Rakefile index bfa451c..3908fc6 100644 --- a/Rakefile +++ b/Rakefile @@ -13,7 +13,7 @@ begin s.add_dependency("builder") s.add_dependency("rest-client") s.add_dependency("hpricot") - s.add_dependency("SystemTimer") + s.add_dependency("SystemTimer") unless RUBY_VERSION =~ /1\.9/ end rescue LoadError puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com" diff --git a/lib/ruby_bosh.rb b/lib/ruby_bosh.rb index 73451d3..91e2174 100644 --- a/lib/ruby_bosh.rb +++ b/lib/ruby_bosh.rb @@ -3,6 +3,8 @@ require 'rexml/document' require 'base64' require 'hpricot' +require 'timeout' +require 'system_timer' unless RUBY_VERSION =~ /1\.9/ class RubyBOSH BOSH_XMLNS = 'http://jabber.org/protocol/httpbind' @@ -18,9 +20,13 @@ class AuthFailed < RubyBOSH::Error; end class ConnFailed < RubyBOSH::Error; end @@logging = true + @@fast_mode = false def self.logging=(value) @@logging = value end + def self.fast_mode=(value) + @@fast_mode = value + end attr_accessor :jid, :rid, :sid, :success def initialize(jid, pw, service_url, opts={}) @@ -46,18 +52,33 @@ def self.initialize_session(*args) def connect initialize_bosh_session - if send_auth_request - send_restart_request - request_resource_binding - @success = send_session_request + if send_auth_request + @success = send_restart_request + unless @@fast_mode + request_resource_binding + @success = send_session_request + end end - raise RubyBOSH::AuthFailed, "could not authenticate #{@jid}" unless success? + raise RubyBOSH::AuthFailed, "count not authenticate #{@jid}" unless success? @rid += 1 #updates the rid for the next call from the browser - + [@jid, @sid, @rid] end + def deliver(xml) + timeout(@timeout) do + send(xml) + recv(RestClient.post(@service_url, xml, @headers)) + end + rescue ::Timeout::Error => e + raise RubyBOSH::TimeoutError, e.message + rescue Errno::ECONNREFUSED => e + raise RubyBOSH::ConnFailed, "could not connect to #{@host}\n#{e.message}" + rescue Exception => e + raise RubyBOSH::Error, e.message + end + private def initialize_bosh_session response = deliver(construct_body(:wait => @wait, :to => @host, @@ -131,35 +152,16 @@ def parse(_response) _response end - begin - require 'system_timer' - def deliver(xml) - SystemTimer.timeout(@timeout) do - send(xml) - recv(RestClient.post(@service_url, xml, @headers)) + def timeout(secs, &block) + if defined?(SystemTimer) + SystemTimer.timeout(secs) do + block.call end - rescue Timeout::Error => e - raise RubyBOSH::TimeoutError, e.message - rescue Errno::ECONNREFUSED => e - raise RubyBOSH::ConnFailed, "could not connect to #{@host}\n#{e.message}" - rescue Exception => e - raise RubyBOSH::Error, e.message - end - rescue LoadError - warn "WARNING: using the built-in Timeout class which is known to have issues when used for opening connections. Install the SystemTimer gem if you want to make sure the Redis client will not hang." unless RUBY_VERSION >= "1.9" || RUBY_PLATFORM =~ /java/ - - require "timeout" - def deliver(xml) - Timeout.timeout(@timeout) do - send(xml) - recv(RestClient.post(@service_url, xml, @headers)) + else + warn "WARNING: using the built-in Timeout class which is known to have issues when used for opening connections. Install the SystemTimer gem if you want to make sure the Redis client will not hang." unless RUBY_VERSION >= "1.9" || RUBY_PLATFORM =~ /java/ + ::Timeout::timeout(secs) do + block.call end - rescue Timeout::Error => e - raise RubyBOSH::TimeoutError, e.message - rescue Errno::ECONNREFUSED => e - raise RubyBOSH::ConnFailed, "could not connect to #{@host}\n#{e.message}" - rescue Exception => e - raise RubyBOSH::Error, e.message end end @@ -168,10 +170,9 @@ def send(msg) end def recv(msg) - puts("Ruby-BOSH - RECV\n[#{now}]: #{msg}") if @logging; msg + puts("Ruby-BOSH - RECV\n[#{now}]: #{msg}") if @@logging; msg end - private def now Time.now.strftime("%a %b %d %H:%M:%S %Y") end diff --git a/ruby_bosh.gemspec b/ruby_bosh.gemspec index 7b3ed6b..a2bec0b 100644 --- a/ruby_bosh.gemspec +++ b/ruby_bosh.gemspec @@ -1,6 +1,6 @@ # Generated by jeweler -# DO NOT EDIT THIS FILE DIRECTLY -# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' +# DO NOT EDIT THIS FILE +# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec` # -*- encoding: utf-8 -*- Gem::Specification.new do |s| @@ -14,28 +14,29 @@ Gem::Specification.new do |s| s.email = %q{pradeep@intridea.com} s.extra_rdoc_files = [ "LICENSE", - "README", - "TODO" + "README" ] s.files = [ - "LICENSE", - "README", - "Rakefile", - "TODO", - "VERSION.yml", - "autotest/discover.rb", - "lib/ruby_bosh.rb", - "ruby_bosh.gemspec", - "spec/ruby_bosh_spec.rb", - "spec/spec_helper.rb" + ".gitignore", + "LICENSE", + "README", + "Rakefile", + "TODO", + "VERSION.yml", + "autotest/discover.rb", + "lib/ruby_bosh.rb", + "ruby_bosh.gemspec", + "spec/ruby_bosh_spec.rb", + "spec/spec_helper.rb" ] s.homepage = %q{http://github.com/skyfallsin/ruby_bosh} + s.rdoc_options = ["--charset=UTF-8"] s.require_paths = ["lib"] - s.rubygems_version = %q{1.6.2} + s.rubygems_version = %q{1.5.2} s.summary = %q{A BOSH session pre-initializer for Ruby web applications} s.test_files = [ "spec/ruby_bosh_spec.rb", - "spec/spec_helper.rb" + "spec/spec_helper.rb" ] if s.respond_to? :specification_version then @@ -56,4 +57,3 @@ Gem::Specification.new do |s| s.add_dependency(%q, [">= 0"]) end end -