Class: Droonga::Client::Connection::DroongaProtocol::Coolio

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

Defined Under Namespace

Classes: InfiniteRequest, Receiver, Request, Sender

Instance Method Summary collapse

Constructor Details

#initialize(host, port, tag, options = {}) ⇒ Coolio

Returns a new instance of Coolio.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/droonga/client/connection/droonga-protocol/coolio.rb', line 168

def initialize(host, port, tag, options={})
  @host = host
  @port = port
  @tag = tag
  default_options = {
  }
  @options = default_options.merge(options)
  @loop = options[:loop] || ::Coolio::Loop.default

  @sender = Sender.connect(@host, @port)
  @sender.attach(@loop)
  @receiver_host = @options[:receiver_host] || Socket.gethostname
  @receiver_port = @options[:receiver_port] || 0
  @receiver = Receiver.new(@receiver_host, @receiver_port)
  @receiver.attach(@loop)
end

Instance Method Details

#closeObject



262
263
264
265
# File 'lib/droonga/client/connection/droonga-protocol/coolio.rb', line 262

def close
  @sender.close
  @receiver.close
end

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/droonga/client/connection/droonga-protocol/coolio.rb', line 185

def request(message, options={}, &block)
  id = message["id"] || generate_id
  message = message.merge("id" => id,
                          "replyTo" => @receiver.droonga_name)
  send(message, options) do |error|
    yield(error)
  end

  sync = block.nil?
  if sync
    response = nil
    block = lambda do |_response|
      response = _response
    end
  end
  @receiver.register(id) do |response|
    @receiver.unregister(id)
    block.call(response)
  end
  request = Request.new(@receiver, id, @loop)
  if sync
    request.wait
    response
  else
    request
  end
end

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



247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/droonga/client/connection/droonga-protocol/coolio.rb', line 247

def send(message, options={}, &block)
  if message["id"].nil? or message["date"].nil?
    id = message["id"] || generate_id
    date = message["date"] || Time.now
    message = message.merge("id" => id, "date" => date)
  end
  @sender.send("#{@tag}.message", message) do
    host = @sender.peeraddr[3]
    port = @sender.peeraddr[1]
    detail = message
    error = ConnectionError.new(host, port, detail)
    yield(error)
  end
end

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



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/droonga/client/connection/droonga-protocol/coolio.rb', line 213

def subscribe(message, options={}, &block)
  id = message["id"] || generate_id
  message = message.merge("id" => id,
                          "replyTo" => @receiver.droonga_name,
                          "from" => @receiver.droonga_name)
  send(message, options) do |error|
    yield(error)
  end

  request = InfiniteRequest.new(@loop)
  sync = block.nil?
  if sync
    yielder = nil
    buffer = []
    @receiver.register(id) do |response|
      if yielder
        while (old_response = buffer.shift)
          yielder << old_response
        end
        yielder << response
      else
        buffer << response
      end
    end
    Enumerator.new do |_yielder|
      yielder = _yielder
      request.wait
    end
  else
    @receiver.register(id, &block)
    request
  end
end