Class: Ass::Server

Inherits:
Object
  • Object
show all
Includes:
Callback
Defined in:
lib/ass.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callback

#build_callback_klass, #callback

Constructor Details

#initialize(server_exchange, q) ⇒ Server

Returns a new instance of Server.



167
168
169
170
# File 'lib/ass.rb', line 167

def initialize(server_exchange,q)
  @queue = q
  @server_exchange = server_exchange
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



172
173
174
# File 'lib/ass.rb', line 172

def queue
  @queue
end

Instance Method Details

#exchangeObject



173
174
175
# File 'lib/ass.rb', line 173

def exchange
  @server_exchange
end

#react(callback = nil, opts = nil, &block) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/ass.rb', line 177

def react(callback=nil,opts=nil,&block)
  if block
    opts = callback
    callback = block
  end
  opts = {} if opts.nil?
  
  @callback_klass = build_callback_klass(callback)
  @ack = opts[:ack]
  @queue.subscribe(opts) do |info,payload|
    payload = ::Marshal.load(payload)
    #p [info,info.reply_to,payload]
    data2 = callback(info,payload)
    payload2 = payload.merge :data => data2
    if info.routing_key == @server_exchange.name
      # addressed to the server's public
      # queue, respond to the routing_key of
      # the client's public queue.
      key = info.reply_to
    else
      # addressed to the private queue
      key = info.routing_key
    end
    MQ.direct(info.reply_to).publish(::Marshal.dump(payload2),:routing_key => key) if info.reply_to
    info.ack if @ack
  end
  self
end