Class: Droonga::Client::Connection::DroongaProtocol::Thread::Receiver

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/client/connection/droonga-protocol/thread.rb

Constant Summary collapse

BUFFER_SIZE =
8192

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Receiver

Returns a new instance of Receiver.



139
140
141
142
143
144
145
# File 'lib/droonga/client/connection/droonga-protocol/thread.rb', line 139

def initialize(options={})
  host = options[:host] || Socket.gethostname
  port = options[:port] || 0
  @socket = TCPServer.new(host, port)
  @read_ios = [@socket]
  @client_handlers = {}
end

Instance Method Details

#closeObject



147
148
149
150
151
152
# File 'lib/droonga/client/connection/droonga-protocol/thread.rb', line 147

def close
  @socket.close
  @client_handlers.each_key do |client|
    client.close
  end
end

#hostObject



154
155
156
# File 'lib/droonga/client/connection/droonga-protocol/thread.rb', line 154

def host
  @socket.addr[3]
end

#portObject



158
159
160
# File 'lib/droonga/client/connection/droonga-protocol/thread.rb', line 158

def port
  @socket.addr[1]
end

#receive(options = {}, &block) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/droonga/client/connection/droonga-protocol/thread.rb', line 163

def receive(options={}, &block)
  timeout = options[:timeout]
  catch do |tag|
    loop do
      start = Time.new
      readable_ios, = IO.select(@read_ios, nil, nil, timeout)
      break if readable_ios.nil?
      if timeout
        timeout -= (Time.now - start)
        timeout = 0 if timeout < 0
      end
      readable_ios.each do |readable_io|
        on_readable(readable_io) do |object|
          begin
            yield(object)
          rescue LocalJumpError
            throw(tag)
          end
        end
      end
    end
  end
end