From commits-return-26863-apmail-qpid-commits-archive=qpid.apache.org@qpid.apache.org Wed Nov 26 20:05:49 2014 Return-Path: X-Original-To: apmail-qpid-commits-archive@www.apache.org Delivered-To: apmail-qpid-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5EE1E1015E for ; Wed, 26 Nov 2014 20:05:49 +0000 (UTC) Received: (qmail 5371 invoked by uid 500); 26 Nov 2014 20:05:49 -0000 Delivered-To: apmail-qpid-commits-archive@qpid.apache.org Received: (qmail 5248 invoked by uid 500); 26 Nov 2014 20:05:49 -0000 Mailing-List: contact commits-help@qpid.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@qpid.apache.org Delivered-To: mailing list commits@qpid.apache.org Received: (qmail 4883 invoked by uid 99); 26 Nov 2014 20:05:49 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Nov 2014 20:05:49 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id AB84D8AF7D2; Wed, 26 Nov 2014 20:05:48 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: gsim@apache.org To: commits@qpid.apache.org Date: Wed, 26 Nov 2014 20:05:57 -0000 Message-Id: <56d48f4f40e34cdf90fcc8a36696edab@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [10/35] qpid-proton git commit: PROTON-747: Around wrappers for Ruby Messenger methods that raise errors PROTON-747: Around wrappers for Ruby Messenger methods that raise errors Each method that could potentially raise an exception in Qpid::Proton::Message is now wrapped by a method which checks for, and then raises, the appropriate runtime error based on the error code. Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/66150aa7 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/66150aa7 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/66150aa7 Branch: refs/heads/examples Commit: 66150aa7a845c2210ab7c1b0c0e011988a60de2e Parents: 0d1b8a8 Author: Darryl L. Pierce Authored: Fri Nov 14 09:45:17 2014 -0500 Committer: Darryl L. Pierce Committed: Tue Nov 18 09:34:43 2014 -0500 ---------------------------------------------------------------------- .../ruby/lib/qpid_proton/exception_handling.rb | 43 +++++++++++++++++ .../bindings/ruby/lib/qpid_proton/messenger.rb | 50 ++++++++++++++------ 2 files changed, 79 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/66150aa7/proton-c/bindings/ruby/lib/qpid_proton/exception_handling.rb ---------------------------------------------------------------------- diff --git a/proton-c/bindings/ruby/lib/qpid_proton/exception_handling.rb b/proton-c/bindings/ruby/lib/qpid_proton/exception_handling.rb index f8ac8c6..4dce3ef 100644 --- a/proton-c/bindings/ruby/lib/qpid_proton/exception_handling.rb +++ b/proton-c/bindings/ruby/lib/qpid_proton/exception_handling.rb @@ -25,6 +25,49 @@ module Qpid # module ExceptionHandling + def self.included(base) + base.extend(self) + + unless defined? base.to_be_wrapped + class << base + @@to_be_wrapped = [] + end + end + + define_method :method_added do |name| + if (!@@to_be_wrapped.nil?) && (@@to_be_wrapped.include? name) + @@to_be_wrapped.delete name + create_exception_handler_wrapper(name) + end + end + end + + def can_raise_exception(method_names) + # coerce the names to be an array + Array(method_names).each do |method_name| + # if the method doesn't already exist then queue this aliasing + unless self.method_defined? method_name + @@to_be_wrapped ||= [] + @@to_be_wrapped << method_name + else + create_exception_handler_wrapper(method_name) + end + end + end + + def create_exception_handler_wrapper(method_name) + original_method_name = method_name.to_s + wrapped_method_name = "_excwrap_#{original_method_name}" + alias_method wrapped_method_name, original_method_name + define_method original_method_name do |*args, &block| + # need to get a reference to the method object itself since + # calls to Class.send interfere with Messenger.send + method = self.method(wrapped_method_name.to_sym) + rc = method.call(*args, &block) + check_for_error(rc) + end + end + # Raises an Proton-specific error if a return code is non-zero. # # Expects the class to provide an +error+ method. http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/66150aa7/proton-c/bindings/ruby/lib/qpid_proton/messenger.rb ---------------------------------------------------------------------- diff --git a/proton-c/bindings/ruby/lib/qpid_proton/messenger.rb b/proton-c/bindings/ruby/lib/qpid_proton/messenger.rb index 044a3ad..66a2f93 100644 --- a/proton-c/bindings/ruby/lib/qpid_proton/messenger.rb +++ b/proton-c/bindings/ruby/lib/qpid_proton/messenger.rb @@ -59,6 +59,11 @@ module Qpid include Qpid::Proton::ExceptionHandling + can_raise_exception [:send, :receive, :password=, :start, :stop, + :perform_put, :perform_get, :interrupt, + :route, :rewrite, :accept, :reject, + :incoming_window=, :outgoing_window=] + # Creates a new +Messenger+. # # The +name+ parameter is optional. If one is not provided then @@ -94,7 +99,7 @@ module Qpid # * password - the password # def password=(password) - check_for_error(Cproton.pn_messenger_set_password(@impl, password)) + Cproton.pn_messenger_set_password(@impl, password) end # Returns the password property for the Messenger.private_key file. @@ -193,14 +198,14 @@ module Qpid # before starting the +Messenger+. # def start - check_for_error(Cproton.pn_messenger_start(@impl)) + Cproton.pn_messenger_start(@impl) end # Stops the +Messenger+, preventing it from sending or receiving # any more messages. # def stop - check_for_error(Cproton.pn_messenger_stop(@impl)) + Cproton.pn_messenger_stop(@impl) end # Returns true iff a Messenger is in the stopped state. @@ -313,10 +318,19 @@ module Qpid raise ArgumentError.new("invalid message type: #{message.class}") unless message.kind_of?(Message) # encode the message first message.pre_encode - check_for_error(Cproton.pn_messenger_put(@impl, message.impl)) + perform_put(message) return outgoing_tracker end + private + + def perform_put(message) # :nodoc: + Cproton.pn_messenger_put(@impl, message.impl) + end + + public + + # This call will block until the indicated number of messages # have been sent, or until the operation times out. # If n is -1 this call will block until all outgoing messages @@ -324,7 +338,7 @@ module Qpid # it can without blocking. # def send(n = -1) - check_for_error(Cproton.pn_messenger_send(@impl, n)) + Cproton.pn_messenger_send(@impl, n) end # Moves the message from the head of the incoming message queue into @@ -348,11 +362,19 @@ module Qpid else msg_impl = msg.impl end - check_for_error(Cproton.pn_messenger_get(@impl, msg_impl)) + perform_get(msg_impl) msg.post_decode unless msg.nil? return incoming_tracker end + private + + def perform_get(msg) # :nodoc: + Cproton.pn_messenger_get(@impl, msg) + end + + public + # Receives up to limit messages into the incoming queue. If no value # for limit is supplied, this call will receive as many messages as it # can buffer internally. If the Messenger is in blocking mode, this @@ -364,7 +386,7 @@ module Qpid # * limit - the maximum number of messages to receive # def receive(limit = -1) - check_for_error(Cproton.pn_messenger_recv(@impl, limit)) + Cproton.pn_messenger_recv(@impl, limit) end def receiving @@ -384,7 +406,7 @@ module Qpid # originated the interrupt. # def interrupt - check_for_error(Cproton.pn_messenger_interrupt(@impl)) + Cproton.pn_messenger_interrupt(@impl) end # Sends or receives any outstanding messages queued for a Messenger. @@ -472,7 +494,7 @@ module Qpid # messenger.route("*", "amqp://user:password@broker/$1") # def route(pattern, address) - check_for_error(Cproton.pn_messenger_route(@impl, pattern, address)) + Cproton.pn_messenger_route(@impl, pattern, address) end # Similar to #route, except that the destination of @@ -494,7 +516,7 @@ module Qpid # * address - the target address # def rewrite(pattern, address) - check_for_error(Cproton.pn_messenger_rewrite(@impl, pattern, address)) + Cproton.pn_messenger_rewrite(@impl, pattern, address) end def selectable @@ -548,7 +570,7 @@ module Qpid else flag = 0 end - check_for_error(Cproton.pn_messenger_accept(@impl, tracker.impl, flag)) + Cproton.pn_messenger_accept(@impl, tracker.impl, flag) end # Rejects the incoming message identified by the tracker. @@ -568,7 +590,7 @@ module Qpid else flag = 0 end - check_for_error(Cproton.pn_messenger_reject(@impl, tracker.impl, flag)) + Cproton.pn_messenger_reject(@impl, tracker.impl, flag) end # Gets the last known remote state of the delivery associated with @@ -624,7 +646,7 @@ module Qpid # def incoming_window=(window) raise TypeError.new("invalid window: #{window}") unless valid_window?(window) - check_for_error(Cproton.pn_messenger_set_incoming_window(@impl, window)) + Cproton.pn_messenger_set_incoming_window(@impl, window) end # Returns the incoming window. @@ -648,7 +670,7 @@ module Qpid # def outgoing_window=(window) raise TypeError.new("invalid window: #{window}") unless valid_window?(window) - check_for_error(Cproton.pn_messenger_set_outgoing_window(@impl, window)) + Cproton.pn_messenger_set_outgoing_window(@impl, window) end # Returns the outgoing window. --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org For additional commands, e-mail: commits-help@qpid.apache.org