Browse files

[BUGFIX] Get connections working when not using Punchblock's XMPP con…

…nection
  • Loading branch information...
1 parent 7290d9f commit e2437a346b9cab1cbde3d3757f0f5c762aa47e1c @benlangfeld benlangfeld committed Jun 15, 2012
Showing with 35 additions and 90 deletions.
  1. +18 −49 lib/adhearsion/xmpp/connection.rb
  2. +17 −41 lib/adhearsion/xmpp/plugin.rb
View
67 lib/adhearsion/xmpp/connection.rb
@@ -3,62 +3,31 @@
module Adhearsion
class XMPP
- module Connection
+ class Connection
- class << self
+ include Blather::DSL
- attr_accessor :client
-
- ##
- # Open the XMPP connection
- #
- # @param [String] jid the client/component JID to connect to
- # @param [String] password
- # @param [String] server
- # @param [Integer] port
- def start(jid, password, server, port)
- Blather.logger = logger
- setup_client_object jid, password, server, port
- register_event_namespaces
- register_default_client_handlers
- Adhearsion::Events.register_callback(:after_initialized) do
- connect
- end
- end
-
- # Close the XMPP connection
- def stop
- shutdown
- end
-
- private
-
- def setup_client_object(jid, password, server, port)
- self.client = Blather::Client.setup(jid, password, server, port)
- end
+ def initialize
+ Blather.logger = logger
+ Blather.default_log_level = :trace if Blather.respond_to? :default_log_level
+ register_default_client_handlers
+ Adhearsion::Events.after_initialized { connect }
+ Adhearsion::Events.shutdown { shutdown }
+ end
- def connect
- EventMachine.run {client.connect}
- end
+ private
- def register_event_namespaces
- Adhearsion::Events.register_namespace_name "/xmpp"
+ def connect
+ logger.info "Connecting to XMPP"
+ Adhearsion::Process.important_threads << Thread.new do
+ EventMachine.run { client.connect }
end
+ end
- def register_default_client_handlers
- client.register_handler(:ready) do
- logger.info "Connected to XMPP server! Send messages to #{client.jid.stripped}."
- end
-
- client.register_handler(:disconnected) do
- if Adhearsion.status == :running
- logger.warn "XMPP Disconnected. Reconnecting."
- connect
- end
- # TODO: fix this to reconnect XMPP cleanly
- end
+ def register_default_client_handlers
+ client.register_handler(:ready) do
+ logger.info "Connected to XMPP server! Send messages to #{client.jid.stripped}."
end
-
end
end
View
58 lib/adhearsion/xmpp/plugin.rb
@@ -4,11 +4,11 @@ class Plugin < Adhearsion::Plugin
# Default configuration for XMPP connection.
config :xmpp do
- use_punchblock true , :desc => "Re-use Punchblock's XMPP connection. Boolean."
- jid nil , :desc => "Client/component JID to connect to. String."
- password nil , :desc => "Password identifier. String."
- server nil , :desc => "XMPP server hostname. May be omitted if server can be determined from JID. String."
- port nil , :desc => "XMPP server port. May be omitted if server can be determined from JID. Integer."
+ use_punchblock true, :desc => "Re-use Punchblock's XMPP connection. Boolean."
+ jid nil, :desc => "Client/component JID to connect to. String."
+ password nil, :desc => "Password identifier. String."
+ server nil, :desc => "XMPP server hostname. May be omitted if server can be determined from JID. String."
+ port nil, :desc => "XMPP server port. May be omitted if server can be determined from JID. Integer."
end
# Include the XMPP service in plugins initialization process
@@ -19,50 +19,26 @@ class Plugin < Adhearsion::Plugin
end unless Adhearsion::XMPP.handlers.nil?
end
- run :xmpp do
- Adhearsion::XMPP.plugin.run_plugin
- end
+ delegate :config, :to => self
+ delegate :use_punchblock, :to => :config
def initialize
- @config ||= Adhearsion.config[:xmpp]
- init_blather unless @config.use_punchblock
- end
-
- def run_plugin
- run_blather unless @config.use_punchblock
+ init_blather config.jid, config.password, config.server, config.port unless use_punchblock
end
- def start_punchblock
- # Nothing needed here; everything is delegated
- nil
- end
-
- def init_blather
- raise "Must supply a jid argument to the XMPP configuration" if (@config.jid.nil? || @config.jid.empty?)
- raise "Must supply a password argument to the XMPP configuration" if (@config.password.nil? || @config.password.empty?)
+ def init_blather(jid, password, server, port)
+ raise "Must supply a jid to the XMPP configuration" unless jid.present?
+ raise "Must supply a password to the XMPP configuration" unless password.present?
- Connection.extend Blather::DSL
- Connection.start @config.jid, @config.password, @config.server, @config.port
- end
-
- ##
- # Stop the XMPP connection
- def stop
- stop_blather unless @config.use_punchblock
- end
-
- def stop_blather
- Connection.stop
+ connection.setup jid, password, server, port
end
def connection
- @config.use_punchblock ?
- PunchblockPlugin.connection :
- Connection
- end
-
- def client
- @config.use_punchblock ? PunchblockPlugin.client : Connection.client
+ if use_punchblock
+ PunchblockPlugin.connection
+ else
+ @connection ||= Connection.new
+ end
end
end
end

0 comments on commit e2437a3

Please sign in to comment.