Kouhei Sutou
null+****@clear*****
Sat Mar 15 23:39:30 JST 2014
Kouhei Sutou 2014-03-15 23:39:30 +0900 (Sat, 15 Mar 2014) New Revision: d051c1c3b469224f331ee7b9d1f4c55c4372ec00 https://github.com/ranguba/groonga-client/commit/d051c1c3b469224f331ee7b9d1f4c55c4372ec00 Message: Make HTTP backend extensible Added files: lib/groonga/client/protocol/http/thread.rb Removed files: lib/groonga/client/protocol/request.rb Modified files: lib/groonga/client/protocol/http.rb test/protocol/test-gqtp.rb Modified: lib/groonga/client/protocol/http.rb (+28 -42) =================================================================== --- lib/groonga/client/protocol/http.rb 2014-03-15 23:35:55 +0900 (d55f55e) +++ lib/groonga/client/protocol/http.rb 2014-03-15 23:39:30 +0900 (3c1156b) @@ -17,66 +17,52 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -require "open-uri" - -require "groonga/client/empty-request" -require "groonga/client/protocol/request" require "groonga/client/protocol/error" module Groonga class Client module Protocol class HTTP + class UnknownBackendError < Error + attr_reader :backend + def initialize(backend, detail) + @backend = backend + super("Unknown HTTP backend: <#{backend}>: #{detail}") + end + end + def initialize(options) @host = options[:host] || "127.0.0.1" @port = options[:port] || 10041 + @options = options + @backend = create_backend end - def send(command) - url = "http://#{@host}:#{@port}#{command.to_uri_format}" - thread = Thread.new do - begin - open(url) do |response| - body = response.read - yield(body) - end - rescue OpenURI::HTTPError, Timeout::Error - raise Error.new($!) - end - end - ThreadRequest.new(thread) + def send(command, &block) + @backend.send(command, &block) end - # @return [false] Always returns false because the current - # implementation doesn't support keep-alive. def connected? - false + @backend.connected? end - # Does nothing because the current implementation doesn't - # support keep-alive. If the implementation supports - # keep-alive, it close the opend connection. - # - # @overload close - # Closes synchronously. - # - # @return [false] It always returns false because there is always - # no connectin. - # - # @overload close {} - # Closes asynchronously. - # - # @yield [] Calls the block when the opened connection is closed. - # @return [#wait] The request object. If you want to wait until - # the request is processed. You can send #wait message to the - # request. def close(&block) - sync = !block_given? - if sync - false - else - EmptyRequest.new + @backend.close(&block) + end + + private + def create_backend + backend = @options[:backend] || :thread + + begin + require "groonga/client/protocol/http/#{backend}" + rescue LoadError + raise UnknownBackendError.new(backend, $!.message) end + + backend_name = backend.to_s.capitalize + backend_class = self.class.const_get(backend_name) + backend_class.new(@host, @port, @options) end end end Added: lib/groonga/client/protocol/http/thread.rb (+94 -0) 100644 =================================================================== --- /dev/null +++ lib/groonga/client/protocol/http/thread.rb 2014-03-15 23:39:30 +0900 (78d42ad) @@ -0,0 +1,94 @@ +# Copyright (C) 2013 Haruka Yoshihara <yoshihara �� clear-code.com> +# Copyright (C) 2013-2014 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +require "open-uri" + +require "groonga/client/empty-request" +require "groonga/client/protocol/error" + +module Groonga + class Client + module Protocol + class HTTP + class Thread + class Request + def initialize(thread) + @thread = thread + end + + def wait + @thread.join + end + end + + def initialize(host, port, options) + @host = host + @port = port + @options = options + end + + def send(command) + url = "http://#{@host}:#{@port}#{command.to_uri_format}" + thread = ::Thread.new do + begin + open(url) do |response| + body = response.read + yield(body) + end + rescue OpenURI::HTTPError, Timeout::Error + raise WrappedError.new($!) + end + end + Request.new(thread) + end + + # @return [false] Always returns false because the current + # implementation doesn't support keep-alive. + def connected? + false + end + + # Does nothing because the current implementation doesn't + # support keep-alive. If the implementation supports + # keep-alive, it close the opend connection. + # + # @overload close + # Closes synchronously. + # + # @return [false] It always returns false because there is always + # no connection. + # + # @overload close {} + # Closes asynchronously. + # + # @yield [] Calls the block when the opened connection is closed. + # @return [#wait] The request object. If you want to wait until + # the request is processed. You can send #wait message to the + # request. + def close(&block) + sync = !block_given? + if sync + false + else + EmptyRequest.new + end + end + end + end + end + end +end Deleted: lib/groonga/client/protocol/request.rb (+0 -33) 100644 =================================================================== --- lib/groonga/client/protocol/request.rb 2014-03-15 23:35:55 +0900 (b7dbe92) +++ /dev/null @@ -1,33 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (C) 2013-2014 Kouhei Sutou <kou �� clear-code.com> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - -module Groonga - class Client - module Protocol - class ThreadRequest - def initialize(thread) - @thread = thread - end - - def wait - @thread.join - end - end - end - end -end Modified: test/protocol/test-gqtp.rb (+1 -1) =================================================================== --- test/protocol/test-gqtp.rb 2014-03-15 23:35:55 +0900 (547d7c0) +++ test/protocol/test-gqtp.rb 2014-03-15 23:39:30 +0900 (95361e2) @@ -84,7 +84,7 @@ class TestProtocolGQTP < Test::Unit::TestCase server = TCPServer.new("127.0.0.1", 0) free_port = server.addr[1] server.close - assert_raise(Groonga::Client::Protocol::Error) do + assert_raise(Groonga::Client::Protocol::WrappedError) do Groonga::Client::Protocol::GQTP.new(:host => "127.0.0.1", :port => free_port) end -------------- next part -------------- HTML����������������������������...Download