0mq 0.3.0 → 0.4.0
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 +4 -4
- data/lib/0mq.rb +1 -0
- data/lib/0mq/poll_interruptible.rb +89 -0
- data/lib/0mq/socket.rb +13 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed94fa721edb7ba1b1406ec9c7601528e7f425c2
|
4
|
+
data.tar.gz: b982fa07e6efe22051a8a9789857dce377deaf5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e02444d6b525dba0d7acaa03afae14f89ba49e956dc1e4d110190abed9c2c1739a06ca895a4ca605a9b174d3c444ffa6bf572bf617aa13887d5b44a8b78701e
|
7
|
+
data.tar.gz: dec5b627b739c246c233d70a05574c3e32bdd678054fb3885ccbe63f906ff1a7b6e65a040d256141b8d6f04e573c382044f608d85a77b31c372a750360034cbd
|
data/lib/0mq.rb
CHANGED
@@ -0,0 +1,89 @@
|
|
1
|
+
|
2
|
+
module ZMQ
|
3
|
+
|
4
|
+
# An interruptible version of Poll.
|
5
|
+
class PollInterruptible < Poll
|
6
|
+
|
7
|
+
# Creates the additional interruption objects and calls super
|
8
|
+
# Note that either #kill or #close MUST be called when done with the object.
|
9
|
+
# There is no automatic finalizer for this object.
|
10
|
+
def initialize(*sockets)
|
11
|
+
@int_context = ZMQ::Context.new
|
12
|
+
@int_sock_rep = ZMQ::Socket.new ZMQ::REP, context:@int_context
|
13
|
+
@int_sock_req = ZMQ::Socket.new ZMQ::REQ, context:@int_context
|
14
|
+
@int_sock_rep.bind "inproc://int"
|
15
|
+
@int_sock_req.connect "inproc://int"
|
16
|
+
|
17
|
+
@dead = false
|
18
|
+
|
19
|
+
super @int_sock_rep, *sockets
|
20
|
+
end
|
21
|
+
|
22
|
+
# Same as Poll#run, but will yield [nil, nil] to the block if interrupted
|
23
|
+
def run(&block)
|
24
|
+
raise "#{self} cannot run; it was permanently killed." if @dead
|
25
|
+
|
26
|
+
super do |socket, revents|
|
27
|
+
if socket == @int_sock_rep
|
28
|
+
result = socket.recv_array
|
29
|
+
|
30
|
+
block.call nil, nil if block
|
31
|
+
|
32
|
+
socket.send_array ["OKAY"]
|
33
|
+
@int_sock_rep.close if result == ["KILL"]
|
34
|
+
else
|
35
|
+
block.call socket, revents if block
|
36
|
+
end
|
37
|
+
end.tap { |hash| hash.delete @int_sock_rep }
|
38
|
+
end
|
39
|
+
|
40
|
+
# Interrupt the running poll loop, but do not clean up.
|
41
|
+
# This should be run anytime to let the poller re-evaluate state, etc..
|
42
|
+
# This should only be accessed from a thread other than the poll thread,
|
43
|
+
# and only if the poll thread is running
|
44
|
+
def interrupt
|
45
|
+
@int_sock_req.send_string ""
|
46
|
+
@int_sock_req.recv_array
|
47
|
+
|
48
|
+
true
|
49
|
+
end
|
50
|
+
|
51
|
+
# Interrupt the running poll loop and permanently kill the Poll object
|
52
|
+
# This should be run once, when the Poll object is no longer needed.
|
53
|
+
# This should only be accessed from a thread other than the poll thread,
|
54
|
+
# and only if the poll thread is running
|
55
|
+
# Use #cleanup instead when there is no poll loop thread running.
|
56
|
+
def kill
|
57
|
+
return nil if @dead
|
58
|
+
|
59
|
+
@int_sock_req.send_array ["KILL"]
|
60
|
+
@int_sock_req.recv_array
|
61
|
+
@int_sock_req.close
|
62
|
+
@int_context.terminate
|
63
|
+
|
64
|
+
@dead = true
|
65
|
+
end
|
66
|
+
|
67
|
+
# Permanently kill the Poll object
|
68
|
+
# This should be run once, when the Poll object is no longer needed.
|
69
|
+
# This should only be accessed when there is no poll thread running.
|
70
|
+
# Use #kill instead when there is a poll loop thread running.
|
71
|
+
def close
|
72
|
+
return nil if @dead
|
73
|
+
|
74
|
+
@int_sock_rep.close
|
75
|
+
@int_sock_req.close
|
76
|
+
@int_context.terminate
|
77
|
+
|
78
|
+
@dead = true
|
79
|
+
end
|
80
|
+
|
81
|
+
# Return true if the object has been killed or closed and cannot be run
|
82
|
+
def dead?
|
83
|
+
@dead
|
84
|
+
end
|
85
|
+
|
86
|
+
# TODO: add finalizer for cleanup for neglectful user developers
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
data/lib/0mq/socket.rb
CHANGED
@@ -15,6 +15,7 @@ module ZMQ
|
|
15
15
|
attr_reader :type
|
16
16
|
|
17
17
|
def initialize(type, opts={})
|
18
|
+
@closed = false
|
18
19
|
@context = opts.fetch :context, ZMQ::DefaultContext
|
19
20
|
@type = type
|
20
21
|
@pointer = LibZMQ.zmq_socket @context.pointer, @type
|
@@ -26,8 +27,15 @@ module ZMQ
|
|
26
27
|
self.class.finalizer(@socket, Process.pid)
|
27
28
|
end
|
28
29
|
|
30
|
+
# Show a useful inspect output
|
31
|
+
def inspect
|
32
|
+
"#<#{self.class}:#{type_sym}:#{object_id.to_s(16)}>"
|
33
|
+
end
|
34
|
+
|
29
35
|
# Close the socket
|
30
36
|
def close
|
37
|
+
@closed = true
|
38
|
+
|
31
39
|
if @pointer
|
32
40
|
ObjectSpace.undefine_finalizer self
|
33
41
|
@temp_buffers.clear if @temp_buffers
|
@@ -39,6 +47,11 @@ module ZMQ
|
|
39
47
|
end
|
40
48
|
end
|
41
49
|
|
50
|
+
# Returns true if the socket is closed.
|
51
|
+
def closed?
|
52
|
+
@closed
|
53
|
+
end
|
54
|
+
|
42
55
|
# Create a safe finalizer for the socket pointer to close on GC of the object
|
43
56
|
def self.finalizer(pointer, pid)
|
44
57
|
Proc.new { LibZMQ.zmq_close pointer if Process.pid == pid }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: 0mq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe McIlvain
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-03-
|
12
|
+
date: 2014-03-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ffi-rzmq-core
|
@@ -122,6 +122,7 @@ files:
|
|
122
122
|
- lib/0mq/curve.rb
|
123
123
|
- lib/0mq/error_map.rb
|
124
124
|
- lib/0mq/poll.rb
|
125
|
+
- lib/0mq/poll_interruptible.rb
|
125
126
|
- lib/0mq/proxy.rb
|
126
127
|
- lib/0mq/socket.rb
|
127
128
|
- lib/0mq/socket/options.rb
|