Class: JSCommander::PipeBroker

Inherits:
Object
  • Object
show all
Defined in:
lib/jscmd/pipebroker.rb

Defined Under Namespace

Classes: PipeMessage, PipeQueue

Instance Method Summary collapse

Constructor Details

#initialize(reader, writer) ⇒ PipeBroker

Returns a new instance of PipeBroker.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jscmd/pipebroker.rb', line 28

def initialize(reader, writer)
  @subscribers = {}
  @queue = PipeQueue.new(reader, writer)
  Thread.start do
    begin
      loop do
        pm = @queue.pop
        if @subscribers[pm.name]
          @subscribers[pm.name].each do |proc|
            proc.call pm.message
          end
        end
      end
    rescue Exception
      $stderr.puts $!
    end
  end
end

Instance Method Details

#send(name, msg) ⇒ Object



57
58
59
60
# File 'lib/jscmd/pipebroker.rb', line 57

def send(name, msg)
#      puts "send #{name}, #{msg.inspect}"
  @queue.push(PipeMessage.new(name, msg))
end

#subscribe(name, &proc) ⇒ Object



47
48
49
50
51
# File 'lib/jscmd/pipebroker.rb', line 47

def subscribe(name, &proc)
  @subscribers[name] ||= []
  @subscribers[name] << proc
  proc
end

#unsubscribe(name, subscriber) ⇒ Object



53
54
55
# File 'lib/jscmd/pipebroker.rb', line 53

def unsubscribe(name, subscriber)
  sub = @subscribers[name].delete(subscriber)
end