bundesstrasse 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bundesstrasse.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  require 'ffi-rzmq'
2
2
 
3
+ module Bundesstrasse
4
+ Poller = ZMQ::Poller
5
+ end
6
+
3
7
  require 'bundesstrasse/errors'
4
8
  require 'bundesstrasse/context'
5
9
  require 'bundesstrasse/socket'
6
- require 'bundesstrasse/sockets'
7
10
  require 'bundesstrasse/device'
8
11
  require 'bundesstrasse/devices'
@@ -7,10 +7,16 @@ module Bundesstrasse
7
7
  @zmq_context = zmq_context
8
8
  end
9
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)
10
+ def socket(socket_type, options={})
11
+ raise ContextError, 'Context terminated' if terminated?
12
+ socket_type = translate_socket_type(socket_type)
13
+ zmq_socket = error_check { @zmq_context.socket(socket_type) }
14
+ case socket_type
15
+ when ZMQ::SUB, ZMQ::XSUB
16
+ SubSocket.new(zmq_socket, options)
17
+ else
18
+ Socket.new(zmq_socket, options)
19
+ end
14
20
  rescue ZMQError => e
15
21
  ContextError.raise_error(e)
16
22
  end
@@ -27,6 +33,23 @@ module Bundesstrasse
27
33
  def self.create(options={})
28
34
  new ZMQ::Context.create(options[:io_threads] || 1)
29
35
  end
36
+
37
+ private
38
+
39
+ SYMBOLIC_TYPES = [:pair, :pub, :sub, :req, :rep, :dealer, :router, :pull, :push, :xpub, :xsub].freeze
40
+
41
+ def translate_socket_type(type)
42
+ return type unless type.is_a?(Symbol)
43
+ SYMBOLIC_TYPES.index(type)
44
+ end
45
+
46
+ public
47
+
48
+ SYMBOLIC_TYPES.each do |type|
49
+ define_method("#{type}_socket") do |options={}|
50
+ socket(type, options)
51
+ end
52
+ end
30
53
  end
31
54
 
32
55
  ContextError = Class.new(ZMQError)
@@ -7,12 +7,12 @@ module Bundesstrasse
7
7
  end
8
8
 
9
9
  def create_endpoint!(options={})
10
- @context.socket(RepSocket, options)
10
+ @context.socket(ZMQ::REP, options)
11
11
  end
12
12
 
13
13
  def self.create(context)
14
- frontend = context.socket(RouterSocket)
15
- backend = context.socket(DealerSocket)
14
+ frontend = context.socket(ZMQ::ROUTER)
15
+ backend = context.socket(ZMQ::DEALER)
16
16
  new(context, frontend, backend)
17
17
  end
18
18
  end
@@ -23,8 +23,8 @@ module Bundesstrasse
23
23
  end
24
24
 
25
25
  def self.create(context)
26
- frontend = context.socket(SubSocket)
27
- backend = context.socket(PubSocket)
26
+ frontend = context.socket(ZMQ::SUB)
27
+ backend = context.socket(ZMQ::PUB)
28
28
  new(frontend, backend)
29
29
  end
30
30
  end
@@ -35,8 +35,8 @@ module Bundesstrasse
35
35
  end
36
36
 
37
37
  def self.create(context)
38
- frontend = context.socket(PullSocket)
39
- backend = context.socket(PushSocket)
38
+ frontend = context.socket(ZMQ::PULL)
39
+ backend = context.socket(ZMQ::PUSH)
40
40
  new(frontend, backend)
41
41
  end
42
42
  end
@@ -60,10 +60,6 @@ module Bundesstrasse
60
60
  @connected
61
61
  end
62
62
 
63
- def self.type
64
- raise NotImplementedError, 'Subclasses must override Socket::type'
65
- end
66
-
67
63
  private
68
64
 
69
65
  def connected_error_check(&block)
@@ -92,6 +88,16 @@ module Bundesstrasse
92
88
  end
93
89
  end
94
90
 
91
+ class SubSocket < Socket
92
+ def subscribe(topic)
93
+ error_check { @socket.setsockopt(ZMQ::SUBSCRIBE, topic) }
94
+ end
95
+
96
+ def unsubscribe(topic)
97
+ error_check { @socket.setsockopt(ZMQ::UNSUBSCRIBE, topic) }
98
+ end
99
+ end
100
+
95
101
  SocketError = Class.new(ZMQError)
96
102
  TermError = Class.new(ZMQError)
97
103
  AgainError = Class.new(ZMQError)
@@ -1,3 +1,3 @@
1
1
  module Bundesstrasse
2
- VERSION = '0.0.9'.freeze
2
+ VERSION = '0.1.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bundesstrasse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
5
4
  prerelease:
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Joel Segerlind
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-27 00:00:00.000000000 Z
12
+ date: 2013-03-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ffi-rzmq
@@ -78,7 +78,6 @@ files:
78
78
  - lib/bundesstrasse/devices.rb
79
79
  - lib/bundesstrasse/errors.rb
80
80
  - lib/bundesstrasse/socket.rb
81
- - lib/bundesstrasse/sockets.rb
82
81
  - lib/bundesstrasse/version.rb
83
82
  homepage: https://github.com/jowl/bundesstrasse
84
83
  licenses: []
@@ -92,9 +91,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
91
  - !ruby/object:Gem::Version
93
92
  segments:
94
93
  - 0
94
+ hash: 2
95
95
  version: !binary |-
96
96
  MA==
97
- hash: 2
98
97
  none: false
99
98
  required_rubygems_version: !ruby/object:Gem::Requirement
100
99
  requirements:
@@ -102,9 +101,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
101
  - !ruby/object:Gem::Version
103
102
  segments:
104
103
  - 0
104
+ hash: 2
105
105
  version: !binary |-
106
106
  MA==
107
- hash: 2
108
107
  none: false
109
108
  requirements: []
110
109
  rubyforge_project:
@@ -1,70 +0,0 @@
1
-
2
- module Bundesstrasse
3
- class ReqSocket < Socket
4
- def self.type
5
- ZMQ::REQ
6
- end
7
- end
8
-
9
- class RepSocket < Socket
10
- def self.type
11
- ZMQ::REP
12
- end
13
- end
14
-
15
- class DealerSocket < Socket
16
- def self.type
17
- ZMQ::DEALER
18
- end
19
- end
20
-
21
- class RouterSocket < Socket
22
- def self.type
23
- ZMQ::ROUTER
24
- end
25
- end
26
-
27
- class PushSocket < Socket
28
- def self.type
29
- ZMQ::PUSH
30
- end
31
- end
32
-
33
- class PullSocket < Socket
34
- def self.type
35
- ZMQ::PULL
36
- end
37
- end
38
-
39
- class PubSocket < Socket
40
- def self.type
41
- ZMQ::PUB
42
- end
43
- end
44
-
45
- class SubSocket < Socket
46
- def self.type
47
- ZMQ::SUB
48
- end
49
-
50
- def subscribe(topic)
51
- error_check { @socket.setsockopt(ZMQ::SUBSCRIBE, topic) }
52
- end
53
-
54
- def unsubscribe(topic)
55
- error_check { @socket.setsockopt(ZMQ::UNSUBSCRIBE, topic) }
56
- end
57
- end
58
-
59
- class XPubSocket < Socket
60
- def self.type
61
- ZMQ::XPUB
62
- end
63
- end
64
-
65
- class XSubSocket < SubSocket
66
- def self.type
67
- ZMQ::XSUB
68
- end
69
- end
70
- end