File: xreqxrep_poll.rb

package info (click to toggle)
ruby-ffi-rzmq 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 380 kB
  • ctags: 204
  • sloc: ruby: 2,945; makefile: 2
file content (92 lines) | stat: -rw-r--r-- 2,339 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

require File.join(File.dirname(__FILE__), '..', 'lib', 'ffi-rzmq')

def assert(rc)
  raise "Last API call failed at #{caller(1)}" unless rc >= 0
end

link = "tcp://127.0.0.1:5555"


begin
  ctx = ZMQ::Context.new
  s1 = ctx.socket(ZMQ::XREQ)
  s2 = ctx.socket(ZMQ::XREP)
rescue ContextError => e
  STDERR.puts "Failed to allocate context or socket"
  raise
end

s1.identity = 'socket1.xreq'
s2.identity = 'socket2.xrep'

assert(s1.setsockopt(ZMQ::LINGER, 100))
assert(s2.setsockopt(ZMQ::LINGER, 100))

assert(s1.bind(link))
assert(s2.connect(link))

poller = ZMQ::Poller.new
poller.register_readable(s2)
poller.register_writable(s1)

start_time = Time.now
@unsent = true

until @done do
  assert(poller.poll_nonblock)

  # send the message after 5 seconds
  if Time.now - start_time > 5 && @unsent
    puts "sending payload nonblocking"

    5.times do |i|
      payload = "#{ i.to_s * 40 }"
      assert(s1.send_string(payload, ZMQ::DONTWAIT))
    end
    @unsent = false
  end

  # check for messages after 1 second
  if Time.now - start_time > 1
    poller.readables.each do |sock|

      if sock.identity =~ /xrep/
        routing_info = ''
        assert(sock.recv_string(routing_info, ZMQ::DONTWAIT))
        puts "routing_info received [#{routing_info}] on socket.identity [#{sock.identity}]"
      else
        routing_info = nil
        received_msg = ''
        assert(sock.recv_string(received_msg, ZMQ::DONTWAIT))

        # skip to the next iteration if received_msg is nil; that means we got an EAGAIN
        next unless received_msg
        puts "message received [#{received_msg}] on socket.identity [#{sock.identity}]"
      end

      while sock.more_parts? do
        received_msg = ''
        assert(sock.recv_string(received_msg, ZMQ::DONTWAIT))

        puts "message received [#{received_msg}]"
      end

      puts "kick back a reply"
      assert(sock.send_string(routing_info, ZMQ::SNDMORE | ZMQ::DONTWAIT)) if routing_info
      time = Time.now.strftime "%Y-%m-%dT%H:%M:%S.#{Time.now.usec}"
      reply = "reply " + sock.identity.upcase + " #{time}"
      puts "sent reply [#{reply}], #{time}"
      assert(sock.send_string(reply))
      @done = true
      poller.register_readable(s1)
    end
  end
end

puts "executed in [#{Time.now - start_time}] seconds"

assert(s1.close)
assert(s2.close)

ctx.terminate