Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
71 changes: 36 additions & 35 deletions lib/ruby_bosh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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={})
Expand All @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
34 changes: 17 additions & 17 deletions ruby_bosh.gemspec
Original file line number Diff line number Diff line change
@@ -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|
Expand All @@ -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
Expand All @@ -56,4 +57,3 @@ Gem::Specification.new do |s|
s.add_dependency(%q<hpricot>, [">= 0"])
end
end