Class: AMQPProcessing::Client
- Inherits:
-
Object
- Object
- AMQPProcessing::Client
- Includes:
- Logging
- Defined in:
- lib/amqp-processing/client.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
A Processing::App which will receive all method calls made on peer Client objects.
-
#thread ⇒ Object
readonly
The server connection Thread, set by the connect method.
Attributes included from Logging
Instance Method Summary collapse
- #connect(options = {}) ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #method_missing(name, *arguments) ⇒ Object
Methods included from Logging
Constructor Details
#initialize ⇒ Client
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
#app ⇒ Object
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 |
#thread ⇒ Object (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( = {}) Thread.abort_on_exception = true @thread = Thread.new do AMQP.start() 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 || call = Marshal.load() 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 |