Skip to content

Instantly share code, notes, and snippets.

@technoweenie
Created June 17, 2011 14:38

Revisions

  1. technoweenie revised this gist Jun 19, 2011. 1 changed file with 45 additions and 12 deletions.
    57 changes: 45 additions & 12 deletions sub.rb
    Original file line number Diff line number Diff line change
    @@ -6,19 +6,52 @@
    require 'rubygems'
    require 'zmq'

    context = ZMQ::Context.new
    chans = %w(rubyonrails ruby-lang ping)
    sub = context.socket ZMQ::SUB
    class Subscriber
    attr_reader :id, :socket
    def initialize(id = nil)
    @id = id || rand.to_s
    end

    if id = ARGV[0]
    sub.setsockopt ZMQ::IDENTITY, id
    puts "Identified as #{id}"
    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

    sub.connect 'tcp://127.0.0.1:5555'
    chans.each { |ch| sub.setsockopt ZMQ::SUBSCRIBE, ch }
    subscriber = Subscriber.new ARGV[0]
    subscriber.connect ZMQ::Context.new, 'tcp://127.0.0.1:5555'
    subscriber.subscribe_to 'rubyonrails', 'ruby-lang', 'ping'

    while line = sub.recv
    chan, user, msg = line.split ' ', 3
    puts "##{chan} [#{user}]: #{msg}"
    end
    loop do
    unless subscriber.process
    subscriber.close
    puts "Quitting..."
    exit
    end
    end
  2. technoweenie renamed this gist Jun 17, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. technoweenie revised this gist Jun 17, 2011. 2 changed files with 11 additions and 1 deletion.
    10 changes: 10 additions & 0 deletions constant.rb
    Original 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
    2 changes: 1 addition & 1 deletion sub.rb
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    require 'zmq'

    context = ZMQ::Context.new
    chans = %w(rubyonrails ruby-lang)
    chans = %w(rubyonrails ruby-lang ping)
    sub = context.socket ZMQ::SUB

    if id = ARGV[0]
  4. technoweenie revised this gist Jun 17, 2011. 2 changed files with 7 additions and 5 deletions.
    7 changes: 4 additions & 3 deletions pub.rb
    Original 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.bind 'tcp://*:5555'
    pub.setsockopt ZMQ::IDENTITY, "#{chan}-#{user}"

    pub.bind 'tcp://*:5555'

    while msg = STDIN.gets
    msg.strip!
    pub.send "#{chan} #{user} #{msg}"
    end
    end
    5 changes: 3 additions & 2 deletions sub.rb
    Original file line number Diff line number Diff line change
    @@ -10,14 +10,15 @@
    chans = %w(rubyonrails ruby-lang)
    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

    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
    end
  5. technoweenie revised this gist Jun 17, 2011. 2 changed files with 7 additions and 8 deletions.
    11 changes: 3 additions & 8 deletions pub.rb
    Original 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'

    context = ZMQ::Context.new
    chan = ARGV[0]
    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
    end
    4 changes: 4 additions & 0 deletions sub.rb
    Original 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
  6. technoweenie revised this gist Jun 17, 2011. 1 changed file with 8 additions and 9 deletions.
    17 changes: 8 additions & 9 deletions pub.rb
    Original 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!

    if msg == 'quit'
    pub.close
    context.close
    exit
    else
    pub.send "#{chan} #{user} #{msg}"
    end
    end
    pub.send "#{chan} #{user} #{msg}"
    end
  7. technoweenie created this gist Jun 17, 2011.
    27 changes: 27 additions & 0 deletions pub.rb
    Original 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
    19 changes: 19 additions & 0 deletions sub.rb
    Original 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