From e76d0b1194c912f7329908d6f799eeccf9ab5456 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Wed, 13 May 2015 20:32:08 +0200 Subject: nanomsg --- nanomsg/master.rb | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 nanomsg/master.rb (limited to 'nanomsg/master.rb') diff --git a/nanomsg/master.rb b/nanomsg/master.rb new file mode 100644 index 0000000..e21f88d --- /dev/null +++ b/nanomsg/master.rb @@ -0,0 +1,57 @@ +require 'nanomsg' + +port = 60000 +socks = [] +m = 1 +m.times { |i| + socks << NanoMsg::PairSocket.new + addr = "tcp://127.0.0.1:#{port}" + socks.last.bind addr + puts "listening on #{addr}" + port += 1 +} + +threads = [] +socks.each_with_index { |n,i| + threads << Thread.new { + puts "sending hello to #{i}" + n.send "hello #{i}" + sleep 1 + n.recv + puts "got hello from #{i}" + } +} + +threads.each { |thr| thr.join } +threads.clear + +socks.each_with_index {|n,i| + threads << Thread.new { + while true + msg = n.recv + puts "message from #{i}: #{msg}" + break if msg == "shutting down" + sleep 1 + end + } +} + +i = 0 +j = 0 +while line = STDIN.gets + puts "sending source #{i} to #{j}" + socks[j].send "#{j} #{i} #{line.strip}" + sleep 1 + i += 1 + j += 1 + j = 0 if j==m +end + +socks.each { |n| + Thread.new { + n.send "shutdown" + } +} + +threads.each { |thr| thr.join } + -- cgit v1.2.3