PROTON-781: Added the WrappedHandler class to the Ruby reactor APIs.

Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/17597761
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/17597761
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/17597761

Branch: refs/heads/PROTON-781-ruby-reactor-apis
Commit: 175977616b46471d758c716a7ed16fe67962e274
Parents: e38957a
Author: Darryl L. Pierce <mcpie...@gmail.com>
Authored: Mon Feb 23 16:18:01 2015 -0500
Committer: Darryl L. Pierce <mcpie...@gmail.com>
Committed: Thu Jun 18 09:27:19 2015 -0400

----------------------------------------------------------------------
 proton-c/bindings/ruby/lib/handler/c_adaptor.rb | 47 ++++++++++++
 .../ruby/lib/handler/wrapped_handler.rb         | 76 ++++++++++++++++++++
 proton-c/bindings/ruby/lib/qpid_proton.rb       |  4 ++
 proton-c/bindings/ruby/ruby.i                   | 53 ++++++++++++++
 4 files changed, 180 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/17597761/proton-c/bindings/ruby/lib/handler/c_adaptor.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/handler/c_adaptor.rb 
b/proton-c/bindings/ruby/lib/handler/c_adaptor.rb
new file mode 100644
index 0000000..ef4852e
--- /dev/null
+++ b/proton-c/bindings/ruby/lib/handler/c_adaptor.rb
@@ -0,0 +1,47 @@
+#--
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#++
+
+module Qpid::Proton::Handler
+
+  # @private
+  class CAdaptor
+
+    def initialize(handler, on_error = nil)
+      @handler = handler
+      @on_error = on_error
+    end
+
+    def dispatch(cevent, ctype)
+      event = Qpid::Proton::Event::Event.wrap(cevent, ctype)
+      # TODO add a variable to enable this programmatically
+      # print "EVENT: #{event} going to #{@handler}\n"
+      event.dispatch(@handler)
+    end
+
+    def exception(error)
+      if @on_error.nil?
+        raise error
+      else
+        @on_error.call(error)
+      end
+    end
+
+  end
+
+end

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/17597761/proton-c/bindings/ruby/lib/handler/wrapped_handler.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/handler/wrapped_handler.rb 
b/proton-c/bindings/ruby/lib/handler/wrapped_handler.rb
new file mode 100644
index 0000000..6d55dee
--- /dev/null
+++ b/proton-c/bindings/ruby/lib/handler/wrapped_handler.rb
@@ -0,0 +1,76 @@
+#--
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#++
+
+module Qpid::Proton::Handler
+
+  class WrappedHandler
+
+    # @private
+    include Qpid::Proton::Util::Wrapper
+
+    def self.wrap(impl, on_error = nil)
+      return nil if impl.nil?
+
+      result = self.fetch_instance(impl) || WrappedHandler.new(impl)
+      result.on_error = on_error
+      return result
+    end
+
+    include Qpid::Proton::Util::Handler
+
+    def initialize(impl_or_constructor)
+      if impl_or_constructor.is_a?(Method)
+        @impl = impl_or_constructor.call
+      else
+        @impl = impl_or_constructor
+        Cproton.pn_incref(@impl)
+      end
+      @on_error = nil
+      self.class.store_instance(self)
+    end
+
+    def add(handler)
+      return if handler.nil?
+
+      impl = chandler(handler, self.method(:_on_error))
+      Cproton.pn_handler_add(@impl, impl)
+      Cproton.pn_decref(impl)
+    end
+
+    def clear
+      Cproton.pn_handler_clear(@impl)
+    end
+
+    def on_error=(on_error)
+      @on_error = on_error
+    end
+
+    private
+
+    def _on_error(info)
+      if self.has?['on_error']
+        self['on_error'].call(info)
+      else
+        raise info
+      end
+    end
+
+  end
+
+end

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/17597761/proton-c/bindings/ruby/lib/qpid_proton.rb
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/lib/qpid_proton.rb 
b/proton-c/bindings/ruby/lib/qpid_proton.rb
index 467d959..2791538 100644
--- a/proton-c/bindings/ruby/lib/qpid_proton.rb
+++ b/proton-c/bindings/ruby/lib/qpid_proton.rb
@@ -82,6 +82,10 @@ require "messenger/tracker"
 require "messenger/selectable"
 require "messenger/messenger"
 
+# Handler classes
+require "handler/c_adaptor"
+require "handler/wrapped_handler"
+
 module Qpid::Proton
   # @private
   def self.registry

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/17597761/proton-c/bindings/ruby/ruby.i
----------------------------------------------------------------------
diff --git a/proton-c/bindings/ruby/ruby.i b/proton-c/bindings/ruby/ruby.i
index 678a085..3d4090e 100644
--- a/proton-c/bindings/ruby/ruby.i
+++ b/proton-c/bindings/ruby/ruby.i
@@ -559,4 +559,57 @@ VALUE pni_address_of(void *object) {
 int pn_ssl_get_peer_hostname(pn_ssl_t *ssl, char *OUTPUT, size_t *OUTPUT_SIZE);
 %ignore pn_ssl_get_peer_hostname;
 
+%inline %{
+
+  VALUE pni_ruby_get_proton_module() {
+    VALUE mQpid = rb_define_module("Qpid");
+    return rb_define_module_under(mQpid, "Proton");
+  }
+
+  void pni_ruby_add_to_registry(VALUE key, VALUE value) {
+    VALUE result = rb_funcall(pni_ruby_get_proton_module(), 
rb_intern("add_to_registry"), 2, key, value);
+  }
+
+  VALUE pni_ruby_get_from_registry(VALUE key) {
+    rb_funcall(pni_ruby_get_proton_module(), rb_intern("get_from_registry"), 
1, key);
+  }
+
+  void pni_ruby_delete_from_registry(VALUE stored_key) {
+    rb_funcall(pni_ruby_get_proton_module(), 
rb_intern("delete_from_registry"), 1, stored_key);
+  }
+
+  typedef struct {
+    VALUE handler_key;
+  } pni_rbhandler_t;
+
+  static pni_rbhandler_t *pni_rbhandler(pn_handler_t *handler) {
+    return (pni_rbhandler_t *) pn_handler_mem(handler);
+  }
+
+  static void pni_rbdispatch(pn_handler_t *handler, pn_event_t *event, 
pn_event_type_t type) {
+    pni_rbhandler_t *rbh = pni_rbhandler(handler);
+    VALUE rbhandler = pni_ruby_get_from_registry(rbh->handler_key);
+
+    rb_funcall(rbhandler, rb_intern("dispatch"), 2, SWIG_NewPointerObj(event, 
SWIGTYPE_p_pn_event_t, 0), INT2FIX(type));
+  }
+
+  static void pni_rbhandler_finalize(pn_handler_t *handler) {
+    pni_rbhandler_t *rbh = pni_rbhandler(handler);
+    pni_ruby_delete_from_registry(rbh->handler_key);
+  }
+
+  pn_handler_t *pn_rbhandler(VALUE handler) {
+    pn_handler_t *chandler = pn_handler_new(pni_rbdispatch, 
sizeof(pni_rbhandler_t), pni_rbhandler_finalize);
+    pni_rbhandler_t *rhy = pni_rbhandler(chandler);
+
+    VALUE ruby_key = rb_class_new_instance(0, NULL, rb_cObject);
+    pni_ruby_add_to_registry(ruby_key, handler);
+
+    rhy->handler_key = ruby_key;
+
+    return chandler;
+  }
+
+%}
+
 %include "proton/cproton.i"


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org
For additional commands, e-mail: commits-h...@qpid.apache.org

Reply via email to