lib/amq/client/adapter.rb in amq-client-0.7.0.alpha3 vs lib/amq/client/adapter.rb in amq-client-0.7.0.alpha4

- old
+ new

@@ -2,10 +2,11 @@ require "amq/client/logging" require "amq/client/settings" require "amq/client/entity" require "amq/client/connection" +require "amq/client/channel" module AMQ # For overview of AMQP client adapters API, see {AMQ::Client::Adapter} module Client # Base adapter class. Specific implementations (for example, EventMachine-based, Cool.io-based or @@ -102,61 +103,31 @@ def logging=(boolean) settings[:logging] = boolean end - # @example Registering Channel implementation - # Adapter.register_entity(:channel, Channel) - # # ... so then I can do: - # channel = client.channel(1) - # # instead of: - # channel = Channel.new(client, 1) - def register_entity(name, klass) - define_method(name) do |*args, &block| - klass.new(self, *args, &block) - end - end - # Establishes connection to AMQ broker and returns it. New connection object is yielded to # the block if it is given. # + # @example Specifying adapter via the :adapter option + # AMQ::Client::Adapter.connect(adapter: "socket") + # @example Specifying using custom adapter class + # AMQ::Client::SocketClient.connect # @param [Hash] Connection parameters, including :adapter to use. # @api public def connect(settings = nil, &block) - if settings && settings[:adapter] - adapter = load_adapter(settings[:adapter]) - else - adapter = self - end + # TODO: this doesn't look very nice, do we need it? + # Let's make it an instance thing by instance = self.new(settings) + @settings = settings = Settings.configure(settings) - @settings = AMQ::Client::Settings.configure(settings) - instance = adapter.new - instance.establish_connection(@settings) - # We don't need anything more, once the server receives the preable, he sends Connection.Start, we just have to reply. + instance = self.new + instance.establish_connection(settings) + instance.register_connection_callback(&block) - if block - block.call(instance) - - instance.disconnect - else - instance - end + instance end - - # Loads adapter from amq/client/adapters. - # - # @raise [InvalidAdapterNameError] When loading attempt failed (LoadError was raised). - def load_adapter(adapter) - require "amq/client/adapters/#{adapter}" - - const_name = adapter.to_s.gsub(/(^|_)(.)/) { $2.upcase! } - const_get(const_name) - rescue LoadError - raise InvalidAdapterNameError.new(adapter) - end - # @see AMQ::Client::Adapter def sync=(boolean) @sync = boolean end @@ -182,21 +153,18 @@ # Behaviors # include AMQ::Client::StatusMixin + extend RegisterEntityMixin + register_entity :channel, AMQ::Client::Channel + # # API # - def self.load_adapter(adapter) - ClassMethods.load_adapter(adapter) - end - - - def initialize(*args) super(*args) self.logger = self.class.logger self.settings = self.class.settings @@ -227,10 +195,12 @@ # # @api plugin # @todo This method should await broker's response with Close-Ok. {http://github.com/michaelklishin MK}. # @see #close_connection def disconnect(reply_code = 200, reply_text = "Goodbye", &block) + @intentionally_closing_connection = true + self.on_disconnection(&block) closing! self.connection.close(reply_code, reply_text) end alias close disconnect @@ -350,7 +320,5 @@ end end # Adapter end # Client end # AMQ - -require "amq/client/channel"