Yoji Shidara
null+****@clear*****
Wed Sep 11 13:25:08 JST 2013
Yoji Shidara 2013-09-11 13:25:08 +0900 (Wed, 11 Sep 2013) New Revision: 7f84ab9a841b9c9b847d8dd654ffcec140fd7973 https://github.com/droonga/droonga-client-ruby/commit/7f84ab9a841b9c9b847d8dd654ffcec140fd7973 Message: Extract Connection::DroongaProtocol Added files: lib/droonga/connection/droonga_protocol.rb Modified files: lib/droonga/client.rb Modified: lib/droonga/client.rb (+3 -64) =================================================================== --- lib/droonga/client.rb 2013-09-10 15:05:25 +0900 (367f391) +++ lib/droonga/client.rb 2013-09-11 13:25:08 +0900 (c5a53fa) @@ -20,77 +20,16 @@ require "msgpack" require "fluent-logger" require "droonga/client/version" +require "droonga/connection/droonga_protocol" module Droonga class Client def initialize(options={}) - default_options = { - :tag => "droonga", - :host => "127.0.0.1", - :port => 24224, - :timeout => 5 - } - options = default_options.merge(options) - @logger = Fluent::Logger::FluentLogger.new(options.delete(:tag), - options) - @timeout = options[:timeout] + @connection = Connection::DroongaProtocol.new(options) end def search(body) - receiver = Receiver.new - begin - envelope = { - "id" => Time.now.to_f.to_s, - "date" => Time.now, - "replyTo" => "#{receiver.host}:#{receiver.port}/droonga", - "statusCode" => 200, - "type" => "search", - "body" => body, - } - @logger.post("message", envelope) - receiver.receive(:timeout => @timeout) - ensure - receiver.close - end - end - end - - class Receiver - def initialize(options={}) - default_options = { - :host => "0.0.0.0", - :port => 0, - } - options = default_options.merge(options) - @socket = TCPServer.new(options[:host], options[:port]) - end - - def close - @socket.close - end - - def host - @socket.addr[3] - end - - def port - @socket.addr[1] - end - - def receive(options={}) - if IO.select([@socket], nil, nil, options[:timeout]) - client =****@socke***** - response = nil - unpacker = MessagePack::Unpacker.new(client) - unpacker.each do |object| - response = object - break - end - client.close - response - else - nil - end + @connection.search(body) end end end Added: lib/droonga/connection/droonga_protocol.rb (+75 -0) 100644 =================================================================== --- /dev/null +++ lib/droonga/connection/droonga_protocol.rb 2013-09-11 13:25:08 +0900 (2202f7e) @@ -0,0 +1,75 @@ +module Droonga + module Connection + class DroongaProtocol + def initialize(options={}) + default_options = { + :tag => "droonga", + :host => "127.0.0.1", + :port => 24224, + :timeout => 5 + } + options = default_options.merge(options) + @logger = Fluent::Logger::FluentLogger.new(options.delete(:tag), + options) + @timeout = options[:timeout] + end + + def search(body) + receiver = Receiver.new + begin + envelope = { + "id" => Time.now.to_f.to_s, + "date" => Time.now, + "replyTo" => "#{receiver.host}:#{receiver.port}/droonga", + "statusCode" => 200, + "type" => "search", + "body" => body, + } + @logger.post("message", envelope) + receiver.receive(:timeout => @timeout) + ensure + receiver.close + end + end + + class Receiver + def initialize(options={}) + default_options = { + :host => "0.0.0.0", + :port => 0, + } + options = default_options.merge(options) + @socket = TCPServer.new(options[:host], options[:port]) + end + + def close + @socket.close + end + + def host + @socket.addr[3] + end + + def port + @socket.addr[1] + end + + def receive(options={}) + if IO.select([@socket], nil, nil, options[:timeout]) + client =****@socke***** + response = nil + unpacker = MessagePack::Unpacker.new(client) + unpacker.each do |object| + response = object + break + end + client.close + response + else + nil + end + end + end + end + end +end -------------- next part -------------- HTML����������������������������...Download