Kouhei Sutou
null+****@clear*****
Fri Jan 17 19:09:17 JST 2014
Kouhei Sutou 2014-01-17 19:09:17 +0900 (Fri, 17 Jan 2014) New Revision: 4de17311c14d8ada62425416626bc5b82c9c1cd7 https://github.com/droonga/droonga-client-ruby/commit/4de17311c14d8ada62425416626bc5b82c9c1cd7 Message: Support HTTP interface Added files: lib/droonga/client/connection/http.rb Modified files: droonga-client.gemspec lib/droonga/client.rb Modified: droonga-client.gemspec (+2 -1) =================================================================== --- droonga-client.gemspec 2014-01-17 18:58:11 +0900 (c9561ba) +++ droonga-client.gemspec 2014-01-17 19:09:17 +0900 (0658452) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2013 Droonga Project +# Copyright (C) 2013-2014 Droonga Project # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -36,6 +36,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency "msgpack" spec.add_runtime_dependency "fluent-logger" + spec.add_runtime_dependency "rack" spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" Modified: lib/droonga/client.rb (+12 -1) =================================================================== --- lib/droonga/client.rb 2014-01-17 18:58:11 +0900 (23ff407) +++ lib/droonga/client.rb 2014-01-17 19:09:17 +0900 (096fca8) @@ -17,6 +17,7 @@ require "droonga/client/version" require "droonga/client/error" +require "droonga/client/connection/http" require "droonga/client/connection/droonga_protocol" module Droonga @@ -60,7 +61,7 @@ module Droonga # The timeout value for connecting to, writing to and reading # from Droonga Engine. def initialize(options={}) - @connection = Connection::DroongaProtocol.new(options) + @connection = create_connection(options) end def request(message, options={}, &block) @@ -87,5 +88,15 @@ module Droonga def close @connection.close end + + private + def create_connection(options) + case options[:protocol] || :droonga + when :http + Connection::HTTP.new(options) + when :droonga + Connection::DroongaProtocol.new(options) + end + end end end Added: lib/droonga/client/connection/http.rb (+156 -0) 100644 =================================================================== --- /dev/null +++ lib/droonga/client/connection/http.rb 2014-01-17 19:09:17 +0900 (039d0e8) @@ -0,0 +1,156 @@ +# Copyright (C) 2014 Droonga Project +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1 as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +require "net/http" +require "thread" + +require "rack" + +module Droonga + class Client + module Connection + class HTTP + class Request + def initialize(thread) + @thread = thread + end + + def wait + @thread.join + end + end + + def initialize(options={}) + @host = options[:host] || "127.0.0.1" + @port = options[:port] || 80 + @timeout = options[:timeout] || 1 + end + + # Sends a request message and receives one or more response + # messages. + # + # @overload request(message, options={}) + # This is synchronously version. + # + # @param message [Hash] Request message. + # @param options [Hash] The options. + # TODO: WRITE ME + # + # @return [Object] The response. TODO: WRITE ME + # + # @overload request(message, options={}, &block) + # This is asynchronously version. + # + # @param message [Hash] Request message. + # @param options [Hash] The options. + # TODO: WRITE ME + # @yield [response] + # The block is called when response is received. + # @yieldparam [Object] response + # The response. + # + # @return [Request] The request object. + def request(message, options={}, &block) + sync = block.nil? + if sync + send(message, options) do |response| + response.body + end + else + thread = Thread.new do + send(message, options) do |response| + yield(response.body) + end + end + Request.new(thread) + end + end + + # Subscribes something and receives zero or more 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) + thread = Thread.new do + send(message, options.merge(:read_timeout => nil)) do |response| + response.read_body(&block) + end + end + Request.new(thread) + end + + # Sends low level request. Normally, you should use other + # convenience methods. + # + # @param envelope [Hash] Request envelope. + # @param options [Hash] The options to send request. + # TODO: WRITE ME + # @return [void] + def send(message, options={}, &block) + http = Net::HTTP.new(@host, @port) + open_timeout = @timeout + read_timeout = @timeout + open_timeout = options[:open_timeout] if options.key?(:open_timeout) + read_timeout = options[:read_timeout] if options.key?(:read_timeout) + http.open_timeout = open_timeout + http.read_timeout = read_timeout + http.start do + get = Net::HTTP::Get.new(build_path(message), options[:headers]) + http.request(get) do |response| + yield(response) + end + end + end + + # Close the connection. This connection can't be used anymore. + # + # @return [void] + def close + end + + private + def build_path(message) + type = nil + parameters = {} + message.each do |key, value| + if key == "type" + type = value + else + parameters[key] = value + end + end + base_path = "/#{type}" + if parameters.empty? + base_path + else + "#{base_path}?#{Rack::Utils.build_nested_query(parameters)}" + end + end + end + end + end +end -------------- next part -------------- HTML����������������������������...Download