Class: AMQPProcessing::Client

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/amqp-processing/client.rb

Instance Attribute Summary collapse

Attributes included from Logging

#logger

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initializeClient

Returns a new instance of Client.



21
22
23
24
# File 'lib/amqp-processing/client.rb', line 21

def initialize
  @client_id = rand(100000000).to_i
  @call_fanout = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/amqp-processing/client.rb', line 46

def method_missing(name, *arguments)
  return_value = @app.__send__(name, *arguments)
  log.debug "sending as #{@client_id}: #{name}, #{arguments.inspect}"
  while @call_fanout == nil
    log.debug "Not yet connected to publishing queue, sleeping"
    sleep 1
  end
  @call_fanout.publish(Marshal.dump([@client_id, name, arguments]))
  return_value
end

Instance Attribute Details

#appObject

A Processing::App which will receive all method calls made on peer Client objects.



12
13
14
# File 'lib/amqp-processing/client.rb', line 12

def app
  @app
end

#threadObject (readonly)

The server connection Thread, set by the connect method.



19
20
21
# File 'lib/amqp-processing/client.rb', line 19

def thread
  @thread
end

Instance Method Details

#connect(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/amqp-processing/client.rb', line 26

def connect(options = {})
  Thread.abort_on_exception = true
  @thread = Thread.new do
    AMQP.start(options) do
      @call_fanout = MQ.fanout('calls')
      @call_queue = MQ.queue("amqp-processing.#{@client_id}", :auto_delete => true)
      @call_queue.bind(@call_fanout, :key => "amqp-processing.*").subscribe do |message|
        call = Marshal.load(message)
        sender_id, name, arguments = call.shift, call.shift, *call
        if sender_id == @client_id
          log.debug "sender_id #{sender_id} == @client_id #{@client_id}, ignoring"
        else
          log.debug "received from #{sender_id}: #{name}, #{arguments.inspect}"
          @app.__send__(name, *arguments)
        end
      end
    end
  end
end