summaryrefslogtreecommitdiff
path: root/nanomsg/bus.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/bus.rb
parent717bead50c8b209dac3f6ac8cb2c081613850d26 (diff)
nanomsg
Diffstat (limited to 'nanomsg/bus.rb')
-rw-r--r--nanomsg/bus.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/nanomsg/bus.rb b/nanomsg/bus.rb
new file mode 100644
index 0000000..8a561e3
--- /dev/null
+++ b/nanomsg/bus.rb
@@ -0,0 +1,46 @@
+require 'nanomsg'
+
+port = 60000
+srv = NanoMsg::BusSocket.new
+srv.bind("tcp://127.0.0.1:#{port}")
+port += 1
+
+slaves = []
+ports = []
+10.times {
+ slaves << NanoMsg::BusSocket.new
+ slaves.last.bind "tcp://127.0.0.1:#{port}"
+ srv.connect "tcp://127.0.0.1:#{port}"
+ ports << port
+ port += 1
+}
+
+slaves.each_with_index { |n,i|
+ ports[i+1..ports.size].each { |p|
+ slaves[i].connect "tcp://127.0.0.1:#{p}"
+ }
+}
+
+slaves.each_with_index { |n,i|
+ Thread.new {
+ me = i
+ while true
+ msg = n.recv
+ to = msg.split.first.split('=')[1].to_i
+ puts "slave #{i} recv: '#{msg}'" if me==to
+ sleep 1
+ end
+ }
+}
+
+i = 0
+j = 0
+while line = STDIN.gets
+ puts "src: sending #{i}"
+ srv.send("for=#{j} #{i} #{line.strip}")
+ sleep 1
+ i += 1
+ j += 1
+ j = 0 if j==10
+end
+