protobuf 1.1.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/.gitignore +3 -0
  2. data/Gemfile.lock +44 -25
  3. data/README.md +2 -2
  4. data/Rakefile +15 -0
  5. data/bin/rpc_server +52 -11
  6. data/lib/protobuf.rb +22 -10
  7. data/lib/protobuf/common/logger.rb +26 -25
  8. data/lib/protobuf/descriptor/file_descriptor.rb +1 -1
  9. data/lib/protobuf/message/field.rb +2 -2
  10. data/lib/protobuf/rpc/buffer.rb +30 -25
  11. data/lib/protobuf/rpc/client.rb +8 -8
  12. data/lib/protobuf/rpc/connector.rb +2 -0
  13. data/lib/protobuf/rpc/connectors/base.rb +0 -1
  14. data/lib/protobuf/rpc/connectors/common.rb +48 -48
  15. data/lib/protobuf/rpc/connectors/em_client.rb +53 -27
  16. data/lib/protobuf/rpc/connectors/eventmachine.rb +14 -17
  17. data/lib/protobuf/rpc/connectors/socket.rb +23 -16
  18. data/lib/protobuf/rpc/connectors/zmq.rb +73 -0
  19. data/lib/protobuf/rpc/error.rb +1 -2
  20. data/lib/protobuf/rpc/error/client_error.rb +4 -4
  21. data/lib/protobuf/rpc/server.rb +31 -43
  22. data/lib/protobuf/rpc/servers/evented/server.rb +43 -0
  23. data/lib/protobuf/rpc/servers/evented_runner.rb +1 -1
  24. data/lib/protobuf/rpc/servers/socket/server.rb +108 -0
  25. data/lib/protobuf/rpc/servers/socket/worker.rb +59 -0
  26. data/lib/protobuf/rpc/servers/socket_runner.rb +3 -3
  27. data/lib/protobuf/rpc/servers/zmq/broker.rb +85 -0
  28. data/lib/protobuf/rpc/servers/zmq/server.rb +50 -0
  29. data/lib/protobuf/rpc/servers/zmq/util.rb +27 -0
  30. data/lib/protobuf/rpc/servers/zmq/worker.rb +72 -0
  31. data/lib/protobuf/rpc/servers/zmq_runner.rb +26 -0
  32. data/lib/protobuf/rpc/service.rb +5 -5
  33. data/lib/protobuf/version.rb +1 -1
  34. data/protobuf.gemspec +12 -10
  35. data/spec/benchmark/tasks.rb +37 -5
  36. data/spec/functional/evented_server_spec.rb +64 -0
  37. data/spec/functional/socket_server_spec.rb +63 -0
  38. data/spec/functional/zmq_server_spec.rb +63 -0
  39. data/spec/helper/server.rb +32 -12
  40. data/spec/lib/protobuf/message/encoder_spec.rb +19 -0
  41. data/spec/proto/test.pb.rb +3 -3
  42. data/spec/proto/test.proto +3 -3
  43. data/spec/proto/test_service.rb +1 -0
  44. data/spec/spec_helper.rb +6 -0
  45. data/spec/unit/message_spec.rb +1 -1
  46. data/spec/unit/rpc/client_spec.rb +11 -3
  47. data/spec/unit/rpc/connectors/common_spec.rb +0 -1
  48. data/spec/unit/rpc/connectors/eventmachine_client_spec.rb +32 -0
  49. data/spec/unit/rpc/connectors/socket_spec.rb +2 -4
  50. data/spec/unit/rpc/connectors/zmq_spec.rb +27 -0
  51. data/spec/unit/rpc/servers/evented_server_spec.rb +3 -3
  52. data/spec/unit/rpc/servers/socket_server_spec.rb +14 -13
  53. data/spec/unit/rpc/servers/zmq/broker_spec.rb +27 -0
  54. data/spec/unit/rpc/servers/zmq/server_spec.rb +37 -0
  55. data/spec/unit/rpc/servers/zmq/util_spec.rb +41 -0
  56. data/spec/unit/rpc/servers/zmq/worker_spec.rb +36 -0
  57. data/spec/unit/rpc/service_spec.rb +22 -18
  58. metadata +87 -40
  59. data/lib/protobuf/rpc/servers/evented_server.rb +0 -28
  60. data/lib/protobuf/rpc/servers/socket_server.rb +0 -146
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gem
2
+ *.swp
2
3
  pkg/*
3
4
  .bundle
4
5
  .rvmrc
@@ -6,3 +7,5 @@ pkg/*
6
7
  coverage
7
8
  doc
8
9
  .yardoc
10
+ .DS_Store
11
+ *.bin
@@ -1,34 +1,52 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- protobuf (1.1.3)
5
- eventmachine (~> 0.12.10)
6
- eventually (~> 0.1.0)
7
- json_pure (~> 1.6.4)
4
+ protobuf (1.3.0)
5
+ eventmachine
6
+ eventually
7
+ ffi-rzmq
8
+ json_pure
8
9
 
9
10
  GEM
10
11
  remote: http://rubygems.org/
11
12
  specs:
13
+ coderay (1.0.5)
12
14
  diff-lcs (1.1.3)
13
15
  eventmachine (0.12.10)
14
- eventually (0.1.0)
15
- json_pure (1.6.5)
16
- multi_json (1.0.4)
17
- rake (0.8.7)
18
- redcarpet (1.17.2)
19
- rspec (2.8.0)
20
- rspec-core (~> 2.8.0)
21
- rspec-expectations (~> 2.8.0)
22
- rspec-mocks (~> 2.8.0)
23
- rspec-core (2.8.0)
24
- rspec-expectations (2.8.0)
25
- diff-lcs (~> 1.1.2)
26
- rspec-mocks (2.8.0)
27
- simplecov (0.5.4)
28
- multi_json (~> 1.0.3)
16
+ eventually (0.1.1)
17
+ ffi (1.0.11)
18
+ ffi-rzmq (0.9.3)
19
+ ffi
20
+ json_pure (1.7.3)
21
+ method_source (0.7.1)
22
+ multi_json (1.2.0)
23
+ pry (0.9.8.4)
24
+ coderay (~> 1.0.5)
25
+ method_source (~> 0.7.1)
26
+ slop (>= 2.4.4, < 3)
27
+ pry (0.9.8.4-java)
28
+ coderay (~> 1.0.5)
29
+ method_source (~> 0.7.1)
30
+ slop (>= 2.4.4, < 3)
31
+ spoon (~> 0.0)
32
+ pry-nav (0.2.0)
33
+ pry (~> 0.9.8.1)
34
+ rake (0.9.2.2)
35
+ rspec (2.9.0)
36
+ rspec-core (~> 2.9.0)
37
+ rspec-expectations (~> 2.9.0)
38
+ rspec-mocks (~> 2.9.0)
39
+ rspec-core (2.9.0)
40
+ rspec-expectations (2.9.0)
41
+ diff-lcs (~> 1.1.3)
42
+ rspec-mocks (2.9.0)
43
+ simplecov (0.6.1)
44
+ multi_json (~> 1.0)
29
45
  simplecov-html (~> 0.5.3)
30
46
  simplecov-html (0.5.3)
31
- yard (0.7.4)
47
+ slop (2.4.4)
48
+ spoon (0.0.1)
49
+ yard (0.7.5)
32
50
 
33
51
  PLATFORMS
34
52
  java
@@ -36,8 +54,9 @@ PLATFORMS
36
54
 
37
55
  DEPENDENCIES
38
56
  protobuf!
39
- rake (~> 0.8.7)
40
- redcarpet (~> 1.17.2)
41
- rspec (~> 2.8.0)
42
- simplecov (~> 0.5.4)
43
- yard (~> 0.7.4)
57
+ pry
58
+ pry-nav
59
+ rake
60
+ rspec
61
+ simplecov
62
+ yard
data/README.md CHANGED
@@ -39,8 +39,8 @@ And `defs.pb.rb` should look like this:
39
39
  ```ruby
40
40
  module Mycompany
41
41
  class User
42
- optional :string, :first_name, 1
43
- optional :string, :last_name, 2
42
+ required :string, :first_name, 1
43
+ required :string, :last_name, 2
44
44
  end
45
45
  end
46
46
  ```
data/Rakefile CHANGED
@@ -3,3 +3,18 @@ $:.push File.expand_path("./spec", File.dirname(__FILE__))
3
3
 
4
4
  require "bundler/gem_tasks"
5
5
  require "benchmark/tasks"
6
+
7
+ require 'rake/testtask'
8
+ Rake::TestTask.new(:test) do |test|
9
+ test.libs << 'test:.'
10
+ test.pattern = 'test/**/test_*.rb'
11
+ test.verbose = true
12
+ end
13
+
14
+ require 'rspec/core/rake_task'
15
+
16
+ desc 'Default: run specs.'
17
+ task :default => :spec
18
+
19
+ desc "Run specs"
20
+ RSpec::Core::RakeTask.new(:spec)
@@ -6,6 +6,7 @@ require 'logger'
6
6
  require 'protobuf/version'
7
7
  require 'protobuf/rpc/servers/evented_runner'
8
8
  require 'protobuf/rpc/servers/socket_runner'
9
+ require 'protobuf/rpc/servers/zmq_runner'
9
10
 
10
11
  # Default options
11
12
  server = OpenStruct.new({
@@ -17,10 +18,18 @@ server = OpenStruct.new({
17
18
  :threshold => 100,
18
19
  :log => File.expand_path('./protobuf.log'),
19
20
  :level => ::Logger::INFO,
20
- :runner => Protobuf::Rpc::EventedRunner,
21
+ :runner => ::Protobuf::Rpc::EventedRunner,
21
22
  :debug => false
22
23
  })
23
24
 
25
+ class ClientServerParse
26
+ def self.parse_client(value)
27
+ end
28
+
29
+ def self.parse_server(value)
30
+ end
31
+ end
32
+
24
33
  parser = OptionParser.new do |opts|
25
34
  opts.banner = "Usage: rpc_server [options] app_file.rb"
26
35
 
@@ -53,12 +62,19 @@ parser = OptionParser.new do |opts|
53
62
  end
54
63
 
55
64
  opts.on("-c", "--client_socket", "Socket Mode for client connections (No EventMachine)") do |v|
56
- Protobuf::ConnectorType = "Socket"
65
+ ENV['PB_CLIENT_TYPE'] = "Socket"
57
66
  end
58
67
 
59
68
  opts.on("-s", "--socket", "Socket Server Mode (No EventMachine)") do |v|
60
- Protobuf::ServerType = "SocketServer"
61
- server.runner = Protobuf::Rpc::SocketRunner
69
+ ENV['PB_SERVER_TYPE'] = "Socket"
70
+ server.runner = ::Protobuf::Rpc::SocketRunner
71
+ end
72
+
73
+ opts.on("-z", "--zmq", "ZeroMQ Socket Mode for client and server connections (they must be used together)") do |v|
74
+ ENV['PB_CLIENT_TYPE'] = 'Zmq'
75
+ ENV['PB_SERVER_TYPE'] = 'Zmq'
76
+
77
+ server.runner = ::Protobuf::Rpc::ZmqRunner
62
78
  end
63
79
 
64
80
  opts.on("-d", "--[no-]debug", "Debug Mode. Override log level to DEBUG.") do |v|
@@ -66,6 +82,14 @@ parser = OptionParser.new do |opts|
66
82
  server.level = ::Logger::DEBUG if v === true
67
83
  end
68
84
 
85
+ opts.on("--client-type=PB_CLIENT_TYPE", String, "Client Type to use (Socket or Zmq)") do |v|
86
+ ENV['PB_CLIENT_TYPE'] = v
87
+ end
88
+
89
+ opts.on("--server-type=PB_SERVER_TYPE", String, "Server Type to use (Socket or Zmq)") do |v|
90
+ ENV['PB_SERVER_TYPE'] = v
91
+ end
92
+
69
93
  opts.separator ""
70
94
  opts.separator "Common options:"
71
95
 
@@ -75,12 +99,29 @@ parser = OptionParser.new do |opts|
75
99
  end
76
100
 
77
101
  opts.on_tail("--version", "Show version") do
78
- puts Protobuf::VERSION
102
+ puts ::Protobuf::VERSION
79
103
  exit
80
104
  end
81
105
  end
82
106
 
83
107
  parser.parse!
108
+
109
+ # Used to ensure we set the PB_CLIENT_TYPE and PB_SERVER_TYPE to the same thing
110
+ # if one of them is set to Zmq
111
+ if(ENV['PB_CLIENT_TYPE'] =~ /zmq/i)
112
+ # make server and client types the same if we're using Zmq
113
+ ENV['PB_SERVER_TYPE'] = ENV['PB_CLIENT_TYPE']
114
+ server.runner = ::Protobuf::Rpc::ZmqRunner
115
+ elsif(ENV['PB_SERVER_TYPE'] =~ /zmq/i)
116
+ # make server and client types the same if we're using Zmq
117
+ ENV['PB_CLIENT_TYPE'] = ENV['PB_SERVER_TYPE']
118
+ server.runner = ::Protobuf::Rpc::ZmqRunner
119
+ elsif(ENV['PB_SERVER_TYPE'] =~ /socket/i)
120
+ server.runner = ::Protobuf::Rpc::SocketRunner
121
+ end
122
+
123
+ # protobuf.rb picks up PB_CLIENT_TYPE and PB_SERVER_TYPE and does something
124
+ # useful with them
84
125
  require 'protobuf'
85
126
 
86
127
  [:INT, :QUIT, :TERM].each do |sig|
@@ -99,12 +140,12 @@ begin
99
140
  raise 'Invalid app file specified (%s).' % server.app unless File.exists?(server.app)
100
141
  end
101
142
 
102
- # Configure the Protobuf::Logger
103
- Protobuf::Logger.configure :file => server.log, :level => server.debug ? ::Logger::DEBUG : server.level
143
+ # Configure the ::Protobuf::Logger
144
+ ::Protobuf::Logger.configure :file => server.log, :level => server.debug ? ::Logger::DEBUG : server.level
104
145
 
105
146
  # Output the server opts
106
- Protobuf::Logger.debug 'Debugging options:'
107
- Protobuf::Logger.debug server.inspect
147
+ ::Protobuf::Logger.debug 'Debugging options:'
148
+ ::Protobuf::Logger.debug server.inspect
108
149
 
109
150
  # Set the name of the process
110
151
  $0 = 'rpc_server %s:%d %s' % [server.host, server.port, server.app]
@@ -116,7 +157,7 @@ begin
116
157
  rescue
117
158
  msg = 'ERROR: RPC Server failed to start. %s' % $!.inspect
118
159
  $stderr.puts msg, *($!.backtrace)
119
- Protobuf::Logger.error msg
120
- Protobuf::Logger.error $!.backtrace.join("\n")
160
+ ::Protobuf::Logger.error msg
161
+ ::Protobuf::Logger.error $!.backtrace.join("\n")
121
162
  exit 1
122
163
  end
@@ -6,12 +6,23 @@ require 'stringio'
6
6
  module Protobuf
7
7
  end
8
8
 
9
-
10
9
  Protobuf::ClientType = ENV['PB_CLIENT_TYPE'] if ENV['PB_CLIENT_TYPE']
11
10
  Protobuf::ServerType = ENV['PB_SERVER_TYPE'] if ENV['PB_SERVER_TYPE']
12
11
 
12
+ # Socket Client/Server loaded by default as it has no impact on cross-platform issues
13
+ require 'protobuf/rpc/servers/socket/server'
14
+ require 'protobuf/rpc/connectors/socket'
15
+ require 'protobuf/rpc/client'
16
+ require 'protobuf/rpc/service'
17
+
13
18
  # When setting up a client
14
- unless defined?(Protobuf::ClientType) && Protobuf::ClientType == "Socket"
19
+ case
20
+ when defined?(Protobuf::ClientType) && Protobuf::ClientType =~ /\Asocket\Z/i then
21
+ #no-op
22
+ when defined?(Protobuf::ClientType) && Protobuf::ClientType =~ /\Azmq\Z/i then
23
+ require 'ffi-rzmq'
24
+ require 'protobuf/rpc/connectors/zmq'
25
+ else
15
26
  Protobuf::ClientType = "EventMachine"
16
27
  require 'eventmachine'
17
28
  require 'protobuf/ext/eventmachine'
@@ -19,14 +30,15 @@ unless defined?(Protobuf::ClientType) && Protobuf::ClientType == "Socket"
19
30
  end
20
31
 
21
32
  # For running the rpc_server
22
- unless defined?(Protobuf::ServerType) && Protobuf::ServerType == "SocketServer"
23
- Protobuf::ServerType = "EventedServer"
33
+ case
34
+ when defined?(Protobuf::ServerType) && Protobuf::ServerType =~ /\Asocket\Z/i then
35
+ #no-op
36
+ when defined?(Protobuf::ServerType) && Protobuf::ServerType =~ /\Azmq\Z/i then
37
+ require 'ffi-rzmq'
38
+ require 'protobuf/rpc/servers/zmq/server'
39
+ else
40
+ Protobuf::ServerType = "Evented::Server"
24
41
  require 'eventmachine'
25
42
  require 'protobuf/ext/eventmachine'
26
- require 'protobuf/rpc/servers/evented_server'
43
+ require 'protobuf/rpc/servers/evented/server'
27
44
  end
28
-
29
- require 'protobuf/rpc/client'
30
- require 'protobuf/rpc/connectors/socket'
31
- require 'protobuf/rpc/service'
32
- require 'protobuf/rpc/servers/socket_server'
@@ -5,30 +5,7 @@ module Protobuf
5
5
 
6
6
  class << self
7
7
  attr_accessor :file, :level
8
-
9
- # One-line file/level configuration
10
- def configure(options)
11
- self.file = options[:file] if options[:file]
12
- self.level = options[:level] if options[:level]
13
- end
14
-
15
- # Use to reset the instance
16
- def reset_device!
17
- self.file = self.level = @__instance = nil
18
- end
19
-
20
- # Singleton instance
21
- def instance
22
- @__instance ||= begin
23
- log = nil
24
- if @file and @level
25
- log = new(self.file)
26
- log.level = self.level
27
- end
28
- log
29
- end
30
- end
31
-
8
+
32
9
  # Stub out the log methods for Protobuf::Logger as singleton methods
33
10
  [:debug, :info, :warn, :error, :fatal, :any, :add, :log].each do |m|
34
11
  define_method(m) do |*params, &block|
@@ -36,6 +13,31 @@ module Protobuf
36
13
  end
37
14
  end
38
15
  end
16
+
17
+ # One-line file/level configuration
18
+ def self.configure(options)
19
+ self.file = options.fetch(:file, false)
20
+ self.level = options.fetch(:level, false)
21
+ end
22
+
23
+ # Use to reset the instance
24
+ def self.reset_device!
25
+ self.file = self.level = @__instance = nil
26
+ end
27
+
28
+ # Singleton instance
29
+ def self.instance
30
+ @__instance ||= begin
31
+ log = nil
32
+
33
+ if @file && @level
34
+ log = new(self.file)
35
+ log.level = self.level
36
+ end
37
+
38
+ log
39
+ end
40
+ end
39
41
 
40
42
  #
41
43
  # LogMethods module for log method including, e.g.:
@@ -59,6 +61,5 @@ module Protobuf
59
61
  base.extend(LogMethods)
60
62
  end
61
63
  end
62
-
63
64
  end
64
65
  end
@@ -1,7 +1,7 @@
1
1
  module Protobuf
2
2
  module Descriptor
3
3
  class FileDescriptor
4
- class <<self
4
+ class << self
5
5
  def proto_type
6
6
  'Google::Protobuf::FileDescriptorProto'
7
7
  end
@@ -685,10 +685,10 @@ module Protobuf
685
685
  when Integer then
686
686
  field.type.const_get(field.type.name_by_value(val)) rescue nil
687
687
  when EnumValue then
688
- raise TypeError, "Invalid value: #{val.inspect}" if val.parent_class != field.type
688
+ raise TypeError, "Invalid value: #{val.inspect} for #{field.name}" if val.parent_class != field.type
689
689
  val
690
690
  end
691
- raise TypeError, "Invalid value: #{val.inspect}" unless val
691
+ raise TypeError, "Invalid value: #{val.inspect} for #{field.name}" unless val
692
692
 
693
693
  @values[field.name] = val
694
694
  end
@@ -1,18 +1,21 @@
1
1
  module Protobuf
2
2
  module Rpc
3
3
  class Buffer
4
-
5
- attr_accessor :mode
6
- attr_reader :data, :size
7
-
4
+
5
+ attr_accessor :mode, :data, :size
6
+
8
7
  MODES = [:read, :write]
9
-
10
- def initialize(mode=:read, data='')
11
- @data = data.is_a?(Protobuf::Message) ? data.serialize_to_string : data.to_s
8
+
9
+ # constantize this so we don't re-initialize the regex every time we need it
10
+ SIZE_REGEX = /^\d+-/
11
+
12
+ def initialize(mode=:read)
12
13
  @flush = false
14
+ @data = ""
15
+ @size = 0
13
16
  self.mode = mode
14
17
  end
15
-
18
+
16
19
  def mode=(mode)
17
20
  if MODES.include?(mode)
18
21
  @mode = mode
@@ -20,18 +23,18 @@ module Protobuf
20
23
  @mode = :read
21
24
  end
22
25
  end
23
-
26
+
24
27
  def write(force_mode=true)
25
28
  if force_mode and reading?
26
29
  mode = :write
27
30
  elsif not force_mode and reading?
28
31
  raise = 'You chose to write the buffer when in read mode'
29
32
  end
30
-
33
+
31
34
  @size = @data.length
32
35
  '%d-%s' % [@size, @data]
33
36
  end
34
-
37
+
35
38
  def <<(data)
36
39
  @data << data
37
40
  if reading?
@@ -39,36 +42,38 @@ module Protobuf
39
42
  check_for_flush
40
43
  end
41
44
  end
42
-
45
+
46
+ def set_data(data)
47
+ @data = data.is_a?(Protobuf::Message) ? data.serialize_to_string : data.to_s
48
+ @size = @data.size
49
+ end
50
+
43
51
  def reading?
44
52
  mode == :read
45
53
  end
46
-
54
+
47
55
  def writing?
48
56
  mode == :write
49
57
  end
50
-
58
+
51
59
  def flushed?
52
60
  @flush
53
61
  end
54
-
55
- private
56
-
62
+
57
63
  def get_data_size
58
- if @size.nil?
59
- sliced_size = @data.slice! /^\d+-/
60
- unless sliced_size.nil?
61
- @size = sliced_size.gsub(/-/, '').to_i
62
- end
64
+ if @size == 0 || @data.match(SIZE_REGEX)
65
+ sliced_size = @data.slice!(SIZE_REGEX)
66
+ @size = sliced_size.gsub('-', '').to_i unless(sliced_size.nil?)
63
67
  end
64
68
  end
65
-
69
+
70
+ private
71
+
66
72
  def check_for_flush
67
- if not @size.nil? and @data.length == @size
73
+ if !@size.nil? && @data.length == @size
68
74
  @flush = true
69
75
  end
70
76
  end
71
-
72
77
  end
73
78
  end
74
79
  end