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.



26
27
28
29
30
31
32
33
34
35
# File 'lib/droonga/handler_messenger.rb', line 26

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

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



24
25
26
# File 'lib/droonga/handler_messenger.rb', line 24

def cluster
  @cluster
end

#database_nameObject (readonly)

Returns the value of attribute database_name.



24
25
26
# File 'lib/droonga/handler_messenger.rb', line 24

def database_name
  @database_name
end

#dispatcherObject (readonly)

Returns the value of attribute dispatcher.



24
25
26
# File 'lib/droonga/handler_messenger.rb', line 24

def dispatcher
  @dispatcher
end

#engine_stateObject (readonly)

Returns the value of attribute engine_state.



24
25
26
# File 'lib/droonga/handler_messenger.rb', line 24

def engine_state
  @engine_state
end

Instance Method Details

#emit(value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/droonga/handler_messenger.rb', line 37

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



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
95
96
97
98
# File 'lib/droonga/handler_messenger.rb', line 62

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:



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

def forward(droonga_message, destination)
  droonga_message["date"] ||= new_date
  @forwarder.forward(droonga_message, destination)
end

#inspectObject



116
117
118
# File 'lib/droonga/handler_messenger.rb', line 116

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