Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'bundler/setup'
- require "ffi-rzmq"
- require "msgpack"
- class FakeSocketClient
- attr_reader :subscriber, :count, :topics
- def initialize(ctx, topics)
- print "Consumer initializing...\n"
- topics = [""] if topics.empty?
- @subscriber = ctx.socket(ZMQ::SUB)
- @subscriber.connect("tcp://localhost:10000")
- topics.each do |topic|
- if topic == ""
- print "\tSubscribed to ALL topics!\n"
- else
- print "\tSubscribed to #{topic}\n"
- end
- @subscriber.setsockopt(ZMQ::SUBSCRIBE, topic)
- end
- @count = 0
- @topics = {}
- end
- def listen
- print "Listening for topics...\n\n"
- loop do
- msg_parts = []
- subscriber.recv_strings msg_parts
- @count +=1
- topic, message = msg_parts
- @topics[topic] ||= []
- @topics[topic] << message
- print "\t#{topic}/#{@count}:#{MessagePack.unpack(message)["tx_id"]}\n"
- end
- end
- def stop
- @subscriber.close if @subscriber
- end
- end
- CTX = ZMQ::Context.new(1)
- c = FakeSocketClient.new CTX, ARGV
- c.listen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement