Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.08 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'bundler/setup'
  4. require "ffi-rzmq"
  5. require "msgpack"
  6.  
  7. class FakeSocketClient
  8.  
  9.   attr_reader :subscriber, :count, :topics
  10.  
  11.   def initialize(ctx, topics)
  12.     print "Consumer initializing...\n"
  13.     topics = [""] if topics.empty?
  14.     @subscriber = ctx.socket(ZMQ::SUB)
  15.     @subscriber.connect("tcp://localhost:10000")
  16.     topics.each do |topic|
  17.       if topic == ""
  18.         print "\tSubscribed to ALL topics!\n"
  19.       else
  20.         print "\tSubscribed to #{topic}\n"
  21.       end
  22.       @subscriber.setsockopt(ZMQ::SUBSCRIBE, topic)
  23.     end
  24.     @count = 0
  25.     @topics = {}
  26.   end
  27.  
  28.   def listen
  29.     print "Listening for topics...\n\n"
  30.     loop do
  31.       msg_parts = []
  32.       subscriber.recv_strings msg_parts
  33.       @count +=1
  34.       topic, message = msg_parts
  35.       @topics[topic] ||= []
  36.       @topics[topic] << message
  37.       print "\t#{topic}/#{@count}:#{MessagePack.unpack(message)["tx_id"]}\n"
  38.     end
  39.   end
  40.  
  41.   def stop
  42.     @subscriber.close if @subscriber
  43.   end
  44.  
  45. end
  46.  
  47. CTX = ZMQ::Context.new(1)
  48. c = FakeSocketClient.new CTX, ARGV
  49. c.listen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement