Class: Termplot::MessageBrokerPool

Inherits:
Object
  • Object
show all
Defined in:
lib/termplot/message_broker.rb

Instance Method Summary collapse

Constructor Details

#initializeMessageBrokerPool

Returns a new instance of MessageBrokerPool.



5
6
7
8
9
# File 'lib/termplot/message_broker.rb', line 5

def initialize
  @brokers = []
  @mutex = Mutex.new
  @on_message_callbacks = []
end

Instance Method Details

#broker(sender:, receiver:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/termplot/message_broker.rb', line 11

def broker(sender:, receiver:)
  mutex.synchronize do
    broker = MessageBroker.new(sender: sender, receiver: receiver)
    broker.on_message do |v|
      on_message_callbacks.each do |block|
        block.call(v)
      end
    end
    brokers.push(broker)
    broker
  end
end

#closed?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/termplot/message_broker.rb', line 30

def closed?
  mutex.synchronize do
    (brokers.count == 0) || brokers.all?(&:closed?)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/termplot/message_broker.rb', line 56

def empty?
  pending_message_count == 0
end

#flush_messagesObject



42
43
44
45
46
# File 'lib/termplot/message_broker.rb', line 42

def flush_messages
  mutex.synchronize do
    brokers.each(&:flush_queue)
  end
end

#on_message(&block) ⇒ Object



24
25
26
27
28
# File 'lib/termplot/message_broker.rb', line 24

def on_message(&block)
  mutex.synchronize do
    on_message_callbacks.push(block)
  end
end

#pending_message_countObject



48
49
50
51
52
53
54
# File 'lib/termplot/message_broker.rb', line 48

def pending_message_count
  mutex.synchronize do
    brokers.inject(0) do |sum, broker|
      sum + broker.pending_message_count
    end
  end
end

#shutdownObject



36
37
38
39
40
# File 'lib/termplot/message_broker.rb', line 36

def shutdown
  mutex.synchronize do
    brokers.each(&:close)
  end
end