ffi-czmq 0.0.1.pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/README.md +4 -0
- data/lib/czmq/libczmq.rb +204 -0
- data/lib/czmq/sock_type.rb +130 -0
- data/lib/czmq/utils.rb +61 -0
- data/lib/czmq/version.rb +3 -0
- data/lib/czmq/zactor.rb +77 -0
- data/lib/czmq/zcert.rb +56 -0
- data/lib/czmq/zcertstore.rb +16 -0
- data/lib/czmq/zconfig.rb +69 -0
- data/lib/czmq/zframe.rb +84 -0
- data/lib/czmq/ziflist.rb +38 -0
- data/lib/czmq/zloop.rb +54 -0
- data/lib/czmq/zmsg.rb +175 -0
- data/lib/czmq/zpoller.rb +33 -0
- data/lib/czmq/zsock.rb +125 -0
- data/lib/czmq/zsys.rb +40 -0
- data/lib/czmq.rb +13 -0
- data/lib/ffi-czmq.rb +13 -0
- metadata +91 -0
data/lib/czmq/zcert.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require_relative 'utils'
|
2
|
+
require_relative 'libczmq'
|
3
|
+
|
4
|
+
if CZMQ::Utils.has_curve
|
5
|
+
module CZMQ
|
6
|
+
class Zcert
|
7
|
+
extend ::LibCZMQ
|
8
|
+
|
9
|
+
czmq_constructor
|
10
|
+
czmq_destructor
|
11
|
+
|
12
|
+
czmq_function :public_txt, :public_txt, [:pointer], :string
|
13
|
+
czmq_function :secret_txt, :secret_txt, [:pointer], :string
|
14
|
+
czmq_function :load_zcert, :load, [:string], :pointer
|
15
|
+
czmq_function :save, :save, [:pointer, :string], :int
|
16
|
+
czmq_function :save_public, :save_public, [:pointer, :string], :int
|
17
|
+
czmq_function :save_secret, :save_secret, [:pointer, :string], :int
|
18
|
+
czmq_function :apply, :apply, [:pointer, :pointer], :void
|
19
|
+
czmq_function :dup_zcert, :dup, [:pointer], :pointer
|
20
|
+
czmq_function :eq, :eq, [:pointer, :pointer], :bool
|
21
|
+
|
22
|
+
def self.convert(cert)
|
23
|
+
if Utils.check_for_pointer(cert)
|
24
|
+
|
25
|
+
return cert
|
26
|
+
elsif cert.respond_to?(:to_zcert) &&
|
27
|
+
Utils.check_for_pointer(cert.to_zcert)
|
28
|
+
|
29
|
+
return cert.to_zcert
|
30
|
+
else
|
31
|
+
fail ArgumentError, "#{cert.class} is not a CZMQ::Zcert"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.load(filename)
|
36
|
+
unless (zcert = load_zcert(filename)).null?
|
37
|
+
new_from_czmq_obj(zcert)
|
38
|
+
else
|
39
|
+
fail IOError, Utils.error
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def dup
|
44
|
+
self.class.new_from_czmq_obj(dup_zcert)
|
45
|
+
end
|
46
|
+
|
47
|
+
def ==(other)
|
48
|
+
eq(self.class.convert(other))
|
49
|
+
end
|
50
|
+
|
51
|
+
def !=(other)
|
52
|
+
! self.==(other)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative 'utils'
|
2
|
+
require_relative 'libczmq'
|
3
|
+
|
4
|
+
if CZMQ::Utils.has_curve
|
5
|
+
module CZMQ
|
6
|
+
class Zcertstore
|
7
|
+
extend ::LibCZMQ
|
8
|
+
|
9
|
+
czmq_constructor [:string]
|
10
|
+
czmq_destructor
|
11
|
+
|
12
|
+
czmq_function :lookup, :lookup, [:pointer, :string], :pointer
|
13
|
+
czmq_function :insert_zcert, :insert, [:pointer, :pointer], :void
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/czmq/zconfig.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require_relative 'libczmq'
|
2
|
+
|
3
|
+
module CZMQ
|
4
|
+
class Zconfig
|
5
|
+
extend ::LibCZMQ
|
6
|
+
|
7
|
+
czmq_constructor [:string, :pointer]
|
8
|
+
czmq_destructor
|
9
|
+
|
10
|
+
czmq_function :name, :name, [:pointer], :string
|
11
|
+
czmq_function :value, :value, [:pointer], :string
|
12
|
+
czmq_function :put, :put, [:pointer, :string, :string], :void
|
13
|
+
czmq_function :set_name, :set_name, [:pointer, :string], :void
|
14
|
+
czmq_function :set_value, :set_value, [:pointer, :string], :void
|
15
|
+
czmq_function :child_zconfig, :child, [:pointer], :pointer
|
16
|
+
czmq_function :next_zconfig, :next, [:pointer], :pointer
|
17
|
+
czmq_function :locate_zconfig, :locate, [:pointer, :string], :pointer
|
18
|
+
czmq_function :resolve, :resolve, [:pointer, :string, :string], :string
|
19
|
+
czmq_function :zconfig_at_depth, :at_depth, [:pointer, :int], :pointer
|
20
|
+
czmq_function :execute_zconfig, :execute, [:pointer, :pointer, :pointer], :int
|
21
|
+
czmq_function :set_comment, :set_comment, [:pointer, :string], :void
|
22
|
+
czmq_function :load_zconfig, :load, [:string], :pointer
|
23
|
+
czmq_function :save_zconfig, :save, [:pointer, :string], :int
|
24
|
+
czmq_function :filename, :filename, [:pointer], :string
|
25
|
+
czmq_function :reload, :reload, [:pointer], :int
|
26
|
+
czmq_function :has_changed, :has_changed, [:pointer], :bool
|
27
|
+
|
28
|
+
def self.load(filename)
|
29
|
+
unless (zconfig = load_zconfig(filename)).null?
|
30
|
+
new_from_czmq_obj(zconfig)
|
31
|
+
else
|
32
|
+
fail IOError, Utils.error
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def child
|
37
|
+
self.class.new_from_czmq_obj(child_zconfig, nil)
|
38
|
+
end
|
39
|
+
|
40
|
+
def next
|
41
|
+
self.class.new_from_czmq_obj(next_zconfig, nil)
|
42
|
+
end
|
43
|
+
|
44
|
+
def execute(&block)
|
45
|
+
zconfig_fct = FFI::Function.new(:int, [:pointer, :pointer, :int], blocking: true) do |zconfig_t, args, level|
|
46
|
+
zconfig = self.class.new_from_czmq_obj(zconfig_t, nil)
|
47
|
+
block.call(zconfig, level)
|
48
|
+
end
|
49
|
+
|
50
|
+
execute_zconfig(zconfig_fct, nil)
|
51
|
+
end
|
52
|
+
|
53
|
+
def locate(path)
|
54
|
+
unless (zconfig = locate_zconfig(path)).null?
|
55
|
+
self.class.new_from_czmq_obj(zconfig, nil)
|
56
|
+
else
|
57
|
+
fail Utils.error
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def at_depth(level)
|
62
|
+
unless (zconfig = zconfig_at_depth(level)).null?
|
63
|
+
self.class.new_from_czmq_obj(zconfig, nil)
|
64
|
+
else
|
65
|
+
fail Utils.error
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/czmq/zframe.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require_relative 'libczmq'
|
2
|
+
require_relative 'utils'
|
3
|
+
require_relative 'zsock'
|
4
|
+
|
5
|
+
module CZMQ
|
6
|
+
class Zframe
|
7
|
+
include Comparable
|
8
|
+
MORE = 1
|
9
|
+
REUSE = 2
|
10
|
+
DONTWAIT = 4
|
11
|
+
|
12
|
+
extend ::LibCZMQ
|
13
|
+
|
14
|
+
czmq_constructor [:pointer, :size_t]
|
15
|
+
czmq_destructor
|
16
|
+
|
17
|
+
czmq_function :new_empty_zframe, :new_empty, [], :pointer
|
18
|
+
czmq_function :recv_zframe, :recv, [:pointer], :pointer
|
19
|
+
czmq_function :send_zframe, :send, [:pointer, :pointer, :int], :int
|
20
|
+
czmq_function :size, :size, [:pointer], :size_t
|
21
|
+
czmq_function :data, :data, [:pointer], :pointer
|
22
|
+
czmq_function :dup_zframe, :dup, [:pointer], :pointer
|
23
|
+
czmq_function :more, :more, [:pointer], :int
|
24
|
+
czmq_function :set_more, :set_more, [:pointer, :int], :void
|
25
|
+
czmq_function :eq, :eq, [:pointer, :pointer], :bool
|
26
|
+
czmq_function :reset, :reset, [:pointer, :pointer, :size_t], :void
|
27
|
+
czmq_function :print, :print, [:pointer, :string], :void
|
28
|
+
|
29
|
+
def self.new_empty
|
30
|
+
unless (zframe = new_empty_zframe).null?
|
31
|
+
new_from_czmq_obj(zframe)
|
32
|
+
else
|
33
|
+
fail Utils.error
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.convert(frame)
|
38
|
+
if Utils.check_for_pointer(frame)
|
39
|
+
|
40
|
+
return frame
|
41
|
+
elsif frame.respond_to?(:to_zframe) &&
|
42
|
+
Utils.check_for_pointer(frame.to_zframe)
|
43
|
+
|
44
|
+
return frame.to_zframe
|
45
|
+
else
|
46
|
+
fail ArgumentError, "#{frame.class} is not a CZMQ::Zframe"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def dup
|
51
|
+
self.class.new_from_czmq_obj(dup_zframe)
|
52
|
+
end
|
53
|
+
|
54
|
+
def more?
|
55
|
+
more & MORE > 0
|
56
|
+
end
|
57
|
+
|
58
|
+
def <=>(other)
|
59
|
+
size <=> other.size
|
60
|
+
end
|
61
|
+
|
62
|
+
def ==(other)
|
63
|
+
(object_id == other.object_id ||eq(self.class.convert(other)))
|
64
|
+
end
|
65
|
+
|
66
|
+
def !=(other)
|
67
|
+
! self.==(other)
|
68
|
+
end
|
69
|
+
|
70
|
+
def to_str
|
71
|
+
data.read_bytes(size)
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.recv(socket)
|
75
|
+
zsock = Zsock.convert(socket)
|
76
|
+
|
77
|
+
unless (zframe = recv_zframe(zsock)).null?
|
78
|
+
new_from_czmq_obj(zframe)
|
79
|
+
else
|
80
|
+
fail IOError, Utils.error
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/czmq/ziflist.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative 'libczmq'
|
2
|
+
|
3
|
+
module CZMQ
|
4
|
+
class Ziflist
|
5
|
+
include Enumerable
|
6
|
+
extend ::LibCZMQ
|
7
|
+
|
8
|
+
czmq_constructor
|
9
|
+
czmq_destructor
|
10
|
+
|
11
|
+
czmq_function :reload, :reload, [:pointer], :void
|
12
|
+
czmq_function :size, :size, [:pointer], :size_t
|
13
|
+
czmq_function :first, :first, [:pointer], :string
|
14
|
+
czmq_function :next, :next, [:pointer], :string
|
15
|
+
czmq_function :address, :address, [:pointer], :string
|
16
|
+
czmq_function :broadcast, :broadcast, [:pointer], :string
|
17
|
+
czmq_function :netmask, :netmask, [:pointer], :string
|
18
|
+
|
19
|
+
def each
|
20
|
+
yield first
|
21
|
+
size.pred.times do
|
22
|
+
yield self.next
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_h
|
27
|
+
hsh = {}
|
28
|
+
each do |interface|
|
29
|
+
hsh[interface] = {
|
30
|
+
address: address,
|
31
|
+
broadcast: broadcast,
|
32
|
+
netmask: netmask
|
33
|
+
}
|
34
|
+
end
|
35
|
+
hsh
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/czmq/zloop.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative 'libczmq'
|
2
|
+
require_relative 'zsock'
|
3
|
+
require_relative 'utils'
|
4
|
+
|
5
|
+
module CZMQ
|
6
|
+
class Zloop
|
7
|
+
extend ::LibCZMQ
|
8
|
+
|
9
|
+
czmq_constructor
|
10
|
+
czmq_destructor
|
11
|
+
|
12
|
+
czmq_function :reader, :reader, [:pointer, :pointer, :pointer, :pointer], :int
|
13
|
+
czmq_function :reader_end, :reader_end, [:pointer, :pointer], :void
|
14
|
+
czmq_function :timer, :timer, [:pointer, :size_t, :size_t, :pointer, :pointer], :int
|
15
|
+
czmq_function :timer_end, :timer_end, [:pointer, :int], :int
|
16
|
+
czmq_function :set_verbose, :set_verbose, [:pointer, :bool], :void
|
17
|
+
czmq_function :start, :start, [:pointer], :int
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@reader_callbacks = []
|
21
|
+
@timer_callbacks = []
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_reader(zsock, &block)
|
25
|
+
zloop_reader_fn = FFI::Function.new(:int, [:pointer, :pointer, :pointer], blocking: true) do |zloop_t, zsock_t, args|
|
26
|
+
zsocky = Zsock.new_from_czmq_obj(zsock_t, nil)
|
27
|
+
block.call(zsocky)
|
28
|
+
end
|
29
|
+
|
30
|
+
reader(Zsock.convert(zsock), zloop_reader_fn, nil)
|
31
|
+
@reader_callbacks << {zsock: zsock, zloop_reader_fn: zloop_reader_fn, block: block}
|
32
|
+
end
|
33
|
+
|
34
|
+
def remove_reader(zsock)
|
35
|
+
@reader_callbacks.delete_if {|reader| reader[:zsock] == zsock}
|
36
|
+
reader_end(Zsock.convert(zsock))
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_timer(delay, times, &block)
|
40
|
+
zloop_timer_fn = FFI::Function.new(:int, [:pointer, :int, :pointer], blocking: true) do |zloop_t, timer_id, args|
|
41
|
+
block.call(timer_id)
|
42
|
+
end
|
43
|
+
|
44
|
+
timer_id = timer(delay, times, zloop_timer_fn, nil)
|
45
|
+
@timer_callbacks << {timer_id: timer_id, delay: delay, times: times, zloop_timer_fn: zloop_timer_fn, block: block}
|
46
|
+
timer_id
|
47
|
+
end
|
48
|
+
|
49
|
+
def remove_timer(timer_id)
|
50
|
+
@timer_callbacks.delete_if {|timer| timer[:timer_id] == timer_id}
|
51
|
+
timer_end(timer_id)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/czmq/zmsg.rb
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
require_relative 'libczmq'
|
2
|
+
require_relative 'zframe'
|
3
|
+
require_relative 'zsock'
|
4
|
+
require_relative 'utils'
|
5
|
+
|
6
|
+
module CZMQ
|
7
|
+
class Zmsg
|
8
|
+
INTF = '%d'.freeze
|
9
|
+
include Enumerable
|
10
|
+
extend ::LibCZMQ
|
11
|
+
|
12
|
+
czmq_constructor
|
13
|
+
czmq_destructor
|
14
|
+
|
15
|
+
czmq_function :recv_zmsg, :recv, [:pointer], :pointer
|
16
|
+
czmq_function :send_zmsg, :send, [:pointer, :pointer], :int
|
17
|
+
czmq_function :size, :size, [:pointer], :size_t
|
18
|
+
czmq_function :content_size, :content_size, [:pointer], :size_t
|
19
|
+
czmq_function :prepend_zframe, :prepend, [:pointer, :pointer], :int
|
20
|
+
czmq_function :append_zframe, :append, [:pointer, :pointer], :int
|
21
|
+
czmq_function :pop_zframe, :pop, [:pointer], :pointer
|
22
|
+
czmq_function :push_mem, :pushmem, [:pointer, :pointer, :size_t], :int
|
23
|
+
czmq_function :add_mem, :addmem, [:pointer, :pointer, :size_t], :int
|
24
|
+
czmq_function :push_zstr, :pushstr, [:pointer, :string], :int
|
25
|
+
czmq_function :add_zstr, :addstr, [:pointer, :string], :int
|
26
|
+
czmq_function :push_zstrf, :pushstrf, [:pointer, :string, :varargs], :int
|
27
|
+
czmq_function :add_zstrf, :addstrf, [:pointer, :string, :varargs], :int
|
28
|
+
czmq_function :remove_zframe, :remove, [:pointer, :pointer], :void
|
29
|
+
czmq_function :first_zframe, :first, [:pointer], :pointer
|
30
|
+
czmq_function :next_zframe, :next, [:pointer], :pointer
|
31
|
+
czmq_function :last_zframe, :last, [:pointer], :pointer
|
32
|
+
czmq_function :encode_zmsg, :encode, [:pointer, :buffer_in], :size_t
|
33
|
+
czmq_function :decode_zmsg, :decode, [:pointer, :size_t], :pointer
|
34
|
+
czmq_function :dup_zmsg, :dup, [:pointer], :pointer
|
35
|
+
czmq_function :print, :print, [:pointer], :void
|
36
|
+
|
37
|
+
def each
|
38
|
+
yield first
|
39
|
+
size.pred.times do
|
40
|
+
yield self.next
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_a
|
45
|
+
ary = []
|
46
|
+
each {|frame| ary << frame.to_str }
|
47
|
+
ary
|
48
|
+
end
|
49
|
+
|
50
|
+
def add(data)
|
51
|
+
case data
|
52
|
+
when Zframe
|
53
|
+
append_zframe(data)
|
54
|
+
when String
|
55
|
+
add_string(data)
|
56
|
+
when Integer
|
57
|
+
add_zstrf(INTF, :long, data)
|
58
|
+
when NilClass
|
59
|
+
add_mem(data, 0)
|
60
|
+
else
|
61
|
+
if data.respond_to?(:to_zframe)
|
62
|
+
append_zframe(data.to_zframe)
|
63
|
+
elsif data.respond_to?(:data) &&
|
64
|
+
data.respond_to?(:size)
|
65
|
+
|
66
|
+
add_mem(data.data, data.size)
|
67
|
+
elsif data.respond_to?(:to_str)
|
68
|
+
add_string(data.to_str)
|
69
|
+
elsif data.nil?
|
70
|
+
add_mem(nil, 0)
|
71
|
+
else
|
72
|
+
fail ArgumentError, 'Unknown data type'
|
73
|
+
end
|
74
|
+
end
|
75
|
+
self
|
76
|
+
end
|
77
|
+
|
78
|
+
alias_method :<<, :add
|
79
|
+
|
80
|
+
def push(data)
|
81
|
+
case data
|
82
|
+
when Zframe
|
83
|
+
prepend_zframe(data)
|
84
|
+
when String
|
85
|
+
push_string(data)
|
86
|
+
when Integer
|
87
|
+
push_zstrf(INTF, :long, data)
|
88
|
+
when NilClass
|
89
|
+
push_mem(data, 0)
|
90
|
+
else
|
91
|
+
if data.respond_to?(:to_zframe)
|
92
|
+
prepend_zframe(data.to_zframe)
|
93
|
+
elsif data.respond_to?(:data) &&
|
94
|
+
data.respond_to?(:size)
|
95
|
+
|
96
|
+
push_mem(data.data, data.size)
|
97
|
+
elsif data.respond_to?(:to_str)
|
98
|
+
push_string(data.to_str)
|
99
|
+
elsif data.nil?
|
100
|
+
push_mem(nil, 0)
|
101
|
+
else
|
102
|
+
fail ArgumentError, 'Unknown data type'
|
103
|
+
end
|
104
|
+
end
|
105
|
+
self
|
106
|
+
end
|
107
|
+
|
108
|
+
[:first, :next, :last].each do |meth|
|
109
|
+
class_eval <<-RUBY, __FILE__, __LINE__
|
110
|
+
def #{meth.to_s}
|
111
|
+
CZMQ::Zframe.new_from_czmq_obj(#{meth.to_s}_zframe, nil)
|
112
|
+
end
|
113
|
+
RUBY
|
114
|
+
end
|
115
|
+
|
116
|
+
[:first=, :next=, :last=].each do |meth|
|
117
|
+
class_eval <<-RUBY, __FILE__, __LINE__
|
118
|
+
def #{meth.to_s}(content)
|
119
|
+
CZMQ::Zframe.reset(#{meth.to_s[0...-1]}_zframe, content, content.bytesize)
|
120
|
+
end
|
121
|
+
RUBY
|
122
|
+
end
|
123
|
+
|
124
|
+
def pop
|
125
|
+
Zframe.new_from_czmq_obj(pop_zframe)
|
126
|
+
end
|
127
|
+
|
128
|
+
def dup
|
129
|
+
self.class.new_from_czmq_obj(dup_zmsg)
|
130
|
+
end
|
131
|
+
|
132
|
+
def remove(zframe)
|
133
|
+
remove_zframe(Zframe.convert(zframe))
|
134
|
+
end
|
135
|
+
|
136
|
+
def self.recv(socket)
|
137
|
+
zsock = Zsock.convert(socket)
|
138
|
+
|
139
|
+
unless (zmsg = recv_zmsg(zsock)).null?
|
140
|
+
new_from_czmq_obj(zmsg)
|
141
|
+
else
|
142
|
+
fail IOError, Utils.error
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def encode
|
147
|
+
buffer = FFI::MemoryPointer.new(:pointer)
|
148
|
+
size = encode_zmsg(buffer)
|
149
|
+
bPtr = buffer.read_pointer()
|
150
|
+
bPtr.null? ? fail(Utils.error) : bPtr.read_bytes(size)
|
151
|
+
end
|
152
|
+
|
153
|
+
def self.decode(bytes)
|
154
|
+
new_from_czmq_obj(decode_zmsg(bytes, bytes.bytesize))
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
|
159
|
+
def add_string(string)
|
160
|
+
if string.encoding == Encoding::ASCII_8BIT
|
161
|
+
add_mem(string, string.size)
|
162
|
+
else
|
163
|
+
add_zstr(string)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def push_string(string)
|
168
|
+
if string.encoding == Encoding::ASCII_8BIT
|
169
|
+
push_mem(string, string.size)
|
170
|
+
else
|
171
|
+
push_zstr(string)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
data/lib/czmq/zpoller.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative 'libczmq'
|
2
|
+
|
3
|
+
module CZMQ
|
4
|
+
class Zpoller
|
5
|
+
extend ::LibCZMQ
|
6
|
+
|
7
|
+
czmq_constructor [:pointer, :varargs]
|
8
|
+
czmq_destructor
|
9
|
+
|
10
|
+
czmq_function :add, :add, [:pointer, :pointer], :int
|
11
|
+
czmq_function :remove, :remove, [:pointer, :pointer], :int
|
12
|
+
czmq_function :wait, :wait, [:pointer, :int], :pointer
|
13
|
+
czmq_function :expired, :expired, [:pointer], :bool
|
14
|
+
czmq_function :terminated, :terminated, [:pointer], :pointer
|
15
|
+
|
16
|
+
def self.new_poller(*pollitems)
|
17
|
+
zsocks = []
|
18
|
+
pollitems.each do |pollitem|
|
19
|
+
zsocks << Zsock.convert(pollitem)
|
20
|
+
end
|
21
|
+
case zsocks.size
|
22
|
+
when 0
|
23
|
+
fail ArgumentError, 'wrong number of arguments (0 for 1..n)'
|
24
|
+
when 1
|
25
|
+
new(zsocks.first, :pointer, nil)
|
26
|
+
else
|
27
|
+
first_sock = zsocks.delete(zsocks.first)
|
28
|
+
other_socks = ([ :pointer ] * zsocks.size).zip(zsocks).flatten
|
29
|
+
new(first_sock, *other_socks, :pointer, nil)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/czmq/zsock.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
require_relative 'libczmq'
|
2
|
+
require_relative 'utils'
|
3
|
+
require_relative 'sock_type'
|
4
|
+
require_relative 'zmsg'
|
5
|
+
|
6
|
+
module CZMQ
|
7
|
+
class Zsock
|
8
|
+
SET_SOCKOPT = /^set_(.+)$/.freeze
|
9
|
+
|
10
|
+
extend ::LibCZMQ
|
11
|
+
|
12
|
+
czmq_constructor
|
13
|
+
czmq_destructor
|
14
|
+
|
15
|
+
czmq_function :bind, :bind, [:pointer, :string], :int
|
16
|
+
czmq_function :endpoint, :endpoint, [:pointer], :string
|
17
|
+
czmq_function :unbind, :unbind, [:pointer, :string], :int
|
18
|
+
czmq_function :connect, :connect, [:pointer, :string], :int
|
19
|
+
czmq_function :disconnect, :disconnect,[:pointer, :string], :int
|
20
|
+
czmq_function :attach, :attach, [:pointer, :string, :bool], :int
|
21
|
+
czmq_function :type_str, :type_str, [:pointer], :string
|
22
|
+
czmq_function :signal, :signal, [:pointer, :uchar], :int
|
23
|
+
czmq_function :wait, :wait, [:pointer], :int
|
24
|
+
czmq_function :resolve, :resolve, [:pointer], :pointer
|
25
|
+
|
26
|
+
def self.convert(czmq_obj)
|
27
|
+
if Utils.check_for_pointer(czmq_obj)
|
28
|
+
|
29
|
+
return czmq_obj
|
30
|
+
elsif czmq_obj.respond_to?(:to_zsock) &&
|
31
|
+
Utils.check_for_pointer(czmq_obj.to_zsock)
|
32
|
+
|
33
|
+
return czmq_obj.to_zsock
|
34
|
+
elsif czmq_obj.respond_to?(:to_zactor) &&
|
35
|
+
Utils.check_for_pointer(czmq_obj.to_zactor)
|
36
|
+
|
37
|
+
return czmq_obj.to_zactor
|
38
|
+
else
|
39
|
+
fail ArgumentError, "#{czmq_obj.class} is not a CZMQ::Zsock or CZMQ::Zactor"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
[:rep, :router, :pull, :pub, :xpub].each do |meth|
|
44
|
+
instance_eval <<-RUBY, __FILE__, __LINE__
|
45
|
+
def new_#{meth.to_s}(endpoints)
|
46
|
+
sock = new(#{meth.inspect}, endpoints)
|
47
|
+
sock.attach(endpoints, true)
|
48
|
+
sock
|
49
|
+
end
|
50
|
+
RUBY
|
51
|
+
end
|
52
|
+
|
53
|
+
[:req, :dealer, :push, :xsub, :pair, :stream].each do |meth|
|
54
|
+
instance_eval <<-RUBY, __FILE__, __LINE__
|
55
|
+
def new_#{meth.to_s}(endpoints)
|
56
|
+
sock = new(#{meth.inspect}, endpoints)
|
57
|
+
sock.attach(endpoints, false)
|
58
|
+
sock
|
59
|
+
end
|
60
|
+
RUBY
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.new_sub(endpoints, *subscriptions)
|
64
|
+
sock = new(:sub, endpoints, *subscriptions)
|
65
|
+
sock.attach(endpoints, false)
|
66
|
+
subscriptions.each do |subscription|
|
67
|
+
sock.set_subscribe(subscription)
|
68
|
+
end
|
69
|
+
sock
|
70
|
+
end
|
71
|
+
|
72
|
+
def tell(*msgs)
|
73
|
+
zmsg = Zmsg.new
|
74
|
+
msgs.each {|msg| zmsg << msg}
|
75
|
+
forward(zmsg)
|
76
|
+
end
|
77
|
+
|
78
|
+
alias_method :<<, :tell
|
79
|
+
|
80
|
+
def forward(zmsg)
|
81
|
+
zmsg.send_zmsg(@czmq_obj)
|
82
|
+
end
|
83
|
+
|
84
|
+
def recv
|
85
|
+
Zmsg.recv(@czmq_obj)
|
86
|
+
end
|
87
|
+
|
88
|
+
def method_missing(meth, *args, &blk)
|
89
|
+
if args.length == 1 &&
|
90
|
+
meth.to_s =~ SET_SOCKOPT &&
|
91
|
+
(args.first.is_a?(Integer) || args.first.is_a?(String))
|
92
|
+
begin
|
93
|
+
|
94
|
+
self.class.instance_eval <<-RUBY, __FILE__, __LINE__
|
95
|
+
attach_function #{meth.inspect}, "zsock_#{meth.to_s}", [:pointer, :varargs], :void
|
96
|
+
RUBY
|
97
|
+
|
98
|
+
rescue FFI::NotFoundError
|
99
|
+
super
|
100
|
+
else
|
101
|
+
|
102
|
+
if args.first.is_a?(Integer)
|
103
|
+
self.class.class_eval <<-RUBY, __FILE__, __LINE__
|
104
|
+
def #{meth.to_s}(arg)
|
105
|
+
self.class.#{meth.to_s}(@czmq_obj, :int, arg)
|
106
|
+
end
|
107
|
+
RUBY
|
108
|
+
elsif args.first.is_a?(String)
|
109
|
+
self.class.class_eval <<-RUBY, __FILE__, __LINE__
|
110
|
+
def #{meth.to_s}(arg)
|
111
|
+
self.class.#{meth.to_s}(@czmq_obj, :string, arg)
|
112
|
+
end
|
113
|
+
RUBY
|
114
|
+
end
|
115
|
+
|
116
|
+
send meth, args.first
|
117
|
+
|
118
|
+
end
|
119
|
+
else
|
120
|
+
super
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
data/lib/czmq/zsys.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require_relative 'libczmq'
|
2
|
+
|
3
|
+
module CZMQ
|
4
|
+
class Zsys
|
5
|
+
extend ::LibCZMQ
|
6
|
+
|
7
|
+
czmq_function :init, :init, [], :pointer
|
8
|
+
czmq_function :handler_set, :handler_set, [:pointer], :void
|
9
|
+
czmq_function :handler_reset, :handler_reset, [], :void
|
10
|
+
czmq_function :set_io_threads, :set_io_threads, [:size_t], :void
|
11
|
+
czmq_function :set_max_sockets, :set_max_sockets, [:size_t], :void
|
12
|
+
czmq_function :socket_limit, :socket_limit, [], :size_t
|
13
|
+
czmq_function :set_linger, :set_linger, [:size_t], :void
|
14
|
+
czmq_function :set_sndhwm, :set_sndhwm, [:size_t], :void
|
15
|
+
czmq_function :set_rcvhwm, :set_rcvhwm, [:size_t], :void
|
16
|
+
czmq_function :set_pipehwm, :set_pipehwm, [:size_t], :void
|
17
|
+
czmq_function :pipehwm, :pipehwm, [], :size_t
|
18
|
+
czmq_function :set_ipv6, :set_ipv6, [:int], :void
|
19
|
+
czmq_function :set_interface, :set_interface, [:string], :void
|
20
|
+
czmq_function :set_logident, :set_logident, [:string], :void
|
21
|
+
czmq_function :set_logstream, :set_logstream, [:pointer], :void
|
22
|
+
czmq_function :set_logsender, :set_logsender, [:string], :void
|
23
|
+
czmq_function :set_logsystem, :set_logsystem, [:bool], :void
|
24
|
+
czmq_function :error, :error, [:string], :void
|
25
|
+
czmq_function :warning, :warning, [:string], :void
|
26
|
+
czmq_function :notice, :notice, [:string], :void
|
27
|
+
czmq_function :info, :info, [:string], :void
|
28
|
+
czmq_function :debug, :debug, [:string], :void
|
29
|
+
|
30
|
+
attach_variable :interrupted, :zsys_interrupted, :int
|
31
|
+
attach_variable :zctx_interrupted, :zctx_interrupted, :int
|
32
|
+
|
33
|
+
handler_set(nil)
|
34
|
+
set_ipv6(1)
|
35
|
+
|
36
|
+
at_exit do
|
37
|
+
zctx_interrupted = interrupted = 1
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|