bundesstrasse 0.0.2-java → 0.0.3-java

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.
@@ -1,12 +1,33 @@
1
1
 
2
2
  module Bundesstrasse
3
- ContextError = Class.new(StandardError)
4
-
5
3
  class Context
6
- def self.context(io_threads=1)
7
- @io_threads = io_threads unless @context
8
- raise ContextError unless @io_threads == io_threads
9
- @context ||= ZMQ::Context.create io_threads
4
+ include Errors
5
+
6
+ def initialize(zmq_context)
7
+ @zmq_context = zmq_context
8
+ end
9
+
10
+ def socket(socket_class, options={})
11
+ raise ContextError.new("Context terminated") if terminated?
12
+ zmq_socket = error_check { @zmq_context.socket(socket_class.type) }
13
+ socket = socket_class.new(zmq_socket, options)
14
+ rescue ZMQError => e
15
+ ContextError.raise_error(e)
16
+ end
17
+
18
+ def terminate!
19
+ @zmq_context.terminate
20
+ true
21
+ end
22
+
23
+ def terminated?
24
+ @zmq_context.context.nil?
25
+ end
26
+
27
+ def self.create(options={})
28
+ new ZMQ::Context.create(options[:io_threads] || 1)
10
29
  end
11
30
  end
31
+
32
+ ContextError = Class.new(ZMQError)
12
33
  end
@@ -0,0 +1,31 @@
1
+ module Bundesstrasse
2
+ class ZMQError < StandardError
3
+ attr_reader :error_code
4
+ def initialize(message, error_code=-1)
5
+ @error_code = error_code
6
+ super(message)
7
+ end
8
+
9
+ def self.raise_error(e)
10
+ raise new(e.message, e.error_code)
11
+ end
12
+ end
13
+
14
+ module Errors
15
+ def errno
16
+ ZMQ::Util.errno
17
+ end
18
+
19
+ def error_string
20
+ ZMQ::Util.error_string
21
+ end
22
+
23
+ def error_check(errors={}, &block)
24
+ if (res = block.call).is_a? Fixnum
25
+ res = res >= 0
26
+ end
27
+ raise ZMQError.new(error_string, errno) unless res
28
+ res
29
+ end
30
+ end
31
+ end
@@ -1,18 +1,11 @@
1
1
 
2
2
  module Bundesstrasse
3
3
  class Socket
4
- def initialize(type, options={})
5
- options = options.dup
6
- context = Context.context(options.delete(:io_threads) || 1)
7
- @socket = context.socket(type)
8
- raise SocketError unless @socket
9
- DEFAULT_OPTIONS.merge(options).each do |option, value|
10
- begin
11
- error_check { @socket.setsockopt ZMQ.const_get(option.upcase), value }
12
- rescue NameError => e
13
- raise ArgumentError, "Unknown socket option '#{option}'", e.backtrace
14
- end
15
- end
4
+ include Errors
5
+
6
+ def initialize(socket, options={})
7
+ @socket = socket
8
+ setup!(options)
16
9
  end
17
10
 
18
11
  def bind(address)
@@ -36,6 +29,10 @@ module Bundesstrasse
36
29
  connected_error_check { @socket.send_string message }
37
30
  end
38
31
 
32
+ def self.type
33
+ raise NotImplementedError, 'Subclasses define constant TYPE'
34
+ end
35
+
39
36
  private
40
37
 
41
38
  def connected_error_check(&block)
@@ -44,25 +41,27 @@ module Bundesstrasse
44
41
  end
45
42
 
46
43
  def error_check(&block)
47
- unless ZMQ::Util.resultcode_ok? block.call
48
- raise SocketError.new(ZMQ::Util.error_string, ZMQ::Util.errno)
44
+ super
45
+ rescue ZMQError => e
46
+ case e.error_code
47
+ when ZMQ::ETERM then close && TermError.raise_error(e)
48
+ when ZMQ::EAGAIN then AgainError.raise_error(e)
49
+ else SocketError.raise_error(e)
49
50
  end
50
- true
51
51
  end
52
52
 
53
- DEFAULT_OPTIONS = {
54
- linger: 0,
55
- rcvtimeo: -1,
56
- sndtimeo: -1,
57
- }
58
-
59
- end
60
-
61
- class SocketError < StandardError
62
- attr_reader :error_code
63
- def initialize(message, error_code=-1)
64
- @error_code = error_code
65
- super(message)
53
+ def setup!(options)
54
+ options.each do |option, value|
55
+ begin
56
+ error_check { @socket.setsockopt ZMQ.const_get(option.upcase), value }
57
+ rescue NameError => e
58
+ raise ArgumentError, "Unknown socket option '#{option}'", e.backtrace
59
+ end
60
+ end
66
61
  end
67
62
  end
63
+
64
+ SocketError = Class.new(ZMQError)
65
+ TermError = Class.new(ZMQError)
66
+ AgainError = Class.new(ZMQError)
68
67
  end
@@ -1,58 +1,58 @@
1
1
 
2
2
  module Bundesstrasse
3
3
  class ReqSocket < Socket
4
- def initialize(options={})
5
- super(ZMQ::REQ, options)
4
+ def self.type
5
+ ZMQ::REQ
6
6
  end
7
7
  end
8
8
 
9
9
  class RepSocket < Socket
10
- def initialize(options={})
11
- super(ZMQ::REP, options)
10
+ def self.type
11
+ ZMQ::REP
12
12
  end
13
13
  end
14
14
 
15
15
  class DealerSocket < Socket
16
- def initialize(options={})
17
- super(ZMQ::DEALER, options)
16
+ def self.type
17
+ ZMQ::DEALER
18
18
  end
19
19
  end
20
20
 
21
21
  class RouterSocket < Socket
22
- def initialize(options={})
23
- super(ZMQ::ROUTER, options)
22
+ def self.type
23
+ ZMQ::ROUTER
24
24
  end
25
25
  end
26
26
 
27
27
  class PushSocket < Socket
28
- def initialize(options={})
29
- super(ZMQ::PUSH, options)
28
+ def self.type
29
+ ZMQ::PUSH
30
30
  end
31
31
  end
32
32
 
33
33
  class PullSocket < Socket
34
- def initialize(options={})
35
- super(ZMQ::PULL, options)
34
+ def self.type
35
+ ZMQ::PULL
36
36
  end
37
37
  end
38
38
 
39
39
  class PubSocket < Socket
40
- def initialize(options={})
41
- super(ZMQ::PUB, options)
40
+ def self.type
41
+ ZMQ::PUB
42
42
  end
43
43
  end
44
44
 
45
45
  class SubSocket < Socket
46
- def initialize(options={})
47
- super(ZMQ::SUB, options)
46
+ def self.type
47
+ ZMQ::SUB
48
48
  end
49
49
 
50
50
  def subscribe(topic)
51
- error_check { @socket.setsockopt(::ZMQ::SUBSCRIBE, topic) }
51
+ error_check { @socket.setsockopt(ZMQ::SUBSCRIBE, topic) }
52
52
  end
53
53
 
54
54
  def unsubscribe(topic)
55
- error_check { @socket.setsockopt(::ZMQ::UNSUBSCRIBE, topic) }
55
+ error_check { @socket.setsockopt(ZMQ::UNSUBSCRIBE, topic) }
56
56
  end
57
57
  end
58
58
  end
@@ -0,0 +1,3 @@
1
+ module Bundesstrasse
2
+ VERSION = '0.0.3'.freeze
3
+ end
data/lib/bundesstrasse.rb CHANGED
@@ -1,6 +1,7 @@
1
1
 
2
2
  require 'ffi-rzmq'
3
3
 
4
+ require 'bundesstrasse/errors'
4
5
  require 'bundesstrasse/context'
5
6
  require 'bundesstrasse/socket'
6
7
  require 'bundesstrasse/sockets'
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: bundesstrasse
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.2
5
+ version: 0.0.3
6
6
  platform: java
7
7
  authors:
8
8
  - Joel Segerlind
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-05 00:00:00.000000000 Z
12
+ date: 2013-02-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ffi-rzmq
@@ -74,8 +74,10 @@ extra_rdoc_files: []
74
74
  files:
75
75
  - lib/bundesstrasse.rb
76
76
  - lib/bundesstrasse/context.rb
77
+ - lib/bundesstrasse/errors.rb
77
78
  - lib/bundesstrasse/socket.rb
78
79
  - lib/bundesstrasse/sockets.rb
80
+ - lib/bundesstrasse/version.rb
79
81
  homepage: https://github.com/jowl/bundesstrasse
80
82
  licenses: []
81
83
  post_install_message:
@@ -86,6 +88,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
88
  requirements:
87
89
  - - ">="
88
90
  - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ hash: 2
89
94
  version: !binary |-
90
95
  MA==
91
96
  none: false