Last active
May 1, 2017 21:45
-
-
Save nickludlam/b19109b08d3569b85f5a9fc473a2de02 to your computer and use it in GitHub Desktop.
Testing EDDN with Ruby
This file contains hidden or 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 characters
require "rbzmq" | |
require "zlib" | |
require "thread" | |
require "json" | |
$queue = Queue.new | |
should_run = true | |
EDDN_RELAY = 'tcp://eddn-relay.elite-markets.net:9500' | |
EDDN_TIMEOUT = 60000 | |
eddn_reader_thread = Thread.new do | |
subscriber = RbZMQ::Socket.new ZMQ::SUB | |
subscriber.connect EDDN_RELAY | |
subscriber.setsockopt(ZMQ::SUBSCRIBE, "") | |
while should_run do | |
raw_data = subscriber.recv(timeout: EDDN_TIMEOUT).to_s | |
json_data = Zlib::Inflate.inflate raw_data | |
native_hash = JSON.parse json_data | |
$queue << native_hash | |
puts "[R] Enqueued a message. Queue at length #{$queue.length}" | |
end | |
end | |
eddn_consumer_thread = Thread.new do | |
while should_run do | |
entry = $queue.pop | |
header = entry["header"] | |
message = entry["message"] | |
if message["event"] == "FSDJump" | |
destination = message["StarSystem"] | |
puts "[C] Someone just jumped to #{destination}" | |
else | |
puts "[C] Skipping unwanted message event >#{message['event']}<" | |
end | |
end | |
end | |
# Main thread here. Simply ask user to press y | |
# and if they do, quit the threads by using the | |
# should_run variable | |
while true do | |
puts "Press 'q' to quit" | |
input = gets.chomp.downcase | |
if input == 'q' | |
should_run = false | |
puts "Quitting eddn_reader_thread" | |
eddn_reader_thread.join | |
puts "Quitting eddn_consumer_thread" | |
eddn_consumer_thread.join | |
break | |
end | |
end | |
puts "Finished!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment