Class: Miu::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/miu/proxy.rb

Constant Summary collapse

PROXY_TO =
'@__proxy_to__'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frontends, backends) ⇒ Proxy

Returns a new instance of Proxy.



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

def initialize(frontends, backends)
  @frontends = Array(frontends).map { |s| s.to_io rescue s }
  @backends = Array(backends).map { |s| s.to_io rescue s }

  @frontends.each { |s| s.instance_variable_set PROXY_TO, @backends }
  @backends.each { |s| s.instance_variable_set PROXY_TO, @frontends }

  @poller = ::ZMQ::Poller.new
  @frontends.each { |s| @poller.register_readable s }
  @backends.each { |s| @poller.register_readable s }
end

Instance Attribute Details

#backendsObject (readonly)

Returns the value of attribute backends.



6
7
8
# File 'lib/miu/proxy.rb', line 6

def backends
  @backends
end

#frontendsObject (readonly)

Returns the value of attribute frontends.



6
7
8
# File 'lib/miu/proxy.rb', line 6

def frontends
  @frontends
end

#pollerObject (readonly)

Returns the value of attribute poller.



7
8
9
# File 'lib/miu/proxy.rb', line 7

def poller
  @poller
end

Instance Method Details

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/miu/proxy.rb', line 23

def run
  loop do
    @poller.poll
    @poller.readables.each do |from|
      loop do
        msg = ::ZMQ::Message.new
        from.recvmsg msg
        more = from.more_parts?

        proxy_to = from.instance_variable_get PROXY_TO
        proxy_to.each do |to|
          ctrl = ::ZMQ::Message.new
          ctrl.copy msg.pointer
          to.sendmsg ctrl, (more ? ::ZMQ::SNDMORE : 0)
        end

        msg.close
        break unless more
      end
    end
  end
end