Created
June 17, 2011 14:38
Revisions
-
technoweenie revised this gist
Jun 19, 2011 . 1 changed file with 45 additions and 12 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,19 +6,52 @@ require 'rubygems' require 'zmq' class Subscriber attr_reader :id, :socket def initialize(id = nil) @id = id || rand.to_s end def connect(*addrs) if !@socket context = addrs.shift @socket = context.socket ZMQ::SUB @socket.setsockopt ZMQ::IDENTITY, @id end addrs.each do |addr| @socket.connect addr end end def subscribe_to(*channels) channels.each { |ch| @socket.setsockopt ZMQ::SUBSCRIBE, ch } end def process(line = nil) line ||= @socket.recv chan, user, msg = line.split ' ', 3 puts "##{chan} [#{user}]: #{msg}" true rescue SignalException process(line) if line false end def close @socket.close @socket = nil end end subscriber = Subscriber.new ARGV[0] subscriber.connect ZMQ::Context.new, 'tcp://127.0.0.1:5555' subscriber.subscribe_to 'rubyonrails', 'ruby-lang', 'ping' loop do unless subscriber.process subscriber.close puts "Quitting..." exit end end -
technoweenie renamed this gist
Jun 17, 2011 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
technoweenie revised this gist
Jun 17, 2011 . 2 changed files with 11 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ require 'zmq' context = ZMQ::Context.new pub = context.socket ZMQ::PUB pub.setsockopt ZMQ::IDENTITY, 'ping-pinger' pub.bind 'tcp://*:5555' i=0 loop do pub.send "ping pinger #{i+=1}" ; sleep 1 end This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ require 'zmq' context = ZMQ::Context.new chans = %w(rubyonrails ruby-lang ping) sub = context.socket ZMQ::SUB if id = ARGV[0] -
technoweenie revised this gist
Jun 17, 2011 . 2 changed files with 7 additions and 5 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,18 +4,19 @@ # # # binds a PUB socket to tcp://*:5555 require 'rubygems' require 'zmq' context = ZMQ::Context.new chan = ARGV[0] user = ARGV[1] pub = context.socket ZMQ::PUB pub.setsockopt ZMQ::IDENTITY, "#{chan}-#{user}" pub.bind 'tcp://*:5555' while msg = STDIN.gets msg.strip! pub.send "#{chan} #{user} #{msg}" end This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,14 +10,15 @@ chans = %w(rubyonrails ruby-lang) sub = context.socket ZMQ::SUB if id = ARGV[0] sub.setsockopt ZMQ::IDENTITY, id puts "Identified as #{id}" end sub.connect 'tcp://127.0.0.1:5555' chans.each { |ch| sub.setsockopt ZMQ::SUBSCRIBE, ch } while line = sub.recv chan, user, msg = line.split ' ', 3 puts "##{chan} [#{user}]: #{msg}" end -
technoweenie revised this gist
Jun 17, 2011 . 2 changed files with 7 additions and 8 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ # # # binds a PUB socket to tcp://*:5555 # require 'rubygems' require 'zmq' @@ -13,14 +13,9 @@ user = ARGV[1] pub = context.socket ZMQ::PUB pub.bind 'tcp://*:5555' pub.setsockopt ZMQ::IDENTITY, "#{chan}-#{user}" while msg = STDIN.gets msg.strip! pub.send "#{chan} #{user} #{msg}" end This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,6 +11,10 @@ sub = context.socket ZMQ::SUB sub.connect 'tcp://127.0.0.1:5555' if id = ARGV[0] sub.setsockopt ZMQ::IDENTITY, id puts "Identified as #{id}" end chans.each { |ch| sub.setsockopt ZMQ::SUBSCRIBE, ch } while line = sub.recv -
technoweenie revised this gist
Jun 17, 2011 . 1 changed file with 8 additions and 9 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,14 +14,13 @@ pub = context.socket ZMQ::PUB pub.bind 'tcp://*:5555' context = ZMQ::Context.new chan = ARGV[0] user = ARGV[1] pub = context.socket ZMQ::PUB pub.bind 'tcp://*:5555' while msg = STDIN.gets msg.strip! pub.send "#{chan} #{user} #{msg}" end -
technoweenie created this gist
Jun 17, 2011 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ # usage: ruby pub.rb CHAN USERNAME # # ruby pub.rb rubyonrails technoweenie # # # binds a PUB socket to tcp://*:5555 require 'rubygems' require 'zmq' context = ZMQ::Context.new chan = ARGV[0] user = ARGV[1] pub = context.socket ZMQ::PUB pub.bind 'tcp://*:5555' while msg = STDIN.gets msg.strip! if msg == 'quit' pub.close context.close exit else pub.send "#{chan} #{user} #{msg}" end end This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ # usage: ruby sub.rb # # Connects a SUB socket to tcp://*:5555. # Subscribes to rubyonrails and ruby-lang. require 'rubygems' require 'zmq' context = ZMQ::Context.new chans = %w(rubyonrails ruby-lang) sub = context.socket ZMQ::SUB sub.connect 'tcp://127.0.0.1:5555' chans.each { |ch| sub.setsockopt ZMQ::SUBSCRIBE, ch } while line = sub.recv chan, user, msg = line.split ' ', 3 puts "##{chan} [#{user}]: #{msg}" end