summaryrefslogtreecommitdiff
path: root/nanomsg/master.rb
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2015-05-13 20:32:08 +0200
committerPatrick Simianer <p@simianer.de>2015-05-13 20:32:08 +0200
commite76d0b1194c912f7329908d6f799eeccf9ab5456 (patch)
tree7f99524a31e86cb11e3661b97f3caa2c33230fca /nanomsg/master.rb
parent717bead50c8b209dac3f6ac8cb2c081613850d26 (diff)
nanomsg
Diffstat (limited to 'nanomsg/master.rb')
-rw-r--r--nanomsg/master.rb57
1 files changed, 57 insertions, 0 deletions
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 }
+