-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriber_poc.rb
More file actions
89 lines (73 loc) · 2 KB
/
subscriber_poc.rb
File metadata and controls
89 lines (73 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'ffi-rzmq'
require 'zlib'
require 'json'
# Reference docs: https://www.rubydoc.info/github/chuckremes/ffi-rzmq/ZMQ/
module EDDN
class SubscriberPoc
attr_reader :relay, :sock_timeout, :acc, :poller, :context, :subscriber
def initialize(accumulator = [])
@relay = 'tcp://eddn.edcd.io:9500'
@sock_timeout = 60000 # 1 minute
@acc = accumulator
end
def run!
while true do
begin
connect
set_poll
while true do
res = poll_routine
if res == -1
break
end
end
rescue => e
puts "Loop error: #{e.message}"
disconnect!(99, "Error in the loop!")
break
end
end
end
def connect
prepare
subscriber.connect(relay)
puts "Connected to EDDB at #{relay}"
end
def set_poll
@poller = ZMQ::Poller.new()
@poller.register(subscriber, ZMQ::POLLIN)
puts "Poller created!"
end
def poll_routine
poll_socks = poller.poll(sock_timeout)
if poll_socks
parse_message
else
disconnect!(98, "Timeout (#{sock_timeout})")
return -1
end
end
def parse_message
new_msg = ZMQ::Message.new()
# https://www.rubydoc.info/github/chuckremes/ffi-rzmq/ZMQ/Socket#recvmsg-instance_method
recv_res = subscriber.recvmsg(new_msg, ZMQ::DONTWAIT)
unless recv_res == -1
decomp_msg = Zlib::Inflate.inflate(new_msg.copy_out_string)
json_msg = ::JSON.parse(decomp_msg)
#acc.push json_msg
puts "Message Received: #{json_msg}"
else
disconnect!(recv_res)
end
end
def disconnect!(errno, errmsg = "")
puts "Disconnected from EDDN! | ERRNO: #{errno} | ERRMSG: #{errmsg}"
subscriber.disconnect(relay)
end
def prepare
@context = ZMQ::Context.new
@subscriber = context.socket(ZMQ::SUB)
@subscriber.setsockopt(ZMQ::SUBSCRIBE, "")
end
end
end