Kouhei Sutou
null+****@clear*****
Fri Jan 17 18:13:25 JST 2014
Kouhei Sutou 2014-01-17 18:13:25 +0900 (Fri, 17 Jan 2014) New Revision: 442edc31a5ca0f1404d8ccc50d5de893bbb166c5 https://github.com/droonga/droonga-client-ruby/commit/442edc31a5ca0f1404d8ccc50d5de893bbb166c5 Message: Add subscribe method for PubSub style Modified files: lib/droonga/client.rb lib/droonga/client/connection/droonga_protocol.rb Modified: lib/droonga/client.rb (+3 -0) =================================================================== --- lib/droonga/client.rb 2014-01-17 16:22:23 +0900 (06994e3) +++ lib/droonga/client.rb 2014-01-17 18:13:25 +0900 (081f371) @@ -67,6 +67,9 @@ module Droonga @connection.reciprocate(message, &block) end + def subscribe(message, options={}, &block) + @connection.subscribe(message, &block) + end def search(body, options={}, &block) reciprocate({ Modified: lib/droonga/client/connection/droonga_protocol.rb (+58 -0) =================================================================== --- lib/droonga/client/connection/droonga_protocol.rb 2014-01-17 16:22:23 +0900 (c2e9de0) +++ lib/droonga/client/connection/droonga_protocol.rb 2014-01-17 18:13:25 +0900 (914d7fd) @@ -90,6 +90,64 @@ module Droonga end end + # Subscribes something and receives zero or more published + # messages. + # + # @overload subscribe(message, options={}) + # This is enumerator version. + # + # @param message [Hash] Subscribe message. + # @param options [Hash] The options. + # TODO: WRITE ME + # + # @return [Enumerator] You can get a published message by + # #next. You can also use #each to get published messages. + # + # @overload subscribe(message, options={}, &block) + # This is asynchronously version. + # + # @param message [Hash] Subscribe message. + # @param options [Hash] The options. + # TODO: WRITE ME + # @yield [message] + # The block is called when a published message is received. + # The block may be called zero or more times. + # @yieldparam [Object] message + # The published message. + # + # @return [Request] The request object. + def subscribe(message, options={}, &block) + receiver = create_receiver + message = message.dup + message["from"] = "#{receiver.host}:#{receiver.port}/droonga" + send(message, options) + + receive_options = { + :timeout => nil, + } + sync = block.nil? + if sync + Enumerator.new do |yielder| + loop do + receiver.receive(receive_options) do |object| + yielder << object + end + end + end + else + thread = Thread.new do + begin + loop do + receiver.receive(receive_options, &block) + end + ensure + receiver.close + end + end + Request.new(thread) + end + end + # Sends low level request. Normally, you should use other # convenience methods. # -------------- next part -------------- HTML����������������������������...Descargar