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.
10 11 12 13 |
# File 'lib/message_channel/druby.rb', line 10 def initialize @mutex = Mutex.new @patterns = {} end |
Instance Method Details
#fetch(pattern) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/message_channel/druby.rb', line 36 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
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/message_channel/druby.rb', line 51 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
15 16 17 18 19 20 21 22 |
# File 'lib/message_channel/druby.rb', line 15 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
24 25 26 27 28 29 30 |
# File 'lib/message_channel/druby.rb', line 24 def unsubscribe( queue_id ) @mutex.synchronize do @patterns.delete( queue_id ) rescue nil end rescue nil end |
#wait(queue_id) ⇒ Object
32 33 34 |
# File 'lib/message_channel/druby.rb', line 32 def wait( queue_id ) @patterns[queue_id].last.pop end |