-
Notifications
You must be signed in to change notification settings - Fork 7
/
zmq_client_ffi.rb
58 lines (49 loc) · 1.1 KB
/
zmq_client_ffi.rb
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
require 'rubygems'
require 'ffi-rzmq'
require 'json'
require 'pp'
Thread.abort_on_exception = true
zmq = ZMQ::Context.new
subscriber = zmq.socket(ZMQ::SUB)
subscriber.connect("tcp://*:6666")
subscriber.setsockopt(ZMQ::SUBSCRIBE,"")
def handle_servicecheck(payload)
puts "Received service check:"
pp payload
end
def handle_hostcheck(payload)
puts "Received host check:"
pp payload
end
def handle_notification(payload)
puts "Received notification:"
pp payload
end
def error_check(rc)
if ZMQ::Util.resultcode_ok?(rc)
false
else
STDERR.puts "Operation failed, errno [#{ZMQ::Util.errno}] description [#{ZMQ::Util.error_string}]"
caller(1).each { |callstack| STDERR.puts(callstack) }
true
end
end
while true
raw=''
rc= subscriber.recv_string(raw)
break if error_check(rc)
info=JSON.parse(raw)
context=info["context"]
payload=info["payload"]
case context
when "SERVICECHECK"
handle_servicecheck(payload)
when "HOSTCHECK"
handle_hostcheck(payload)
when "NOTIFICATION"
handle_notification(payload)
else
puts "Unknown context #{context}"
end
end
exit 0