Kouhei Sutou
null+****@clear*****
Mon Aug 19 18:37:47 JST 2013
Kouhei Sutou 2013-08-19 18:37:47 +0900 (Mon, 19 Aug 2013) New Revision: 9af30f77131b02d890b687b6cac563d2842a8d14 https://github.com/droonga/droonga-client-ruby/commit/9af30f77131b02d890b687b6cac563d2842a8d14 Message: Add droonga protocol client Added files: LICENSE.txt lib/droonga/client.rb Added: LICENSE.txt (+14 -0) 100644 =================================================================== --- /dev/null +++ LICENSE.txt 2013-08-19 18:37:47 +0900 (fac5456) @@ -0,0 +1,14 @@ +Copyright (C) 2013 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 Added: lib/droonga/client.rb (+94 -0) 100644 =================================================================== --- /dev/null +++ lib/droonga/client.rb 2013-08-19 18:37:47 +0900 (c3b7f61) @@ -0,0 +1,94 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2013 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 "socket" +require "msgpack" +require "fluent-logger" + +module DroongaProtocol + class Client + def initialize(options={}) + default_options = { + :tag => "kotoumi", + :host => "127.0.0.1", + :port => 23003, + :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 + 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 -------------- next part -------------- HTML����������������������������... 下載