zmqmachine 0.3.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.
- data/.bnsignore +20 -0
- data/History.txt +21 -0
- data/README.rdoc +85 -0
- data/Rakefile +21 -0
- data/examples/fake_ftp.rb +242 -0
- data/examples/one_handed_ping_pong.rb +74 -0
- data/examples/ping_pong.rb +86 -0
- data/examples/pub_sub.rb +195 -0
- data/examples/throttled_ping_pong.rb +103 -0
- data/lib/zm/address.rb +70 -0
- data/lib/zm/deferrable.rb +209 -0
- data/lib/zm/exceptions.rb +45 -0
- data/lib/zm/message.rb +52 -0
- data/lib/zm/reactor.rb +458 -0
- data/lib/zm/sockets/base.rb +216 -0
- data/lib/zm/sockets/pair.rb +87 -0
- data/lib/zm/sockets/pub.rb +80 -0
- data/lib/zm/sockets/rep.rb +96 -0
- data/lib/zm/sockets/req.rb +96 -0
- data/lib/zm/sockets/sub.rb +83 -0
- data/lib/zm/sockets/xrep.rb +86 -0
- data/lib/zm/sockets/xreq.rb +86 -0
- data/lib/zm/sockets.rb +4 -0
- data/lib/zm/timers.rb +220 -0
- data/lib/zmqmachine.rb +76 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/zmqmachine_spec.rb +6 -0
- data/version.txt +1 -0
- data/zmqmachine.gemspec +48 -0
- metadata +133 -0
@@ -0,0 +1,87 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Chuck Remes
|
4
|
+
# Homepage:: http://github.com/chuckremes/zmqmachine
|
5
|
+
# Date:: 20100602
|
6
|
+
#
|
7
|
+
#----------------------------------------------------------------------------
|
8
|
+
#
|
9
|
+
# Copyright (C) 2010 by Chuck Remes. All Rights Reserved.
|
10
|
+
# Email: cremes at mac dot com
|
11
|
+
#
|
12
|
+
# (The MIT License)
|
13
|
+
#
|
14
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
15
|
+
# a copy of this software and associated documentation files (the
|
16
|
+
# 'Software'), to deal in the Software without restriction, including
|
17
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
18
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
19
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
20
|
+
# the following conditions:
|
21
|
+
#
|
22
|
+
# The above copyright notice and this permission notice shall be
|
23
|
+
# included in all copies or substantial portions of the Software.
|
24
|
+
#
|
25
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
26
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
27
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
28
|
+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
29
|
+
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
30
|
+
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
31
|
+
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
32
|
+
#
|
33
|
+
#---------------------------------------------------------------------------
|
34
|
+
#
|
35
|
+
#
|
36
|
+
|
37
|
+
module ZMQMachine
|
38
|
+
|
39
|
+
module Socket
|
40
|
+
|
41
|
+
class Pair
|
42
|
+
include ZMQMachine::Socket::Base
|
43
|
+
|
44
|
+
def initialize context, handler
|
45
|
+
@poll_options = ZMQ::POLLIN | ZMQ::POLLOUT
|
46
|
+
@kind = :pair
|
47
|
+
|
48
|
+
super
|
49
|
+
@state = :ready
|
50
|
+
end
|
51
|
+
|
52
|
+
# Attach a handler to the PAIR socket.
|
53
|
+
#
|
54
|
+
# A PAIR socket may send and receive messages without
|
55
|
+
# restriction to order or quantity of send/recv
|
56
|
+
# operations.
|
57
|
+
#
|
58
|
+
# This socket expects its +handler+ to
|
59
|
+
# implement the #on_readable and #on_writable methods.
|
60
|
+
# The #on_readable method will be called whenever a
|
61
|
+
# message may be dequeued without blocking. The
|
62
|
+
# #on_writable method will be called whenever a
|
63
|
+
# message may be enqueued without blocking.
|
64
|
+
#
|
65
|
+
# For error handling purposes, the handler must also
|
66
|
+
# implement #on_readable_error and #on_writable_error.
|
67
|
+
#
|
68
|
+
def on_attach handler
|
69
|
+
raise ArgumentError, "Handler must implement an #on_readable method" unless handler.respond_to? :on_readable
|
70
|
+
raise ArgumentError, "Handler must implement an #on_writable method" unless handler.respond_to? :on_writable
|
71
|
+
raise ArgumentError, "Handler must implement an #on_readable_error method" unless handler.respond_to? :on_readable_error
|
72
|
+
raise ArgumentError, "Handler must implement an #on_writable_error method" unless handler.respond_to? :on_writable_error
|
73
|
+
super
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def allocate_socket context
|
80
|
+
sock = ZMQ::Socket.new context.pointer, ZMQ::PAIR
|
81
|
+
sock
|
82
|
+
end
|
83
|
+
end # class Pair
|
84
|
+
|
85
|
+
end # module Socket
|
86
|
+
|
87
|
+
end # module ZMQMachine
|
@@ -0,0 +1,80 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Chuck Remes
|
4
|
+
# Homepage:: http://github.com/chuckremes/zmqmachine
|
5
|
+
# Date:: 20100602
|
6
|
+
#
|
7
|
+
#----------------------------------------------------------------------------
|
8
|
+
#
|
9
|
+
# Copyright (C) 2010 by Chuck Remes. All Rights Reserved.
|
10
|
+
# Email: cremes at mac dot com
|
11
|
+
#
|
12
|
+
# (The MIT License)
|
13
|
+
#
|
14
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
15
|
+
# a copy of this software and associated documentation files (the
|
16
|
+
# 'Software'), to deal in the Software without restriction, including
|
17
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
18
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
19
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
20
|
+
# the following conditions:
|
21
|
+
#
|
22
|
+
# The above copyright notice and this permission notice shall be
|
23
|
+
# included in all copies or substantial portions of the Software.
|
24
|
+
#
|
25
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
26
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
27
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
28
|
+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
29
|
+
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
30
|
+
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
31
|
+
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
32
|
+
#
|
33
|
+
#---------------------------------------------------------------------------
|
34
|
+
#
|
35
|
+
#
|
36
|
+
|
37
|
+
module ZMQMachine
|
38
|
+
|
39
|
+
module Socket
|
40
|
+
|
41
|
+
class Pub
|
42
|
+
include ZMQMachine::Socket::Base
|
43
|
+
|
44
|
+
def initialize context, handler
|
45
|
+
@poll_options = ZMQ::POLLOUT
|
46
|
+
@kind = :pub
|
47
|
+
|
48
|
+
super
|
49
|
+
@state = :ready
|
50
|
+
end
|
51
|
+
|
52
|
+
# Attach a handler to the PUB socket.
|
53
|
+
#
|
54
|
+
# A PUB socket may *only* send messages.
|
55
|
+
#
|
56
|
+
# This socket expects its +handler+ to
|
57
|
+
# implement the #on_writable methods.
|
58
|
+
# The #on_writable method will be called whenever a
|
59
|
+
# message may be enqueued without blocking.
|
60
|
+
#
|
61
|
+
# For error handling purposes, the handler must also
|
62
|
+
# implement #on_writable_error.
|
63
|
+
#
|
64
|
+
def on_attach handler
|
65
|
+
raise ArgumentError, "Handler must implement an #on_writable method" unless handler.respond_to? :on_writable
|
66
|
+
raise ArgumentError, "Handler must implement an #on_writable_error method" unless handler.respond_to? :on_writable_error
|
67
|
+
super
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def allocate_socket context
|
73
|
+
sock = ZMQ::Socket.new context.pointer, ZMQ::PUB
|
74
|
+
sock
|
75
|
+
end
|
76
|
+
end # class Pub
|
77
|
+
|
78
|
+
end # module Socket
|
79
|
+
|
80
|
+
end # module ZMQMachine
|
@@ -0,0 +1,96 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Chuck Remes
|
4
|
+
# Homepage:: http://github.com/chuckremes/zmqmachine
|
5
|
+
# Date:: 20100602
|
6
|
+
#
|
7
|
+
#----------------------------------------------------------------------------
|
8
|
+
#
|
9
|
+
# Copyright (C) 2010 by Chuck Remes. All Rights Reserved.
|
10
|
+
# Email: cremes at mac dot com
|
11
|
+
#
|
12
|
+
# (The MIT License)
|
13
|
+
#
|
14
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
15
|
+
# a copy of this software and associated documentation files (the
|
16
|
+
# 'Software'), to deal in the Software without restriction, including
|
17
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
18
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
19
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
20
|
+
# the following conditions:
|
21
|
+
#
|
22
|
+
# The above copyright notice and this permission notice shall be
|
23
|
+
# included in all copies or substantial portions of the Software.
|
24
|
+
#
|
25
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
26
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
27
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
28
|
+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
29
|
+
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
30
|
+
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
31
|
+
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
32
|
+
#
|
33
|
+
#---------------------------------------------------------------------------
|
34
|
+
#
|
35
|
+
#
|
36
|
+
|
37
|
+
module ZMQMachine
|
38
|
+
|
39
|
+
module Socket
|
40
|
+
|
41
|
+
class Rep
|
42
|
+
include ZMQMachine::Socket::Base
|
43
|
+
|
44
|
+
def initialize context, handler
|
45
|
+
@poll_options = ZMQ::POLLIN
|
46
|
+
@kind = :reply
|
47
|
+
|
48
|
+
super
|
49
|
+
@state = :ready
|
50
|
+
end
|
51
|
+
|
52
|
+
# Attach a handler to the REP socket.
|
53
|
+
#
|
54
|
+
# A REP socket must alternate between recv.send (i.e.
|
55
|
+
# it cannot receive twice in a row without an intervening
|
56
|
+
# send). This socket expects its +handler+ to
|
57
|
+
# implement at least the #on_readable method. This method
|
58
|
+
# will be called whenever a request arrives.
|
59
|
+
#
|
60
|
+
# For error handling purposes, the handler must also
|
61
|
+
# implement #on_readable_error.
|
62
|
+
#
|
63
|
+
def on_attach handler
|
64
|
+
raise ArgumentError, "Handler must implement an #on_readable method" unless handler.respond_to? :on_readable
|
65
|
+
raise ArgumentError, "Handler must implement an #on_readable_error method" unless handler.respond_to? :on_readable_error
|
66
|
+
super
|
67
|
+
end
|
68
|
+
|
69
|
+
# +timeout+ is measured in milliseconds; default is 0 (never timeout)
|
70
|
+
def send_message message
|
71
|
+
unless waiting_for_request?
|
72
|
+
rc = super
|
73
|
+
@state = :waiting_for_request
|
74
|
+
else
|
75
|
+
rc = -1
|
76
|
+
end
|
77
|
+
|
78
|
+
rc
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def waiting_for_request?
|
85
|
+
:waiting_for_request == @state
|
86
|
+
end
|
87
|
+
|
88
|
+
def allocate_socket context
|
89
|
+
sock = ZMQ::Socket.new context.pointer, ZMQ::REP
|
90
|
+
sock
|
91
|
+
end
|
92
|
+
end # class Rep
|
93
|
+
|
94
|
+
end # module Socket
|
95
|
+
|
96
|
+
end # module ZMQMachine
|
@@ -0,0 +1,96 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Chuck Remes
|
4
|
+
# Homepage:: http://github.com/chuckremes/zmqmachine
|
5
|
+
# Date:: 20100602
|
6
|
+
#
|
7
|
+
#----------------------------------------------------------------------------
|
8
|
+
#
|
9
|
+
# Copyright (C) 2010 by Chuck Remes. All Rights Reserved.
|
10
|
+
# Email: cremes at mac dot com
|
11
|
+
#
|
12
|
+
# (The MIT License)
|
13
|
+
#
|
14
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
15
|
+
# a copy of this software and associated documentation files (the
|
16
|
+
# 'Software'), to deal in the Software without restriction, including
|
17
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
18
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
19
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
20
|
+
# the following conditions:
|
21
|
+
#
|
22
|
+
# The above copyright notice and this permission notice shall be
|
23
|
+
# included in all copies or substantial portions of the Software.
|
24
|
+
#
|
25
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
26
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
27
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
28
|
+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
29
|
+
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
30
|
+
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
31
|
+
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
32
|
+
#
|
33
|
+
#---------------------------------------------------------------------------
|
34
|
+
#
|
35
|
+
#
|
36
|
+
|
37
|
+
module ZMQMachine
|
38
|
+
|
39
|
+
module Socket
|
40
|
+
|
41
|
+
class Req
|
42
|
+
include ZMQMachine::Socket::Base
|
43
|
+
|
44
|
+
def initialize context, handler
|
45
|
+
@poll_options = ZMQ::POLLOUT
|
46
|
+
@kind = :request
|
47
|
+
|
48
|
+
super
|
49
|
+
@state = :ready
|
50
|
+
end
|
51
|
+
|
52
|
+
# Attach a handler to the REQ socket.
|
53
|
+
#
|
54
|
+
# A REQ socket must alternate between send/recv (i.e.
|
55
|
+
# it cannot send twice in a row without an intervening
|
56
|
+
# receive). This socket expects its +handler+ to
|
57
|
+
# implement at least the #on_readable method. This method
|
58
|
+
# will be called whenever a reply arrives.
|
59
|
+
#
|
60
|
+
# For error handling purposes, the handler must also
|
61
|
+
# implement #on_readable_error.
|
62
|
+
#
|
63
|
+
def on_attach handler
|
64
|
+
raise ArgumentError, "Handler must implement an #on_readable method" unless handler.respond_to? :on_readable
|
65
|
+
raise ArgumentError, "Handler must implement an #on_readable_error method" unless handler.respond_to? :on_readable_error
|
66
|
+
super
|
67
|
+
end
|
68
|
+
|
69
|
+
# +timeout+ is measured in milliseconds; default is 0 (never timeout)
|
70
|
+
def send_message message
|
71
|
+
unless waiting_for_reply?
|
72
|
+
rc = super
|
73
|
+
@state = :waiting_for_reply
|
74
|
+
else
|
75
|
+
rc = -1
|
76
|
+
end
|
77
|
+
|
78
|
+
rc
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def waiting_for_reply?
|
85
|
+
:waiting_for_reply == @state
|
86
|
+
end
|
87
|
+
|
88
|
+
def allocate_socket context
|
89
|
+
sock = ZMQ::Socket.new context.pointer, ZMQ::REQ
|
90
|
+
sock
|
91
|
+
end
|
92
|
+
end # class Req
|
93
|
+
|
94
|
+
end # module Socket
|
95
|
+
|
96
|
+
end # module ZMQMachine
|
@@ -0,0 +1,83 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Chuck Remes
|
4
|
+
# Homepage:: http://github.com/chuckremes/zmqmachine
|
5
|
+
# Date:: 20100602
|
6
|
+
#
|
7
|
+
#----------------------------------------------------------------------------
|
8
|
+
#
|
9
|
+
# Copyright (C) 2010 by Chuck Remes. All Rights Reserved.
|
10
|
+
# Email: cremes at mac dot com
|
11
|
+
#
|
12
|
+
# (The MIT License)
|
13
|
+
#
|
14
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
15
|
+
# a copy of this software and associated documentation files (the
|
16
|
+
# 'Software'), to deal in the Software without restriction, including
|
17
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
18
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
19
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
20
|
+
# the following conditions:
|
21
|
+
#
|
22
|
+
# The above copyright notice and this permission notice shall be
|
23
|
+
# included in all copies or substantial portions of the Software.
|
24
|
+
#
|
25
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
26
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
27
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
28
|
+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
29
|
+
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
30
|
+
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
31
|
+
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
32
|
+
#
|
33
|
+
#---------------------------------------------------------------------------
|
34
|
+
#
|
35
|
+
#
|
36
|
+
|
37
|
+
module ZMQMachine
|
38
|
+
|
39
|
+
module Socket
|
40
|
+
|
41
|
+
class Sub
|
42
|
+
include ZMQMachine::Socket::Base
|
43
|
+
|
44
|
+
def initialize context, handler
|
45
|
+
@poll_options = ZMQ::POLLIN
|
46
|
+
@kind = :sub
|
47
|
+
|
48
|
+
super
|
49
|
+
@state = :ready
|
50
|
+
end
|
51
|
+
|
52
|
+
# Attach a handler to the SUB socket.
|
53
|
+
#
|
54
|
+
# A SUB socket may *only*receive messages.
|
55
|
+
#
|
56
|
+
# This socket expects its +handler+ to
|
57
|
+
# implement the #on_readable method.
|
58
|
+
# The #on_readable method will be called whenever a
|
59
|
+
# message may be dequeued without blocking.
|
60
|
+
#
|
61
|
+
# For error handling purposes, the handler must also
|
62
|
+
# implement #on_readable_error.
|
63
|
+
#
|
64
|
+
def on_attach handler
|
65
|
+
raise ArgumentError, "Handler must implement an #on_readable method" unless handler.respond_to? :on_readable
|
66
|
+
raise ArgumentError, "Handler must implement an #on_readable_error method" unless handler.respond_to? :on_readable_error
|
67
|
+
super
|
68
|
+
end
|
69
|
+
|
70
|
+
def subscribe topic
|
71
|
+
@raw_socket.setsockopt ZMQ::SUBSCRIBE, topic
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def allocate_socket context
|
77
|
+
ZMQ::Socket.new context.pointer, ZMQ::SUB
|
78
|
+
end
|
79
|
+
end # class Sub
|
80
|
+
|
81
|
+
end # module Socket
|
82
|
+
|
83
|
+
end # module ZMQMachine
|
@@ -0,0 +1,86 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Chuck Remes
|
4
|
+
# Homepage:: http://github.com/chuckremes/zmqmachine
|
5
|
+
# Date:: 20100602
|
6
|
+
#
|
7
|
+
#----------------------------------------------------------------------------
|
8
|
+
#
|
9
|
+
# Copyright (C) 2010 by Chuck Remes. All Rights Reserved.
|
10
|
+
# Email: cremes at mac dot com
|
11
|
+
#
|
12
|
+
# (The MIT License)
|
13
|
+
#
|
14
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
15
|
+
# a copy of this software and associated documentation files (the
|
16
|
+
# 'Software'), to deal in the Software without restriction, including
|
17
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
18
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
19
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
20
|
+
# the following conditions:
|
21
|
+
#
|
22
|
+
# The above copyright notice and this permission notice shall be
|
23
|
+
# included in all copies or substantial portions of the Software.
|
24
|
+
#
|
25
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
26
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
27
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
28
|
+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
29
|
+
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
30
|
+
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
31
|
+
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
32
|
+
#
|
33
|
+
#---------------------------------------------------------------------------
|
34
|
+
#
|
35
|
+
#
|
36
|
+
|
37
|
+
module ZMQMachine
|
38
|
+
|
39
|
+
module Socket
|
40
|
+
|
41
|
+
class XRep
|
42
|
+
include ZMQMachine::Socket::Base
|
43
|
+
|
44
|
+
def initialize context, handler
|
45
|
+
@poll_options = ZMQ::POLLIN | ZMQ::POLLOUT
|
46
|
+
@kind = :xreply
|
47
|
+
|
48
|
+
super
|
49
|
+
@state = :ready
|
50
|
+
end
|
51
|
+
|
52
|
+
# Attach a handler to the XREP socket.
|
53
|
+
#
|
54
|
+
# A XREP socket has no restrictions on the number of sends and
|
55
|
+
# recieves. Each send will silently prepend a message part to
|
56
|
+
# your outgoing data which the REQ/XREQ socket on the other end
|
57
|
+
# will use for matching up the transaction.
|
58
|
+
#
|
59
|
+
# This socket expects its +handler+ to
|
60
|
+
# implement at least the #on_readable method. This method
|
61
|
+
# will be called whenever a reply arrives. The #on_writable method
|
62
|
+
# will be called continually until the socket HWM is breached.
|
63
|
+
#
|
64
|
+
# For error handling purposes, the handler must also
|
65
|
+
# implement #on_readable_error.
|
66
|
+
#
|
67
|
+
def on_attach handler
|
68
|
+
raise ArgumentError, "Handler must implement an #on_readable method" unless handler.respond_to? :on_readable
|
69
|
+
raise ArgumentError, "Handler must implement an #on_readable_error method" unless handler.respond_to? :on_readable_error
|
70
|
+
raise ArgumentError, "Handler must implement an #on_readable method" unless handler.respond_to? :on_writable
|
71
|
+
raise ArgumentError, "Handler must implement an #on_readable_error method" unless handler.respond_to? :on_writable_error
|
72
|
+
super
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def allocate_socket context
|
79
|
+
sock = ZMQ::Socket.new context.pointer, ZMQ::XREP
|
80
|
+
sock
|
81
|
+
end
|
82
|
+
end # class XRep
|
83
|
+
|
84
|
+
end # module Socket
|
85
|
+
|
86
|
+
end # module ZMQMachine
|
@@ -0,0 +1,86 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Chuck Remes
|
4
|
+
# Homepage:: http://github.com/chuckremes/zmqmachine
|
5
|
+
# Date:: 20100602
|
6
|
+
#
|
7
|
+
#----------------------------------------------------------------------------
|
8
|
+
#
|
9
|
+
# Copyright (C) 2010 by Chuck Remes. All Rights Reserved.
|
10
|
+
# Email: cremes at mac dot com
|
11
|
+
#
|
12
|
+
# (The MIT License)
|
13
|
+
#
|
14
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
15
|
+
# a copy of this software and associated documentation files (the
|
16
|
+
# 'Software'), to deal in the Software without restriction, including
|
17
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
18
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
19
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
20
|
+
# the following conditions:
|
21
|
+
#
|
22
|
+
# The above copyright notice and this permission notice shall be
|
23
|
+
# included in all copies or substantial portions of the Software.
|
24
|
+
#
|
25
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
26
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
27
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
28
|
+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
29
|
+
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
30
|
+
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
31
|
+
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
32
|
+
#
|
33
|
+
#---------------------------------------------------------------------------
|
34
|
+
#
|
35
|
+
#
|
36
|
+
|
37
|
+
module ZMQMachine
|
38
|
+
|
39
|
+
module Socket
|
40
|
+
|
41
|
+
class XReq
|
42
|
+
include ZMQMachine::Socket::Base
|
43
|
+
|
44
|
+
def initialize context, handler
|
45
|
+
@poll_options = ZMQ::POLLIN | ZMQ::POLLOUT
|
46
|
+
@kind = :xrequest
|
47
|
+
|
48
|
+
super
|
49
|
+
@state = :ready
|
50
|
+
end
|
51
|
+
|
52
|
+
# Attach a handler to the XREQ socket.
|
53
|
+
#
|
54
|
+
# A XREQ socket has no restrictions on the number of sends and
|
55
|
+
# recieves. Each send will silently prepend a message part to
|
56
|
+
# your outgoing data which the REP/XREP socket on the other end
|
57
|
+
# will use for matching up the transaction.
|
58
|
+
#
|
59
|
+
# This socket expects its +handler+ to
|
60
|
+
# implement at least the #on_readable method. This method
|
61
|
+
# will be called whenever a reply arrives. The #on_writable method
|
62
|
+
# will be called continually until the socket HWM is breached.
|
63
|
+
#
|
64
|
+
# For error handling purposes, the handler must also
|
65
|
+
# implement #on_readable_error.
|
66
|
+
#
|
67
|
+
def on_attach handler
|
68
|
+
raise ArgumentError, "Handler must implement an #on_readable method" unless handler.respond_to? :on_readable
|
69
|
+
raise ArgumentError, "Handler must implement an #on_readable_error method" unless handler.respond_to? :on_readable_error
|
70
|
+
raise ArgumentError, "Handler must implement an #on_readable method" unless handler.respond_to? :on_writable
|
71
|
+
raise ArgumentError, "Handler must implement an #on_readable_error method" unless handler.respond_to? :on_writable_error
|
72
|
+
super
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def allocate_socket context
|
79
|
+
sock = ZMQ::Socket.new context.pointer, ZMQ::XREQ
|
80
|
+
sock
|
81
|
+
end
|
82
|
+
end # class XReq
|
83
|
+
|
84
|
+
end # module Socket
|
85
|
+
|
86
|
+
end # module ZMQMachine
|
data/lib/zm/sockets.rb
ADDED