Class: Droonga::HandlerMessenger

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/handler_messenger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(forwarder, message, options = {}) ⇒ HandlerMessenger

Returns a new instance of HandlerMessenger.



23
24
25
26
27
28
29
30
31
# File 'lib/droonga/handler_messenger.rb', line 23

def initialize(forwarder, message, options={})
  @forwarder = forwarder
  @message = message
  @options = options
  @replier = Replier.new(@forwarder)
  @dispatcher = options[:dispatcher]
  @engine_state = options[:engine_state]
  @database_name = options[:database]
end

Instance Attribute Details

#database_nameObject (readonly)

Returns the value of attribute database_name.



21
22
23
# File 'lib/droonga/handler_messenger.rb', line 21

def database_name
  @database_name
end

#dispatcherObject (readonly)

Returns the value of attribute dispatcher.



21
22
23
# File 'lib/droonga/handler_messenger.rb', line 21

def dispatcher
  @dispatcher
end

#engine_stateObject (readonly)

Returns the value of attribute engine_state.



21
22
23
# File 'lib/droonga/handler_messenger.rb', line 21

def engine_state
  @engine_state
end

Instance Method Details

#emit(value) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/droonga/handler_messenger.rb', line 33

def emit(value)
  descendants = @message.descendants
  raw_message = @message.raw
  if descendants.empty?
    return if raw_message["replyTo"].nil?
    @replier.reply(raw_message.merge("body" => value))
  else
    descendants.each do |name, dests|
      body = {
        "id"    => @message.id,
        "input" => name,
        "value" => value[name],
      }
      dests.each do |dest|
        if @dispatcher
          @dispatcher.dispatch(body, dest)
        else
          message = raw_message.merge("body" => body)
          forward(message, "to" => dest, "type" => "dispatcher")
        end
      end
    end
  end
end

#error(status_code, body) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/droonga/handler_messenger.rb', line 58

def error(status_code, body)
  descendants = @message.descendants
  raw_message = @message.raw
  if descendants.empty?
    return if raw_message["replyTo"].nil?
    response = raw_message.merge("statusCode" => status_code,
                                 "body" => body)
    @replier.reply(response)
  else
    #XXX This is just a temporary solution. We should rewrite this,
    #    to put errors outside of the message body. Otherwise
    #    plugins cannot use the name "errors" for their message body.
    body = {
      "id"    => @message.id,
      "input" => "errors",
      "value" => {
        database_name => {
          "statusCode" => status_code,
          "body" => body,
        },
      },
    }
    all_dests = []
    descendants.each do |name, dests|
      all_dests += dests
    end
    all_dests.each do |dest|
      if @dispatcher
        @dispatcher.dispatch(body, dest)
      else
        message = raw_message.merge("statusCode" => status_code,
                                    "body" => body,)
        forward(message, "to" => dest, "type" => "dispatcher")
      end
    end
  end
end

#forward(droonga_message, destination) ⇒ void

This method returns an undefined value.

Forwards a Droonga message to other Droonga Engine.

Parameters:

  • droonga_message (Hash)

    The Droonga message to be forwarded.

  • destination (Hash)

    The destination of the Droonga message. See Forwarder#forward to know about how to specify destination.

See Also:



107
108
109
# File 'lib/droonga/handler_messenger.rb', line 107

def forward(droonga_message, destination)
  @forwarder.forward(droonga_message, destination)
end

#inspectObject



111
112
113
# File 'lib/droonga/handler_messenger.rb', line 111

def inspect
  "\#<#{self.class} id=#{object_id}>"
end