From db4b45f138b07ee1ea5718aa4ca090b121e841df Mon Sep 17 00:00:00 2001 From: tomlion Date: Wed, 6 Apr 2011 21:47:14 +0800 Subject: [PATCH 1/8] remove SystemTimer dependency when ruby version greater than 1.9 --- Rakefile | 2 +- ruby_bosh.gemspec | 39 ++++++++++++++++++--------------------- 2 files changed, 19 insertions(+), 22 deletions(-) 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/ruby_bosh.gemspec b/ruby_bosh.gemspec index 5407324..90b596b 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| @@ -9,33 +9,34 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Pradeep Elankumaran"] - s.date = %q{2011-03-17} + s.date = %q{2011-04-06} s.description = %q{An XMPP BOSH session pre-initializer for Ruby web applications} 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 @@ -45,18 +46,14 @@ Gem::Specification.new do |s| s.add_runtime_dependency(%q, [">= 0"]) s.add_runtime_dependency(%q, [">= 0"]) s.add_runtime_dependency(%q, [">= 0"]) - s.add_runtime_dependency(%q, [">= 0"]) else s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) end else s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) end end - From a9d1f6e134b93d218eb371b514cdd783c41d463c Mon Sep 17 00:00:00 2001 From: tomlion Date: Wed, 6 Apr 2011 21:54:53 +0800 Subject: [PATCH 2/8] don't require 'system_timer' if ruby version greater than 1.9 --- lib/ruby_bosh.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby_bosh.rb b/lib/ruby_bosh.rb index 014c79e..364b962 100644 --- a/lib/ruby_bosh.rb +++ b/lib/ruby_bosh.rb @@ -4,7 +4,7 @@ require 'base64' require 'hpricot' require 'timeout' -require 'system_timer' +require 'system_timer' unless RUBY_VERSION =~ /1\.9/ class RubyBOSH BOSH_XMLNS = 'http://jabber.org/protocol/httpbind' From e33799d73ab2162ac267bb8aad8544a78f5f923e Mon Sep 17 00:00:00 2001 From: tomlion Date: Fri, 8 Apr 2011 19:30:27 +0800 Subject: [PATCH 3/8] use stand Timeout's timeout if SystemTimer is not defined --- lib/ruby_bosh.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/ruby_bosh.rb b/lib/ruby_bosh.rb index 364b962..2c3eb8d 100644 --- a/lib/ruby_bosh.rb +++ b/lib/ruby_bosh.rb @@ -133,8 +133,20 @@ def parse(_response) _response end + def timeout(secs, &block) + if defined?(SystemTimer) + SystemTimer.timeout(secs) do + block.call + end + else + ::Timeout::timeout(secs) do + block.call + end + end + end + def deliver(xml) - SystemTimer.timeout(@timeout) do + timeout(@timeout) do send(xml) recv(RestClient.post(@service_url, xml, @headers)) end @@ -151,7 +163,7 @@ def send(msg) end def recv(msg) - puts("Ruby-BOSH - RECV\n#{msg}") if @logging; msg + puts("Ruby-BOSH - RECV\n#{msg}") if @@logging; msg end end From ea8794d2da87d70141f7061662cdb895ab16c870 Mon Sep 17 00:00:00 2001 From: tomlion Date: Fri, 29 Jul 2011 12:00:49 +0800 Subject: [PATCH 4/8] Add fast mode to work with openfire, using fast_mode we can shortcut resource_binding which openfire didn't follow, see http://xmpp.org/extensions/xep-0206.html#preconditions-sasl --- lib/ruby_bosh.rb | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/ruby_bosh.rb b/lib/ruby_bosh.rb index a7f199a..11a596f 100644 --- a/lib/ruby_bosh.rb +++ b/lib/ruby_bosh.rb @@ -20,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,19 +50,21 @@ def self.initialize_session(*args) new(*args).connect end - def connect - initialize_bosh_session - if send_auth_request - send_restart_request - request_resource_binding - @success = send_session_request - end - - raise RubyBOSH::AuthFailed, "could not authenticate #{@jid}" unless success? - @rid += 1 #updates the rid for the next call from the browser - - [@jid, @sid, @rid] - end +  def connect +    initialize_bosh_session +    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? +    @rid += 1 #updates the rid for the next call from the browser +     +    [@jid, @sid, @rid] +  end private def initialize_bosh_session From 3be43513c352e24233fd53be8ac61f68d42d1dfe Mon Sep 17 00:00:00 2001 From: tomlion Date: Fri, 29 Jul 2011 14:28:50 +0800 Subject: [PATCH 5/8] Add send_presence stanza method which can be called after connected to server --- lib/ruby_bosh.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/ruby_bosh.rb b/lib/ruby_bosh.rb index 11a596f..3b13985 100644 --- a/lib/ruby_bosh.rb +++ b/lib/ruby_bosh.rb @@ -66,6 +66,19 @@ def self.initialize_session(*args)    [@jid, @sid, @rid]  end + def send_presence + request = construct_body(:sid => @sid) do |body| + body.presence(:from => @jid, :xmlns => "jabber:client") do |p| + p.c(:xmlns => "http://jabber.org/protocol/caps", :node => "http://psi-im.org/caps", :ver => "0.12", :ext => "cs ep-notify html") + p.c(:xmlns => "http://jabber.org/protocol/caps", :node => "urn:xmpp:microblog", :ver => "0.01", :ext => "cs ep-notify html") + p.c(:xmlns => "http://jabber.org/protocol/caps", :node => "urn:xmpp:microblog+notify", :ver => "0.01", :ext => "cs ep-notify html") + p.priority(5) + end + end + + response = deliver(request) + end + private def initialize_bosh_session response = deliver(construct_body(:wait => @wait, :to => @host, From 984dde6d0177eec61813bdb982fcf26cf54ef119 Mon Sep 17 00:00:00 2001 From: tomlion Date: Fri, 29 Jul 2011 15:20:17 +0800 Subject: [PATCH 6/8] fix a invisble char error --- lib/ruby_bosh.rb | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/ruby_bosh.rb b/lib/ruby_bosh.rb index 3b13985..1efb086 100644 --- a/lib/ruby_bosh.rb +++ b/lib/ruby_bosh.rb @@ -50,21 +50,21 @@ def self.initialize_session(*args) new(*args).connect end -  def connect -    initialize_bosh_session -    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? -    @rid += 1 #updates the rid for the next call from the browser -     -    [@jid, @sid, @rid] -  end + def connect + initialize_bosh_session + if send_auth_request + @success = send_restart_request + unless @@fast_mode + request_resource_binding + @success = send_session_request + end + end + + 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 send_presence request = construct_body(:sid => @sid) do |body| From 7adff916174d0aa492394b6ac6c0e5aeff90beb3 Mon Sep 17 00:00:00 2001 From: tomlion Date: Fri, 29 Jul 2011 20:51:09 +0800 Subject: [PATCH 7/8] Add connect_and_presence method --- lib/ruby_bosh.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/ruby_bosh.rb b/lib/ruby_bosh.rb index 1efb086..bc81049 100644 --- a/lib/ruby_bosh.rb +++ b/lib/ruby_bosh.rb @@ -66,6 +66,23 @@ def connect [@jid, @sid, @rid] end + def connect_and_presence + initialize_bosh_session + if send_auth_request + @success = send_restart_request + unless @@fast_mode + request_resource_binding + @success = send_session_request + end + send_presence + end + + 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 send_presence request = construct_body(:sid => @sid) do |body| body.presence(:from => @jid, :xmlns => "jabber:client") do |p| From ea954c90b8b09985f5aa05b59744ca65a7bb6645 Mon Sep 17 00:00:00 2001 From: tomlion Date: Sat, 30 Jul 2011 12:32:23 +0800 Subject: [PATCH 8/8] Remove send_presence and expose deliver method --- lib/ruby_bosh.rb | 51 ++++++++++-------------------------------------- 1 file changed, 10 insertions(+), 41 deletions(-) diff --git a/lib/ruby_bosh.rb b/lib/ruby_bosh.rb index bc81049..91e2174 100644 --- a/lib/ruby_bosh.rb +++ b/lib/ruby_bosh.rb @@ -66,34 +66,17 @@ def connect [@jid, @sid, @rid] end - def connect_and_presence - initialize_bosh_session - if send_auth_request - @success = send_restart_request - unless @@fast_mode - request_resource_binding - @success = send_session_request - end - send_presence - end - - 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 send_presence - request = construct_body(:sid => @sid) do |body| - body.presence(:from => @jid, :xmlns => "jabber:client") do |p| - p.c(:xmlns => "http://jabber.org/protocol/caps", :node => "http://psi-im.org/caps", :ver => "0.12", :ext => "cs ep-notify html") - p.c(:xmlns => "http://jabber.org/protocol/caps", :node => "urn:xmpp:microblog", :ver => "0.01", :ext => "cs ep-notify html") - p.c(:xmlns => "http://jabber.org/protocol/caps", :node => "urn:xmpp:microblog+notify", :ver => "0.01", :ext => "cs ep-notify html") - p.priority(5) - end + def deliver(xml) + timeout(@timeout) do + send(xml) + recv(RestClient.post(@service_url, xml, @headers)) end - - response = deliver(request) + 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 @@ -182,19 +165,6 @@ def timeout(secs, &block) end 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 - def send(msg) puts("Ruby-BOSH - SEND\n[#{now}]: #{msg}") if @@logging; msg end @@ -203,7 +173,6 @@ def recv(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