Class: MessageChannel::Druby::Broker
- Inherits:
-
Object
- Object
- MessageChannel::Druby::Broker
- Defined in:
- lib/message_channel/druby.rb
Instance Method Summary collapse
- #fetch(pattern) ⇒ Object
-
#initialize ⇒ Broker
constructor
A new instance of Broker.
- #publish(topic, message) ⇒ Object
- #subscribe(pattern) ⇒ Object
- #unsubscribe(queue_id) ⇒ Object
- #wait(queue_id) ⇒ Object
Constructor Details
#initialize ⇒ Broker
Returns a new instance of Broker.
9 10 11 12 |
# File 'lib/message_channel/druby.rb', line 9 def initialize @mutex = Mutex.new @patterns = {} end |
Instance Method Details
#fetch(pattern) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/message_channel/druby.rb', line 35 def fetch( pattern ) queue = Queue.new queue_id = queue.object_id @mutex.synchronize do @patterns[queue_id] = [pattern, queue] end topic, items = * queue.pop rescue nil ensure @mutex.synchronize do @patterns.delete( queue_id ) rescue nil end end |
#publish(topic, message) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/message_channel/druby.rb', line 50 def publish( topic, ) @mutex.synchronize do @patterns.each do |_queue_id, items| pattern, queue = *items if File.fnmatch( pattern, topic, File::FNM_PATHNAME ) queue.push( [topic, ] ) end end end end |
#subscribe(pattern) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/message_channel/druby.rb', line 14 def subscribe( pattern ) queue = Queue.new queue_id = queue.object_id @mutex.synchronize do @patterns[queue_id] = [pattern, queue] end queue_id end |
#unsubscribe(queue_id) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/message_channel/druby.rb', line 23 def unsubscribe( queue_id ) @mutex.synchronize do @patterns.delete( queue_id ) rescue nil end rescue nil end |
#wait(queue_id) ⇒ Object
31 32 33 |
# File 'lib/message_channel/druby.rb', line 31 def wait( queue_id ) @patterns[queue_id].last.pop end |