rbczmq 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -175,11 +175,7 @@ VALUE rb_czmq_nogvl_socket_bind(void *ptr)
175
175
  struct nogvl_conn_args *args = ptr;
176
176
  errno = 0;
177
177
  zmq_sock_wrapper *socket = args->socket;
178
- #ifdef HAVE_RB_THREAD_BLOCKING_REGION
179
- rc = zsocket_bind(socket->socket, args->endpoint);
180
- #else
181
178
  rc = zsocket_bind(socket->socket, args->endpoint);
182
- #endif
183
179
  return (VALUE)rc;
184
180
  }
185
181
 
@@ -194,11 +190,7 @@ VALUE rb_czmq_nogvl_socket_connect(void *ptr)
194
190
  struct nogvl_conn_args *args = ptr;
195
191
  errno = 0;
196
192
  zmq_sock_wrapper *socket = args->socket;
197
- #ifdef HAVE_RB_THREAD_BLOCKING_REGION
198
- rc = zsocket_connect(socket->socket, args->endpoint);
199
- #else
200
193
  rc = zsocket_connect(socket->socket, args->endpoint);
201
- #endif
202
194
  return (VALUE)rc;
203
195
  }
204
196
 
@@ -1595,8 +1587,8 @@ void _init_rb_czmq_socket()
1595
1587
  rb_define_method(rb_cZmqSocket, "close", rb_czmq_socket_close, 0);
1596
1588
  rb_define_method(rb_cZmqSocket, "endpoint", rb_czmq_socket_endpoint, 0);
1597
1589
  rb_define_method(rb_cZmqSocket, "state", rb_czmq_socket_state, 0);
1598
- rb_define_method(rb_cZmqSocket, "bind", rb_czmq_socket_bind, 1);
1599
- rb_define_method(rb_cZmqSocket, "connect", rb_czmq_socket_connect, 1);
1590
+ rb_define_method(rb_cZmqSocket, "real_bind", rb_czmq_socket_bind, 1);
1591
+ rb_define_method(rb_cZmqSocket, "real_connect", rb_czmq_socket_connect, 1);
1600
1592
  rb_define_method(rb_cZmqSocket, "fd", rb_czmq_socket_fd, 0);
1601
1593
  rb_define_alias(rb_cZmqSocket, "to_i", "fd");
1602
1594
  rb_define_method(rb_cZmqSocket, "verbose=", rb_czmq_socket_set_verbose, 1);
data/lib/zmq.rb CHANGED
@@ -63,6 +63,13 @@ module ZMQ
63
63
  [poller.readables, poller.writables, []] if ready
64
64
  end
65
65
 
66
+ def self.resolver
67
+ @resolver ||= begin
68
+ require 'resolv'
69
+ Resolv::DNS.new
70
+ end
71
+ end
72
+
66
73
  autoload :Handler, 'zmq/handler'
67
74
  autoload :DefaultHandler, 'zmq/default_handler'
68
75
  end
@@ -1,6 +1,8 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  class ZMQ::Socket
4
+ PROTO_REXP = /^inproc|ipc|tcp|e?pgm:\/\//
5
+
4
6
  def self.unsupported_api(*methods)
5
7
  methods.each do |m|
6
8
  class_eval <<-"evl", __FILE__, __LINE__
@@ -74,6 +76,54 @@ class ZMQ::Socket
74
76
  def poll_writable?
75
77
  true
76
78
  end
79
+
80
+ # Binds to a given endpoint. Attemps to resolve URIs without a protocol through DNS SRV records.
81
+ #
82
+ # socket = ctx.socket(:PUB)
83
+ # socket.bind "tcp://127.0.0.1:9000"
84
+ #
85
+ # socket.bind "collector.domain.com" # resolves 10.0.0.2:9000
86
+ #
87
+ def bind(uri)
88
+ uri = resolve(uri) if uri && uri !~ PROTO_REXP
89
+ real_bind(uri)
90
+ end
91
+
92
+ # Connects to a given endpoint. Attemps to resolve URIs without a protocol through DNS SRV records.
93
+ #
94
+ # socket = ctx.socket(:PUB)
95
+ # socket.connect "tcp://127.0.0.1:9000"
96
+ #
97
+ # socket.connect "collector.domain.com" # resolves 10.0.0.2:9000
98
+ #
99
+ def connect(uri)
100
+ uri = resolve(uri) if uri && uri !~ PROTO_REXP
101
+ real_connect(uri)
102
+ end
103
+
104
+ private
105
+ # Attempt to resolve DNS SRV records ( http://en.wikipedia.org/wiki/SRV_record ). Respects priority and weight
106
+ # to provide a combination of load balancing and backup.
107
+ def resolve(uri)
108
+ parts = uri.split('.')
109
+ service = parts.shift
110
+ domain = parts.join(".")
111
+ # ZeroMQ does not yet support udp, but may look into possibly supporting [e]pgm here
112
+ resources = ZMQ.resolver.getresources("_#{service}._tcp.#{domain}", Resolv::DNS::Resource::IN::SRV)
113
+ # lowest-numbered priority value is preferred
114
+ resources.sort!{|a,b| a.priority <=> b.priority }
115
+ res = resources.first
116
+ # detetermine if we should filter by weight as well (multiple records with equal priority)
117
+ priority_peers = resources.select{|r| res.priority == r.priority }
118
+ if priority_peers.size > 1
119
+ # highest weight preferred
120
+ res = priority_peers.sort{|a,b| a.weight <=> b.weight }.last
121
+ end
122
+ return uri unless res
123
+ "tcp://#{res.target}:#{res.port}"
124
+ rescue
125
+ uri
126
+ end
77
127
  end
78
128
 
79
129
  module ZMQ::DownstreamSocket
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module ZMQ
4
- VERSION = "0.3"
4
+ VERSION = "0.4"
5
5
  end
@@ -121,6 +121,20 @@ class TestZmqSocket < ZmqTestCase
121
121
  ctx.destroy
122
122
  end
123
123
 
124
+ def test_bind_connect_errors
125
+ ctx = ZMQ::Context.new
126
+ req = ctx.socket(:REQ)
127
+ rep = ctx.socket(:REP)
128
+ assert_raises Errno::EINVAL do
129
+ rep.bind "bad uri"
130
+ end
131
+ assert_raises Errno::EINVAL do
132
+ req.connect "bad uri"
133
+ end
134
+ ensure
135
+ ctx.destroy
136
+ end
137
+
124
138
  def test_to_s
125
139
  ctx = ZMQ::Context.new
126
140
  sock = ctx.socket(:PAIR)
@@ -59,4 +59,9 @@ class TestZmq < ZmqTestCase
59
59
  assert_equal STDOUT, item.pollable
60
60
  assert_equal ZMQ::POLLIN, item.events
61
61
  end
62
+
63
+ def test_resolver
64
+ require 'resolv'
65
+ assert_instance_of Resolv::DNS, ZMQ.resolver
66
+ end
62
67
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbczmq
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
9
- version: "0.3"
8
+ - 4
9
+ version: "0.4"
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Lourens Naud\xC3\xA9"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-18 00:00:00 Z
18
+ date: 2012-04-27 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake-compiler
@@ -173,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
173
  requirements: []
174
174
 
175
175
  rubyforge_project:
176
- rubygems_version: 1.8.15
176
+ rubygems_version: 1.8.23
177
177
  signing_key:
178
178
  specification_version: 3
179
179
  summary: "Ruby extension for CZMQ - High-level C Binding for \xC3\x98MQ (http://czmq.zeromq.org)"